Repository navigation
Expand file tree
/
Copy pathinkextrude
More file actions
executable file
·183 lines (151 loc) · 3.46 KB
/
Copy pathinkextrude
File metadata and controls
executable file
·183 lines (151 loc) · 3.46 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
print_usage () {
>&2 cat << EOF
inkextrude is a wrapper for https://github.com/jbenf/inkextrude
usage: inkextrude [-d] [-w] <input.svg>
-d: Use Docker Execution Mode
-r: Rootless Docker Mode
-w: Watch Input File for changes
-f: Force overwrite
EOF
}
prepare_docker() {
echo "Preparing xslt3 Docker Image..."
pwd=$(dirname $(realpath $0))
pushd $pwd
dockerimage=$(docker build -q -f - . <<EOF
FROM node:19.5.0-alpine3.17
RUN npm i -g xslt3
ADD ./src/inkextrude.xslt /inkextrude.xslt
WORKDIR /out
EOF
)
popd
if [ "$dockerimage" = "" ] ; then
echo "Image creation failed"
exit 8
fi
echo "Image created: $dockerimage"
}
cleanup() {
if [ "$dockermode" = true ] && [ "$dockerimage" != "" ] ; then
echo "Cleaning up Docker Image..."
docker image rm $dockerimage > /dev/null
fi
}
abort() {
echo "Aborting..."
cleanup
exit 7
}
transform() {
echo "Transforming..."
tmpfile=$(mktemp)
if [ "$dockermode" = true ] ; then
if [ "$rootless" = true ] ; then
group=""
else
group="--user $(id -u):$(id -g)"
fi
docker run -it --rm $group -v $outdir:/out:rw $dockerimage xslt3 -xsl:/inkextrude.xslt -s:/out/$in_name > $tmpfile
else
pwd=$(dirname $(realpath $0))
xslt3 -xsl:$pwd/src/inkextrude.xslt -s:$outdir/$in_name > $tmpfile
fi
cat $tmpfile > $outdir/$out_name
rm $tmpfile
echo "done"
}
dockermode=false
watchmode=false
force=false
dockerimage=""
rootless=false
while getopts 'drwfh' opt; do
case "$opt" in
d)
echo "Using Docker Execution Mode"
dockermode=true
;;
r)
echo "Using Rootless Docker Execution Mode"
dockermode=true
rootless=true
;;
w)
echo "Watching"
watchmode=true
;;
f)
echo "Force overwrite"
force=true
;;
?|h)
print_usage
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
inputfile="${@: -1}"
extension="${inputfile##*.}"
in_name=$(basename $inputfile)
out_name=${in_name%.*}.scad
outdir=$(realpath $(dirname $inputfile))
if [ "$extension" != "svg" ] ; then
>&2 cat << EOF
The input File has to be a SVG File
EOF
print_usage
exit 2
fi
if [ "$force" = false ] ; then
fail=false
if test -f "$outdir/$out_name"; then
echo "$outdir/$out_name exists."
fail=true
fi
if test -d "$outdir/svg_gen"; then
echo "$outdir/svg_gen exists."
fail=true
fi
if [ "$fail" = true ] ; then
echo "You have to call inkextrude with '-f' to force overwrite."
exit 3
fi
fi
if [ "$dockermode" = true ] ; then
if ! command -v docker &> /dev/null
then
echo "Dependency 'docker' could not be found"
exit 4
fi
else
if ! command -v xslt3 &> /dev/null
then
echo "Dependency 'xslt3' could not be found. Please install it with 'npm' globally or use the Docker execution method"
exit 5
fi
fi
if [ "$watchmode" = true ] ; then
if ! command -v watch &> /dev/null
then
echo "Dependency 'watch' could not be found"
exit 6
fi
fi
trap 'abort' SIGINT
if [ "$dockermode" = true ] ; then
prepare_docker
fi
if [ "$watchmode" = true ] ; then
while true
do
transform
echo "Watching for changes..."
watch -g sha1sum $outdir/$in_name >/dev/null
done
else
transform
fi
cleanup