Skip to content

Commit

Permalink
Add GitHub actions, compiling CoilSnake for Windows on pushes and PRs (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
charasyn committed Feb 12, 2024
1 parent 37bc567 commit be5261b
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/pyinstaller.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
/coilsnake/assets/ccc/ccc
/coilsnake/assets/ccc/lib/*.ccs
.DS_store
git_commit.py
15 changes: 11 additions & 4 deletions coilsnake/ui/information.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"]
Expand Down
17 changes: 17 additions & 0 deletions rename_exe_with_version.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pip>=19.0
pyinstaller>=6.3.0
37 changes: 37 additions & 0 deletions set_git_commit.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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 = []

Expand Down

0 comments on commit be5261b

Please sign in to comment.