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 e0f58e8
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 62 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:3": {},
"ghcr.io/tomgrv/devcontainer-features/githooks:3": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
Expand Down
27 changes: 27 additions & 0 deletions src/common-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.
48 changes: 48 additions & 0 deletions src/common-utils/_configure-feature.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh
set -e

### Check if feature is provided
if [ -z "$1" ]; then
echo "Usage: $0 <feature>"
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
28 changes: 28 additions & 0 deletions src/common-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 "*.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
47 changes: 47 additions & 0 deletions src/common-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/common-utils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
10 changes: 10 additions & 0 deletions src/common-utils/install-bin.sh
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions src/common-utils/install.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions src/githooks/devcontainer-feature.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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": {
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 githooks"
}
13 changes: 8 additions & 5 deletions src/gitutils/devcontainer-feature.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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": {
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 gitutils"
}
9 changes: 5 additions & 4 deletions src/gitversion/devcontainer-feature.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -17,5 +18,5 @@
"description": "The version of GitVersion to install"
}
},
"postStartCommand": "/usr/local/share/gitversion/configure.sh"
"postStartCommand": "configure-feature gitversion"
}
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
17 changes: 9 additions & 8 deletions src/larasets/devcontainer-feature.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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",
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"
"postStartCommand": "configure-feature larasets"
}
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:3": {},
"ghcr.io/tomgrv/devcontainer-features/githooks:3": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
Expand Down

0 comments on commit e0f58e8

Please sign in to comment.