Skip to content

Commit

Permalink
refactor: move commands to subdir 'lib/cmd'
Browse files Browse the repository at this point in the history
This also comes with an update to the Glue scripts, which I couldn't really avoid
  • Loading branch information
hyperupcall committed May 30, 2021
1 parent a151090 commit 32a8aa0
Show file tree
Hide file tree
Showing 23 changed files with 206 additions and 148 deletions.
60 changes: 28 additions & 32 deletions .glue/actions/auto/result-pacman-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,56 @@ main() {
ensure.file glue.toml
ensure.file glue-auto.toml

# TODO: use function
mkdir -p .glue/generated/tool-makepkg
toml.get_key project glue.toml
local myProject="$REPLY"

# TODO: get these values somewhere else
toml.get_key name glue.toml
local myPkg="$REPLY"
local myName="Edwin Kofler"
local myEmail="[email protected]"
toml.get_key desc glue.toml
local myDesc="$REPLY"

toml.get_key name glue.toml
local myName="$REPLY"

toml.get_key email glue.toml
local myEmail="$REPLY"

toml.get_key version glue-auto.toml
local myVer="$REPLY"
myVer="${myVer/-/_}"
local myVer="$REPLY"; myVer="${myVer/-/_}"

ensure.nonZero 'myProject' "$myProject"
ensure.nonZero 'myDesc' "$myDesc"
ensure.nonZero 'myName' "$myName"
ensure.nonZero 'myEmail' "$myEmail"
ensure.nonZero 'myVer' "$myVer"
# glue useConfig(tool-makepkg)
util.get_config "tool-makepkg/dev/PKGBUILD"

# glue useConfig(result-pacman-package)
util.get_config "result-pacman-package/dev/PKGBUILD"
pkgbuildFile="$REPLY"

generated.in 'result-pacman-package'
(
mkdir -p .glue/generated/tool-makepkg/dev
cd .glue/generated/tool-makepkg/dev || error.cd_failed
cd "$GENERATED_DIR" || error.cd_failed
mkdir dev
cd dev || error.cd_failed

cp "$pkgbuildFile" .
tar --create --directory "$GLUE_WD" --file "$myProject-$myVer.tar.gz" --exclude './.git' \
--exclude "$myProject-$myVer.tar.gz" --transform "s/^\./$myProject-$myVer/" ./

# TODO: bash templating
cp "$pkgbuildFile" .
sed -i -e "s/# Maintainer:.*/# Maintainer: $myName <$myEmail>/g" PKGBUILD
sed -i -e "s/pkgname=.*\$/pkgname='$myPkg'/g" PKGBUILD
sed -i -e "s/pkgname=.*\$/pkgname='$myProject'/g" PKGBUILD
sed -i -e "s/pkgver=.*\$/pkgver='$myVer'/g" PKGBUILD
sed -i -e "s/pkgdesc=.*\$/pkgdesc='$myDesc'/g" PKGBUILD
sed -i -e "s/url=.*\$/url='https:\/\/github.com\/eankeen\/$myPkg'/g" PKGBUILD
sed -i -e "s/source=.*\$/source=\(\$pkgname-\$pkgver.tar.gz::http:\/\/localhost:9334\/v\$pkgver.tar.gz\)/g" PKGBUILD

# TODO: assumption on working directory
tar --create --directory "$GLUE_WD" --file "$myPkg-$myVer.tar.gz" ../"$myPkg"
rm -rf "$myPkg-$myVer"
# tar xf "$myPkg-$myVer.tar.gz"
# mv "$myPkg" "$myPkg-$myVer"
# rm -rf "$myPkg-$myVer/.git"

local sum="$(sha256sum "$myPkg-$myVer.tar.gz")"
sed -i -e "s/url=.*\$/url='https:\/\/github.com\/eankeen\/$myProject'/g" PKGBUILD
sed -i -e "s/source=.*\$/source=\(\$pkgname-\$pkgver.tar.gz::\)/g" PKGBUILD
local sum="$(sha256sum "$myProject-$myVer.tar.gz")"
sum="${sum%% *}"
sed -i -e "s/sha256sums=.*\$/sha256sums=\('$sum'\)/g" PKGBUILD

namcap PKGBUILD
makepkg -Cfsrc
namcap ./*.zst
) || exit
generated.out

# TODO: think about more fine grained linting control in the whole SDLC
# namcap PKGBUILD
# namcap ./*.zst

}

main "$@"
Expand Down
20 changes: 20 additions & 0 deletions .glue/actions/auto/util-Bash-version-bump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
eval "$GLUE_BOOTSTRAP"
bootstrap || exit

unset main
main() {
local newVersion="$1"
ensure.nonZero 'newVersion' "$newVersion"

# TODO: show which files changed
find . -ignore_readdir_race -regex '\./pkg/.*\.\(sh\|bash\)' -print0 2>/dev/null \
| xargs -r0 \
sed -i -e "s|\(PROGRAM_VERSION=\"\).*\(\"\)|\1$newVersion\2|g"
log.info "util-Bash-version-bump: Bump done"
}

main "$@"
unset main

unbootstrap
5 changes: 2 additions & 3 deletions .glue/actions/auto/util-get-version.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env bash
eval "$GLUE_BOOTSTRAP"
bootstrap || exit

unset main
main() {
# If the working tree is dirty and there are unstaged changes
# for both tracked and untracked files
Expand All @@ -24,4 +23,4 @@ main() {
}

main "$@"
unbootstrap
unset main
68 changes: 46 additions & 22 deletions .glue/actions/auto/util-release-post.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
#!/usr/bin/env bash
# eval "$GLUE_BOOTSTRAP"
# bootstrap || exit
eval "$GLUE_BOOTSTRAP"
bootstrap || exit

# @file util-release-post.sh
# @brief Steps to perform after specialized version bumping
# - Ensure a dirty Git working tree
# - Add bumped files to commit with version number
# - Make GitHub release

unset main
main() {
local -r dryStatus="$1"
local newVersion="$2"

ensure.nonZero 'newVersion' "$newVersion"

ensure.cmd 'git'
ensure.cmd 'gh'

ensure.nonZero 'newVersion' "$newVersion"

isDry() {
# must be set to 'notDry' to not be dry.
# Defaults to 'not dry'
[ "$dryStatus" != "notDry" ]
}

if isDry; then
log.info "Running release process in dry mode"
fi

# Ensure working tree is dirty
if [ -z "$(git status --porcelain)" ]; then
die 'Working tree is not dirty. Cannot make a release if versions have not been bumped in their respective files'
if isDry; then
local cmd="log.warn"
else
local cmd="die"
fi

"$cmd" 'Working tree is not dirty. Cannot make a release if versions have not been bumped in their respective files'
fi

# Local Release
git add -A
git commit -m "chore(release): v$newVersion"
git tag -a "v$newVersion" -m "Release $newVersion" HEAD
git push --follow-tags origin HEAD

local -a args=()
if [ -f CHANGELOG.md ]; then
args+=("-F" "CHANGELOG.md")
elif [ -f changelog.md ]; then
args+=("-F" "changelog.md")
if isDry; then
log.info "Skipping Git taging and artifact release"
else
# '-F' is required for non-interactivity
args+=("-F" "")
log.warn 'CHANGELOG.md file not found. Creating empty notes file for release'
fi
git add -A
git commit -m "chore(release): v$newVersion"
git tag -a "v$newVersion" -m "Release $newVersion" HEAD
git push --follow-tags origin HEAD

# Remote Release
gh release create "v$newVersion" --target main --title "v$newVersion" "${args[@]}"
local -a args=()
if [ -f CHANGELOG.md ]; then
args+=("-F" "CHANGELOG.md")
elif [ -f changelog.md ]; then
args+=("-F" "changelog.md")
else
# '-F' is required for non-interactivity
args+=("-F" "")
log.warn 'CHANGELOG.md file not found. Creating empty notes file for release'
fi

# Remote Release
gh release create "v$newVersion" --target main --title "v$newVersion" "${args[@]}"
fi
}

main "$@"
unset main

# unbootstrap
unbootstrap
25 changes: 13 additions & 12 deletions .glue/actions/auto/util-release-pre.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env bash
eval "$GLUE_BOOTSTRAP"
bootstrap || exit

# @file util-release-pre.sh
# @brief Steps to perform before specialized version bumping
# @description Before the release is made, this ensures there
# are no changes in the working directory, and from the version
# in 'glue-auto.toml', increments the version. This does not handle
# anything language specific, so this is usually called along with
# 'util-release-post.sh', and any other files that need version bumping
# is performed in the interum
# @description This does the following
# - Ensures a clean Git working tree
# - Ensures a shared history (no force pushing)
# - Update version in 'glue-auto.toml'

unset main
main() {
Expand All @@ -17,12 +17,12 @@ main() {
local -r dryStatus="$1"
isDry() {
# must be set to 'notDry' to not be dry.
# Defaults to 'not try'
# Defaults to 'not dry'
[ "$dryStatus" != "notDry" ]
}

if isDry; then
log.info "Running release process in dry mode"
log.info "Running pre-release process in dry mode"
fi

# Ensure working tree not dirty
Expand Down Expand Up @@ -55,10 +55,8 @@ main() {
# glue useAction(util-get-version.sh)
util.get_action "util-get-version.sh"
source "$REPLY"
newVersion="$REPLY-DRY"
newVersion="$REPLY"
else
# Prompt for new version

# Get current version
toml.get_key version glue-auto.toml
local currentVersion="$REPLY"
Expand All @@ -72,7 +70,7 @@ main() {

# Ensure new version is valid (does not already exist)
if [ -n "$(git tag -l "v$newVersion")" ]; then
# TODO: ensure there are no tag sthat exists that are greater than it
# TODO: ensure there are no tags that exists that are greater than it
die 'Version already exists in a Git tag'
fi
fi
Expand All @@ -81,3 +79,6 @@ main() {
}

main "$@"
unset main

unbootstrap
12 changes: 7 additions & 5 deletions .glue/commands/auto/Bash.build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
eval "$GLUE_BOOTSTRAP"
bootstrap || exit

# glue useAction(util-release-pre.sh)
util.get_action 'util-release-pre.sh'
source "$REPLY" 'dry'
newVersion="$REPLY"
# glue useAction(util-get-version.sh)
util.get_action 'util-get-version.sh'
source "$REPLY"
declare newVersion="$REPLY"

sed -ie "s|\(version[ \t]*=[ \t]*\"\).*\(\"\)|\1${newVersion}\2|g" glue-auto.toml
# glue useAction(util-Bash-version-bump.sh)
util.get_action 'util-Bash-version-bump.sh'
source "$REPLY" "$newVersion"

unbootstrap
9 changes: 7 additions & 2 deletions .glue/commands/auto/Bash.release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
eval "$GLUE_BOOTSTRAP"
bootstrap || exit

#
# glue useAction(util-release-pre.sh)
util.get_action 'util-release-pre.sh'
source "$REPLY" 'notDry'
newVersion="$REPLY"

# TODO: generalize
sed -i -e "s|\(PROGRAM_VERSION=\"\).*\(\"\)|\1${newVersion}\2|g" glue.sh || :
# Bash version bump
(
find . -ignore_readdir_race -regex '\./pkg/.*\.\(sh\|bash\)' -print0 \
| xargs -r0 \
sed -i -e "s|\(PROGRAM_VERSION=\"\).*\(\"\)|\1${newVersion}\2|g" || :
) || exit

# glue useAction(util-release-post.sh)
util.get_action 'util-release-post.sh'
Expand Down
8 changes: 6 additions & 2 deletions .glue/commands/auto/Bash.releaseDry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ util.get_action 'util-release-pre.sh'
source "$REPLY" 'dry'
newVersion="$REPLY"

# TODO: generalize
sed -i -e "s|\(PROGRAM_VERSION=\"\).*\(\"\)|\1${newVersion}\2|g" glue.sh || :
# Bash version bump
(
find . -ignore_readdir_race -regex '\./pkg/.*\.\(sh\|bash\)' -print0 \
| xargs -r0 \
sed -i -e "s|\(PROGRAM_VERSION=\"\).*\(\"\)|\1${newVersion}\2|g" || :
) || exit

# glue useAction(util-release-post.sh)
util.get_action 'util-release-post.sh'
Expand Down
44 changes: 14 additions & 30 deletions .glue/commands/auto/Bash.run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,24 @@
eval "$GLUE_BOOTSTRAP"
bootstrap || exit

dirty=
if [ -n "$(git status --porcelain)" ]; then
dirty=yes
declare cmdName="$1"
ensure.nonZero 'cmdName' "$cmdName"
shift

if [ -d pkg ]; then
cd pkg || error.cd_failed
fi

if version="$(git describe --match 'v*' --abbrev=0 2>/dev/null)"; then
version="${version/#v/}"
declare execPath="$PWD/bin/$cmdName"
if [ -f "$execPath" ]; then
if [ -x "$execPath" ]; then
"$execPath" "$@"
else
error.not_executable "$execPath"
fi
else
version="0.0.0"
echo "Executable file '$execPath' not found"
fi

id="$(git rev-parse --short HEAD)"
version+="+$id${dirty:+-DIRTY}"

sed -i -e "s|\(version=\"\).*\(\"\)|\1${version}\2|g" glue.toml

# TODO: exec project

# if [ -f Taskfile.yml ]; then
# if command -v go-task &>/dev/null; then
# go-task run "$@"
# elif command -v task &>/dev/null; then
# if ! task help | grep -q Taskwarrior; then
# task run "$@"
# else
# ensure.cmd 'go-task'
# fi
# else
# ensure.cmd 'go-task'
# fi
# elif [ -f Justfile ]; then
# ensure.cmd 'just'

# just run "$@"
# fi

unbootstrap
Loading

0 comments on commit 32a8aa0

Please sign in to comment.