forked from acquia/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package
executable file
·232 lines (203 loc) · 5.49 KB
/
package
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env php
<?php
class IniEncoder {
/**
* Serializes an array to legacy make format.
*
* @param array $input
* The data to serialize.
*
* @return string
* The serialized data.
*/
public function encode(array $input) {
return implode("\n", $this->doEncode($input));
}
/**
* Recursively serializes data to legacy make format.
*
* @param array $input
* The data to serialize.
* @param array $keys
* The current key path.
*
* @return string[]
* The serialized data as a flat array of lines.
*/
protected function doEncode(array $input, array $keys = []) {
$output = array();
foreach ($input as $key => $value) {
$keys[] = $key;
if (is_array($value)) {
if ($this->isAssociative($value)) {
$output = array_merge($output, $this->doEncode($value, $keys));
}
else {
foreach ($value as $j) {
$output[] = $this->keysToString($keys) . '[] = ' . $j;
}
}
}
else {
$output[] = $this->keysToString($keys) . ' = ' . $value;
}
array_pop($keys);
}
return $output;
}
/**
* Transforms an key path to a string.
*
* @param string[] $keys
* The key path.
*
* @return string
* The flattened key path.
*/
protected function keysToString(array $keys) {
$head = array_shift($keys);
if ($keys) {
return $head . '[' . implode('][', $keys) . ']';
}
else {
return $head;
}
}
/**
* Tests if an array is associative.
*
* @param array $input
* The array to test.
*
* @return bool
* Whether or not the array has non-numeric keys.
*/
protected function isAssociative(array $input) {
$keys = implode('', array_keys($input));
return !is_numeric($keys);
}
}
class Bower implements \IteratorAggregate {
/**
* The directory where Bower packages are installed.
*
* @var string
*
* @see getDirectory()
*/
protected $directory;
/**
* Loops over lock info for all installed dependencies.
*/
public function getIterator() {
$info = file_get_contents('bower.json');
$info = json_decode($info, TRUE);
foreach (array_keys($info['dependencies']) as $package) {
yield $this->getLockInfo($package);
}
}
/**
* Returns Bower lock info for a specific package.
*
* @param string $package
* The name of the package.
*
* @return array
* The package info.
*
* @throws \RuntimeException
* If the package is not installed.
*/
public function getLockInfo($package) {
$info = $this->getDirectory() . "/$package/.bower.json";
if (file_exists($info)) {
$info = file_get_contents($info);
return json_decode($info, TRUE);
}
else {
throw new \RuntimeException('Package ' . $package . ' is not installed.');
}
}
/**
* Returns the contents of .bowerrc, if it exists.
*
* @return array
* The parsed contents of .bowerrc.
*/
protected function getConfig() {
if (file_exists('.bowerrc')) {
$config = file_get_contents('.bowerrc');
return json_decode($config, TRUE);
}
else {
return [];
}
}
/**
* Returns the directory where Bower dependencies are installed.
*
* @return string
* The directory where Bower dependencies are installed; defaults to
* bower_components if not set.
*/
public function getDirectory() {
if (empty($this->directory)) {
$config = $this->getConfig();
$this->directory = @($config['directory'] ?: 'bower_components');
}
return $this->directory;
}
}
require_once './vendor/autoload.php';
$encoder = new IniEncoder();
$make = \Symfony\Component\Yaml\Yaml::parse(`./bin/drush make-convert composer.lock`);
// drush make-convert considers non-Drupal Composer dependencies, like Drush,
// to be libraries. That is totally wrong, so head that off at the pass by
// destroying any libraries it thinks it's found.
unset($make['libraries']);
if (isset($make['projects']['drupal'])) {
// Always use drupal.org's core repository, or patches will not apply.
$make['projects']['drupal']['download']['url'] = 'https://git.drupal.org/project/drupal.git';
$core = [
'api' => 2,
'core' => '8.x',
'projects' => [
'drupal' => [
'type' => 'core',
'version' => $make['projects']['drupal']['download']['tag'],
],
],
];
if (isset($make['projects']['drupal']['patch'])) {
$core['projects']['drupal']['patch'] = $make['projects']['drupal']['patch'];
}
file_put_contents('drupal-org-core.make', $encoder->encode($core));
unset($make['projects']['drupal']);
}
foreach ($make['projects'] as $key => &$project) {
if ($project['download']['type'] == 'git') {
$tag = $project['download']['tag'];
preg_match('/[0-9]\.x-[0-9]\.0/', $tag, $match);
$tag = str_replace($match, str_replace('x-', NULL, $match), $tag);
preg_match('/[0-9]\.[0-9]\.0/', $tag, $match);
$tag = str_replace($match, substr($match[0], 0, -2), $tag);
$project['version'] = $tag;
unset($project['download']);
}
}
if (file_exists('bower.json')) {
$bower = new Bower();
foreach ($bower as $lib) {
$package = $lib['name'];
$make['libraries'][$package] = [
'type' => 'library',
'directory_name' => $package,
'download' => [
'type' => 'git',
'url' => $lib['_source'],
'tag' => $lib['_resolution']['tag'],
],
];
}
}
file_put_contents('drupal-org.make', $encoder->encode($make));