Skip to content

Commit

Permalink
ISSUE-370: check patches are stored locally
Browse files Browse the repository at this point in the history
  • Loading branch information
beto-aveiga committed Oct 2, 2024
1 parent de7e0a9 commit 51e4e12
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions src/ScaffoldInstallerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,69 @@ public static function getSubscribedEvents()
];
}

/**
* Composer configuration advice: Use local copies of patch files.
*
* @return void
*/
private function _checkComposerPatchesAreLocal()
{
$patchesInComposer = $this->extra['patches'] ?? false;
$patchesInExtraFile = $this->extra['patches-file'] ?? false;

// Patches defined on a separate file.
if ($patchesInExtraFile) {
if (!file_exists($patchesInExtraFile. '2')) {
$this->io->warning("The patches file `$patchesInExtraFile` can't be read.");
return;
}

$patchesJsonEncoded = file_get_contents($patchesInExtraFile);
$patchesContent = json_decode($patchesJsonEncoded, true)['patches'] ?? [];
} else if ($patchesInComposer) {
$patchesContent = $patchesInComposer;
}

// Patches content is not a string.
if (!is_array($patchesContent)) {
$this->io->warning("The patches content can't be validated. Check your patches defined in Composer.");
return;
}

// Patches content is not empty.
if (!$patchesContent) {
return;
}

// Collecting remote patches (if any).
$remotePatches = [];
foreach ($patchesContent as $projectName => $patches) {
foreach ($patches as $patchName => $patchUri) {
if (str_starts_with($patchUri, 'http')) {
$remotePatches[$projectName] = "$patchName | $patchUri";
}
}
}

if (!$remotePatches) {
return;
}

// Collect the remote patches info.
$patchesInfo = PHP_EOL;
$count = 1;
foreach ($remotePatches as $projectName => $remote_patch) {
$patchesInfo.= "[$count] $projectName: $remote_patch " . PHP_EOL;
$count++;
}
$patchesInfo = rtrim($patchesInfo, PHP_EOL);

// Communicate the user.
$msg = 'Use local copies of patch files. See';
$link = 'https://architecture.lullabot.com/adr/20220429-composer-patch-files/';
$this->io->warning("$msg $link $patchesInfo");
}

/**
* Composer configuration advice: "composer-exit-on-patch-failure": true
*
Expand All @@ -94,7 +157,7 @@ private function _checkComposerBreaksIfPatchesDoNotApply()

$msg = 'Break Composer install if patches don\'t apply. See ';
$link = 'https://architecture.lullabot.com/adr/20220429-composer-exit-failure/';
$this->io->warning("{$msg} {$link}");
$this->io->warning("$msg $link");
}

/**
Expand All @@ -116,7 +179,7 @@ private function _checkDrupalCoreComposerPatchesLevel()
$msg = 'Configure Composer patches to Use `-p2`
as `patchLevel` for Drupal core. See ';
$link = 'https://architecture.lullabot.com/adr/20220429-composer-patchlevel/';
$this->io->warning("{$msg} {$link}");
$this->io->warning("$msg $link");
}

/**
Expand All @@ -132,7 +195,7 @@ private function _checkPatchesStoredInComposerJson()

$msg = 'Store Composer patches configuration in `composer.json`. See ';
$link = 'https://architecture.lullabot.com/adr/20220429-composer-patches-inline/';
$this->io->warning("{$msg} {$link}");
$this->io->warning("$msg $link");
}

/**
Expand All @@ -152,6 +215,7 @@ public function onPostInstallCmd(Event $event)
$this->_checkDrupalCoreComposerPatchesLevel();
$this->_checkComposerBreaksIfPatchesDoNotApply();
$this->_checkPatchesStoredInComposerJson();
$this->_checkComposerPatchesAreLocal();
}

/**
Expand Down

0 comments on commit 51e4e12

Please sign in to comment.