Skip to content

Commit

Permalink
Merge pull request #3 from rynkowsg/g/embrace-sosh
Browse files Browse the repository at this point in the history
refactor: Embrace sosh
  • Loading branch information
rynkowsg authored Apr 12, 2024
2 parents edfca42 + 5b2576b commit b89b2c4
Show file tree
Hide file tree
Showing 27 changed files with 864 additions and 281 deletions.
55 changes: 55 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
version: 2.1

orbs:
# orb info: https://circleci.com/developer/orbs/orb/rynkowsg/asdf
asdf: rynkowsg/[email protected]
# orb info: https://circleci.com/developer/orbs/orb/rynkowsg/checkout
checkout: rynkowsg/[email protected]
# orb info: https://circleci.com/developer/orbs/orb/rynkowsg/rynkowsg
rynkowsg: rynkowsg/[email protected]

jobs:
checks:
executor: rynkowsg/docker_x86_cimg_base
steps:
- checkout/checkout: {depth: 1}
- asdf/install: {version: 0.14.0}
- restore_cache:
name: asdf - restore cache (partial)
keys:
- asdf-{{ checksum ".tool-versions" }}
- run:
name: asdf - add plugin
command: |
set -x
cd /tmp
git config --global advice.detachedHead false
asdf plugin add babashka https://github.com/fredZen/asdf-babashka.git
asdf plugin add java https://github.com/halcyon/asdf-java.git
asdf plugin add shellcheck https://github.com/luizm/asdf-shellcheck.git
asdf plugin add shfmt https://github.com/luizm/asdf-shfmt.git
- run:
name: asdf - install tools
command: |
set -x
bash --version
asdf install
bb --version
java --version
shellcheck --version
shfmt --version
- save_cache:
name: asdf - save cache (partial)
key: asdf-{{ checksum ".tool-versions" }}
paths:
- ~/.asdf/installs
- ~/.asdf/plugins
- ~/.asdf/shims
- rynkowsg/install_sosh: { version: "v0.2.0" }
- run: make lint
- run: make format-check

workflows:
lint_and_format:
jobs:
- checks
8 changes: 1 addition & 7 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ on:
push:
branches:
- main
- dev
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected] # https://github.com/actions/checkout/releases
- uses: asdf-vm/actions/[email protected] # https://github.com/asdf-vm/actions/releases
- run: scripts/lint.bash

actionlint:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- dev
pull_request:

