Skip to content

Commit

Permalink
chore: Add justfile and scripts for task management
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed Dec 1, 2023
1 parent b3c86c8 commit 606ef27
Show file tree
Hide file tree
Showing 5 changed files with 413 additions and 0 deletions.
54 changes: 54 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 1,54 @@
GIT_DIR := `git rev-parse --show-toplevel`

MAIN := "."
BIN_NAME := `basename $(git rev-parse --show-toplevel)`
BIN_DIR := "bin"
DIST_DIR := "dist"

# list available recipes
default:
@just --list

# format code
fmt:
go fmt ./...

# lint code
lint:
golangci-lint run ./...

# vet code
vet:
go vet ./...

# build application
build *args="":
{{GIT_DIR}}/scripts/build.sh -p {{MAIN}} {{args}}

# create binary distribution
dist *args="":
{{GIT_DIR}}/scripts/dist.sh -p {{MAIN}} {{args}}

# create a new release
release *args="":
#!/bin/sh
export GITHUB_OWNER=cluttrdev
export GITHUB_REPO=deepl-tui
{{GIT_DIR}}/scripts/release.sh -p {{MAIN}} {{args}}

changes from="" to="":
#!/bin/sh
source {{GIT_DIR}}/scripts/functions.sh
get_changes {{from}} {{to}}

clean:
@# build artifacts
@echo "rm {{BIN_DIR}}/{{BIN_NAME}}"
@-[ -f {{BIN_DIR}}/{{BIN_NAME}} ] && rm {{BIN_DIR}}/{{BIN_NAME}}
@-[ -d {{BIN_DIR}} ] && rmdir {{BIN_DIR}}

@# distribution binaries
@echo "rm {{DIST_DIR}}/{{BIN_NAME}}_*"
@rm {{DIST_DIR}}/{{BIN_NAME}}_* 2>/dev/null || true
@-[ -d {{DIST_DIR}} ] && rmdir {{DIST_DIR}}

61 changes: 61 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 1,61 @@
#!/usr/bin/env bash

set -euo pipefail

usage() { echo "usage: $(basename -- $0) [-h] [-v] [-o OUT] [-p PKG]" 1>&2; }

package=""
output=""
verbose=false
while getopts ":ho:p:v" opt; do
case "${opt}" in
h)
usage
exit 0
;;
o)
output=${OPTARG}
;;
p)
package=${OPTARG}
;;
v)
verbose=true
;;
*)
usage
exit 1
;;
esac
done

# load common helpers
scriptsdir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
source "${scriptsdir}/functions.sh"

version=$(get_version)
commit_sha=$(get_commit_sha)
commit_date=$(get_commit_date)

if [ -z "${package}" ]; then
package="."
fi

if [ -z "${output}" ]; then
git_dir=$(get_git_dir)
bin_dir="${git_dir}/bin"
bin_name=$(basename $(realpath "${package}"))
output="${bin_dir}/${bin_name}"
fi

goos=${GOOS:-$(go env GOHOSTOS)}
goarch=${GOARCH:-$(go env GOHOSTARCH)}

${verbose} && echo "Building for ${goos}/${goarch}..."
GOOS=${goos} GOARCH=${goarch} go build \
-ldflags "-X 'main.version=${version}' -X 'main.commit=${commit_sha}' -X 'main.date=${commit_date}'" \
-o "${output}" \
"${package}"
${verbose} && echo "Building for ${goos}/${goarch}... done"

exit 0
72 changes: 72 additions & 0 deletions scripts/dist.sh
Original file line number Diff line number Diff line change
@@ -0,0 1,72 @@
#!/usr/bin/env bash

set -euo pipefail

usage() { echo "usage: $(basename -- $0) [-h] [-v] [-n NAME] [-d DIR] [-p PKG]" 1>&2; }

package=""
dist_dir=""
bin_name=""
verbose=false
while getopts ":hd:n:p:v" opt; do
case "${opt}" in
h)
usage
exit 0
;;
d)
dist_dir=${OPTARG}
;;
n)
bin_name=${OPTARG}
;;
p)
package=${OPTARG}
;;
v)
verbose=true
;;
*)
usage
exit 1
;;
esac
done

# load common helpers
scriptsdir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
source "${scriptsdir}/functions.sh"

if [ -z "${package}" ]; then
package="."
fi

if [ -z "${dist_dir}" ]; then
git_dir=$(get_git_dir)
dist_dir="${git_dir}/dist"
fi

if [ -z "${bin_name}" ]; then
bin_name=$(basename $(realpath "${package}"))
fi

version=$(get_version)

declare -A OSARCHMAP=(
[linux]="amd64,arm,arm64"
[darwin]="amd64,arm64"
[windows]="amd64,arm,arm64"
)

${verbose} && echo "Building binaries..."
for os in ${!OSARCHMAP[@]}; do
for arch in ${OSARCHMAP[$os]//,/ }; do
out="${dist_dir}/${bin_name}_${version}_${os}_${arch}"

${verbose} && echo " for ${os}/${arch}"
GOOS=${os} GOARCH=${arch} ${scriptsdir}/build.sh -o "${out}" -p "${package}"
done
done
${verbose} && echo "Building binaries... done"

exit 0
71 changes: 71 additions & 0 deletions scripts/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 1,71 @@
#!/bin/sh

get_git_dir() {
git rev-parse --show-toplevel
}

get_tag() {
git describe --exact-match 2>/dev/null
}

get_commit_sha() {
git rev-parse --verify ${1:-HEAD}
}

get_commit_date() {
TZ=UTC git show --no-patch --format='' --date='format-local:%Y-%m-%dT%H:%M:%SZ' ${1:-HEAD}
}

get_pseudo_version() {
ref=${1:-"HEAD"}
prefix=${2:-"v0.0.0"}

latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || true)
if [ -n "${latest_tag}" ]; then
prefix=${latest_tag}-$(git rev-list --count ${latest_tag}..${ref})
fi

# UTC time the revision was created (yyyymmddhhmmss).
timestamp=$(TZ=UTC git show --no-patch --format='' --date='format-local:%Y%m%d%H%M%S' ${ref})

# 12-character prefix of the commit hash
revision=$(git rev-parse --short=12 --verify ${ref})

echo "${prefix}-${timestamp}-${revision}"
}

get_version() {
tag=$(git describe --exact-match 2>/dev/null || true)
if [ -n "${tag}" ]; then
echo ${tag}
else
get_pseudo_version
fi
}

is_dirty() {
! git diff --quiet
}

is_tagged() {
test -n "$(get_tag)"
}

get_changes() {
from=${1:-""}
to=${2:-HEAD}

if [ -z "${from}" ]; then
from=$(git describe --tags --abbrev=0 2>/dev/null)
fi

if [ "${from}" == "$(git describe --tags --exact-match ${to} 2>/dev/null)" ]; then
from=$(git describe --tags --abbrev=0 --exclude=${from} 2>/dev/null)
fi

if [ -n "${from}" ]; then
git log --oneline --no-decorate ${from}..${to}
else
git log --oneline --no-decorate ${to}
fi
}
Loading

0 comments on commit 606ef27

Please sign in to comment.