1 |
#!/bin/sh |
2 |
# |
3 |
# mkdist -- a script to create software distributions |
4 |
# |
5 |
# This script is part of OSN Commons. Copyright (C) 2024 OSN Developers. |
6 |
# |
7 |
# OSN Commons is free software: you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation, either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# OSN Commons is distributed in the hope that it will be useful, |
13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with OSN Commons. If not, see <https://www.gnu.org/licenses/>. |
19 |
# |
20 |
|
21 |
me=$0 |
22 |
canonical_name=$(echo $me | rev | cut -d'/' -f1 | rev) |
23 |
|
24 |
if [ $(echo $me | cut -c1) = "/" ]; then |
25 |
me=$canonical_name |
26 |
fi |
27 |
|
28 |
version="1.0.0" |
29 |
|
30 |
check_requirements() { |
31 |
if [ -z "$(command -v tar)" ]; then |
32 |
echo "$me: GNU tar is required to use this script" >&2 |
33 |
exit 1 |
34 |
fi |
35 |
|
36 |
if [ -z "$(command -v jq)" ]; then |
37 |
echo "$me: jq is required to use this script" >&2 |
38 |
exit 1 |
39 |
fi |
40 |
|
41 |
if [ -z "$(command -v rsync)" ]; then |
42 |
echo "$me: rsync is required to use this script" >&2 |
43 |
exit 1 |
44 |
fi |
45 |
|
46 |
if [ -z "$(command -v sed)" ]; then |
47 |
echo "$me: sed is required to use this script" >&2 |
48 |
exit 1 |
49 |
fi |
50 |
} |
51 |
|
52 |
show_help() { |
53 |
cat <<EOF |
54 |
Usage: |
55 |
$me [options] <directory> |
56 |
|
57 |
Create a software distribution from a directory. |
58 |
|
59 |
Options: |
60 |
-h, --help Display this help and exit |
61 |
-v, --version Display version information and exit |
62 |
-f, --formats=FORMATS Specify the format(s) of the output |
63 |
archives. Multiple formats can be specified, |
64 |
separated by commas. |
65 |
Supported formats are: tar.gz, tar.bz2, |
66 |
tar.xz, zip. Defaults to tar.gz. |
67 |
-o, --output-path Specify the path where the output archives |
68 |
will be saved. Defaults to the current |
69 |
directory. |
70 |
-n, --output-basename Specify the basename of the output archives. |
71 |
Defaults to the name of the directory being |
72 |
archived. |
73 |
-e, --exclude=PATTERN Exclude files or directories matching PATTERN |
74 |
from the archive. This option can be specified |
75 |
multiple times. |
76 |
--exclude-from Read the list of patterns to exclude from the |
77 |
file specified by the argument. |
78 |
-t, --archive-version Create a .tarball-version/.zipball-version |
79 |
file in the output archives containing the |
80 |
given version argument. |
81 |
-V, --verbose Enable verbose output. |
82 |
|
83 |
Bug reports and questions should be sent to <[email protected]>. |
84 |
EOF |
85 |
} |
86 |
|
87 |
show_version() { |
88 |
cat <<EOF |
89 |
$canonical_name (OSN Commons) v$version |
90 |
|
91 |
This program is free software: you can redistribute it and/or modify |
92 |
it under the terms of the GNU General Public License as published by |
93 |
the Free Software Foundation, either version 3 of the License, or |
94 |
(at your option) any later version. |
95 |
|
96 |
Written by Ar Rakin. |
97 |
EOF |
98 |
} |
99 |
|
100 |
stat_directory() { |
101 |
local result |
102 |
result=$(stat "$1" 2>&1) |
103 |
|
104 |
if [ $? -ne 0 ]; then |
105 |
local msg=$(echo $result | cut -c 7-) |
106 |
echo "$me: $msg" >&2 |
107 |
exit 1 |
108 |
fi |
109 |
|
110 |
if [ ! -d "$1" ]; then |
111 |
echo "$me: '$1' is not a directory" >&2 |
112 |
exit 1 |
113 |
fi |
114 |
|
115 |
if [ ! -r "$1" ]; then |
116 |
echo "$me: cannot read directory '$1'" >&2 |
117 |
exit 1 |
118 |
fi |
119 |
|
120 |
if [ ! -x "$1" ]; then |
121 |
echo "$me: cannot access directory '$1'" >&2 |
122 |
exit 1 |
123 |
fi |
124 |
} |
125 |
|
126 |
check_optarg() { |
127 |
if [ -z "$2" ]; then |
128 |
echo "$me: option '$1' requires an argument" >&2 |
129 |
echo "Try '$me --help' for more information." >&2 |
130 |
exit 1 |
131 |
fi |
132 |
} |
133 |
|
134 |
find_version() { |
135 |
json_filename="" |
136 |
|
137 |
if [ -f "$1/package.json" ]; then |
138 |
json_filename="$1/package.json" |
139 |
elif [ -f "$1/composer.json" ]; then |
140 |
json_filename="$1/composer.json" |
141 |
elif [ -f "$1/version.json" ]; then |
142 |
json_filename="$1/version.json" |
143 |
fi |
144 |
|
145 |
if [ ! -z "$json_filename" ]; then |
146 |
local version=$(jq -r .version "$1/package.json") |
147 |
|
148 |
if [ -n "$version" ]; then |
149 |
echo $version |
150 |
return |
151 |
fi |
152 |
fi |
153 |
|
154 |
if [ -f "$1/.version" ]; then |
155 |
local version=$(cat "$1/.version") |
156 |
|
157 |
if [ -n "$version" ]; then |
158 |
echo $version |
159 |
return |
160 |
fi |
161 |
fi |
162 |
|
163 |
latest_git_tag=$(git -C "$1" describe --tags --abbrev=0 2>/dev/null) |
164 |
|
165 |
if [ -n "$latest_git_tag" ]; then |
166 |
echo $latest_git_tag |
167 |
return |
168 |
fi |
169 |
} |
170 |
|
171 |
find_name() { |
172 |
json_filename="" |
173 |
|
174 |
if [ -f "$1/package.json" ]; then |
175 |
json_filename="$1/package.json" |
176 |
elif [ -f "$1/composer.json" ]; then |
177 |
json_filename="$1/composer.json" |
178 |
elif [ -f "$1/version.json" ]; then |
179 |
json_filename="$1/version.json" |
180 |
fi |
181 |
|
182 |
if [ ! -z "$json_filename" ]; then |
183 |
local name=$(jq -r .name "$1/package.json") |
184 |
|
185 |
if [ -n "$name" ]; then |
186 |
echo $name |
187 |
return |
188 |
fi |
189 |
fi |
190 |
} |
191 |
|
192 |
# Default values |
193 |
formats="tar.gz" |
194 |
output_path="." |
195 |
output_basename="" |
196 |
excludes="--exclude=.git --exclude=.svn" |
197 |
archive_version="" |
198 |
verbose=0 |
199 |
target="" |
200 |
|
201 |
# Parse command line arguments |
202 |
posarg_start=0 |
203 |
|
204 |
while [ $# -gt 0 ]; do |
205 |
if [ $posarg_start -eq 1 ]; then |
206 |
break |
207 |
fi |
208 |
|
209 |
case $1 in |
210 |
-h | --help) |
211 |
show_help |
212 |
exit 0 |
213 |
;; |
214 |
-v | --version) |
215 |
show_version |
216 |
exit 0 |
217 |
;; |
218 |
-f | --formats) |
219 |
check_optarg $@ |
220 |
shift |
221 |
formats=$(echo $1 | tr ',' ' ') |
222 |
;; |
223 |
-o | --output-path) |
224 |
check_optarg $@ |
225 |
shift |
226 |
output_path=$1 |
227 |
;; |
228 |
-n | --output-basename) |
229 |
check_optarg $@ |
230 |
shift |
231 |
output_basename="$1" |
232 |
;; |
233 |
-e | --exclude) |
234 |
check_optarg $@ |
235 |
shift |
236 |
escaped=$(echo "$1" | sed 's/"/\\"/g') |
237 |
excludes="$excludes --exclude \"$escaped\"" |
238 |
;; |
239 |
--exclude-from) |
240 |
check_optarg $@ |
241 |
shift |
242 |
escaped=$(echo "$1" | sed 's/"/\\"/g') |
243 |
excludes="$excludes --exclude-from \"$escaped\"" |
244 |
;; |
245 |
-t | --archive-version) |
246 |
check_optarg $@ |
247 |
shift |
248 |
archive_version="$1" |
249 |
;; |
250 |
-V | --verbose) |
251 |
verbose=1 |
252 |
;; |
253 |
--) |
254 |
posarg_start=1 |
255 |
shift |
256 |
break |
257 |
;; |
258 |
-*) |
259 |
echo "$me: invalid option '$1'" >&2 |
260 |
echo "Try '$me --help' for more information." >&2 |
261 |
exit 1 |
262 |
;; |
263 |
*) |
264 |
if [ ! -z "$target" ]; then |
265 |
echo "$me: too many arguments provided" >&2 |
266 |
echo "Try '$me --help' for more information." >&2 |
267 |
exit 1 |
268 |
fi |
269 |
|
270 |
target="$1" |
271 |
;; |
272 |
esac |
273 |
shift |
274 |
done |
275 |
|
276 |
if [ -z "$target" ]; then |
277 |
echo "$me: missing directory operand" >&2 |
278 |
echo "Try '$me --help' for more information." >&2 |
279 |
exit 1 |
280 |
fi |
281 |
|
282 |
stat_directory "$target" |
283 |
|
284 |
# Set the output basename if not provided |
285 |
if [ -z "$output_basename" ]; then |
286 |
output_basename=$(find_name "$target") |
287 |
fi |
288 |
|
289 |
if [ -z "$output_basename" ]; then |
290 |
output_basename=$(readlink -f "$target") |
291 |
output_basename=$(basename "$output_basename") |
292 |
fi |
293 |
|
294 |
if [ -z "$archive_version" ]; then |
295 |
archive_version=$(find_version "$target") |
296 |
fi |
297 |
|
298 |
if [ -z "$archive_version" ]; then |
299 |
archive_version="0.0.0" |
300 |
fi |
301 |
|
302 |
echo "$me: preparing '$target' for packaging" |
303 |
|
304 |
tmpdir=$(mktemp -d) |
305 |
rsync_tmpdir="$tmpdir/${output_basename}-${archive_version}" |
306 |
|
307 |
mkdir $rsync_tmpdir |
308 |
|
309 |
if [ $verbose -eq 1 ]; then |
310 |
echo "$me: copying files to temporary directory" |
311 |
fi |
312 |
|
313 |
rsync_cmd="rsync -a $excludes $target/ $rsync_tmpdir/" |
314 |
|
315 |
if [ $verbose -eq 1 ]; then |
316 |
echo "$me: executing: $rsync_cmd" |
317 |
fi |
318 |
|
319 |
eval $rsync_cmd |
320 |
|
321 |
for format in "$formats"; do |
322 |
archive_name="${output_basename}-${archive_version}.$format" |
323 |
|
324 |
if echo "$format" | grep -o "tar" >/dev/null 2>&1; then |
325 |
echo "$archive_version" >$rsync_tmpdir/.tarball-version |
326 |
fi |
327 |
|
328 |
if [ "$format" = "zip" ]; then |
329 |
echo "$archive_version" >$rsync_tmpdir/.zipball-version |
330 |
fi |
331 |
|
332 |
case "$format" in |
333 |
tar.gz | tar.bz2 | tar.xz) |
334 |
flag="" |
335 |
|
336 |
case "$format" in |
337 |
tar.gz) |
338 |
flag="z" |
339 |
;; |
340 |
tar.bz2) |
341 |
flag="j" |
342 |
;; |
343 |
tar.xz) |
344 |
flag="J" |
345 |
;; |
346 |
esac |
347 |
|
348 |
tar_cmd="tar -c${flag}f "$output_path/$archive_name" -C "$tmpdir" ." |
349 |
|
350 |
if [ $verbose -eq 1 ]; then |
351 |
echo "$me: executing: $tar_cmd" |
352 |
fi |
353 |
|
354 |
eval $tar_cmd |
355 |
;; |
356 |
zip) |
357 |
zip_cmd="zip -r "$output_path/$archive_name" $tmpdir" |
358 |
|
359 |
if [ $verbose -eq 1 ]; then |
360 |
echo "$me: executing: $zip_cmd" |
361 |
fi |
362 |
|
363 |
eval $zip_cmd |
364 |
;; |
365 |
|
366 |
*) |
367 |
echo "$me: unsupported format '$format'" >&2 |
368 |
exit 1 |
369 |
;; |
370 |
esac |
371 |
|
372 |
rm -f $rsync_tmpdir/.tarball-version |
373 |
rm -f $rsync_tmpdir/.zipball-version |
374 |
done |
375 |
|
376 |
rm_flags=-rf |
377 |
|
378 |
if [ $verbose -eq 1 ]; then |
379 |
rm_flags="${rm_flags}v" |
380 |
fi |
381 |
|
382 |
rm $rm_flags $rsync_tmpdir |