jobs:
Expand Down
16 changes: 15 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
.idea
# JetBrains IDEs
/.idea/*
!/.idea/codeStyles/
!/.idea/copyright/
!/.idea/dictionaries/
!/.idea/fileTemplates/
!/.idea/repl-commands.xml
!/.idea/runConfigurations/
*.iml

# MacOS
.DS_Store

# sosh
/.github_deps
9 changes: 8 additions & 1 deletion .idea/dictionaries/user.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# PLUGIN: https://github.com/fredZen/asdf-babashka
# RELEASES: https://github.com/babashka/babashka/releases
babashka 1.3.189

# required for: sosh
# PLUGIN: https://github.com/halcyon/asdf-java
java openjdk-11.0.2

# PLUGIN: https://github.com/luizm/asdf-shellcheck
# RELEASES: https://github.com/koalaman/shellcheck/releases
shellcheck 0.9.0

# PLUGIN: https://github.com/luizm/asdf-shfmt
# RELEASES: https://github.com/mvdan/sh/releases
shfmt 3.7.0
41 changes: 41 additions & 0 deletions @bin/format.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Copyright (c) 2024 Greg Rynkowski. All rights reserved.
# License: MIT License

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# Validates/corrects format
#
# Example:
#
# - check: @bin/format.bash check
# - apply: @bin/format.bash apply
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Bash Strict Mode Settings
set -euo pipefail
# Path Initialization
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P || exit 1)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd -P || exit 1)"
export SHELL_GR_DIR="${ROOT_DIR}/.github_deps/rynkowsg/shell-gr@1a30383" # needed for shell-gr to resolve paths
# Library Sourcing
source "${ROOT_DIR}/.github_deps/rynkowsg/shell-gr@1a30383/lib/tool/format.bash" # format_with_env

main() {
local format_cmd_type="${1:-"apply"}"
local error=0
format_with_env "${format_cmd_type}" bash \
< <(
find "${ROOT_DIR}" \( -type f \( -name '*.bash' -o -name '*.sh' \) -o -path '*/bin/*' \) \
| grep -v -E '(.github_deps|/gen/)' \
| sort
) \
|| ((error += $?))
format_with_env "${format_cmd_type}" bats < <(find "${ROOT_DIR}" -type f -name '*.bats' | sort) || ((error += $?))
if ((error > 0)); then
exit "$error"
fi
}

main "$@"
39 changes: 39 additions & 0 deletions @bin/lint.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
# Copyright (c) 2024 Greg Rynkowski. All rights reserved.
# License: MIT License

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# Lint bash source files
#
# Example:
#
# @bin/lint.bash
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Bash Strict Mode Settings
set -euo pipefail
# Path Initialization
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P || exit 1)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd -P || exit 1)"
export SHELL_GR_DIR="${ROOT_DIR}/.github_deps/rynkowsg/shell-gr@1a30383" # needed for shell-gr to resolve paths
# Library Sourcing
source "${ROOT_DIR}/.github_deps/rynkowsg/shell-gr@1a30383/lib/tool/lint.bash" # lint

main() {
local error=0
lint bash \
< <(
find "${ROOT_DIR}" \( -type f \( -name '*.bash' -o -name '*.sh' \) -o -path '*/bin/*' \) \
| grep -v -E '(.github_deps|/gen/)' \
| sort
) \
|| ((error += $?))
lint bats < <(find "${ROOT_DIR}" -type f -name '*.bats' | sort) || ((error += $?))
if ((error > 0)); then
exit "$error"
fi
}

main
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.PHONY: deps-clean _format_deps format-check format _lint_deps lint

SCRIPTS := $(wildcard bin/*)
deps: $(SCRIPTS)
@$(foreach script,$^,echo "Fetching for $(script)"; sosh fetch $(script);)

deps-clean:
rm -rf lib/.github_deps
make deps

_format_deps: @bin/format.bash
sosh fetch @bin/format.bash

format-check: _format_deps
\@bin/format.bash check

format: _format_deps
\@bin/format.bash apply

_lint_deps: @bin/lint.bash
sosh fetch @bin/lint.bash

lint: _format_deps _lint_deps deps
\@bin/lint.bash
140 changes: 35 additions & 105 deletions bin/download
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
# Copyright (c) 2024 Greg Rynkowski. All rights reserved.
# License: MIT License

###
# Script used to download package by 'install' command
Expand All @@ -10,112 +12,40 @@
# To see debug logs use env var `DEBUG=true`.
###

#set -euo pipefail

CURRENT_SCRIPT_PATH=${BASH_SOURCE[0]}
PLUGIN_DIR=$(dirname "$(dirname "${CURRENT_SCRIPT_PATH}")")

# shellcheck source=./lib/common.bash
source "${PLUGIN_DIR}/lib/common.bash" # CURL_OPTS, GH_REPO, TOOL_NAME
# shellcheck source=./lib/log.bash
source "${PLUGIN_DIR}/lib/log.bash" # log_debug log_info is_debug
# shellcheck source=./lib/trap.bash
source "${PLUGIN_DIR}/lib/trap.bash" # add_on_exit

linux_id() {
# shellcheck disable=SC2002
cat /etc/os-release | grep -w ID | cut -d '=' -f 2
}

compose_download_url() {
local version="$1"

local platform_uname platform
platform_uname="$(uname -s)"
case "${platform_uname}" in
Linux*)
if [ "$(linux_id)" == "alpine" ]; then
platform="linux-static"
else
platform="linux"
fi
;;
Darwin*) platform="macos" ;;
*) fail "Platform \"${platform_uname}\" is not yet supported." ;;
esac

local uname_arch arch
uname_arch="$(uname -m)"
case "${uname_arch}" in
x86_64) arch="amd64" ;;
*) fail "Architecture \"${uname_arch}\" is not yet supported." ;;
esac
# possible values:
# https://stackoverflow.com/questions/45125516/possible-values-for-uname-m

# releases: https://github.com/clj-kondo/clj-kondo/releases
# sample URL: https://github.com/clj-kondo/clj-kondo/releases/download/v2023.12.15/clj-kondo-2023.12.15-linux-amd64.zip
local download_url="${GH_REPO}/releases/download/v${version}/${TOOL_NAME}-${version}-${platform}-${arch}.zip"
printf "%s" "${download_url}"
}

download_release() {
local version="$1"
local dest="$2"

# prepare temp directory
local temp_dir
temp_dir=$(mktemp -d -t "asdf-${TOOL_NAME}.XXXX")
add_on_exit rm -rf "${temp_dir}"
log_debug "Temporary directory created at ${temp_dir}"
log_debug

# download zip & sha
log_info "* Downloading ${TOOL_NAME} release ${version}..."
local zip_url sha_url
zip_url="$(compose_download_url "${version}")"
sha_url="${zip_url}.sha256"
log_info " - ${zip_url}"
log_info " - ${sha_url}"
local temp_zip_path="${temp_dir}/${TOOL_NAME}.zip"
local temp_sha_path="${temp_dir}/${TOOL_NAME}.zip.sha256"
curl "${CURL_OPTS[@]}" -o "${temp_zip_path}" -C - "${zip_url}" || fail "Could not download ${zip_url}"
curl "${CURL_OPTS[@]}" -o "${temp_sha_path}" -C - "${sha_url}" || fail "Could not download ${sha_url}"
log_info

# verify sha

if command -v sha256sum >/dev/null; then
if echo "$(cat "${temp_sha_path}") ${temp_zip_path}" | sha256sum --check >/dev/null 2>&1; then
log_info "* Checksum verification successful: The file is intact."
else
log_info "* Checksum verification failed: The file's integrity is compromised. Try do download ${TOOL_NAME} again."
fail "Installation terminated due to integrity check failure."
fi
else
log_info "* Check verification skipped due to missing sha256sum."
fi
log_info

# move the downloaded file to final destination
mkdir -p "${dest}"
local unzip_opts=(-o "${temp_zip_path}" -d "${dest}")
! is_debug && unzip_opts=(-q "${unzip_opts[@]}")
unzip "${unzip_opts[@]}" || fail "Could not extract ${temp_zip_path}"

log_info "* Downloading ${TOOL_NAME} release ${version}... DONE"
log_info
}

# Bash Strict Mode Settings
set -euo pipefail
# Path Initialization
if [ -z "${SHELL_GR_DIR:-}" ]; then
SCRIPT_PATH_1="${BASH_SOURCE[0]:-$0}"
SCRIPT_PATH="$([[ ! "${SCRIPT_PATH_1}" =~ /bash$ ]] && readlink -f "${SCRIPT_PATH_1}" || echo "")"
SCRIPT_DIR="$([ -n "${SCRIPT_PATH}" ] && (cd "$(dirname "${SCRIPT_PATH}")" && pwd -P) || echo "")"
ROOT_DIR="$([ -n "${SCRIPT_DIR}" ] && (cd "${SCRIPT_DIR}/.." && pwd -P) || echo "/tmp")"
export SHELL_GR_DIR="${ROOT_DIR}/lib/.github_deps/rynkowsg/shell-gr@1a30383" # needed for shell-gr to resolve paths
fi
# Library Sourcing
source "${ROOT_DIR}/lib/.github_deps/rynkowsg/shell-gr@1a30383/lib/error.bash" # fail
source "${ROOT_DIR}/lib/.github_deps/rynkowsg/shell-gr@1a30383/lib/log.bash" # log_debug
source "${ROOT_DIR}/lib/.github_deps/rynkowsg/shell-gr@1a30383/lib/install/clj_kondo.bash" # GRI_CLJ_KONDO__download

# `bin/download` spec: https://github.com/asdf-vm/asdf/blob/master/docs/plugins/create.md#bindownload-
main() {
[ -z "${ASDF_DOWNLOAD_PATH+x}" ] && fail "ASDF_DOWNLOAD_PATH is required"
[ -z "${ASDF_INSTALL_VERSION+x}" ] && fail "ASDF_INSTALL_VERSION is required"

log_debug "ASDF_DOWNLOAD_PATH: ${ASDF_DOWNLOAD_PATH}"
log_debug "ASDF_INSTALL_VERSION: ${ASDF_INSTALL_VERSION}"
log_debug "TOOL_NAME: ${TOOL_NAME}"

download_release "${ASDF_INSTALL_VERSION}" "${ASDF_DOWNLOAD_PATH}"
# inputs validation
[ -z "${ASDF_DOWNLOAD_PATH:-}" ] && fail "ASDF_DOWNLOAD_PATH is required"
[ -z "${ASDF_INSTALL_VERSION:-}" ] && fail "ASDF_INSTALL_VERSION is required"

# Environment Variables available to script
log_debug "ASDF_INSTALL_TYPE: ${ASDF_INSTALL_TYPE:-}"
log_debug "ASDF_INSTALL_VERSION: ${ASDF_INSTALL_VERSION:-}"
log_debug "ASDF_INSTALL_PATH: ${ASDF_INSTALL_PATH:-}"
log_debug "ASDF_DOWNLOAD_PATH: ${ASDF_DOWNLOAD_PATH:-}"
log_debug "GITHUB_API_TOKEN: ${GITHUB_API_TOKEN:+"<masked>"}"

GRI_CLJ_KONDO__INSTALL_TYPE="${ASDF_INSTALL_TYPE:-"version"}" \
GRI_CLJ_KONDO__INSTALL_VERSION="${ASDF_INSTALL_VERSION}" \
GRI_CLJ_KONDO__INSTALL_PATH="${ASDF_INSTALL_PATH%/bin}/bin" \
GRI_CLJ_KONDO__DOWNLOAD_PATH="${ASDF_DOWNLOAD_PATH}" \
GRI_CLJ_KONDO__GITHUB_API_TOKEN="${GITHUB_API_TOKEN:-}" \
GRI_CLJ_KONDO__download
}

main "$@"
Loading

0 comments on commit b89b2c4

Please sign in to comment.