From be5261bf53bf6b1656f693658c45dc321f8565c3 Mon Sep 17 00:00:00 2001 From: Cooper Harasyn Date: Mon, 12 Feb 2024 13:43:24 -0800 Subject: [PATCH] Add GitHub actions, compiling CoilSnake for Windows on pushes and PRs (#287) * First test of github actions * Apparently the action needs requirements.txt? * Action failed because PyInstaller was missing - install requirements * Add support for writing Git commit during setup.py and displaying it * Add git_commit.py to gitignore and fix setup.py * Add more error handling, and change format of version number * Update GitHub action to set BUILD_GIT_COMMIT env var * Separate writing the Git commit into its own script * Rename output executable to contain the version * Update policy on when a build occurs --- .github/workflows/pyinstaller.yaml | 32 ++++++++++++++++++++++++++ .gitignore | 1 + coilsnake/ui/information.py | 15 ++++++++---- rename_exe_with_version.py | 17 ++++++++++++++ requirements.txt | 2 ++ set_git_commit.py | 37 ++++++++++++++++++++++++++++++ setup.py | 3 ++- 7 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/pyinstaller.yaml create mode 100644 rename_exe_with_version.py create mode 100644 requirements.txt create mode 100755 set_git_commit.py diff --git a/.github/workflows/pyinstaller.yaml b/.github/workflows/pyinstaller.yaml new file mode 100644 index 00000000..bc5c9f3f --- /dev/null +++ b/.github/workflows/pyinstaller.yaml @@ -0,0 +1,32 @@ +name: Build CoilSnake with Win32 Py3.8 + +on: + push: # by vote, we'll only build on pushes to master + branches: [ master ] + pull_request: # build on pull request against any branch + +jobs: + build: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.8' + cache: 'pip' # caching pip dependencies + architecture: 'x86' + - name: Install requirements + run: pip install -r requirements.txt + - name: Write build Git commit info + run: python set_git_commit.py --write + - name: Build CoilSnake + run: python setup.py install + - name: Build .exe + run: python setup_exe.py + - name: Rename .exe + run: python rename_exe_with_version.py + - name: Upload .exe + uses: actions/upload-artifact@v4 + with: + name: CoilSnake (Py3.8-win32) + path: dist/CoilSnake*.exe diff --git a/.gitignore b/.gitignore index 48ec28e3..a9a68430 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ /coilsnake/assets/ccc/ccc /coilsnake/assets/ccc/lib/*.ccs .DS_store +git_commit.py diff --git a/coilsnake/ui/information.py b/coilsnake/ui/information.py index f0de3670..1a3b9d28 100644 --- a/coilsnake/ui/information.py +++ b/coilsnake/ui/information.py @@ -1,6 +1,13 @@ from coilsnake.util.common import project +# In case the file was not properly generated or bundled... +try: + from coilsnake.ui.git_commit import GIT_COMMIT +except: + GIT_COMMIT = None VERSION = project.VERSION_NAMES[project.FORMAT_VERSION] +if GIT_COMMIT: + VERSION = f"{VERSION}-next-{GIT_COMMIT}" RELEASE_DATE = "March 19, 2023" WEBSITE = "http://pk-hack.github.io/CoilSnake" @@ -33,10 +40,10 @@ def coilsnake_about(): - description = """CoilSnake {version} - {website} -Created by {author} -Released on {release_date} -""".format(version=VERSION, author=AUTHOR, release_date=RELEASE_DATE, website=WEBSITE) + description = f"""CoilSnake {VERSION} - {WEBSITE} +Created by {AUTHOR} +Released on {RELEASE_DATE} +""" for dependency in DEPENDENCIES: description += "\n- " + dependency["name"] diff --git a/rename_exe_with_version.py b/rename_exe_with_version.py new file mode 100644 index 00000000..1560ca08 --- /dev/null +++ b/rename_exe_with_version.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +import glob +import os +from coilsnake.ui.information import VERSION + +def rename_exe(): + exe_paths = glob.glob('dist/CoilSnake*') + if not exe_paths: + print("Couldn't locate CoilSnake executable. Doing nothing...") + return + for exe_path in exe_paths: + new_exe_path = exe_path.replace('CoilSnake', f'CoilSnake-{VERSION}') + if exe_path != new_exe_path: + os.rename(exe_path, new_exe_path) + +if __name__ == '__main__': + rename_exe() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..324331ef --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pip>=19.0 +pyinstaller>=6.3.0 diff --git a/set_git_commit.py b/set_git_commit.py new file mode 100755 index 00000000..d4feb50c --- /dev/null +++ b/set_git_commit.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +import os +import subprocess +import sys + +GITHUB_SHA_ENV_VAR_NAME = 'GITHUB_SHA' +COILSNAKE_GIT_COMMIT_PY_PATH = 'coilsnake/ui/git_commit.py' + +def get_git_commit(): + # Try looking at the GitHub variable + revision = os.environ.get(GITHUB_SHA_ENV_VAR_NAME) + # If it wasn't set, run against head + if not revision: + revision = "HEAD" + # Try to run git rev-parse to get the short hash + try: + print('Getting short hash for Git revision:', revision) + git_commit = subprocess.check_output(['git', 'rev-parse', '--short', revision]) + return git_commit.strip().decode() + except Exception as e: + print('Error when running rev-parse:', e, sep='\n') + # In case of error, return None + return None + +def write_git_commit(git_commit): + # Force git_commit to be a value, or None + git_commit = git_commit or None + git_commit_file_text = f'GIT_COMMIT = {git_commit!r}' + print(f"Writing '{COILSNAKE_GIT_COMMIT_PY_PATH}' with: {git_commit_file_text}") + with open(COILSNAKE_GIT_COMMIT_PY_PATH, 'w') as f: + print(git_commit_file_text, file=f) + +if __name__ == '__main__': + git_commit = get_git_commit() + print('Found Git commit short hash:', git_commit) + if '--write' in sys.argv[1:]: + write_git_commit(git_commit) diff --git a/setup.py b/setup.py index 5e5ff532..0b92cc38 100755 --- a/setup.py +++ b/setup.py @@ -1,8 +1,9 @@ #!/usr/bin/env python +import os +import platform from setuptools import setup, find_packages from setuptools.extension import Extension -import platform extra_compile_args = []