~rycwo/tmplgen

ab5b8b723e7e84d01c2a74c7e7763d4ef88cc5fd — Ryan Chan 4 months ago c83b060 0.1.0
Add helper scripts to generate release
5 files changed, 80 insertions(+), 21 deletions(-)

M .build.yml
M .editorconfig
M Makefile
M build-release
A contrib/upload-artifacts
M .build.yml => .build.yml +0 -9
@@ 11,15 11,6 @@ tasks:
    - test: |
        cd tmplgen
        make test
    - release: |
        cd tmplgen
        if git describe --exact-match > /dev/null 2>&1; then
            # TODO(rycwo)
            # make release
            true
        else
            echo "No deployment"
        fi
triggers:
    - action: email
      condition: always

M .editorconfig => .editorconfig +1 -1
@@ 9,7 9,7 @@ insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true

[*.yml]
[*.{yml,py}]
indent_style = space

[*.md]

M Makefile => Makefile +2 -2
@@ 14,7 14,7 @@ GO ?= go
GOFLAGS ?=

# Current Git version
version := $(shell git describe --long --dirty --tags 2> /dev/null)
version := $(shell git describe --long --tags 2> /dev/null)

tmplgen: tmplgen.go
	$(GO) build $(GOFLAGS) \


@@ 28,7 28,7 @@ test: all test.sh
install: all
	$(GO) install

release: test  # Only build release binaries if tests have passed
release: all build-release
	./build-release

clean:

M build-release => build-release +15 -9
@@ 17,22 17,28 @@ fi

basename="tmplgen-""$version"

mkdir -p build
mkdir -p build/release

for target in "${targets[@]}"; do
	IFS="/" read -ra tokens <<< "$target"
	os="${tokens[0]}"
	arch="${tokens[1]}"

	GOOS="$os" GOARCH="$arch" go build \
		-ldflags "-s -w -X main.version=""$version" \
		-o build/tmplgen
	platform="$os""_""$arch"

	archive="build/""$basename""-""$os""-""$arch"".tar.gz"
	tar -czf "$archive" build/tmplgen LICENSE
	outdir="build/""$platform"
	mkdir -p "$outdir"

	checksum="$(sha256sum "$archive")"
	echo "$checksum"" ""$archive" >> "build/""$basename""_checksums.txt"
	GOOS="$os" GOARCH="$arch" go build \
		-ldflags "-s -w -X main.version=""$version" \
		-o "$outdir""/tmplgen"

	rm build/tmplgen
	archive="$basename""_""$platform"".tar.gz"
	tar -czf "$archive" LICENSE -C "$outdir" tmplgen
	echo "$archive"
	mv "$archive" build/release
done

cd build/release
sha256sum *.tar.gz > "$basename""_checksums.txt"
cd - > /dev/null

A contrib/upload-artifacts => contrib/upload-artifacts +62 -0
@@ 0,0 1,62 @@
#!/usr/bin/env python3
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
import fileinput
import json
import os
import re
import subprocess
import sys


p = subprocess.run(["git", "describe", "--abbrev=0"],
        stdout=subprocess.PIPE, check=True)
tag = p.stdout.decode().strip()

if not re.match(r"^(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)$", tag):
    print(f"Error: '{tag}' does not match semver requirements", sys.stderr)
    sys.exit(1)

oauth_token = os.environ.get("SRHT_OAUTH_TOKEN")
if not oauth_token:
    print(f"Error: OAuth token required", sys.stderr)
    sys.exit(1)

transport = AIOHTTPTransport(
        url="https://git.sr.ht/query",
        headers={"Authorization": f"Bearer {oauth_token}"})

client = Client(transport=transport, fetch_schema_from_transport=True)

get_repo_id = gql(
        """
query getRepoId($repo: String!) {
    repositoryByName(name: $repo) {
        id
    }
}
"""
)

repo_name = os.path.basename(os.path.abspath(os.getcwd()))
r = client.execute(get_repo_id, variable_values={"repo": repo_name})
repo_id = r["repositoryByName"]["id"]

upload_artifact = gql(
        """
mutation($repo: Int!, $ref: String! $file: Upload!) {
    uploadArtifact(repoId: $repo, revspec: $ref, file: $file) {
        id
    }
}
"""
)

for arg in sys.argv[1:]:
    if not os.path.isfile(arg):
        continue
    with open(arg, "rb") as f:
        params = {"repo": repo_id, "ref": tag, "file": f}
        r = client.execute(upload_artifact,
                variable_values=params, upload_files=True)
        print(r["uploadArtifact"]["id"])