-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert
More file actions
executable file
·101 lines (88 loc) · 3.21 KB
/
Copy pathinsert
File metadata and controls
executable file
·101 lines (88 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
# shellcheck shell=bash
set -e -o pipefail
# Check if jq is installed
if ! command -v jq >/dev/null 2>&1; then
echo "Error: jq is required but not installed."
exit 1
fi
# Path to composer.json
COMPOSER_JSON="composer.json"
# Check if composer.json exists
if [ ! -f "$COMPOSER_JSON" ]; then
echo "Error: $COMPOSER_JSON not found"
exit 1
fi
# Function to add a repository
add_repository() {
local url="$1"
local repo_url="https://github.com/$url"
# Check if repository already exists
if jq -e --arg url "$repo_url" '.repositories[] | select(.url == $url)' "$COMPOSER_JSON" >/dev/null; then
echo "Repository $repo_url already exists, skipping."
return 0
fi
# Add the repository
jq --indent 4 --arg url "$repo_url" '.repositories += [{"type": "github", "url": $url}]' "$COMPOSER_JSON" > "$COMPOSER_JSON.tmp"
mv "$COMPOSER_JSON.tmp" "$COMPOSER_JSON"
echo "Added repository: $repo_url"
}
# Function to add a script
add_script() {
local name="$1"
local command="$2"
# Check if script exists
if jq -e --arg name "$name" '.scripts[$name]' "$COMPOSER_JSON" >/dev/null; then
# Get existing value
local existing_value
existing_value=$(jq -r --arg name "$name" '.scripts[$name]' "$COMPOSER_JSON")
# If it's the same command, do nothing
if [ "$existing_value" = "$command" ]; then
echo "Script '$name' already exists with the same command, skipping."
return 0
fi
# If it's an array, check if command exists in array
if jq -e --arg name "$name" --arg cmd "$command" '.scripts[$name] | if type == "array" then .[] == $cmd else false end' "$COMPOSER_JSON" >/dev/null; then
echo "Script '$name' already contains this command, skipping."
return 0
fi
# Convert to array if it's not already one and add the new command
jq --indent 4 --arg name "$name" --arg cmd "$command" \
'.scripts[$name] = if (.scripts[$name] | type) == "array"
then .scripts[$name] + [$cmd]
else [.scripts[$name], $cmd]
end' "$COMPOSER_JSON" > "$COMPOSER_JSON.tmp"
mv "$COMPOSER_JSON.tmp" "$COMPOSER_JSON"
echo "Added command to existing script '$name'"
else
# Script doesn't exist, add it
jq --indent 4 --arg name "$name" --arg cmd "$command" '.scripts[$name] = $cmd' "$COMPOSER_JSON" > "$COMPOSER_JSON.tmp"
mv "$COMPOSER_JSON.tmp" "$COMPOSER_JSON"
echo "Added new script '$name'"
fi
}
# Normalize arguments
if [ "$0" = "repository" ] || [ "$0" = "script" ]; then
# We're being run via bash -c, use $0 as the command
cmd="$0"
# Don't shift, keep $1 as our argument
else
# Normal execution, use $1 as the command
cmd="$1"
shift
fi
# Main execution
case "$cmd" in
repository)
add_repository "$1"
;;
script)
add_script "$1" "$2"
;;
*)
echo "Usage: $0 {repository|script} [args...]"
echo " repository <vendor/package> Add a GitHub repository"
echo " script <name> <command> Add a composer script"
exit 1
;;
esac