taskman_composer/composer.inc.php

215 lines
5.0 KiB
PHP
Raw Normal View History

2023-10-23 15:18:16 +03:00
<?php
namespace taskman;
use Exception;
task('composer_update', ['alias' => 'deps_update'], function()
{
composer_update(true);
});
2023-10-23 18:15:37 +03:00
task('composer_reset', function()
{
global $GAME_ROOT;
echo "Going to reset composer package manager...\n";
are_you_sure();
ensure_rm("$GAME_ROOT/composer/vendor/");
ensure_rm("$GAME_ROOT/composer/composer.last");
});
2023-10-24 21:33:00 +03:00
task('composer', function(array $args) {
composer_run($args);
2023-10-23 15:18:16 +03:00
});
task('composer_vendor_push', function($args) {
$usage = "Usage: ./gamectl composer_vendor_push <path/to/repo> <commit message> [^[^[^]]]\n";
if(count($args) < 2)
throw new Exception($usage);
$up_mode = 1;
if(count($args) > 2)
{
if($args[2] === '^')
$up_mode = 1;
else if($args[2] === '^^')
$up_mode = 2;
else if($args[2] === '^^^')
$up_mode = 3;
else
throw new Exception("Invalid up mode: {$args[2]} (supported ^, ^^ and ^^^)");
}
$relpath = $args[0];
$commit_msg = $args[1];
$repo = realpath("composer" .DIRECTORY_SEPARATOR. "vendor" .DIRECTORY_SEPARATOR. $relpath) ?: realpath($relpath);
if(!$repo)
throw new Exception("Failed to find a valid directory for arg '$relpath'\n");
echo "Detected folder: '$repo'\n";
if(!git_is_repo($repo))
throw new Exception("'$repo' is not a valid Git repo!\n");
echo "It's a valid Git repo\n";
echo "======== Git status:\n";
$status = [];
git_do($repo, 'status', $status, false);
echo implode("\n", $status) . "\n";
echo "========\n";
$last_remote_tag = get_git_last_remote_tag($repo);
echo "The last remote Git tag: $last_remote_tag\n";
$last_remote_version = GitVersion::parse($last_remote_tag);
$last_remote_version->bump($up_mode);
echo "New version expected to be: {$last_remote_version->encode()}\n";
are_you_sure();
git_do($repo, 'add .');
git_do($repo, "commit -am \"$commit_msg\"", $commit_output);
$rev = git_rev_from_commit_message(trim($commit_output[0]));
echo "Commited revision hash: $rev\n";
if($rev == null || strlen($rev) < 6)
{
echo "Failed to detect a new commit. Abort.\n";
return;
}
git_do($repo, 'branch', $branch_output);
$branch_was = trim($branch_output[0]);
echo "Current branch: $branch_was\n";
git_do($repo, 'checkout master');
git_do($repo, 'pull');
git_do($repo, 'merge -m"Merge"');
$tags = [];
git_do($repo, 'tag', $tags, false);
$last_tag = _get_last_version_tag($tags);
echo "Last tag: $last_tag\n";
$last_version = GitVersion::parse($last_tag);
$last_version->bump($up_mode);
$new_tag = $last_version->encode();
echo "New tag: $new_tag\n";
if($branch_was != "master")
git_do($repo, 'cherry-pick ' . $rev);
git_do($repo, 'tag ' . $new_tag);
git_do($repo, 'push origin master');
git_do($repo, 'push --tags');
git_do($repo, 'rev-parse HEAD', $output);
if(composer_try_update_lock_entry($relpath, trim($output[0]), $new_tag))
return;
echo "composer.lock file was not updated, run 'composer update $relpath'?\n";
are_you_sure();
composer_run(['update', $relpath]);
2023-10-23 15:18:16 +03:00
});
task('composer_vendor_status', function(array $args) {
global $GAME_ROOT;
$cmd = 'status -s';
if(isset($args[0]) && $args[0] == '-p')
{
$cmd = 'diff';
array_shift($args);
}
$project_filter = '*/*';
if(isset($args[0]))
$project_filter = $args[0];
$items = glob("$GAME_ROOT/composer/vendor/$project_filter");
foreach($items as $item)
{
if(is_dir($item) && is_dir("$item/.git"))
{
$status = [];
exec("cd $item && git $cmd", $status);
if($status)
{
$package = str_replace(normalize_path("$GAME_ROOT/composer/vendor/"), '', normalize_path($item));
2024-05-03 10:42:36 +03:00
$last_remote_tag = get_git_last_remote_tag($item);
echo "==== Vendor package '$package'[$last_remote_tag] changes:\n";
2023-10-23 15:18:16 +03:00
echo implode("\n", $status) . "\n";
}
}
}
});
function composer_try_update_lock_entry($package, $rev, $new_tag)
{
global $GAME_ROOT;
$file = "$GAME_ROOT/composer/composer.lock";
$changed = false;
if(!is_file($file))
return $changed;
$json = json_decode(ensure_read($file), true);
if(!isset($json['packages']))
return $changed;
foreach($json['packages'] as &$item)
{
if(!isset($item['source']['reference']))
continue;
if($item['name'] == $package)
{
$changed = true;
$item['version'] = $new_tag;
$item['source']['reference'] = $rev;
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');
$item['time'] = date(DATE_ATOM);
date_default_timezone_set($tz);
break;
}
}
if($changed)
ensure_write($file, json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
return $changed;
}
2023-10-24 21:33:00 +03:00
function composer_run(array $args)
{
global $GAME_ROOT;
putenv('COMPOSER_RUN='.implode(' ', $args));
include("$GAME_ROOT/composer/update.php");
2023-10-24 21:33:00 +03:00
}
//NOTE: keeping this function for BC
function composer_update($update = false)
{
if($update)
composer_run(['update']);
2023-10-24 21:33:00 +03:00
else
composer_run(['install']);
}
2023-10-24 21:33:00 +03:00