diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index abeb79e..38284f6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,14 +1,27 @@ -on: [push] +on: + push: + branches: + - 'master' + tags: + - '[0-9]+.[0-9]+' + paths: + - '.github/workflows/**' + - 'FiraCode.glyphs' + - 'script/**' + jobs: build: runs-on: ubuntu-latest container: tonsky/firacode:latest steps: - uses: actions/checkout@v2 - - run: echo ${GITHUB_REF#refs/heads/} - - run: echo "version=$(./script/update_version.py)" >> $GITHUB_ENV + - if: startsWith(github.ref, 'refs/tags/') + run: python3 ./script/update_version.py - run: ./script/build.sh + - run: echo "hash=$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV - uses: actions/upload-artifact@v2 with: - name: Fira_Code_${{ env.version }} - path: distr \ No newline at end of file + name: Fira_Code_${{ env.hash }} + path: distr + - if: startsWith(github.ref, 'refs/tags/') + run: python3 ./script/release.py \ No newline at end of file diff --git a/script/common.py b/script/common.py new file mode 100644 index 0000000..4498a7d --- /dev/null +++ b/script/common.py @@ -0,0 +1,17 @@ +#! /usr/bin/env python3 +import argparse, os, re, subprocess + +root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) + +def version(): + parser = argparse.ArgumentParser() + parser.add_argument('--version') + (args, _) = parser.parse_known_args() + if args.version: + return args.version + + ref = os.getenv('GITHUB_REF') + if ref and ref.startswith('refs/tags/'): + return ref[len('refs/tags/'):] + + raise Exception("Can’t identify version") \ No newline at end of file diff --git a/script/release b/script/release deleted file mode 100755 index 1744ee1..0000000 --- a/script/release +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -euo pipefail -cd "`dirname $0`/.." - -npm publish - -# Update https://github.com/Homebrew/homebrew-cask-fonts -# Update https://github.com/matthewjberger/scoop-nerd-fonts/blob/master/bucket/FiraCode.json \ No newline at end of file diff --git a/script/release.py b/script/release.py new file mode 100755 index 0000000..763b3f8 --- /dev/null +++ b/script/release.py @@ -0,0 +1,74 @@ +#! /usr/bin/env python3 +import argparse, base64, common, glob, os, platform, re, subprocess, sys, urllib.request, zipfile + +def log_errors(name): + def wrap(f): + def result(*args, **kwargs): + try: + f(*args, **kwargs) + except Exception as e: + print(f"{name}: Failed {e}") + return result + return wrap + +def package(version): + zip = f"Fira_Code_v{version}.zip" + + print('Package:', zip) + with zipfile.ZipFile(zip, 'w', compression = zipfile.ZIP_DEFLATED, compresslevel = 9) as archive: + for f in glob.glob("distr/**", recursive = True): + arcname = f[len("distr/"):] + if arcname and not os.path.basename(arcname).startswith("."): + archive.write(f, arcname) + +def github_headers(): + if os.environ.get('GITHUB_BASIC'): + auth = 'Basic ' + base64.b64encode(os.environ.get('GITHUB_BASIC').encode('utf-8')).decode('utf-8') + else: + auth = 'token ' + os.environ.get('API_TOKEN') + return { + 'Accept': 'application/vnd.github.v3+json', + 'Authorization': auth + } + +@log_errors("github_release") +def github_release(version): + zip = f"Fira_Code_v{version}.zip" + + data = '{"tag_name":"' + version + '","name":"' + version + '"}' + headers = github_headers() + resp = urllib.request.urlopen(urllib.request.Request('https://api.github.com/repos/tonsky/FiraCode/releases', data=data.encode('utf-8'), headers=headers)).read() + upload_url = re.match('https://.*/assets', json.loads(resp.decode('utf-8'))['upload_url']).group(0) + + print('github_release: Uploading', zip, 'to', upload_url) + headers['Content-Type'] = 'application/zip' + headers['Content-Length'] = os.path.getsize(zip) + with open(zip, 'rb') as data: + urllib.request.urlopen(urllib.request.Request(upload_url + '?name=' + zip, data=data, headers=headers)) + +@log_errors("npm_publish") +def npm_publish(version): + print("npm_publish: Skip") + +@log_errors("update_homebrew") +def update_homebrew(version): + print("update_homebrew: Skip") # Update https://github.com/Homebrew/homebrew-cask-fonts + +@log_errors("update_scoop") +def update_scoop(version): + print("update_scoop: Skip") # Update https://github.com/matthewjberger/scoop-nerd-fonts/blob/master/bucket/FiraCode.json + +@log_errors("update_google_fonts") +def update_google_fonts(version): + print("update_google_fonts: Skip") + +if __name__ == '__main__': + os.chdir(common.root) + version = common.version() + package(version) + github_release(version) + npm_publish(version) + update_homebrew(version) + update_scoop(version) + update_google_fonts(version) + sys.exit(0) \ No newline at end of file diff --git a/script/update_version.py b/script/update_version.py index 2355dd6..159cbef 100755 --- a/script/update_version.py +++ b/script/update_version.py @@ -1,19 +1,8 @@ #! /usr/bin/env python3 +import common, os, re, subprocess, sys -import os, re, subprocess, sys - -def version(): - desc = subprocess.check_output(["git", "describe", "--tags"], cwd = os.path.dirname(__file__)).decode("utf-8") - match = re.match(r"([0-9]+)\.([0-9]+)-([0-9]+)-([a-z0-9]+)", desc) - if match: - major = int(match.group(1)) - minor = int(match.group(2)) + int(match.group(3)) - sha = match.group(4) - return (major, minor, sha) - else: - raise Exception("Can’t parse version from: " + desc) - -def update_version(major, minor, src, dst): +def update_version(major, minor, src): + print(f"update_version: {major}.{minor} in {src}") with open(src, 'r') as f: contents = f.read() contents = re.sub(r"versionMajor\s+=\s+\d+;", @@ -22,12 +11,11 @@ def update_version(major, minor, src, dst): contents = re.sub(r"versionMinor\s+=\s+\d+;", f"versionMinor = {minor};", contents) - with open(dst, 'w') as f: + with open(src, 'w') as f: f.write(contents) if __name__ == '__main__': - os.chdir(os.path.abspath(os.path.dirname(__file__) + '/..')) - (major, minor, sha) = version() - update_version(major, minor, 'FiraCode.glyphs', 'FiraCode.glyphs') - print(f"{major}.{minor}-{sha}") + os.chdir(common.root) + (major, minor) = common.version().split(".") + update_version(major, minor, 'FiraCode.glyphs') sys.exit(0) \ No newline at end of file