summaryrefslogtreecommitdiffstats
path: root/src/UpgradeBuild.php
blob: 30a318bf242767342d6e698cdc6ca2b8db776302 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php

namespace Acquia\Lightning;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Script\Event;

/**
 * Changes a composer.json to use the official packages.drupal.org repository.
 */
final class UpgradeBuild {

  /**
   * List of packages installed from the Drupal Composer repository.
   *
   * @var string[]
   */
  protected $drupalPackages = [];

  /**
   * The Composer instance.
   *
   * @var \Composer\Composer
   */
  protected $composer;

  /**
   * The I/O handler.
   *
   * @var \Composer\IO\IOInterface
   */
  protected $io;

  /**
   * A wrapper around the root package's composer.json.
   *
   * @var JsonFile
   */
  protected $rootPackage;

  /**
   * UpgradeBuild constructor.
   *
   * @param \Composer\Composer $composer
   *   The Composer instance.
   * @param \Composer\IO\IOInterface $io
   *   The I/O handler.
   */
  public function __construct(Composer $composer, IOInterface $io) {
    $this->composer = $composer;
    $this->io = $io;
    $this->rootPackage = new JsonFile('composer.json', NULL, $io);
  }

  /**
   * Executes the script.
   *
   * @param \Composer\Script\Event $event
   *   The script event.
   */
  public static function execute(Event $event) {
    (new static(
      $event->getComposer(),
      $event->getIO()
    ))->run();
  }

  /**
   * Runs the upgrade process.
   */
  protected function run() {
    // Search upwards for a composer.json which depends on drupal/lightning.
    $this->io->write('Searching for root package...');

    $package = $this->findRootPackage();
    if (empty($package)) {
      $this->io->writeError('No root package found; aborting.');
      return;
    }

    // Use the lock file to determine the packages whose version constraints
    // will need rewriting.
    $this->getDrupalPackages();

    // Rewrite all version constraints to packages.drupal.org format.
    if (isset($package['require-dev'])) {
      $package['require-dev'] = $this->convertVersions($package['require-dev']);
    }
    $package['require'] = $this->convertVersions($package['require']);

    // Use the Lightning package from the Acquia vendor space.
    $package['require']['acquia/lightning'] = '^2.0.0';
    unset($package['require']['drupal/lightning']);

    // If using the Drupal Composer repository, ice it.
    if (isset($package['repositories'])) {
      foreach ($package['repositories'] as $key => $repository) {
        if ($repository['type'] == 'composer' && $this->isUnofficial($repository['url'])) {
          unset($package['repositories'][$key]);
        }
      }
    }

    // Add the packages.drupal.org repository.
    $package['repositories']['packages.drupal.org'] = [
      'type' => 'composer',
      'url' => 'https://packages.drupal.org/8',
    ];

    $this->rootPackage->write($package);
    $this->io->write('Successfully updated your root composer.json file. Switch back to your project root and run "composer update".');
  }

  /**
   * Determines if a URL points to the Drupal Composer (unofficial) repository.
   *
   * @param string $url
   *   The URL to check.
   *
   * @return bool
   *   TRUE if the URL points to the unofficial repository.
   */
  protected function isUnofficial($url) {
    return strpos($url, 'https://packagist.drupal-composer.org') === 0;
  }

  /**
   * Rewrites a set of version constraints.
   *
   * Only packages that came from the Drupal Composer repository are modified.
   *
   * @param array $packages
   *   The constraints to rewrite, keyed by package name.
   *
   * @return string[]
   *   The rewritten constraints.
   */
  protected function convertVersions(array $packages) {
    foreach ($packages as $id => $version) {
      if (in_array($id, $this->drupalPackages)) {
        $packages[$id] = $this->convertVersion($version);
      }
    }
    return $packages;
  }

