commit 2ff2170fd5470f592005320f88d8fd0bd03a241f Author: Pavel Shevaev Date: Mon Oct 23 15:18:16 2023 +0300 Initial commit 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.inc.php b/composer.inc.php new file mode 100644 index 0000000..54adf54 --- /dev/null +++ b/composer.inc.php @@ -0,0 +1,193 @@ + [^[^[^]]]\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); + composer_try_update_lock_entry($relpath, trim($output[0]), $new_tag); +}); + +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)); + echo "==== Vendor package '$package' changes:\n"; + 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; +} + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..dee0028 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "name": "bit/taskman_composer", + "description": "taskman composer utils", + "homepage": "https://git.bit5.ru/composer/taskman_composer", + "require": { + "php": ">=7.4" + }, + "autoload": { + "classmap": ["composer.inc.php"] + } +}