Skip to content

Instantly share code, notes, and snippets.

@unclecheese
Created September 23, 2019 02:07
Show Gist options
  • Save unclecheese/0683140b8d1300638131ba9e9b20ee78 to your computer and use it in GitHub Desktop.
Save unclecheese/0683140b8d1300638131ba9e9b20ee78 to your computer and use it in GitHub Desktop.
Generates Github compare links between two given tags of a silverstripe module
#!/usr/bin/env php
<?php
$path = isset($argv[1]) ? $argv[1] : './';
$isMinor = isset($argv[2]) && $argv[2] === '-m';
$path = rtrim($path, '/') . '/';
$lockFile = $path . 'composer.lock';
if (!file_exists($lockFile)) {
echo 'Could not find "'.$path . 'composer.lock". Please specify the correct path as an argument' . PHP_EOL;
exit;
}
$lock = json_decode(file_get_contents($lockFile), true);
$cowpat = json_decode(file_get_contents($path.'.cow.pat.json'), true);
foreach ($lock['packages'] as $package) {
if (substr($package['type'], 0, 12) !== 'silverstripe') {
continue;
}
$truncateName = strlen($package['name']) > 40 ? substr($package['name'], 0, 37) . '...' : $package['name'];
echo substr($truncateName, 0, 40) . ': ' . str_repeat("\t", 1 + ceil((38 - strlen($truncateName)) / 8));
$repo = resolveRepo($package);
if (!$repo) {
echo 'Could not determine repo...' . PHP_EOL;
continue;
}
$pat = matchPat($cowpat, $package['name']);
if (!$pat) {
echo 'Could not find entry in .cow.pat' . PHP_EOL;
continue;
}
$version = $pat['PriorVersion'] ?: $pat['Version'];
$end = substr($version, strripos($version, '.') + 1);
if (is_numeric($end) && ($end = (int) $end) > 0) {
$end--;
$version = substr($version, 0, strripos($version, '.') + 1).$end;
}
$compareVersion = substr($version, 0, strripos($version, '.'));
if (!$isMinor) {
$compareVersion = substr($compareVersion, 0, strripos($compareVersion, '.'));
}
echo $repo . '/compare/'.$version.'...'.$compareVersion.PHP_EOL;
}
function resolveRepo($package) {
if (isset($package['source']) && isset($package['source']['type']) && $package['source']['type'] === 'git') {
return substr($package['source']['url'], 0, -4);
}
return null;
}
function matchPat($pat, $search) {
foreach ($pat as $candidate => $data) {
if ($candidate === $search) {
return $data;
}
if (isset($data['Items'])) {
if ($result = matchPat($data['Items'], $search)) {
return $result;
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment