From e0f58e8eafa5a01f865ca8b8d6ac3c68abb6c111 Mon Sep 17 00:00:00 2001 From: "Thomas G." <1809566+tomgrv@users.noreply.github.com> Date: Fri, 27 Dec 2024 21:11:03 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20common=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .devcontainer/devcontainer.json | 4 +- src/common-utils/README.md | 27 ++++++++++++ src/common-utils/_configure-feature.sh | 48 ++++++++++++++++++++++ src/common-utils/_install-feature.sh | 28 +++++++++++++ src/common-utils/_install-stubs.sh | 47 +++++++++++++++++++++ src/common-utils/devcontainer-feature.json | 19 +++++++++ src/common-utils/install-bin.sh | 10 +++++ src/common-utils/install.sh | 24 +++++++++++ src/githooks/devcontainer-feature.json | 9 ++-- src/gitutils/devcontainer-feature.json | 13 +++--- src/gitversion/devcontainer-feature.json | 9 ++-- src/gitversion/install.sh | 39 +----------------- src/larasets/devcontainer-feature.json | 17 ++++---- stubs/.devcontainer/devcontainer.json | 4 +- 14 files changed, 236 insertions(+), 62 deletions(-) create mode 100644 src/common-utils/README.md create mode 100755 src/common-utils/_configure-feature.sh create mode 100755 src/common-utils/_install-feature.sh create mode 100755 src/common-utils/_install-stubs.sh create mode 100755 src/common-utils/devcontainer-feature.json create mode 100755 src/common-utils/install-bin.sh create mode 100755 src/common-utils/install.sh mode change 100644 => 100755 src/githooks/devcontainer-feature.json mode change 100644 => 100755 src/gitutils/devcontainer-feature.json mode change 100644 => 100755 src/gitversion/devcontainer-feature.json mode change 100644 => 100755 src/larasets/devcontainer-feature.json diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 3fc162f..cbc1897 100755 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -7,8 +7,8 @@ "ghcr.io/devcontainers/features/node:1": "lts", "ghcr.io/devcontainers/features/php:1": "8.3", "ghcr.io/devcontainers/features/docker-in-docker:2": {}, - "ghcr.io/tomgrv/devcontainer-features/gitutils:2.2": {}, - "ghcr.io/tomgrv/devcontainer-features/githooks:2.1": {} + "ghcr.io/tomgrv/devcontainer-features/gitutils:3": {}, + "ghcr.io/tomgrv/devcontainer-features/githooks:3": {} }, // Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [], diff --git a/src/common-utils/README.md b/src/common-utils/README.md new file mode 100644 index 0000000..ee0b992 --- /dev/null +++ b/src/common-utils/README.md @@ -0,0 +1,27 @@ + + +# Utils Feature + +This feature installs the PHP Extension Community Library (PECL) in the dev container. PECL is a repository for PHP extensions. + +More information about PECL can be found on the [PECL website](https://pecl.php.net/). + +## Example Usage + +```json +"features": { + "ghcr.io/tomgrv/devcontainer-features/pecl:1": { + "extension": "zip" + } +} +``` + +## Options + +| Options Id | Description | Type | Default Value | +| ---------- | ------------------------------------- | ------ | ------------- | +| version | The version of GitVersion to install. | string | latest | + +## Contributing + +If you have a feature that you would like to add to this repository, please open an issue or submit a pull request. diff --git a/src/common-utils/_configure-feature.sh b/src/common-utils/_configure-feature.sh new file mode 100755 index 0000000..276a277 --- /dev/null +++ b/src/common-utils/_configure-feature.sh @@ -0,0 +1,48 @@ +#!/bin/sh +set -e + +### Check if feature is provided +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi + +### Init directories +export feature=$1 +export source=/usr/local/share/$1 + +### Get indent size from devcontainer.json with jq, default to 2 if not found +export tabSize=$(sed 's/\/\/.*//' .devcontainer/devcontainer.json | jq '.customizations.vscode.settings["editor.tabSize"] // 2') + +echo "Configuring feature <$feature>" +echo "from <$source>" + +### Go to the module root +cd "$(git rev-parse --show-toplevel)" >/dev/null + +### Log +echo "Merge all package folder json files into top level package.json" | npx --yes chalk-cli --stdin blue + +### Create package.json if not exists or is empty +if [ ! -f package.json -o ! -s package.json ]; then + # Create empty package.json + echo "{}" >package.json +else + # Pre-sort + npx --yes sort-package-json +fi + +### Merge all package folder json files into top level package.json +find $source -name _*.json | sort | while read file; do + echo "Merge $file" | npx --yes chalk-cli --stdin yellow + jq --indent ${tabSize:-2} -s '.[0] * .[1]' $file package.json >/tmp/package.json && mv -f /tmp/package.json package.json +done + +### Call all configure-xxx.sh scripts +find $source -name configure-*.sh | sort | while read file; do + echo "Run $file" | npx --yes chalk-cli --stdin yellow + $file || true +done + +### Sort package.json +npx --yes sort-package-json diff --git a/src/common-utils/_install-feature.sh b/src/common-utils/_install-feature.sh new file mode 100755 index 0000000..c318908 --- /dev/null +++ b/src/common-utils/_install-feature.sh @@ -0,0 +1,28 @@ +#!/bin/sh +set -e + +### Init directories +caller_filename=$(ps -o args= $PPID) +caller_filepath=$(readlink -f ${caller_filename##/bin/sh}) +export source=$(dirname $caller_filepath) +export feature=$(basename $source | sed 's/_.*$//') +export target=${1:-/usr/local/share}/$feature + +### Logs +echo "Activating feature <$feature>" +echo "from <$source>" +echo "to <$target>" + +### Makes sure the target directory exists +mkdir -p $target + +### Install specific utils +find $source \( -name "_*" -o -name "configure-*.sh" \) -type f -exec cp {} $target \; +find $target -type f -name "*.sh" -exec chmod +x {} \; + +### Call all the install-xxx scripts in the feature directory +echo "Calling all install scripts in $source..." +find $source -type f -name "install-*.sh" | while read script; do + echo "Calling $script..." + sh $script +done diff --git a/src/common-utils/_install-stubs.sh b/src/common-utils/_install-stubs.sh new file mode 100755 index 0000000..87b580a --- /dev/null +++ b/src/common-utils/_install-stubs.sh @@ -0,0 +1,47 @@ +#!/bin/sh +set -e + +### Init directories +caller_filename=$(ps -o args= $PPID) +caller_filepath=$(readlink -f ${caller_filename##/bin/sh}) +export source=$(dirname $caller_filepath) +export feature=$(basename $source | sed 's/_.*$//') +export target=${1:-/usr/local/share}/$feature + +### Logs +echo "Merging stubs files of <$feature>" +echo "from <$source>" +echo "to <$target>" + +### Merge all files from stub folder to root with git merge-file +for file in $(find $source/stubs -type f); do + + ### Get middle part of the path + folder=$(dirname ${file#$source/stubs/}) + + ### Create folder if not exists + mkdir -p $folder + + ### Merge file + echo "Merge $folder/$(basename $file)" + git merge-file -p $file $folder/$(basename $file) ${folder#$source/}/$(basename $file) >$folder/$(basename $file) + + ### Apply rights + chmod $(stat -c "%a" $file) $folder/$(basename $file) +done + +### Find all file with a trailing slash outside dist folder, make sure they are added to .gitignore and remove the trailing slash +echo "Add files to .gitignore" +for file in $(find . -type f -name "#*" ! -path "*/stubs/*" ! -path "./node_modules/*" ! -path "./vendors/*"); do + + echo "Add $file to .gitignore" + + ### Remove # occurences in file path + clean=${file#./#} + + ### Add to .gitignore if not already there + grep -qxF $clean .gitignore || echo "$clean" >>.gitignore + + ### Rename file + mv $file $clean +done diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json new file mode 100755 index 0000000..07406f0 --- /dev/null +++ b/src/common-utils/devcontainer-feature.json @@ -0,0 +1,19 @@ +{ + "name": "Common Utils", + "version": "1.1.5", + "description": "Common utils for tomgrv/devcontainer-features", + "dependsOn": { + "ghcr.io/devcontainers/features/common-utils": {} + }, + "id": "common-utils", + "installsAfter": [ + "ghcr.io/devcontainers/features/common-utils" + ], + "options": { + "utils": { + "type": "string", + "default": "jq dos2unix", + "description": "utilities to install" + } + } +} diff --git a/src/common-utils/install-bin.sh b/src/common-utils/install-bin.sh new file mode 100755 index 0000000..9753252 --- /dev/null +++ b/src/common-utils/install-bin.sh @@ -0,0 +1,10 @@ +#!/bin/sh +set -e + +### Create links to utils in /usr/bin +find $target -type f -name "_*.sh" -exec echo {} \; -exec chmod +x {} \; | while read file +do + link=/usr/bin/$(basename $file | sed -e 's/^_//' -e 's/.sh$//') + rm -f $link || true + ln -s $file $link || true +done diff --git a/src/common-utils/install.sh b/src/common-utils/install.sh new file mode 100755 index 0000000..4071f86 --- /dev/null +++ b/src/common-utils/install.sh @@ -0,0 +1,24 @@ +#!/bin/sh +set -e + +### Options +UTILS="${UTILS:-"jq dos2unix"}" + +### Install utils +for bin in $UTILS; do + if [ -n "$(command -v $bin)" ]; then + echo "$bin is installed." + elif [ -f /etc/alpine-release ]; then + apk update + apk add $bin + elif [ $(uname) = "Linux" ] || [ $(uname) = "Darwin" ]; then + sudo apt-get update + sudo apt-get install -y $bin + else + echo "Please install $bin." + exit 1 + fi +done + +### Install this feature +$(dirname $0)/_install-feature.sh diff --git a/src/githooks/devcontainer-feature.json b/src/githooks/devcontainer-feature.json old mode 100644 new mode 100755 index eb40968..18a1ba1 --- a/src/githooks/devcontainer-feature.json +++ b/src/githooks/devcontainer-feature.json @@ -1,6 +1,6 @@ { "name": "Git Hooks", - "version": "2.1.8", + "version": "3.0.1", "description": "A feature to add useful Git hooks to your project", "customizations": { "vscode": { @@ -13,11 +13,12 @@ } }, "dependsOn": { - "ghcr.io/devcontainers/features/node": {} + "ghcr.io/devcontainers/features/node": {}, + "ghcr.io/tomgrv/devcontainer-features/common-utils": {} }, "id": "githooks", "installsAfter": [ - "ghcr.io/devcontainers/features/common-utils" + "ghcr.io/tomgrv/devcontainer-features/common-utils" ], - "postStartCommand": "/usr/local/share/githooks/configure.sh" + "postStartCommand": "configure-feature githooks" } diff --git a/src/gitutils/devcontainer-feature.json b/src/gitutils/devcontainer-feature.json old mode 100644 new mode 100755 index 16ba3bc..0b98ddd --- a/src/gitutils/devcontainer-feature.json +++ b/src/gitutils/devcontainer-feature.json @@ -1,6 +1,6 @@ { "name": "Git Aliases", - "version": "2.2.3", + "version": "3.0.1", "description": "A feature to add useful Git aliases to your shell.", "customizations": { "vscode": { @@ -18,13 +18,16 @@ } }, "dependsOn": { - "ghcr.io/devcontainers/features/node": "lts", - "ghcr.io/tomgrv/devcontainer-features/gitversion": "5.*" + "ghcr.io/devcontainers/features/node": "", + "ghcr.io/tomgrv/devcontainer-features/gitversion": "5.*", + "ghcr.io/tomgrv/devcontainer-features/common-utils": { + "utils": "jq dos2unix git-flow" + } }, "id": "gitutils", "installsAfter": [ - "ghcr.io/devcontainers/features/common-utils", + "ghcr.io/tomgrv/devcontainer-features/common-utils", "ghcr.io/tomgrv/devcontainer-features/gitversion" ], - "postStartCommand": "/usr/local/share/gitutils/configure.sh" + "postStartCommand": "configure-feature gitutils" } diff --git a/src/gitversion/devcontainer-feature.json b/src/gitversion/devcontainer-feature.json old mode 100644 new mode 100755 index 4049680..385ba00 --- a/src/gitversion/devcontainer-feature.json +++ b/src/gitversion/devcontainer-feature.json @@ -1,13 +1,14 @@ { "name": "GitVersion feature", - "version": "1.1.9", + "version": "3.0.1", "description": "Add gitversion to your devcontainer", "dependsOn": { - "ghcr.io/devcontainers/features/dotnet": "lts" + "ghcr.io/devcontainers/features/dotnet": "", + "ghcr.io/tomgrv/devcontainer-features/common-utils": "" }, "id": "gitversion", "installsAfter": [ - "ghcr.io/devcontainers/features/common-utils", + "ghcr.io/tomgrv/devcontainer-features/common-utils", "ghcr.io/devcontainers/features/dotnet" ], "options": { @@ -17,5 +18,5 @@ "description": "The version of GitVersion to install" } }, - "postStartCommand": "/usr/local/share/gitversion/configure.sh" + "postStartCommand": "configure-feature gitversion" } diff --git a/src/gitversion/install.sh b/src/gitversion/install.sh index 1c87c25..ddeec3a 100755 --- a/src/gitversion/install.sh +++ b/src/gitversion/install.sh @@ -1,40 +1,5 @@ #!/bin/sh set -e -### Check if utils are installed -utils() { - for bin in $* dos2unix jq; do - if [ -n "$(command -v $bin)" ]; then - echo "$bin is installed." - elif [ -f /etc/alpine-release ]; then - apk update - apk add $bin - elif [ $(uname) = "Linux" ] || [ $(uname) = "Darwin" ]; then - sudo apt-get update - sudo apt-get install -y $bin - else - echo "Please install $bin." - exit 1 - fi - done -} && utils - -### Init directories -export source=$(dirname $(readlink -f $0)) -export feature=$(basename $source | sed 's/_.*$//') -export target=${1:-/usr/local/share}/$feature -echo "Activating feature <$feature>..." - -### Makes sure the target directory exists -mkdir -p $target - -### Copy the config script to the target directory and create a git alias for it -find $source \( -name "_*" -o -name "configure*.sh" \) -type f -exec cp {} $target \; -find $target -type f -name "*.sh" -exec chmod +x {} \; - -### Call all the install-xxx scripts in the feature directory -echo "Calling all install scripts in $source..." -find $source -type f -name "install-*.sh" | while read script; do - echo "Calling $script..." - sh $script -done +### Install feature +install-feature diff --git a/src/larasets/devcontainer-feature.json b/src/larasets/devcontainer-feature.json old mode 100644 new mode 100755 index 6005033..d549807 --- a/src/larasets/devcontainer-feature.json +++ b/src/larasets/devcontainer-feature.json @@ -1,14 +1,14 @@ { "name": "Laravel settings", - "version": "1.3.0", + "version": "3.0.1", "description": "A feature to add useful laravel settings to your shell.", "containerEnv": { "APP_DEBUG": "true", "APP_ENV": "local", "APP_PORT": "80", - "APP_URL": "https:\/\/${localEnv:CODESPACE_NAME}-${containerEnv:APP_PORT}.${localEnv:GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}", - "ASSET_URL": "${containerEnv:APP_URL}" - "LARAVEL_SAIL": "1",, + "APP_URL": "https://${localEnv:CODESPACE_NAME}-${containerEnv:APP_PORT}.${localEnv:GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}", + "ASSET_URL": "${containerEnv:APP_URL}", + "LARAVEL_SAIL": "1", "DB_CONNECTION": "sqlite", "DB_DATABASE": "database/database.sqlite", "SAIL_XDEBUG_MODE": "develop,debug", @@ -75,13 +75,14 @@ } }, "dependsOn": { - "ghcr.io/devcontainers/features/node:1": "lts", + "ghcr.io/devcontainers/features/node:1": "", "ghcr.io/devcontainers/features/php:1": "8.3", - "ghcr.io/devcontainers/features/docker-in-docker": "lts" + "ghcr.io/devcontainers/features/docker-in-docker": "", + "ghcr.io/tomgrv/devcontainer-features/common-utils": "" }, "id": "larasets", "installsAfter": [ - "ghcr.io/devcontainers/features/common-utils" + "ghcr.io/tomgrv/devcontainer-features/common-utils" ], - "postStartCommand": "/usr/local/share/larasets/configure.sh" + "postStartCommand": "configure-feature larasets" } diff --git a/stubs/.devcontainer/devcontainer.json b/stubs/.devcontainer/devcontainer.json index 3fc162f..cbc1897 100755 --- a/stubs/.devcontainer/devcontainer.json +++ b/stubs/.devcontainer/devcontainer.json @@ -7,8 +7,8 @@ "ghcr.io/devcontainers/features/node:1": "lts", "ghcr.io/devcontainers/features/php:1": "8.3", "ghcr.io/devcontainers/features/docker-in-docker:2": {}, - "ghcr.io/tomgrv/devcontainer-features/gitutils:2.2": {}, - "ghcr.io/tomgrv/devcontainer-features/githooks:2.1": {} + "ghcr.io/tomgrv/devcontainer-features/gitutils:3": {}, + "ghcr.io/tomgrv/devcontainer-features/githooks:3": {} }, // Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [],