~rycwo/tmplgen

bf112fba47cc17953a8bb2f084f5a0d1eaed16e7 — Ryan Chan 1 year, 4 days ago 60311ec master 0.1.1
Make flags package-level variables

Remove packaging scripts. tmplgen will be packaged for Arch Linux
instead.
5 files changed, 7 insertions(+), 121 deletions(-)

M Makefile
M README.md
D build-release
D contrib/upload-artifacts
M tmplgen.go
M Makefile => Makefile +1 -4
@@ 28,11 28,8 @@ test: all test.sh
install: all
	$(GO) install

release: all build-release
	./build-release

clean:
	$(GO) clean
	$(RM_DIR) build

.PHONY: all test install release clean
.PHONY: all test install clean

M README.md => README.md +0 -4
@@ 10,9 10,6 @@ This module depends on [Sprig][sprig] to provide additional template functions.
`tmplgen` is currently being used in [Forge][forge] to generate source files.
See [`vector.h.in`][forge-vec] for an example.

Pre-compiled binary releases can be downloaded from the [refs page][refs].
Navigate to the required version and download the archive for your platform.

## Usage

Please refer to the [text/template docs][pkg-template] for details on the


@@ 33,6 30,5 @@ tmplgen -o foo.h foo.h.in
[forge-vec]: https://git.sr.ht/~rycwo/forge/tree/master/item/include/forge/vector.h.in
[forge]: https://git.sr.ht/~rycwo/forge/
[pkg-template]: https://golang.org/pkg/text/template/
[refs]: https://git.sr.ht/~rycwo/tmplgen/refs
[sprig-funcs]: http://masterminds.github.io/sprig/
[sprig]: https://github.com/Masterminds/sprig

D build-release => build-release +0 -44
@@ 1,44 0,0 @@
#!/usr/bin/env bash
set -euo pipefail

readonly targets=(
	"linux/amd64"
	"linux/arm64"
	"freebsd/amd64"
	"openbsd/amd64"
)

version="$(git describe --abbrev=0 2> /dev/null)"
# Validate version string matches semver
if [[ ! "$version" =~ ^([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)$ ]]; then
	echo "Error: Version string '""$version""' invalid" >&2
	exit 1
fi

basename="tmplgen-""$version"

mkdir -p build/release

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

	platform="$os""_""$arch"

	outdir="build/""$platform"
	mkdir -p "$outdir"

	GOOS="$os" GOARCH="$arch" go build \
		-ldflags "-s -w -X main.version=""$version" \
		-o "$outdir""/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

D contrib/upload-artifacts => contrib/upload-artifacts +0 -62
@@ 1,62 0,0 @@
#!/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"])

M tmplgen.go => tmplgen.go +6 -7
@@ 13,14 13,13 @@ import (

var version string

var outFilePath = flag.String("o", "", "output `path` to write file to")
var showVersion = flag.Bool("v", false, "show version information and exit")

func main() {
	var outFilePath string
	var showVersion bool
	flag.StringVar(&outFilePath, "o", "", "output `path` to write file to")
	flag.BoolVar(&showVersion, "v", false, "show version information and exit")
	flag.Parse()

	if showVersion {
	if *showVersion {
		fmt.Println("tmplgen", version)
		os.Exit(0)
	}


@@ 50,8 49,8 @@ func main() {
	}

	var outFile io.Writer
	if outFilePath != "" {
		f, err := os.Create(outFilePath)
	if *outFilePath != "" {
		f, err := os.Create(*outFilePath)
		if err != nil {
			log.Fatal(err)
		}