Skip to content

Commit

Permalink
Try a layer of indirection
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Jun 18, 2024
1 parent 1514030 commit 8622efe
Showing 1 changed file with 79 additions and 59 deletions.
138 changes: 79 additions & 59 deletions make_tool.cue
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import (
"tool/exec"
"strings"
"tool/file"
"tool/cli"
"list"
// "tool/cli"
// "encoding/json"
)

whencuefmtimports: cli.Print
#vars: {
rootdir: string
testdir: string
gitver: string
version: string
ldflags: string
latest: string
}

meta: {
vars: {
vars: #vars & {
rootdir: strings.TrimSpace(_commands.reporoot.stdout)
testdir: "\(rootdir)/test"
gitver: strings.TrimSpace(_commands.gitver.stdout)
Expand Down Expand Up @@ -42,141 +50,153 @@ meta: {
}
}

command: build: {
cfg: meta
task: build: {
vars: #vars

OutFile: *"xtemplate" | string

gobuild: exec.Run & {
OutFile: string | *"xtemplate"
env: {[string]: string}
cmd: ["go", "build", "-ldflags", cfg.vars.ldflags, "-buildmode", "exe", "-o", OutFile, "./cmd"]
dir: cfg.vars.rootdir
cmd: ["go", "build", "-ldflags", vars.ldflags, "-buildmode", "exe", "-o", OutFile, "./cmd"]
dir: vars.rootdir
success: true
}
}

command: run: {
cfg: meta
task: run: {
vars: #vars

gobuild: command.build & {gobuild: {OutFile: "\(cfg.vars.testdir)/xtemplate"}}
rmdataw: file.RemoveAll & {path: "\(cfg.vars.testdir)/dataw"}
mkdataw: file.Mkdir & {path: "\(cfg.vars.testdir)/dataw", $dep: rmdataw.$done}
mklog: file.Create & {filename: "\(cfg.vars.testdir)/xtemplate.log", contents: ""}
gobuild: task.build & {"vars": vars, OutFile: "\(vars.testdir)/xtemplate"}
rmdataw: file.RemoveAll & {path: "\(vars.testdir)/dataw"}
mkdataw: file.Mkdir & {path: "\(vars.testdir)/dataw", $dep: rmdataw.$done}
mklog: file.Create & {filename: "\(vars.testdir)/xtemplate.log", contents: ""}

ready: exec.Run & {
$dep: mklog.$done
cmd: ["bash", "-c", "grep -q 'starting server' <(tail -f xtemplate.log)"]
dir: cfg.vars.testdir
dir: vars.testdir
}

start: exec.Run & {
$dep: mkdataw.$done && mklog.$done && gobuild.$done
cmd: ["bash", "-c", "./xtemplate --loglevel -4 -d DB:sql:sqlite3:file:./dataw/test.sqlite -d FS:fs:./data --config-file config.json >xtemplate.log 2>&1"]
dir: cfg.vars.testdir
dir: vars.testdir
}
}

command: test: {
cfg: meta
task: test: {
vars: #vars

port: int | *8080

list: file.Glob & {glob: "\(cfg.vars.testdir)/tests/*.hurl"}
list: file.Glob & {glob: "\(vars.testdir)/tests/*.hurl"}
hurl: exec.Run & {
port: int | *8080
cmd: ["hurl", "--continue-on-error", "--test", "--report-html", "report", "--connect-to", "localhost:8080:localhost:\(port)"] + list.files
dir: cfg.vars.testdir
dir: vars.testdir
}
}

command: gotest: {
cfg: meta
task: gotest: {
vars: #vars

gotest: exec.Run & {
cmd: "go test -v ./..."
dir: cfg.vars.rootdir
dir: vars.rootdir
}
}

command: run_test: {
run: command.run & {start: mustSucceed: false}

test: command.test & {hurl: {$dep: run.ready.$done}}

kill: exec.Run & {$dep: test.hurl.$done, cmd: "pkill xtemplate"} // better way?
}

command: dist: {
cfg: meta
task: dist: {
vars: #vars

rmdist: file.RemoveAll & {path: "\(cfg.vars.rootdir)/dist"}
rmdist: file.RemoveAll & {path: "\(vars.rootdir)/dist"}

oses: ["linux", "darwin", "windows"]
arches: ["amd64"]
arches: ["amd64", "arm64"]
matrix: [for os in oses for arch in arches {GOOS: os, GOARCH: arch}]

for env in matrix {
(env.GOOS + "_" + env.GOARCH): {
dir: "\(cfg.vars.rootdir)/dist/xtemplate-\(env.GOARCH)-\(env.GOOS)"
dir: "\(vars.rootdir)/dist/xtemplate-\(env.GOARCH)-\(env.GOOS)"
exe: string | *"xtemplate"
if env.GOOS == "windows" {
exe: "xtemplate.exe"
}

mkdir: file.MkdirAll & {path: dir, $dep: rmdist.$done}
build: command.build.gobuild & {env: env, OutFile: "\(dir)/\(exe)", $dep: mkdir.$done}
build: task.build.gobuild & {"vars": vars, "env": env, OutFile: "\(dir)/\(exe)", $dep: mkdir.$done}
cp: exec.Run & {cmd: ["cp", "README.md", "LICENSE", "\(dir)"], $dep: mkdir.$done}
// tar: exec.Run & {cmd: ["tar", "czf", "\(dir)_\(cfg.vars.version).tar.gz", "-C", dir, "."], $dep: cp.$done && build.$done}
zip: exec.Run & {cmd: ["zip", "-jqr6", "\(dir)_\(cfg.vars.version).zip", dir], $dep: cp.$done && build.$done}
// tar: exec.Run & {cmd: ["tar", "czf", "\(dir)_\(vars.version).tar.gz", "-C", dir, "."], $dep: cp.$done && build.$done}
zip: exec.Run & {cmd: ["zip", "-jqr6", "\(dir)_\(vars.version).zip", dir], $dep: cp.$done && build.$done}
// rm: file.RemoveAll & {path: dir, $dep: zip.$done}
}
}

wait: {$dep: and([for name, step in command.dist if name =~ "_" {step.$done}])}
}

command: test_docker: {
cfg: meta
task: test_docker: {
vars: #vars

build: exec.Run & {
cmd: ["docker", "build", "-t", "xtemplate-test", "--target", "test", "--build-arg", "LDFLAGS=\(cfg.vars.ldflags)", "."]
dir: cfg.vars.rootdir
cmd: ["docker", "build", "-t", "xtemplate-test", "--target", "test", "--build-arg", "LDFLAGS=\(vars.ldflags)", "."]
dir: vars.rootdir
}
run: exec.Run & {
cmd: ["bash", "-c", "docker run -d --rm --name xtemplate-test -p 8081:80 xtemplate-test"]
cmd: "docker run -d --rm --name xtemplate-test -p 8081:80 xtemplate-test"
$dep: build.$done
}
ready: exec.Run & {
cmd: ["bash", "-c", "grep -q 'starting server' <(docker logs -f xtemplate-test)"]
$dep: run.$done
}
test: command.test & {hurl: {port: 8081, $dep: ready.$done}}
test: task.test & {port: 8081, hurl: $dep: ready.$done}
stop: exec.Run & {cmd: "docker stop xtemplate-test", $dep: test.hurl.$done} // be nice if we can always run this even if previous steps fail
}

command: build_docker: {
cfg: meta
task: build_docker: {
vars: #vars

tags: [...string] | *["infogulch/xtemplate:\(cfg.vars.version)"]
if cfg.vars.version == cfg.vars.latest {
tags: ["infogulch/xtemplate:\(cfg.vars.version)", "infogulch/xtemplate:latest"]
_latest: [...string] | *[]
if vars.version == vars.latest {
_latest: ["infogulch/xtemplate:latest"]
}
tags: ["infogulch/xtemplate:\(vars.version)"] + _latest

build: exec.Run & {
cmd: ["docker", "build"] + list.FlattenN([for t in tags {["-t", t]}], 1) + ["--build-arg", "LDFLAGS=\(cfg.vars.ldflags)", "."]
dir: cfg.vars.rootdir
cmd: ["docker", "build"] + list.FlattenN([for t in tags {["-t", t]}], 1) + ["--build-arg", "LDFLAGS=\(vars.ldflags)", "."]
dir: vars.rootdir
}
}

command: {
for k, t in task {
(k): {cfg: meta, vars: cfg.vars, t}
}
}

command: run_test: {
cfg: meta

run: task.run & {"vars": cfg.vars, start: mustSucceed: false}
test: task.test & {"vars": cfg.vars, hurl: $dep: run.ready.$done}
kill: exec.Run & {$dep: test.hurl.$done, cmd: "pkill xtemplate"} // better way?
}

command: ci: {
gotest: command.gotest
run_test: command.run_test
cfg: meta

dist: command.dist & {rmdist: $dep: run_test.kill.$done}
gotest: task.gotest & {"vars": cfg.vars}

test_docker: command.test_docker
run: task.run & {"vars": cfg.vars, start: mustSucceed: false}
test: task.test & {"vars": cfg.vars, hurl: $dep: run.ready.$done}
kill: exec.Run & {cmd: "pkill xtemplate", $dep: test.hurl.$done} // better way?

build_docker: command.build_docker & {build: $dep: test_docker.stop.$done}
dist: task.dist & {"vars": cfg.vars, rmdist: $dep: kill.$done}

test_docker: task.test_docker & {"vars": cfg.vars}
build_docker: task.build_docker & {"vars": cfg.vars, build: $dep: test_docker.stop.$done}
push: exec.Run & {
cmd: ["docker", "push"] + build_docker.tags
cmd: ["echo", "docker", "push"] + build_docker.tags
$dep: build_docker.build.$done
}
}

0 comments on commit 8622efe

Please sign in to comment.