-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·163 lines (134 loc) · 3.54 KB
/
build.sh
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
source src/check_os.sh
function build() {
local out=$1
generate_bin "$out"
generate_checksum "$out"
echo "⚡️Build completed⚡️"
}
function generate_bin() {
local out=$1
local temp
temp="$(dirname "$out")/temp.sh"
echo '#!/bin/bash' > "$temp"
echo "Generating the '$(basename "$out")' in the '$(dirname "$out")' folder..."
for file in src/*.sh; do
{
echo "# $file"
tail -n +2 "$file" >> "$temp"
echo ""
} >> "$temp"
done
cat "$ENTRY_POINT" >> "$temp"
grep -v '^source' "$temp" > "$out"
rm "$temp"
chmod u+x "$out"
}
function generate_checksum() {
local out=$1
if [[ "$_OS" == "Windows" ]]; then
return
fi
if [[ "$_OS" == "OSX" ]]; then
checksum=$(shasum -a 256 "$out")
elif [[ "$_OS" == "Linux" ]]; then
checksum=$(sha256sum "$out")
fi
echo "$checksum" > "$(dirname "$out")/checksum"
echo "$checksum"
}
function get_current_version() {
local file_with_version=$1
grep "declare -r $VERSION_VAR_NAME=" "$file_with_version" \
| sed "s/declare -r $VERSION_VAR_NAME=\"\([0-9]*\.[0-9]*\.[0-9]*\)\"/\1/"
}
function update_version() {
local version_type=$1
local file_with_version=$2
local current_version
current_version=$(grep "declare -r $VERSION_VAR_NAME=" "$file_with_version" \
| sed "s/declare -r $VERSION_VAR_NAME=\"\([0-9]*\.[0-9]*\.[0-9]*\)\"/\1/")
IFS='.' read -r major minor patch <<< "$current_version"
# Increment the appropriate version part
case "$version_type" in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch)
patch=$((patch + 1))
;;
*)
echo "Invalid version increment option: $version_type. Use major, minor, or patch."
exit 1
;;
esac
local new_version="$major.$minor.$patch"
local search_pattern="declare -r $VERSION_VAR_NAME=\"[0-9]*\.[0-9]*\.[0-9]*\""
local replace_pattern="declare -r $VERSION_VAR_NAME=\"$new_version\""
sed -i.bak \
"s/$search_pattern/$replace_pattern/" \
"$file_with_version"
rm "${file_with_version}.bak"
echo "$new_version"
}
function update_changelog() {
local target_version=$1
local new_version=$2
local changelog_file="CHANGELOG.md"
local current_date
current_date=$(date +'%Y-%m-%d')
local commits
commits=$(git log "$target_version"..HEAD --oneline)
local temp_file
temp_file=$(mktemp)
# Add the new entry
{
echo "# Changelog"
echo
local url="https://github.com/Purpose-Green/deploy/compare/$target_version...$new_version"
echo "## [$new_version]($url) - $current_date"
echo
while IFS= read -r commit; do
echo "- $commit"
done <<< "$commits"
# Append existing changelog content
sed '1{/^# Changelog/d;}' "$changelog_file"
} > "$temp_file"
# Replace the original changelog with the updated one
mv "$temp_file" "$changelog_file"
}
########################
# MAIN #
########################
VERSION_VAR_NAME="BASH_SKELETON_VERSION"
ENTRY_POINT="entry-point"
OUT_DIR="bin"
NEW_VERSION_TYPE=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--new)
shift
NEW_VERSION_TYPE="$1"
;;
*)
OUT_DIR="$1"
;;
esac
shift
done
if [[ "$NEW_VERSION_TYPE" != "" ]]; then
NEW_VERSION=$(update_version "$NEW_VERSION_TYPE" "$ENTRY_POINT")
echo "Updated version: $NEW_VERSION"
update_changelog "origin/prod" "$NEW_VERSION"
else
echo "Current version: $(get_current_version "$ENTRY_POINT")"
fi
mkdir -p "$OUT_DIR"
build "$OUT_DIR/$ENTRY_POINT"