'deps_update'], function() { composer_update(true); }); 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"); }); task('composer', function($args) { global $GAME_ROOT; $composer_dir = $GAME_ROOT . '/composer/'; $files = glob($composer_dir . '/composer-*.phar'); $PHAR = basename($files[0]); $cmd = "cd $composer_dir && "; if(PHP_OS_FAMILY === 'Darwin') { $cmd .= "DYLD_LIBRARY_PATH='" . getenv("DYLD_LIBRARY_PATH") . "' " . "PHP_INI_SCAN_DIR='" . getenv("PHP_INI_SCAN_DIR") . "' "; $cmd .= PHP_BINARY . " -c '". getenv("PHP_INI") . "' "; $ext_dir = realpath(ini_get('extension_dir')); $cmd .= " -d extension_dir='$ext_dir' "; } else $cmd .= PHP_BINARY . ' '; $cmd .= "$PHAR " . implode(' ', $args); shell($cmd); }); task('composer_vendor_push', function($args) { $usage = "Usage: ./gamectl composer_vendor_push [^[^[^]]]\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; } //NOTE: COMPOSER_FORCE_UPDATE = true will make composer to update packages and // a lock file // COMPOSER_FORCE_UPDATE = false will make composer install dependencies // listed in a lock file in case if it's present function composer_update($COMPOSER_FORCE_UPDATE = false) { global $GAME_ROOT; include("$GAME_ROOT/composer/update.php"); }