Skip to content

Commit

Permalink
feat: ✨ add common utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgrv authored Dec 28, 2024
1 parent 8bbc3cf commit 00cee0f
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:2": {},
"ghcr.io/tomgrv/devcontainer-features/githooks:2": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
Expand Down
9 changes: 5 additions & 4 deletions src/githooks/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Git Hooks",
"version": "2.1.8",
"version": "3.0.0",
"description": "A feature to add useful Git hooks to your project",
"customizations": {
"vscode": {
Expand All @@ -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"
}
13 changes: 8 additions & 5 deletions src/gitutils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Git Aliases",
"version": "2.2.3",
"version": "3.0.0",
"description": "A feature to add useful Git aliases to your shell.",
"customizations": {
"vscode": {
Expand All @@ -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"
}
9 changes: 5 additions & 4 deletions src/gitversion/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "GitVersion feature",
"version": "1.1.9",
"version": "3.0.0",
"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": {
Expand All @@ -17,5 +18,5 @@
"description": "The version of GitVersion to install"
}
},
"postStartCommand": "/usr/local/share/gitversion/configure.sh"
"postStartCommand": "configure-feature"
}
39 changes: 2 additions & 37 deletions src/gitversion/install.sh
Original file line number Diff line number Diff line change
@@ -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
15 changes: 8 additions & 7 deletions src/larasets/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "Laravel settings",
"version": "1.3.0",
"version": "3.0.0",
"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",
Expand Down Expand Up @@ -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"
}
27 changes: 27 additions & 0 deletions src/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- @format -->

# 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.
44 changes: 44 additions & 0 deletions src/utils/_configure-feature.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/sh
set -e

### 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')

### 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

echo "Configuring feature <$feature>"
echo "from <$source>"
echo "to <$target>"

### Go to the module root
cd "$(git rev-parse --show-toplevel)" >/dev/null

### Create package.json if not exists or is empty
echo "Merge all package folder json files into top level package.json" | npx --yes chalk-cli --stdin blue
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
28 changes: 28 additions & 0 deletions src/utils/_install-feature.sh
Original file line number Diff line number Diff line change
@@ -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 "configure-*.sh" -exec chmod +x {} \; -exec ln -s {} /usr/bin/ \;

### 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
47 changes: 47 additions & 0 deletions src/utils/_install-stubs.sh
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions src/utils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Common Utils",
"version": "1.1.0",
"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"
}
}
}
31 changes: 31 additions & 0 deletions src/utils/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/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

### Init directories
export source=$(dirname $(readlink -f $0))
export target=${1:-/usr/local/share}/$(basename $source | sed 's/_.*$//')

### Installs "install" scripts
find $source -name "_*.sh" -type f -exec cp {} $target \;

### Make a link to the script without the leading underscore and without the .sh extension
find $target -type f -name "_*.sh" -exec chmod +x {} \; -exec ln -s \$\(echo {} | sed -e 's/^_//' -e 's/\.sh$//'\) /usr/bin/ \;
4 changes: 2 additions & 2 deletions stubs/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:2": {},
"ghcr.io/tomgrv/devcontainer-features/githooks:2": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
Expand Down

0 comments on commit 00cee0f

Please sign in to comment.