From 36ba255f91ddae80ac3bd953204c9e2034b9461c Mon Sep 17 00:00:00 2001 From: "Thomas G." Date: Mon, 16 Sep 2024 08:55:24 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20=F0=9F=8E=B8=20add=20out=20?= =?UTF-8?q?of=20container=20install?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- local-install.sh | 61 +++++++++++ package.json | 182 +++++++++++++++++++++----------- src/githooks/install.sh | 2 +- src/gitutils/install-aliases.sh | 27 +++-- src/gitutils/install.sh | 2 +- 5 files changed, 200 insertions(+), 74 deletions(-) create mode 100755 local-install.sh diff --git a/local-install.sh b/local-install.sh new file mode 100755 index 0000000..8552fda --- /dev/null +++ b/local-install.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +### Go to root +cd $(git rev-parse --show-toplevel) >/dev/null + +### +features=$(jq -r '.config.local[]' package.json) +case $1 in + --help) + echo "Usage: $0 [--help] []" + exit + ;; + *) + echo "Selected features: $*" | npx --yes chalk-cli --stdin blue + features=$* + ;; +esac + +### Stash all changes including untracked files +stash=$(git stash -u && echo true) + +### Ask to restart in container if this is not already the case +if [ "$CODESPACES" != "true" ] && [ "$REMOTE_CONTAINERS" != "true" ]; then + echo "You are not in a container" | npx --yes chalk-cli --stdin green + + ### Call the install.sh script in all selected features + for feature in $features; do + if [ -f "./src/$feature/install.sh" ]; then + ### Run the install.sh script + echo "Running src/$feature/install.sh..." | npx --yes chalk-cli --stdin blue + bash ./src/$feature/install.sh /tmp + else + echo "$feature not found" | npx --yes chalk-cli --stdin red + fi + done + + ### Call the configure.sh script in all selected features + for feature in $features; do + if [ -f "./src/$feature/configure.sh" ]; then + ### Run the install.sh script + echo "Running src/$feature/configure.sh..." | npx --yes chalk-cli --stdin blue + bash /tmp/$feature/configure.sh + else + echo "$feature not found" | npx --yes chalk-cli --stdin red + fi + done +else + echo "You are in a container: use devutils as devcontainer features:" | npx --yes chalk-cli --stdin yellow + for feature in $features; do + echo "ghcr.io/tomgrv/devcontainer-features/$feature" + done | npx --yes chalk-cli --stdin yellow + exit +fi + +### Stage non withespace changes +git ls-files --others --exclude-standard | xargs -I {} bash -c 'if [ -s "{}" ]; then git add "{}"; fi' +git diff -w --no-color | git apply --cached --ignore-whitespace +git checkout -- . && git reset && git add . + +### Unstash changes +test -n "$stash" && git stash apply && git stash drop \ No newline at end of file diff --git a/package.json b/package.json index 5759901..8ab3f89 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,123 @@ { + "name": "@tomgrv/devutils", + "version": "1.0.0", + "description": "Configure dev environment with devcontainer, gitflow, gitversion, git aliases & hooks. Can be used a devcontainer features", + "keywords": [ + "dev", + "git", + "hooks", + "commitlint", + "commitizen", + "lint-staged", + "prettier", + "devmoji", + "git-precommit-checks", + "husky", + "conventional-changelog", + "gitversion", + "gitflow", + "changelog", + "semver" + ], + "homepage": "https://github.com/tomgrv/devcontainer-features#readme", + "bugs": { + "url": "https://github.com/tomgrv/devcontainer-features/issues", + "email": "project@hostname.com" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/tomgrv/devcontainer-features.git" + }, + "funding": { + "type": "individual", + "url": "https://buymeacoffee.com/tomgrv" + }, + "author": "tomgrv", + "type": "module", + "bin": "./local-install.sh", + "scripts": { + "lint": "npx --yes lint-staged", + "release": "npx --yes commit-and-tag-version --no-verify --", + "test": "echo \"Warning: no test specified\"", + "update": "npm-check-updates -i -u", + "update-all": "npm run update -ws --root" + }, + "commitlint": { + "extends": [ + "@commitlint/config-conventional" + ], + "rules": { + "subject-case": [ + 2, + "never", + [ + "start-case", + "pascal-case", + "upper-case" + ] + ], + "scope-enum": [ + 2, + "always", + [ + "deps", + "release", + "security", + "i18n", + "config", + "add", + "remove", + "breaking", + "modules", + "packages", + "ui-ux", + "api", + "model" + ] + ] + } + }, + "lint-staged": { + "*.{js,jsx,ts,tsx,md,html,css,json,vue, yaml, yml}": [ + "npx --yes prettier --write" + ], + "*.php": [ + "composer lint" + ], + "package.json": [ + "npx --yes sort-package-json" + ] + }, + "config": { + "commitizen": { + "path": "@commitlint/cz-commitlint" + }, + "local": [ + "gitutils", + "githooks" + ] + }, + "prettier": { + "insertPragma": true, + "semi": false, + "singleQuote": true, + "tabWidth": 4, + "trailingComma": "es5" + }, + "peerDependencies": { + "@commitlint/cli": "^19.2.2", + "@commitlint/config-conventional": "^19.2.2", + "@commitlint/core": "^19.2.2", + "@commitlint/cz-commitlint": "^19.2.0", + "commit-and-tag-version": "^12.4.1", + "commitizen": "^4.3.0", + "conventional-changelog-cli": "^2.1.1", + "devmoji": "^2.3.0", + "git-precommit-checks": "3.1.0", + "husky": "^9.0.11", + "lint-staged": "^15.2.2", + "prettier": "^3.2.5" + }, "commit-and-tag-version": { "bumpFiles": [ { @@ -47,53 +166,9 @@ "type": "test", "hidden": true } - ] - }, - "scripts": { - "release": "npx commit-and-tag-version --no-verify --", - "lint": "npx lint-staged", - "update": "npx npm-check-updates -i -u", - "update-all": "npx npm run update -ws --root", - "test": "echo \"Warning: no test specified\"" - }, - "commitlint": { - "extends": [ - "@commitlint/config-conventional" ], - "rules": { - "subject-case": [ - 2, - "never", - [ - "start-case", - "pascal-case", - "upper-case" - ] - ], - "scope-enum": [ - 2, - "always", - [ - "deps", - "release", - "security", - "i18n", - "config", - "add", - "remove", - "breaking", - "modules", - "packages", - "ui-ux", - "api", - "model" - ] - ] - } - }, - "config": { - "commitizen": { - "path": "@commitlint/cz-commitlint" + "scripts": { + "prebump": "gitversion -config .gitversion -showvariable MajorMinorPatch" } }, "git-precommit-checks": { @@ -109,20 +184,5 @@ "regex": "(?:FIXME|TODO)" } ] - }, - "lint-staged": { - "*.{js,jsx,ts,tsx,md,html,css,json,vue, yaml, yml}": [ - "npx prettier --write" - ], - "*.php": [ - "composer lint" - ] - }, - "prettier": { - "trailingComma": "es5", - "tabWidth": 4, - "semi": false, - "singleQuote": true, - "insertPragma": true } } diff --git a/src/githooks/install.sh b/src/githooks/install.sh index 846b395..23fdb06 100644 --- a/src/githooks/install.sh +++ b/src/githooks/install.sh @@ -4,7 +4,7 @@ set -e ### Init directories export source=$(dirname $(readlink -f $0)) export feature=$(basename $source | sed 's/_.*$//') -export target=/usr/local/share/$feature +export target=${1:-/usr/local/share}/$feature echo "Activating feature <$feature>..." ### Makes sure the target directory exists diff --git a/src/gitutils/install-aliases.sh b/src/gitutils/install-aliases.sh index dac4a5c..af5f1a1 100644 --- a/src/gitutils/install-aliases.sh +++ b/src/gitutils/install-aliases.sh @@ -1,20 +1,25 @@ #!/bin/sh set -e -### Install GitFlow with apt on Ubuntu or apk on Alpine -if [ -f /etc/alpine-release ]; then - apk update - apk add git-flow dos2unix jq -else - apt-get update - apt-get install -y git-flow dos2unix jq -fi +### Check if utils are installed +for bin in git-flow dos2unix jq; do + if [ -n "$(command -v $bin)" ] + then + echo "$bin is installed." | npx --yes chalk-cli --stdin green + elif [ -f /etc/alpine-release ]; then + apk update + apk add $bin + else + sudo apt-get update + sudo apt-get install -y $bin + fi +done ### For each entry in config.json file next to this file, create corresponding git config from key and value. ### if value is an object, parse it as json and create dotted keys echo "Configuring git with <$source/config.json>..." jq -r 'paths(scalars) as $p | [($p|join(".")), (getpath($p)|tostring)] | join(" ")' $source/config.json | while read key value; do - git config --system $key "$value" + git config --global $key "$value" echo "Created config $key => $value" done @@ -22,7 +27,7 @@ done echo "Configuring aliases with <$source/alias.json>..." jq -r 'keys[]' $source/alias.json | dos2unix | while read key; do value=$(jq -r ".$key" $source/alias.json) - git config --system alias.$key "!sh -c '$value' - " + git config --global alias.$key "!sh -c '$value' - " echo "Created alias $key => $value" done @@ -30,6 +35,6 @@ done echo "Configuring scripts with <$target/_xx.sh>..." for script in $target/_*.sh; do alias=$(basename $script | sed -e 's/^_//g' -e 's/.sh$//g') - git config --system alias.$alias "!sh -c '$(readlink -f $script)' - " + git config --global alias.$alias "!sh -c '$(readlink -f $script)' - " echo "Created alias $alias => $(readlink -f $script)" done diff --git a/src/gitutils/install.sh b/src/gitutils/install.sh index 846b395..23fdb06 100644 --- a/src/gitutils/install.sh +++ b/src/gitutils/install.sh @@ -4,7 +4,7 @@ set -e ### Init directories export source=$(dirname $(readlink -f $0)) export feature=$(basename $source | sed 's/_.*$//') -export target=/usr/local/share/$feature +export target=${1:-/usr/local/share}/$feature echo "Activating feature <$feature>..." ### Makes sure the target directory exists