summaryrefslogtreecommitdiff
path: root/artix/home/.local/bin/helper
blob: 57b0b5299d885fdb7106662972dc617a1311b1f7 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#! /bin/sh

# Can override this ENV variables
quiet=${quiet:-0}
format=${format:-"pdf"}
typst_src_dir=${typst_src_dir:-"drafts"}
typst_output_dir=${typst_output_dir:-"content"}

blue="\033[34m"
bold_blue="\033[1;34m"
green="\033[32m"
bold_green="\033[1;32m"
yellow="\033[33m"
red="\033[31m"
no_color="\033[0m"

colored_output() {
	color_name=$1
	shift
	case $color_name in
		blue)
			color=$blue;;
		bold_blue)
			color=$bold_blue;;
		green)
			color=$green;;
		bold_green)
			color=$bold_green;;
		yellow)
			color=$yellow;;
		red)
			color=$red;;
		*)
			color=$no_color;;
	esac

	printf "${color}$@${no_color}\n"
}

helper_log() {

	if [ $quiet -gt 0 ]; then
		return 0
	fi

	log_request=$1
	shift
	case $log_request in
		info)
			log_level=$(colored_output blue INFO);;
		success)
			log_level=$(colored_output green SUCCESS);;
		warn)
			log_level=$(colored_output yellow WARNING);;
		testing)
			log_level=$(colored_output yellow TEST);;
		err)
			log_level=$(colored_output red ERROR);;
		fail)
			log_level=$(colored_output red FAILED);;
		*)
			log_level=?
	esac

	if [ $log_level != "FAILED" ] && [ $log_level != "ERROR" ]; then
		printf "[$log_level] $@\n"
	else
		printf "[$log_level] $@\n >&2"
	fi
}

display_help() {
 echo -e "
$(colored_output bold_green "Usage:")

	$(colored_output bold_blue "helper") COMMAND

	or

	$(colored_output bold_blue "helper")
	> COMMAND
	> COMMAND
	> quit


$(colored_output bold_green "Commands:")

	$(colored_output bold_blue "help")
		Prints help.

	$(colored_output bold_blue "quit")
		Quit helper.

	$(colored_output bold_blue "check")
		Checks that all dependencies are installed

	$(colored_output bold_blue "sync")
		git pull

	$(colored_output bold_blue "build")
		Compiles typst file in pdf.
		There's other formats, see ENV vars section

	$(colored_output bold_blue "clean")
		Deletes all typst compiled files of a given format


$(colored_output bold_green "ENV vars:")

	$(colored_output bold_blue "quiet")
		Make the helper silent :
		quiet=1 ./helper COMMANDE
		default value : 0

	$(colored_output bold_blue "format")
		You can change the target format of helper's commands
		Example : format=png ./helper build will compile all typst source code into png
		default value : pdf

	$(colored_output bold_blue "typst_src_dir")
		Directory with the typst source code
		default value : drafts

	$(colored_output bold_blue "typst_output_dir")
		Target directory with compiled typst
		default value : content
"
}

greet_user() {
	helper_log info "hi $(whoami)"
}

unknown_cmd() {
	echo "??? get help with './helper help'"
}

eval_input() {
	case $1 in
		help)display_help;;
		quit)echo "bye $(whoami) ♥" ; exit 0;;
		check)check;;
		sync)sync;;
		build)build;;
		clean)clean;;
		*)unknown_cmd;;
	esac
}

check() {
	helper_log testing "Checking dependencies."
	git -v > /dev/null 2>&1 || (helper_log fail "git isn't installed ???" && exit 1)
	typst --version > /dev/null 2>&1 || (helper_log fail "typst isn't installed" && exit 1)
	helper_log success "You're all good :)"
}

sync() {
	git pull
}

build() {
	typ_files="$(find $typst_src_dir -name "*.typ")"

	compiled=0

	for file in $typ_files; do
		target_path=$(echo $file | sed "s/"$typst_src_dir"/"$typst_output_dir"/g; s/\.typ$/."$format"/g")
		target_dir=$(dirname $target_path)

		if ! [ -d $target_dir ]; then
			mkdir -p $target_dir
		fi

		if [ -f $target_path ]; then
			helper_log warn "File $file is already compiled here : $target_path, aborting."
		else
			helper_log info "Compiling $file here : $target_path"
			typst compile $file $target_path
			if [ -f $target_path ]; then
				helper_log success "Successfully compiled $file here : $target_path ."
				compiled=$(expr $compiled + 1)
			else
				helper_log fail "Couldn't compile $file here : $target_path"
			fi
		fi
	done

	helper_log info "Found : $(echo "$typ_files" | wc -l) Typst files."
	helper_log info "Compiled : $compiled $format."
}

clean() {
	build_files="$(find $typst_output_dir -name "*.$format")"

	deleted=0

	for file in $build_files; do
		helper_log info "Deleting $file"
		rm $file
		if ! [ -f $file ]; then
			helper_log success "Successfully deleted $file ."
			deleted=$(expr $deleted + 1)
		else
			helper_log fail "Couldn't delete $file ."
		fi
	done

	helper_log info "Found : $(echo "$build_files" | wc -l) Typst files."
	helper_log info "Deleted : $deleted $format."

	empty_dirs="$(find . -type d -empty -not -path "*/.git/*")"

	while [ "$empty_dirs" != "" ]; do
		if [ -n "$empty_dirs" ]; then
			helper_log warn "$(echo "$empty_dirs" | wc -l) empty dirs found."
			helper_log info "deleting empty dirs"

			for dir in $empty_dirs; do
				helper_log info "Deleting $dir"
				rmdir $dir && helper_log success "Successfully deleted $dir." || helper_log fail "Couldn't delete $dir"
			done
		fi
		empty_dirs="$(find . -type d -empty -not -path "*/.git/*")"
	done
}

if ! [ -d $typst_src_dir ]; then
	mkdir $typst_src_dir
fi

if ! [ -d $typst_output_dir ]; then
	mkdir $typst_output_dir
fi

if [ $# -eq 0 ]; then
	greet_user
	while true; do
		echo -n "> "
		read user_input
		eval_input $user_input
	done
elif [ $# -eq 1 ]; then
	eval_input $1
else
	unknown_cmd
fi