-
Notifications
You must be signed in to change notification settings - Fork 2
/
print-packages-markdown-list.php
executable file
·115 lines (86 loc) · 3.18 KB
/
print-packages-markdown-list.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/php
<?php
function git_get_all_branches(): array
{
exec('git for-each-ref refs/heads --format="%(refname:short)" 2> /dev/null', $branches);
return $branches;
}
function git_file_exists_on_branch(string $branch_name, string $file_name): bool
{
exec(sprintf('git show %s:%s 2> /dev/null', $branch_name, $file_name), $output, $exit_code);
return ! (int) $exit_code;
}
function git_file_get_contents_on_branch(string $branch_name, string $file_name): string
{
exec(sprintf('git show %s:%s 2> /dev/null', $branch_name, $file_name), $output, $exit_code);
if ($exit_code !== 0) {
throw new Exception;
}
return implode(PHP_EOL, $output);
}
function rpm_get_field_from_spec_file(string $spec_file_name, string $field): string
{
exec(sprintf('rpmspec -q --srpm --qf "%%{%s}" %s 2> /dev/null', $field, $spec_file_name), $output, $exit_code);
if ($exit_code !== 0) {
throw new Exception;
}
return $output[0];
}
function rpm_eval(string $expression): string
{
exec(sprintf('rpm --eval "%s" 2> /dev/null', $expression), $output, $exit_code);
if ($exit_code !== 0) {
throw new Exception;
}
return implode(PHP_EOL, $output);
}
function get_package_names(): array
{
return array_filter(git_get_all_branches(), function($branch_name){
$is_dead = git_file_exists_on_branch($branch_name, 'dead.package');
$has_spec_file = git_file_exists_on_branch($branch_name, sprintf('%s.spec', $branch_name));
if ($is_dead || 'master' === $branch_name) {
return false;
}
if (! $has_spec_file) {
throw new Exception;
}
return $has_spec_file;
});
}
function get_package_rpm_spec_file(string $package_name): string
{
$rpm_spec = git_file_get_contents_on_branch($package_name, sprintf('%s.spec', $package_name));
$temp_file_name = tempnam(sys_get_temp_dir(), 'print-pkg-list');
file_put_contents($temp_file_name, $rpm_spec);
register_shutdown_function(function() use ($temp_file_name){ unlink($temp_file_name); });
return $temp_file_name;
}
function get_package_summary(string $package_name): string
{
$rpm_spec_file = get_package_rpm_spec_file($package_name);
$summary = rpm_get_field_from_spec_file($rpm_spec_file, 'summary');
return rtrim($summary, '.');
}
function get_package_version(string $package_name): string
{
$rpm_spec_file = get_package_rpm_spec_file($package_name);
$version = rpm_get_field_from_spec_file($rpm_spec_file, 'version');
$release = str_replace(rpm_eval('%dist'), '', rpm_get_field_from_spec_file($rpm_spec_file, 'release'));
return sprintf('%s-%s', $version, $release);
}
function get_package_branch_url(string $package_name): string
{
$github_repository_url = 'TomaszGasior/mushrooms-rpms';
return sprintf('https://github.com/%s/tree/%s', $github_repository_url, $package_name);
}
function main(): void
{
foreach (get_package_names() as $name) {
$summary = get_package_summary($name);
$url = get_package_branch_url($name);
$version = get_package_version($name);
printf("- [**%s**](%s) *%s* \n %s.\n", $name, $url, $version, $summary);
}
}
main();