From 099bf688460085ce5ec201b884aede464e54a1fb Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Mon, 4 Nov 2019 17:34:17 +0800 Subject: [PATCH] Add publish and bump helper scripts --- scripts/bump.py | 56 +++++++++++++++++++++++++++++++++++++++++++ scripts/publish.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 scripts/bump.py create mode 100644 scripts/publish.py diff --git a/scripts/bump.py b/scripts/bump.py new file mode 100644 index 0000000..ed843ee --- /dev/null +++ b/scripts/bump.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +import argparse +import io +import os +import re +import sys + +ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +PACKAGES = ('v8-android', 'v8-android-nointl') + + +class PackageConfigPatcher: + def __init__(self, root, version): + self._config_path = os.path.join(root, 'package.json') + self._version = version + + @classmethod + def _replace_file_content(cls, + file_path, + old_pattern, + new_pattern, + re_flags=0): + with io.open(file_path, 'r', encoding='utf8') as f: + content = str(f.read()) + new_content = re.sub(old_pattern, + new_pattern, + content, + flags=re_flags) + with io.open(file_path, 'w', encoding='utf8') as f: + f.write(new_content) + + def patch(self): + self._replace_file_content(self._config_path, r'("version": )("[^"]+")(,)', '\\1"' + self._version + '"\\3') + +def parse_args(): + arg_parser = argparse.ArgumentParser() + + arg_parser.add_argument('--version', '-V', type=str, required=True, help='Bump packages version') + + args = arg_parser.parse_args() + return args + + +def main(): + args = parse_args() + version = args.version + + PackageConfigPatcher(ROOT_DIR, version).patch() + + for package in PACKAGES: + print('\nBump {} package to version {}'.format(package, version)) + package_root = os.path.join(ROOT_DIR, 'packages', package) + PackageConfigPatcher(package_root, version).patch() + +if __name__ == '__main__': + main() diff --git a/scripts/publish.py b/scripts/publish.py new file mode 100644 index 0000000..aa57ab4 --- /dev/null +++ b/scripts/publish.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +import argparse +import os +import shutil +import subprocess +import sys + +ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +PACKAGES = ('v8-android', 'v8-android-nointl') + + +def parse_args(): + arg_parser = argparse.ArgumentParser() + + arg_parser.add_argument('--dry-run', + action='store_true', + help='Dry run mode for npm publish') + arg_parser.add_argument('--tag', + '-T', + type=str, + required=True, + help='NPM published tag') + arg_parser.add_argument('dist_tar_file', + action='store', + help='dist.tgz created from CI') + + args = arg_parser.parse_args() + if not args.dist_tar_file: + arg_parser.print_help() + sys.exit(1) + return args + + +def main(): + args = parse_args() + + workdir = os.path.join(ROOT_DIR, 'build', 'publish') + if not os.path.exists(workdir): + os.makedirs(workdir) + subprocess.run( + ['tar', '-xf', args.dist_tar_file, '-C', workdir]) + + for package in PACKAGES: + print('\n\n========== Publish {} package =========='.format(package)) + cwd = os.path.join(ROOT_DIR, 'packages', package) + source_dir_in_tar_file = os.path.join(workdir, 'dist', 'packages', package) + distdir = os.path.join(cwd, 'dist') + if os.path.exists(distdir): + shutil.rmtree(distdir) + shutil.move(source_dir_in_tar_file, distdir) + cmds = ['npm', 'publish', '--tag', args.tag] + if args.dry_run: + cmds.append('--dry-run') + subprocess.run(cmds, cwd=cwd) + + shutil.rmtree(workdir) + + +if __name__ == '__main__': + main()