-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswiftw
More file actions
executable file
·68 lines (54 loc) · 1.56 KB
/
swiftw
File metadata and controls
executable file
·68 lines (54 loc) · 1.56 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
#!/bin/bash
echo "Content-Type: text/html"
echo "Content:"
echo
f() {
if [ "$#" -ne 0 ]; then
echo
else
>&2 echo -e "Usage:\n$0: [-o, --output] [OUTPUT FILE] [SWIFT FILES TO COMPILE...]\n\n-o: Save executable into given file to be ran from Web Browser\n\nNote: The last file will be the main file"
exit 1
fi
while getopts ":o:-output:" opt; do
case $opt in
o)
output=$OPTARG;
;;
-output)
output=$OPTARG;
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
}
f "$@"
mkdir .CGISwiftCompile
# Copy libraries
cp /usr/local/lib/SwiftyWeb.swift .CGISwiftCompile/SwiftyWeb.swift
cp /usr/local/lib/SwiftyHTM.swift .CGISwiftCompile/SwiftyHTM.swift
# Setup Swift files
cp ${@:$#} .CGISwiftCompile/main.swift
argumentsLessLast=${@:1:$(($#-1))}
argumentsLessLast=${argumentsLessLast//$output/}
argumentsLessLast=${argumentsLessLast/"-o"/}
argumentsLessLast=${argumentsLessLast//"--output"/}
cp $argumentsLessLast .CGISwiftCompile/
# Compile
swiftc .CGISwiftCompile/* 2>&1
# Run
./main
# If the ouput is set, add code to display in Web Browser
if [ -z ${output+x} ]; then printf ""; else sed -i '1iprint("Content-Type: text/html");print("Content:");print("")' .CGISwiftCompile/main.swift; fi
# Compile again if the output is set
if [ -z ${output+x} ]; then printf ""; else swiftc .CGISwiftCompile/* 2>&1; fi
# Remove all
rm -rf .CGISwiftCompile
# If $output is set, keep the file
if [ -z ${output+x} ]; then rm main; else mv main $output; fi