taskman_upm/upm.inc.php

168 lines
4.4 KiB
PHP
Raw Normal View History

2023-10-23 15:28:08 +03:00
<?php
namespace taskman;
use Exception;
task('upm_vendor_status', function(array $args) {
global $GAME_ROOT;
$cmd = isset($args[0]) && $args[0] == '-p' ? 'diff' : 'status -s';
$items = glob("$GAME_ROOT/unity/Packages/*");
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/unity/Packages/"), '', normalize_path($item));
echo "==== Vendor package '$package' changes:\n";
echo implode("\n", $status) . "\n";
}
}
}
});
task('upm_vendor_push', function($args) {
global $GAME_ROOT;
$usage = "Usage: ./gamectl upm_vendor_push <package> <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("$GAME_ROOT/unity/packages/$relpath");
if(!$repo)
throw new Exception("Failed to find a valid directory for '$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";
$upm_info = upm_get_package_info($repo);
if(!isset($upm_info['name']))
throw new Exception("'name' is not present in package.json");
if(!isset($upm_info['version']))
throw new Exception("'version' is not present in package.json");
$upm_version = $upm_info['version'];
$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);
$new_tag = $last_remote_version->encode();
echo "New version expected to be: {$new_tag}\n";
echo "Current package.json version is: {$upm_version}\n";
$upm_new_version = ltrim($new_tag, 'v');
echo "New package.json expected to be: {$upm_new_version}\n";
are_you_sure();
$upm_info['version'] = $upm_new_version;
upm_set_package_info($repo, $upm_info);
git_do($repo, 'add .');
git_do($repo, "commit -am \"$commit_msg\"", $commit_output);
echo "New tag: $new_tag\n";
git_do($repo, 'tag ' . $new_tag);
git_do($repo, 'push origin master');
git_do($repo, 'push --tags');
echo "Updating Packages/manifest.json\n";
upm_update_manifest_package_version($upm_info['name'], $upm_new_version);
echo "Remove vendor local package?\n";
are_you_sure();
ensure_rm($repo);
upm_remove_package_lock_entry($upm_info['name']);
echo "Wait about 10 seconds while UPM package is built by CI, then focus on Unity to check if code compiles.\n";
});
function upm_get_package_info($repo)
{
$json = json_decode(ensure_read("$repo/package.json"), true);
if(!$json)
throw new Exception("Invalid package.json");
return $json;
}
function upm_set_package_info($repo, array $json)
{
ensure_write("$repo/package.json",
json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
function upm_update_manifest_package_version($package, $version)
{
global $GAME_ROOT;
$file = "$GAME_ROOT/unity/Packages/manifest.json";
$manifest = json_decode(ensure_read($file), true);
if(!isset($manifest['dependencies']))
throw new Exception("No 'dependencies' section in manifest");
if(!isset($manifest['dependencies'][$package]))
throw new Exception("No package '{$package}' in dependencies section in manifest");
$manifest['dependencies'][$package] = $version;
ensure_write($file, json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
function upm_remove_package_lock_entry($package)
{
global $GAME_ROOT;
$file = "$GAME_ROOT/unity/Packages/packages-lock.json";
if(!file_exists($file))
return;
$lock = json_decode(ensure_read($file), true);
if(!isset($lock['dependencies']))
return;
unset($lock['dependencies'][$package]);
ensure_write($file, json_encode($lock, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
//let's force recreation of the lock file
touch("$GAME_ROOT/unity/Packages/manifest.json");
}