From bb87b781b8743d7078c43bbe126c38ae90ee7fd5 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Mon, 23 Oct 2023 15:28:08 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + composer.json | 11 ++++ upm.inc.php | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 upm.inc.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e92f57 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tags diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..07fd7b5 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "name": "bit/taskman_upm", + "description": "taskman UPM utils", + "homepage": "https://git.bit5.ru/composer/taskman_upm", + "require": { + "php": ">=7.4" + }, + "autoload": { + "classmap": ["upm.inc.php"] + } +} diff --git a/upm.inc.php b/upm.inc.php new file mode 100644 index 0000000..c42fa1e --- /dev/null +++ b/upm.inc.php @@ -0,0 +1,167 @@ + [^[^[^]]]\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"); +}