GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 8.x-2.x ( d5ef2d...39375c )
by Devin
15:25
created

Package::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acquia\DF\Composer;
4
5
use Acquia\Lightning\IniEncoder;
6
use Composer\Package\Locker;
7
use Composer\Package\RootPackageInterface;
8
use Composer\Script\Event;
9
10
/**
11
 * Generates Drush make files for drupal.org's ancient packaging system.
12
 */
13
class Package {
14
15
  /**
16
   * The root Composer package (i.e., this composer.json).
17
   *
18
   * @var \Composer\Package\RootPackageInterface
19
   */
20
  protected $rootPackage;
21
22
  /**
23
   * The locker.
24
   *
25
   * @var \Composer\Package\Locker
26
   */
27
  protected $locker;
28
29
  /**
30
   * Package constructor.
31
   *
32
   * @param \Composer\Package\RootPackageInterface $root_package
33
   *   The root package (i.e., this composer.json).
34
   * @param \Composer\Package\Locker $locker
35
   *   The locker.
36
   */
37
  public function __construct(RootPackageInterface $root_package, Locker $locker) {
38
    $this->rootPackage = $root_package;
39
    $this->locker = $locker;
40
  }
41
42
  /**
43
   * Script entry point.
44
   *
45
   * @param \Composer\Script\Event $event
46
   *   The script event.
47
   */
48
  public static function execute(Event $event) {
49
    $composer = $event->getComposer();
50
51
    $handler = new static(
52
      $composer->getPackage(),
53
      $composer->getLocker()
54
    );
55
56
    $encoder = new IniEncoder();
57
58
    $make = $handler->make();
59
    $handler->makeFix($make);
60
    $core = $handler->makeCore($make);
61
    file_put_contents('drupal-org-core.make', $encoder->encode($core));
62
    file_put_contents('drupal-org.make', $encoder->encode($make));
63
  }
64
65
  /**
66
   * Extracts a core-only make file from a complete make file.
67
   *
68
   * @param array $make
69
   *   The complete make file.
70
   *
71
   * @return array
72
   *   The core-only make file structure.
73
   */
74
  protected function makeCore(array &$make) {
75
    $project = $make['projects']['drupal'];
76
    unset($make['projects']['drupal']);
77
78
    return [
79
      'core' => $make['core'],
80
      'api' => $make['api'],
81
      'projects' => [
82
        'drupal' => $project,
83
      ],
84
    ];
85
  }
86
87
  /**
88
   * Generates a complete make file structure from the root package.
89
   *
90
   * @return array
91
   *   The complete make file structure.
92
   */
93
  protected function make() {
94
    $info = [
95
      'core' => '8.x',
96
      'api' => 2,
97
      'defaults' => [
98
        'projects' => [
99
          'subdir' => 'contrib',
100
        ],
101
      ],
102
      'projects' => [],
103
      'libraries' => [],
104
    ];
105
    $lock = $this->locker->getLockData();
106
107
    foreach ($lock['packages'] as $package) {
108
      list(, $name) = explode('/', $package['name'], 2);
109
110
      if ($this->isDrupalPackage($package)) {
111
        if ($package['type'] == 'drupal-core') {
112
          $name = 'drupal';
113
        }
114
115
        $info['projects'][$name] = $this->buildProject($package);
116
      }
117
      elseif ($this->isLightning($package)) {
118
        $info['projects'][$name] = $this->buildProject($package);
119
120
        // The Lightning project uses semantic versioning.
121
        $sem_ver = explode('.', $package['version']);
122
        list($major, $minor, $patch) = $sem_ver;
123
        $info['projects'][$name]['version'] = "$major.$minor$patch";
124
125
        // Override the default 'contrib' subdirectory set above in order to
126
        // prevent the profile from being placed in a 'contrib' folder.
127
        $info['projects'][$name]['subdir'] = '""';
128
      }
129
      // Include any non-drupal libraries that exist in both .lock and .json.
130
      elseif ($this->isLibrary($package)) {
131
        $info['libraries'][$name] = $this->buildLibrary($package);
132
      }
133
    }
134
135
    return $info;
136
  }
137
138
  /**
139
   * 'Fixes' a make file by manually adjusting known package inaccuracies.
140
   *
141
   * @param array $make
142
   *   The complete make file.
143
   */
144
  protected function makeFix(array &$make) {
145
    // Build a whitelist of libraries which are approved for distribution via
146
    // drupal.org.
147
    // Composer supports adding libraries from anywhere, while drupal.org only
148
    // allows distributions to include libraries that have been specifically
149
    // approved. See the Packaging Whitelist for a list of approved libraries
150
    // (https://www.drupal.org/packaging-whitelist).
151
    $whitelist = ['dropzone', 'masonry', 'imagesloaded', 'ckeditor-track-changes', 'slick-carousel', 'lightbox2', 'cropper'];
152
153
    foreach ($make['libraries'] as $name => $info) {
154
      // Libraries must be located in the root 'libraries' folder.
155
      $make['libraries'][$name]['destination'] = "../../libraries";
156
157
      // Remove any libraries that are not specifically whitelisted above.
158
      // Users will need to download these libraries manually.
159
      if (!in_array($name, $whitelist)) {
160
        unset($make['libraries'][$name]);
161
      }
162
    }
163
164
    // The ckeditor-track-changes library is designed to be renamed, after
165
    // download, to 'lite'.
166
    if (isset($make['libraries']['ckeditor-track-changes'])) {
167
      $make['libraries']['ckeditor-track-changes']['directory_name'] = 'lite';
168
    }
169
170
    // The Zurb Foundation theme had its shortname changed from
171
    // 'zurb-foundation' to 'zurb_foundation' on drupal.org. Unfortunately,
172
    // drupal.org requires the old shortname to be used when retrieving the
173
    // theme using drush make.
174
    if (isset($make['projects']['zurb_foundation'])) {
175
      $make['projects']['zurb-foundation'] = $make['projects']['zurb_foundation'];
176
      unset($make['projects']['zurb_foundation']);
177
    }
178
  }
179
180
  /**
181
   * Builds a make structure for a library (i.e., not a Drupal project).
182
   *
183
   * @param array $package
184
   *   The Composer package definition.
185
   *
186
   * @return array
187
   *   The generated make structure.
188
   */
189
  protected function buildLibrary(array $package) {
190
    $info = [
191
      'type' => 'library',
192
    ];
193
    return $info + $this->buildPackage($package);
194
  }
195
196
  /**
197
   * Builds a make structure for a Drupal module, theme, profile, or core.
198
   *
199
   * @param array $package
200
   *   The Composer package definition.
201
   *
202
   * @return array
203
   *   The generated make structure.
204
   */
205
  protected function buildProject(array $package) {
206
    $info = [];
207
208
    switch ($package['type']) {
209
      case 'drupal-core':
210
      case 'drupal-profile':
211
      case 'drupal-theme':
212
      case 'drupal-module':
213
        $info['type'] = substr($package['type'], 7);
214
        break;
215
    }
216
    $info += $this->buildPackage($package);
217
218
    // Dev versions should use git branch + revision, otherwise a tag is used.
219
    if (strstr($package['version'], 'dev')) {
220
      // 'dev-' prefix indicates a branch-alias. Stripping the dev prefix from
221
      // the branch name is sufficient.
222
      // @see https://getcomposer.org/doc/articles/aliases.md
223
      if (strpos($package['version'], 'dev-') === 0) {
224
        $info['download']['branch'] = substr($package['version'], 4);
225
      }
226
      // Otherwise, leave as is. Version may already use '-dev' suffix.
227
      else {
228
        $info['download']['branch'] = $package['version'];
229
      }
230
      $info['download']['revision'] = $package['source']['reference'];
231
    }
232
    else {
233
      if ($package['type'] == 'drupal-core') {
234
        $version = $package['version'];
235
      }
236
      else {
237
        // Make tag versioning Drupal-friendly. 8.1.0-alpha1 => 8.x-1.0-alpha1.
238
        $version = sprintf(
239
          '%d.x-%s',
240
          $package['version']{0},
241
          substr($package['version'], 2)
242
        );
243
      }
244
      // Make the version Drush make-compatible: 1.x-13.0-beta2 --> 1.13-beta2
245
      $info['version'] = preg_replace(
246
        '/^([0-9]+)\.x-([0-9]+)\.[0-9]+(-.+)?/',
247
        '$1.$2$3',
248
        $version
249
      );
250
      unset($info['download']);
251
    }
252
253
    return $info;
254
  }
255
256
  /**
257
   * Builds a make structure for any kind of package.
258
   *
259
   * @param array $package
260
   *   The Composer package definition.
261
   *
262
   * @return array
263
   *   The generated make structure.
264
   */
265
  protected function buildPackage(array $package) {
266
    $info = [
267
      'download' => [
268
        'type' => 'git',
269
        'url' => $package['source']['url'],
270
        'branch' => $package['version'],
271
        'revision' => $package['source']['reference'],
272
      ],
273
    ];
274
275
    if (isset($package['extra']['patches_applied'])) {
276
      $info['patch'] = array_values($package['extra']['patches_applied']);
277
    }
278
    return $info;
279
  }
280
281
  /**
282
   * Determines if a package is a Drupal core, module, theme, or profile.
283
   *
284
   * @param array $package
285
   *   The package info.
286
   *
287
   * @return bool
288
   *   TRUE if the package is a Drupal core, module, theme, or profile;
289
   *   otherwise FALSE.
290
   */
291
  protected function isDrupalPackage(array $package) {
292
    $package_types = [
293
      'drupal-core',
294
      'drupal-module',
295
      'drupal-theme',
296
      'drupal-profile',
297
    ];
298
299
    return (
300
      strpos($package['name'], 'drupal/') === 0 &&
301
      in_array($package['type'], $package_types)
302
    );
303
  }
304
305
  /**
306
   * Determines if a package is the Lightning profile.
307
   *
308
   * @param array $package
309
   *   The package info.
310
   *
311
   * @return bool
312
   *   TRUE if the package is Lightning; otherwise FALSE.
313
   */
314
  protected function isLightning(array $package) {
315
    return (
316
      $package['name'] == 'acquia/lightning' &&
317
      $package['type'] == 'drupal-profile'
318
    );
319
  }
320
321
  /**
322
   * Determines if a package is an asset library.
323
   *
324
   * @param array $package
325
   *   The package info.
326
   *
327
   * @return bool
328
   *   TRUE if the package is an asset library, otherwise FALSE.
329
   */
330
  protected function isLibrary(array $package) {
331
    $package_types = [
332
      'drupal-library',
333
      'bower-asset',
334
      'npm-asset',
335
    ];
336
    return (
337
    in_array($package['type'], $package_types)
338
    );
339
  }
340
341
}