Skip to content

Commit 4905eb5

Browse files
committed
github release on tag
1 parent 776770e commit 4905eb5

File tree

5 files changed

+116
-31
lines changed

5 files changed

+116
-31
lines changed

.github/workflows/build.yml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
on: [push]
1+
on:
2+
push:
3+
branches:
4+
- 'master'
5+
tags:
6+
- '[0-9]+.[0-9]+'
7+
paths:
8+
- '.github/workflows/**'
9+
- 'FiraCode.glyphs'
10+
- 'script/**'
11+
212
jobs:
313
build:
414
runs-on: ubuntu-latest
515
container: tonsky/firacode:latest
616
steps:
717
- uses: actions/checkout@v2
8-
- run: echo ${GITHUB_REF#refs/heads/}
9-
- run: echo "version=$(./script/update_version.py)" >> $GITHUB_ENV
18+
- if: startsWith(github.ref, 'refs/tags/')
19+
run: python3 ./script/update_version.py
1020
- run: ./script/build.sh
21+
- run: echo "hash=$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
1122
- uses: actions/upload-artifact@v2
1223
with:
13-
name: Fira_Code_${{ env.version }}
14-
path: distr
24+
name: Fira_Code_${{ env.hash }}
25+
path: distr
26+
- if: startsWith(github.ref, 'refs/tags/')
27+
run: python3 ./script/release.py

script/common.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#! /usr/bin/env python3
2+
import argparse, os, re, subprocess
3+
4+
root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
5+
6+
def version():
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument('--version')
9+
(args, _) = parser.parse_known_args()
10+
if args.version:
11+
return args.version
12+
13+
ref = os.getenv('GITHUB_REF')
14+
if ref and ref.startswith('refs/tags/'):
15+
return ref[len('refs/tags/'):]
16+
17+
raise Exception("Can’t identify version")

script/release

Lines changed: 0 additions & 7 deletions
This file was deleted.

script/release.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#! /usr/bin/env python3
2+
import argparse, base64, common, glob, os, platform, re, subprocess, sys, urllib.request, zipfile
3+
4+
def log_errors(name):
5+
def wrap(f):
6+
def result(*args, **kwargs):
7+
try:
8+
f(*args, **kwargs)
9+
except Exception as e:
10+
print(f"{name}: Failed {e}")
11+
return result
12+
return wrap
13+
14+
def package(version):
15+
zip = f"Fira_Code_v{version}.zip"
16+
17+
print('Package:', zip)
18+
with zipfile.ZipFile(zip, 'w', compression = zipfile.ZIP_DEFLATED, compresslevel = 9) as archive:
19+
for f in glob.glob("distr/**", recursive = True):
20+
arcname = f[len("distr/"):]
21+
if arcname and not os.path.basename(arcname).startswith("."):
22+
archive.write(f, arcname)
23+
24+
def github_headers():
25+
if os.environ.get('GITHUB_BASIC'):
26+
auth = 'Basic ' + base64.b64encode(os.environ.get('GITHUB_BASIC').encode('utf-8')).decode('utf-8')
27+
else:
28+
auth = 'token ' + os.environ.get('API_TOKEN')
29+
return {
30+
'Accept': 'application/vnd.github.v3+json',
31+
'Authorization': auth
32+
}
33+
34+
@log_errors("github_release")
35+
def github_release(version):
36+
zip = f"Fira_Code_v{version}.zip"
37+
38+
data = '{"tag_name":"' + version + '","name":"' + version + '"}'
39+
headers = github_headers()
40+
resp = urllib.request.urlopen(urllib.request.Request('https://api.github.com/repos/tonsky/FiraCode/releases', data=data.encode('utf-8'), headers=headers)).read()
41+
upload_url = re.match('https://.*/assets', json.loads(resp.decode('utf-8'))['upload_url']).group(0)
42+
43+
print('github_release: Uploading', zip, 'to', upload_url)
44+
headers['Content-Type'] = 'application/zip'
45+
headers['Content-Length'] = os.path.getsize(zip)
46+
with open(zip, 'rb') as data:
47+
urllib.request.urlopen(urllib.request.Request(upload_url + '?name=' + zip, data=data, headers=headers))
48+
49+
@log_errors("npm_publish")
50+
def npm_publish(version):
51+
print("npm_publish: Skip")
52+
53+
@log_errors("update_homebrew")
54+
def update_homebrew(version):
55+
print("update_homebrew: Skip") # Update https://github.com/Homebrew/homebrew-cask-fonts
56+
57+
@log_errors("update_scoop")
58+
def update_scoop(version):
59+
print("update_scoop: Skip") # Update https://github.com/matthewjberger/scoop-nerd-fonts/blob/master/bucket/FiraCode.json
60+
61+
@log_errors("update_google_fonts")
62+
def update_google_fonts(version):
63+
print("update_google_fonts: Skip")
64+
65+
if __name__ == '__main__':
66+
os.chdir(common.root)
67+
version = common.version()
68+
package(version)
69+
github_release(version)
70+
npm_publish(version)
71+
update_homebrew(version)
72+
update_scoop(version)
73+
update_google_fonts(version)
74+
sys.exit(0)

script/update_version.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
#! /usr/bin/env python3
2+
import common, os, re, subprocess, sys
23

3-
import os, re, subprocess, sys
4-
5-
def version():
6-
desc = subprocess.check_output(["git", "describe", "--tags"], cwd = os.path.dirname(__file__)).decode("utf-8")
7-
match = re.match(r"([0-9]+)\.([0-9]+)-([0-9]+)-([a-z0-9]+)", desc)
8-
if match:
9-
major = int(match.group(1))
10-
minor = int(match.group(2)) + int(match.group(3))
11-
sha = match.group(4)
12-
return (major, minor, sha)
13-
else:
14-
raise Exception("Can’t parse version from: " + desc)
15-
16-
def update_version(major, minor, src, dst):
4+
def update_version(major, minor, src):
5+
print(f"update_version: {major}.{minor} in {src}")
176
with open(src, 'r') as f:
187
contents = f.read()
198
contents = re.sub(r"versionMajor\s+=\s+\d+;",
@@ -22,12 +11,11 @@ def update_version(major, minor, src, dst):
2211
contents = re.sub(r"versionMinor\s+=\s+\d+;",
2312
f"versionMinor = {minor};",
2413
contents)
25-
with open(dst, 'w') as f:
14+
with open(src, 'w') as f:
2615
f.write(contents)
2716

2817
if __name__ == '__main__':
29-
os.chdir(os.path.abspath(os.path.dirname(__file__) + '/..'))
30-
(major, minor, sha) = version()
31-
update_version(major, minor, 'FiraCode.glyphs', 'FiraCode.glyphs')
32-
print(f"{major}.{minor}-{sha}")
18+
os.chdir(common.root)
19+
(major, minor) = common.version().split(".")
20+
update_version(major, minor, 'FiraCode.glyphs')
3321
sys.exit(0)

0 commit comments

Comments
 (0)