~rycwo/tmplgen

c83b060e4001f4be37df9b679e053cf05a656cd3 — Ryan Chan 4 months ago 5a014f6
Add Makefile and build manifest
9 files changed, 152 insertions(+), 7 deletions(-)

A .build.yml
M .editorconfig
A .gitignore
A Makefile
M README.md
A build-release
M go.mod
A test.sh
R tmpgen.go => tmplgen.go
A .build.yml => .build.yml +27 -0
@@ 0,0 1,27 @@
---
image: alpine/edge
packages:
    - go
sources:
    - https://git.sr.ht/~rycwo/tmplgen
tasks:
    - build: |
        cd tmplgen
        make
    - 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
      to: "Ryan Chan <rycwo@posteo.net>"
...

M .editorconfig => .editorconfig +3 -0
@@ 9,6 9,9 @@ insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true

[*.yml]
indent_style = space

[*.md]
indent_size = 2
indent_style = space

A .gitignore => .gitignore +2 -0
@@ 0,0 1,2 @@
build/
tmplgen

A Makefile => Makefile +38 -0
@@ 0,0 1,38 @@
# Boilerplate variables from:
# http://clarkgrubb.com/makefile-style-guide
MAKEFLAGS += --warn-undefined-variables
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.DEFAULT_GOAL := all
.DELETE_ON_ERROR:
.SUFFIXES:

RM := rm
RM_DIR := $(RM) --recursive --force

GO ?= go
GOFLAGS ?=

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

tmplgen: tmplgen.go
	$(GO) build $(GOFLAGS) \
		-ldflags "-s -w -X main.version=""$(version)"

all: tmplgen

test: all test.sh
	$(SHELL) test.sh

install: all
	$(GO) install

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

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

.PHONY: all test install release clean

M README.md => README.md +7 -5
@@ 1,11 1,13 @@
## Overview

[![builds.sr.ht status](https://builds.sr.ht/~rycwo/tmplgen.svg)](https://builds.sr.ht/~rycwo/tmplgen?)

Simple wrapper around Go [text/template][pkg-template] to read
**self-contained** template files and write generated results to a file.

This module depends on [Sprig][sprig] to provide additional template functions.

`tmpgen` is currently being used in [Forge][forge] to generate source files.
`tmplgen` is currently being used in [Forge][forge] to generate source files.
See [`vector.h.in`][forge-vec] for an example.

## Usage


@@ 15,15 17,15 @@ template syntax. Additional Sprig function docs can be [found
here][sprig-funcs].

```sh
# tmpgen -o <filename> <filename>
tmpgen -o foo.h foo.h.in
# tmplgen -o <filename> <filename>
tmplgen -o foo.h foo.h.in
```

`tmpgen` can also read from stdin and write to stdout.
`tmplgen` can also read from stdin and write to stdout.

## License

[Do whatever you want to do!](LICENSE)
[Do what you want to!](LICENSE)

[forge-vec]: https://git.sr.ht/~rycwo/forge/tree/master/item/include/forge/vector.h.in
[forge]: https://git.sr.ht/~rycwo/forge/

A build-release => build-release +38 -0
@@ 0,0 1,38 @@
#!/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

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

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

	checksum="$(sha256sum "$archive")"
	echo "$checksum"" ""$archive" >> "build/""$basename""_checksums.txt"

	rm build/tmplgen
done

M go.mod => go.mod +1 -1
@@ 1,4 1,4 @@
module git.sr.ht/~rycwo/tmpgen
module git.sr.ht/~rycwo/tmplgen

go 1.16


A test.sh => test.sh +25 -0
@@ 0,0 1,25 @@
# Basic test to see if tmplgen can successfully execute a template
set -euo pipefail

readonly expected="{
	\"foo\": 0,
	\"bar\": 1,
	\"baz\": 2
}"

result="$(./tmplgen << EOF
{{\$myList := list "foo" "bar" "baz" -}}
{
	{{- range \$v, \$k := \$myList}}
	{{printf "\"%s\": %d" \$k \$v}}{{if ne \$k (\$myList | last)}},{{end}}
	{{- end}}
}
EOF
)"

if [[ "$result" != "$expected" ]]; then
	echo "Test failed!" >&2
	exit 1
fi

echo "Test passed!" >&2

R tmpgen.go => tmplgen.go +11 -1
@@ 2,6 2,7 @@ package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"


@@ 10,11 11,20 @@ import (
	"github.com/Masterminds/sprig/v3"
)

var version string

func main() {
	var outFilePath string
	flag.StringVar(&outFilePath, "o", "", "output `path` to write output to")
	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 {
		fmt.Println("tmplgen", version)
		os.Exit(0)
	}

	var inFile io.Reader
	if inFilePath := flag.Arg(0); inFilePath != "" {
		f, err := os.Open(inFilePath)