diff --git a/.gitignore b/.gitignore index d868acb..958549b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -# Created by https://www.toptal.com/developers/gitignore/api/goland+all,intellij+all,go,windows,linux,macos -# Edit at https://www.toptal.com/developers/gitignore?templates=goland+all,intellij+all,go,windows,linux,macos +# Created by https://www.toptal.com/developers/gitignore/api/goland+all,intellij+all,go,windows,linux,macos,python,pycharm+all +# Edit at https://www.toptal.com/developers/gitignore?templates=goland+all,intellij+all,go,windows,linux,macos,python,pycharm+all ### Go ### # If you prefer the allow list template instead of the deny list, see community template: @@ -215,6 +215,232 @@ Temporary Items # iCloud generated files *.icloud +### PyCharm+all ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff + +# AWS User-specific + +# Generated files + +# Sensitive or high-churn files + +# Gradle + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake + +# Mongo Explorer plugin + +# File-based project format + +# IntelliJ + +# mpeltonen/sbt-idea plugin + +# JIRA plugin + +# Cursive Clojure plugin + +# SonarLint plugin + +# Crashlytics plugin (for Android Studio and IntelliJ) + +# Editor-based Rest Client + +# Android studio 3.1+ serialized cache file + +### PyCharm+all Patch ### +# Ignore everything but code style settings and run configurations +# that are supposed to be shared within teams. + + + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + +# ruff +.ruff_cache/ + +# LSP config files +pyrightconfig.json + ### Windows ### # Windows thumbnail cache files Thumbs.db @@ -241,4 +467,4 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk -# End of https://www.toptal.com/developers/gitignore/api/goland+all,intellij+all,go,windows,linux,macos \ No newline at end of file +# End of https://www.toptal.com/developers/gitignore/api/goland+all,intellij+all,go,windows,linux,macos,python,pycharm+all \ No newline at end of file diff --git a/app/cmd/root.go b/app/cmd/root.go index 4666f7e..6445f08 100644 --- a/app/cmd/root.go +++ b/app/cmd/root.go @@ -23,9 +23,11 @@ var ( // These values will be injected by the build system appVersion = "Unknown" appDate = "Unknown" + appType = "Unknown" appCommit = "Unknown" - appVersionLong = fmt.Sprintf("Version:\t%s\nBuildDate:\t%s\nCommitHash:\t%s", appVersion, appDate, appCommit) + appVersionLong = fmt.Sprintf("Version:\t%s\nBuildDate:\t%s\nBuildType:\t%s\nCommitHash:\t%s", + appVersion, appDate, appType, appCommit) ) var logger *zap.Logger diff --git a/hyperbole.py b/hyperbole.py new file mode 100644 index 0000000..10db661 --- /dev/null +++ b/hyperbole.py @@ -0,0 +1,222 @@ +import argparse +import os +import subprocess +import datetime +import shutil + +# Hyperbole is the official build script for Hysteria. +# Available environment variables for controlling the build: +# - HY_APP_VERSION: App version +# - HY_APP_COMMIT: App commit hash +# - HY_APP_PLATFORMS: Platforms to build for (e.g. "windows/amd64,linux/arm") + + +LOGO = """ +░█░█░█░█░█▀█░█▀▀░█▀▄░█▀▄░█▀█░█░░░█▀▀ +░█▀█░░█░░█▀▀░█▀▀░█▀▄░█▀▄░█░█░█░░░█▀▀ +░▀░▀░░▀░░▀░░░▀▀▀░▀░▀░▀▀░░▀▀▀░▀▀▀░▀▀▀ +""" + +BUILD_DIR = 'build' + +APP_SRC_DIR = './app' +APP_SRC_CMD_PKG = 'github.com/apernet/hysteria/app/cmd' + + +def check_command(args): + try: + subprocess.check_call(args, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + return True + except Exception: + return False + + +def check_build_env(): + if not check_command(['git', '--version']): + print('Git is not installed. Please install Git and try again.') + return False + if not check_command(['git', 'rev-parse', '--is-inside-work-tree']): + print('Not in a Git repository. Please go to the project root and try again.') + return False + if not check_command(['go', 'version']): + print('Go is not installed. Please install Go and try again.') + return False + return True + + +def get_app_version(): + app_version = os.environ.get('HY_APP_VERSION') + if not app_version: + try: + output = subprocess.check_output( + ['git', 'describe', '--tags', '--always', '--match', 'app/v*']).decode().strip() + app_version = output.split('/')[-1] + except Exception: + app_version = 'Unknown' + return app_version + + +def get_app_commit(): + app_commit = os.environ.get('HY_APP_COMMIT') + if not app_commit: + try: + app_commit = subprocess.check_output( + ['git', 'rev-parse', 'HEAD']).decode().strip() + except Exception: + app_commit = 'Unknown' + return app_commit + + +def get_app_platforms(): + platforms = os.environ.get('HY_APP_PLATFORMS') + if not platforms: + d_os = subprocess.check_output(['go', 'env', 'GOOS']).decode().strip() + d_arch = subprocess.check_output( + ['go', 'env', 'GOARCH']).decode().strip() + return [(d_os, d_arch)] + + result = [] + for platform in platforms.split(','): + platform = platform.strip() + if not platform: + continue + parts = platform.split('/') + if len(parts) != 2: + continue + result.append((parts[0], parts[1])) + return result + + +def cmd_build(release=False): + if not check_build_env(): + return + + os.makedirs(BUILD_DIR, exist_ok=True) + + app_version = get_app_version() + app_date = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') + app_commit = get_app_commit() + + ldflags = [ + '-X', APP_SRC_CMD_PKG + '.appVersion=' + app_version, + '-X', APP_SRC_CMD_PKG + '.appDate=' + app_date, + '-X', APP_SRC_CMD_PKG + '.appType=' + + ('release' if release else 'dev'), + '-X', APP_SRC_CMD_PKG + '.appCommit=' + app_commit, + ] + if release: + ldflags.append('-s') + ldflags.append('-w') + + for os_name, arch in get_app_platforms(): + print('Building for %s/%s...' % (os_name, arch)) + + out_name = 'hysteria-%s-%s' % (os_name, arch) + if os_name == 'windows': + out_name += '.exe' + + env = {**os.environ, 'GOOS': os_name, 'GOARCH': arch} + cmd = ['go', 'build', '-o', + os.path.join(BUILD_DIR, out_name), '-ldflags', ' '.join(ldflags)] + if release: + cmd.append('-trimpath') + cmd.append(APP_SRC_DIR) + + try: + subprocess.check_call(cmd, env=env) + except Exception: + print('Failed to build for %s/%s' % (os_name, arch)) + return + + print('Built %s' % out_name) + + +def cmd_run(args): + if not check_build_env(): + return + + app_version = get_app_version() + app_date = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') + app_commit = get_app_commit() + + ldflags = [ + '-X', APP_SRC_CMD_PKG + '.appVersion=' + app_version, + '-X', APP_SRC_CMD_PKG + '.appDate=' + app_date, + '-X', APP_SRC_CMD_PKG + '.appType=dev-run', + '-X', APP_SRC_CMD_PKG + '.appCommit=' + app_commit, + ] + + cmd = ['go', 'run', '-ldflags', ' '.join(ldflags)] + cmd.append(APP_SRC_DIR) + cmd.extend(args) + + try: + subprocess.check_call(cmd) + except Exception: + print('Failed to run app') + return + + +def cmd_format(): + if not check_command(['gofumpt', '-version']): + print('gofumpt is not installed. Please install gofumpt and try again.') + return + + try: + subprocess.check_call(['gofumpt', '-w', '-l', '-extra', '.']) + except Exception: + print('Failed to format code') + return + + +def cmd_clean(): + shutil.rmtree(BUILD_DIR, ignore_errors=True) + + +def cmd_about(): + print(LOGO) + print('Hyperbole is the official build script for Hysteria.') + + +def main(): + parser = argparse.ArgumentParser() + + p_cmd = parser.add_subparsers(dest='command') + p_cmd.required = True + + # Run + p_run = p_cmd.add_parser('run', help='Run the app') + p_run.add_argument('args', nargs=argparse.REMAINDER) + + # Build + p_build = p_cmd.add_parser('build', help='Build the app') + p_build.add_argument('-r', '--release', action='store_true', + help='Build a release version') + + # Format + p_cmd.add_parser('format', help='Format the code') + + # Clean + p_cmd.add_parser('clean', help='Clean the build directory') + + # About + p_cmd.add_parser('about', help='Print about information') + + args = parser.parse_args() + + if args.command == 'run': + cmd_run(args.args) + elif args.command == 'build': + cmd_build(args.release) + elif args.command == 'format': + cmd_format() + elif args.command == 'clean': + cmd_clean() + elif args.command == 'about': + cmd_about() + + +if __name__ == '__main__': + main()