-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenereate.php
153 lines (133 loc) · 3.56 KB
/
genereate.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
require_once __DIR__ . '/remote.php';
define('TAG_REF_PREFIX', 'refs/tags/');
define('VERSION_REGEXP', '#^v?[0-9]+\.[0-9]+(\.[0-9]+)(-(dev|patch|alpha|beta|RC|p)[0-9]*)?$#');
global $packages;
$packages = [];
foreach(explode(PHP_EOL, file_get_contents(__DIR__ . '/github.list')) as $github_row)
{
$github_row = trim($github_row);
if($github_row)
{
list($github_path, $type) = explode(' ', $github_row, 2) + [NULL, NULL];
add_github($github_path, $type);
}
}
foreach(explode(PHP_EOL, file_get_contents(__DIR__ . '/external.list')) as $external_row)
{
$external_row = trim($external_row);
if($external_row)
{
list($name_path, $full_path) = explode(' ', $external_row, 2) + [NULL, NULL];
if($name_path AND $full_path)
{
add_external($name_path, $full_path);
}
}
}
file_put_contents(__DIR__ . '/packages.json', json_encode(['packages' => $packages], JSON_PRETTY_PRINT) . PHP_EOL);
function add_github($repo_name_path, $type = NULL)
{
global $remote_site;
global $packages;
$raw = $remote_site->get_page("https://api.github.com/repos/{$repo_name_path}/git/refs/tags");
/** @var phpdoc_github_tag[] $json */
$json = json_decode($raw);
if($json AND is_array($json) AND $json[0])
{
foreach($json as $tag_data)
{
$tag = substr($tag_data->ref, strlen(TAG_REF_PREFIX));
if($tag_data->ref !== TAG_REF_PREFIX . $tag)
{
continue;
}
if(!preg_match(VERSION_REGEXP, $tag))
{
continue;
}
if(isset($packages[$repo_name_path][$tag]))
{
continue;
}
$package_json = NULL;
$package_json_raw = $remote_site->get_page("https://raw.githubusercontent.com/{$repo_name_path}/{$tag_data->object->sha}/composer.json");
if($package_json_raw)
{
$package_json = json_decode($package_json_raw, TRUE);
}
if(!$package_json)
{
$package_json = ['name' => $repo_name_path];
}
$package_json['version'] = $tag;
if($type AND empty($package_json['type']))
{
$package_json['type'] = $type;
}
$package_json['source'] = [
'reference' => $tag_data->object->sha,
'type' => 'git',
'url' => 'https://github.com/' . $repo_name_path . '.git',
];
$packages[$repo_name_path][$tag] = $package_json;
}
}
else
{
echo 'Failed to parse json:', $raw, PHP_EOL;
}
}
function add_external($repo_name_path, $full_path)
{
global $packages;
$safe_path = escapeshellarg($full_path);
$cmd = 'git ls-remote --tags --refs ' . $safe_path;
foreach(explode(PHP_EOL, shell_exec($cmd)) as $row)
{
$columns = explode("\t", $row);
if(count($columns) !== 2)
{
continue;
}
list($hash, $ref) = $columns;
$tag = substr($ref, strlen(TAG_REF_PREFIX));
if($ref !== TAG_REF_PREFIX . $tag)
{
continue;
}
$safe_tag = escapeshellarg($tag);
$cmd = 'git archive --remote=' . $safe_path . ' ' . $safe_tag . ' composer.json | tar xf - --to-stdout';
$json_raw = shell_exec($cmd);
$package_json = json_decode($json_raw, TRUE);
if(!$package_json)
{
$package_json = ['name' => $repo_name_path];
}
$package_json['version'] = $tag;
$package_json['source'] = [
'reference' => $hash,
'type' => 'git',
'url' => $full_path,
];
$packages[$repo_name_path][$tag] = $package_json;
}
}
/**
* Class phpdoc_github_object
* @property string sha
* @property string type
* @property string url
*/
class phpdoc_github_object
{
}
/**
* Class phpdoc_github_tag
* @property string ref
* @property string url
* @property phpdoc_github_object object
*/
class phpdoc_github_tag
{
}