Skip to content

Commit

Permalink
feat(render): complex conditionals, goreleaser, build date and more
Browse files Browse the repository at this point in the history
  • Loading branch information
Depado committed Sep 21, 2023
1 parent f412d73 commit 1255ea6
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.vscode/
coverage.txt
qk
quokka
dist/
39 changes: 39 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
before:
hooks:
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
main: ./cmd/qk/main.go
binary: qk
ldflags:
- -s -w -X "main.Version={{.Env.VERSION}}" -X "main.Build={{ .Env.BUILD }}" -X "main.BuildDate={{ .Env.BUILDDATE }}"

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
29 changes: 0 additions & 29 deletions .goreleaser.yml

This file was deleted.

10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

BINARY=qk
VERSION=$(shell git describe --abbrev=0 --tags 2> /dev/null || echo "0.1.0")
SUFFIX=$(shell git describe --exact-match > /dev/null 2>&1 || echo "-dev")
BUILD=$(shell git rev-parse HEAD 2> /dev/null || echo "undefined")
LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.Build=$(BUILD)"
BUILDDATE=$(shell LANG=en_us_88591 date)
LDFLAGS=-ldflags "-X 'main.Version=$(VERSION)$(SUFFIX)' -X 'main.Build=$(BUILD)' -X 'main.BuildDate=$(BUILDDATE)' -s -w"

.PHONY: help
help:
Expand All @@ -19,16 +21,16 @@ install: ## Build and install

.PHONY: release
release: ## Create a new release on Github
VERSION=$(VERSION) BUILD=$(BUILD) goreleaser
VERSION="$(VERSION)" BUILD="$(BUILD)" BUILDDATE="$(BUILDDATE)" goreleaser releasee

.PHONY: snapshot
snapshot: ## Create a new snapshot release
VERSION=$(VERSION) BUILD=$(BUILD) goreleaser --snapshot --rm-dist
VERSION="$(VERSION)" BUILD="$(BUILD)" BUILDDATE="$(BUILDDATE)" goreleaser release --snapshot --clean

.PHONY: test
test: ## Run the test suite
go test ./...