  /**
   * Rewrites a single version constraint.
   *
   * This code was written by @grasmash for BLT. He is a genius.
   *
   * @param string $version_constraint
   *   The version constraint to rewrite.
   *
   * @return string
   *   The rewritten constraint.
   */
  protected function convertVersion($version_constraint) {
    /*
     * 8.2.x-dev => 2.x-dev
     * 8.x-2.x-dev => 2.x-dev
     * 8.2.x-dev#a1b2c3 => 2.x-dev#a1b2c3
     * 8.x-2.x-dev#a1b2c3 => 2.x-dev#a1b2c3
     */
    if (preg_match('/-dev(#[0-9a-f]+)?$/', $version_constraint)) {
      return preg_replace('/^8\.(x-)?/', NULL, $version_constraint);
    }

    /*
     * dev-master => master-dev
     * dev-8.x-1.x => 1.x-dev
     * dev-8.x-1.x#abc123 => 1.x-dev#abc123
     * dev-8.2.x => 2.x-dev
     * dev-8.2.x#abc123 => 2.x-dev#abc123
     * dev-something_else#123abc => something_else-dev#123abc
     */
    if (strpos($version_constraint, 'dev-') === 0) {
      return preg_replace('/^dev-(8\.(x-)?)?([^#]+)(#[a-f0-9]+)?$/', '$3-dev$4', $version_constraint);
    }

    /*
     * 8.* => *
     */
    if (preg_match('/^8\.\*$/', $version_constraint)) {
      return "*";
    }

    /*
     * ~8 => *@stable
     * ^8 => *@stable
     */
    if (preg_match('/^[\^~]8$/', $version_constraint)) {
      return '*@stable';
    }

    /*
     * ~8.1.0-alpha1 > ~1.0.0-alpha1
     * ^8.1.0-alpha1 > ^1.0.0-alpha1
     * ^8.1.0 > ^1.0
     * 8.1.0-alpha1 > 1.0.0-alpha1
     * 8.1.0-beta12 > 1.0.0-beta12
     * 8.12.0-rc22 > 12.0.0-rc22
     */
    if (preg_match('/^([\^~]|[><=!]{1,2})?8(\.)?(\d+)?(\.\d+)?(-(alpha|beta|rc)\d+)?(\.\*)?(@(dev|alpha|beta|rc))?/', $version_constraint, $matches)) {
      /*
       * Group 0. `~8.1.0-alpha1.*@alpha`
       * Group 1. `~`
       * Group 2. `.`
       * Group 3. `1`
       * Group 4. `.0`
       * Group 5. `-alpha1`
       * Group 6. `alpha`
       * Group 7. `.*`
       * Group 8. `@alpha`
       * Group 9. `alpha`
       */

      $new_version_constraint = $matches[3];
      if (isset($matches[4])) {
        $new_version_constraint .= $matches[4];
      }
      if (isset($matches[3])) {
        $new_version_constraint .= '.0';
      }
      else {
        $new_version_constraint .= '*';
      }

      if (isset($matches[5])) {
        $new_version_constraint .= $matches[5];
      }
      elseif (isset($matches[7])) {
        $new_version_constraint .= $matches[7];
      }

      if (isset($matches[8])) {
        $new_version_constraint .= $matches[8];
      }

      if (isset($matches[1])) {
        $new_version_constraint = $matches[1] . $new_version_constraint;
      }

      return $new_version_constraint;
    }

    return $version_constraint;
  }

  /**
   * Builds the list of packages installed from the Drupal Composer repository.
   */
  protected function getDrupalPackages() {
    $reader = new JsonFile('composer.lock', NULL, $this->io);
    $lock = $reader->read();

    foreach ($lock['packages'] as $package) {
      if (isset($package['notification-url']) && $this->isUnofficial($package['notification-url'])) {
        $this->drupalPackages[] = $package['name'];
      }
    }
  }

  /**
   * Reads the root package's composer.json.
   *
   * This will be the composer.json closest to the current working directory
   * that contains a dependency on drupal/lightning.
   *
   * @return array|null
   *   The parsed contents of the root package's composer.json, or NULL if none
   *   was found.
   */
  protected function findRootPackage() {
    // Split the current working directory into an array, accounting for leading
    // and trailing directory separators.
    $dir = explode(DIRECTORY_SEPARATOR, trim(getcwd(), DIRECTORY_SEPARATOR));

    do {
      if ($this->rootPackage->exists()) {
        $package = $this->rootPackage->read();

        if (isset($package['require']['drupal/lightning'])) {
          return $package;
        }
      }
      chdir('..');
      array_pop($dir);
    } while ($dir);
  }

}