forked from hamza72x/web2app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.sh
executable file
·120 lines (97 loc) · 4.04 KB
/
watch.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
#!/opt/homebrew/bin/bash
# this watch.sh requires bash 4.0 or higher
# `brew install bash` on macos
bash_version=$(bash --version | head -n 1 | awk '{print $4}')
echo ""
echo "bash version is $bash_version -----------------------"
echo "watch.sh requires bash 4.0 or higher ----------------"
echo ""
build_mod() {
template_mod="src/generated.rs"
echo '// Auto generated by $$ make build_template' >$template_mod
echo "// Don't edit this file by HAND, edit the ../template directory instead." >>$template_mod
echo "// Then keep running watch.sh" >>$template_mod
echo "" >>$template_mod
os=$(uname -s)
opts=""
if [ "$os" = "Linux" ]; then
opts="-w0"
fi
# dir: template/src
echo "pub const FILE_APP_CONFIG_RS: &str = \"$(cat template/src/app_config.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_APP_DATA_RS: &str = \"$(cat template/src/app_data.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_APP_MENU_RS: &str = \"$(cat template/src/app_menu.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_BUILDER_RS: &str = \"$(cat template/src/builder.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_GENERATED_RS: &str = \"$(cat template/src/generated.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_MAIN_RS: &str = \"$(cat template/src/main.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_UTIL_RS: &str = \"$(cat template/src/util.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
# dir: template
echo "pub const FILE_BUILD_RS: &str = \"$(cat template/build.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_CARGO_TOML: &str = \"$(cat template/Cargo.toml | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_CARGO_LOCK: &str = \"$(cat template/Cargo.lock | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
echo "pub const FILE_TAURI_CONF_JSON: &str = \"$(cat template/tauri.conf.json | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
}
build_template_gen() {
script_js="template/src/script.js"
out="template/src/generated.rs"
echo "// This file is generated by watch.sh" >"$out"
echo "// Do not edit it directly" >>"$out"
echo "" >>"$out"
echo "pub const INIT_SCRIPT: &str = r#\"" >>"$out"
echo "$(cat "$script_js")" >>"$out"
echo "\"#;" >>"$out"
}
autogen() {
build_template_gen
build_mod
}
watch_files() {
# array of allowed files to watch
files=(
"template/src/app_config.rs"
"template/src/app_data.rs"
"template/src/app_menu.rs"
"template/src/builder.rs"
"template/src/generated.rs"
"template/src/main.rs"
"template/src/util.rs"
"template/src/script.js"
"template/build.rs"
"template/Cargo.toml"
"template/Cargo.lock"
"template/tauri.conf.json"
"template/icons/app-icon.png"
)
# map of states of files (map key is the sha256 of $file and state value is the string of : ls -l $file)
declare -A states
# init states
for file in "${files[@]}"; do
echo "watching $file"
if [ ! -f "$file" ]; then
echo "file $file does not exist skipping"
continue
fi
key="$(sha256sum $file)"
value="$(ls -l $file)"
states[$key]="$value"
done
# watch loop
while true; do
for file in "${files[@]}"; do
value="$(ls -l $file)"
key="$(sha256sum $file)"
if [ ! -f "$file" ]; then
echo "file $file does not exist skipping"
continue
fi
if [ "${states[$key]}" != "$value" ]; then
echo "file $file changed"
states[$key]="$value"
autogen
fi
done
sleep 1
done
}
autogen
watch_files