.PHONY: clean
clean: ## Remove the binary
if [ -f $(BINARY) ] ; then rm $(BINARY) ; fi
if [ -f $(BINARY) ] ; then rm $(BINARY) ; fi
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![Go Version](https://img.shields.io/badge/Go%20Version-latest-brightgreen.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/Depado/quokka)](https://goreportcard.com/report/github.com/Depado/quokka)
[![Build Status](https://drone.depado.eu/api/badges/Depado/quokka/status.svg)](https://drone.depado.eu/Depado/quokka)
[![Build Status](https://drone.depa.do/api/badges/Depado/quokka/status.svg)](https://drone.depa.do/Depado/quokka)
[![codecov](https://codecov.io/gh/Depado/quokka/branch/master/graph/badge.svg)](https://codecov.io/gh/Depado/quokka)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Depado/quokka/blob/master/LICENSE)
[![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/Depado)
Expand Down Expand Up @@ -407,6 +407,18 @@ copy: true
You can even add per-file variables, or modify the delimiters. In fact, it's
like an inline `.quokka.yml` that applies to a single file.

Supported instructions are as follows:

- `if: condition`: Conditional rendering using an [expr](https://expr.medv.io/docs/Language-Definition)
expression that must return a boolean value
- `copy: true`: Do not attempt to render the file and simply copy it to its
destination
- `delimiters: ["[[", "]]"]`: Change the delimiters used for template rendering.
This can be useful for files that already are templates or use extensively the
`{}` chars
- `rename: newname`: Rename the file to this new name once rendered
- `ignore: true`: Completely ignore the file (no render, no copy)

## Conditional Rendering/Copy

You may want some files to not be copied or rendered according to what the user
Expand Down Expand Up @@ -435,9 +447,3 @@ workspace:
This file will be rendered if, and only if, the user answered yes to that
question. Note that `if` and `copy` can work together if you just want
to copy the file and not render it.

## Todo

- Per-file config:
- [ ] Allow more complex conditional rendering
- [ ] Unable to access sub variables in condition
12 changes: 9 additions & 3 deletions cmd/qk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import (
"fmt"
"log"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/Depado/quokka/cmd"
"github.com/Depado/quokka/renderer"
"github.com/Depado/quokka/utils"
)

// Build number and versions injected at compile time
var (
Version = "unknown"
Build = "unknown"
Version = "unknown"
Build = "unknown"
BuildDate = "unknown"
)

var qkdesc = `Quokka (qk) is a template engine that enables to render local or
Expand All @@ -32,6 +35,7 @@ var rootc = &cobra.Command{
Long: qkdesc,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
utils.OkPrintln("quokka", color.GreenString(Version))
renderer.Render(
args[0],
args[1],
Expand All @@ -50,7 +54,9 @@ var rootc = &cobra.Command{
var versionc = &cobra.Command{
Use: "version",
Short: "Show build and version",
Run: func(c *cobra.Command, args []string) { fmt.Printf("Build: %s\nVersion: %s\n", Build, Version) },
Run: func(c *cobra.Command, args []string) {
fmt.Printf("Build: %s\nVersion: %s\nBuild Date: %s\n", Build, Version, BuildDate)
},
}

// New command that will create a new empty quokka template
Expand Down
46 changes: 35 additions & 11 deletions conf/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"os"
"path"
"path/filepath"
"strings"
"text/template"
"unicode"

"github.com/antonmedv/expr"
"github.com/fatih/color"
"golang.org/x/text/cases"
"golang.org/x/text/language"
Expand Down Expand Up @@ -240,19 +242,41 @@ func (f *File) Render() error {
return nil
}
if condition != "" {
if v, ok := ctx[condition]; ok {
switch o := v.(type) {
case bool:
if !o {
utils.OkPrintln("Ignored ", color.GreenString(f.NewPath))
return nil
}
case string:
if o == "" {
utils.OkPrintln("Ignored ", color.GreenString(f.NewPath))
return nil
if len(strings.Fields(condition)) == 1 {
if v, ok := ctx[condition]; ok {
switch o := v.(type) {
case bool:
if !o {
utils.OkPrintln("Ignored ", color.GreenString(f.NewPath))
return nil
}
case string:
if o == "" {
utils.OkPrintln("Ignored ", color.GreenString(f.NewPath))
return nil
}
}
}
} else {
p, err := expr.Compile(condition, expr.AsBool())
if err != nil {
utils.ErrPrintln("Invalid conditional in", color.YellowString(f.Path), "-", color.RedString(err.Error()))
return nil
}
out, err := expr.Run(p, ctx)
if err != nil {
utils.ErrPrintln("Failed to run condition in", color.YellowString(f.Path), "-", color.RedString(err.Error()))
return nil
}
res, ok := out.(bool)
if !ok {
utils.ErrPrintln("Condition didn't return a boolean value in", color.YellowString(f.Path))
return nil
}
if !res {
utils.OkPrintln("Ignored ", color.GreenString(f.NewPath))
return nil
}
}
}
if copy {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/Depado/quokka
go 1.21

require (
github.com/antonmedv/expr v1.15.2
github.com/briandowns/spinner v1.23.0
github.com/fatih/color v1.15.0
github.com/spf13/cobra v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/g
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/antonmedv/expr v1.15.2 h1:afFXpDWIC2n3bF+kTZE1JvFo+c34uaM3sTqh8z0xfdU=
github.com/antonmedv/expr v1.15.2/go.mod h1:0E/6TxnOlRNp81GMzX9QfDPAmHo2Phg00y4JUv1ihsE=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
Expand Down

0 comments on commit 1255ea6

Please sign in to comment.