-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.sh
211 lines (181 loc) · 6.68 KB
/
create.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
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
#!/bin/bash
####################################
# Utility script used to download an
# input file for an Advent Of Code
# problem day, and create the Java
# files from templates.
####################################
YEAR_REGEX='^20(1[5-9]|2[0-4])$'
DAY_REGEX='^(0?[1-9]|1[0-9]|2[0-5])$'
TYPE_REGEX='^(basic|char_grid|int_grid)$'
USER_AGENT_VALUE="https://github.com/zodac/advent-of-code by zodac"
function main() {
year=""
day=""
type="basic"
type_name="Basic"
type_directory="basic"
while getopts 'y:d:t:' flag; do
case "${flag}" in
y) year="${OPTARG}" ;;
d) day="${OPTARG}" ;;
t) type="${OPTARG}" ;;
*) print_usage
exit 1 ;;
esac
done
if [[ -z "${year}" ]]; then
year=$(date +%Y)
fi
if [[ -z "${day}" ]]; then
day=$(date +%d)
fi
if ! [[ "${year}" =~ ${YEAR_REGEX} ]] ; then
_error "Input year '${year}' is not a valid year"
print_usage
exit 1
fi
if ! [[ "${day}" =~ ${DAY_REGEX} ]] ; then
_error "Input day '${day}' is not a valid day"
print_usage
exit 1
fi
if ! [[ "${type}" =~ ${TYPE_REGEX} ]] ; then
_error "Input type '${type}' is not a valid type"
print_usage
exit 1
else
case "${type}" in
basic)
type_name="Basic"
type_directory="basic"
;;
char_grid)
type_name="Character Grid"
type_directory="grid/char"
;;
int_grid)
type_name="Integer Grid"
type_directory="grid/int"
;;
esac
fi
if [[ ${day} -lt 10 ]]; then
# Add leading 0 to be used in files, but keep option without a leading 0 for HTTP requests
day_long="0${day: -1}"
day="${day: -1}"
else
day_long="${day}"
fi
_debug "Creating ${type_name} template for ${year}, Day ${day_long}:"
if [[ ! -d "./${year}" ]]; then
_error "\t- New year, please create directory structure"
exit 1
fi
# Input file
_info "\t- Creating actual input file"
if [[ -f "./advent-of-code-inputs/${year}/day${day_long}.txt" ]]; then
_warning "\t\t- Input file already exists, skipping"
elif [[ -z "${AOC_COOKIE}" ]]; then
_warning "\t\t- No cookie set for AOC, cannot create actual input file"
else
# Do not include '\n' when writing out the HTTP code since it will already be on its own line, and would add an extra line for actual inputs
output=$(curl \
--user-agent "${USER_AGENT_VALUE}" \
--silent --header "Cookie: session=${AOC_COOKIE}" \
--write-out "%{http_code}" \
"https://adventofcode.com/${year}/day/${day}/input")
http_status_code=$(echo "${output}" | tail -1)
if [[ ${http_status_code} != "200" ]]; then
_error "\t\t- Invalid response code for input ${http_status_code}"
exit 1;
fi
echo "${output}" | head -n -1 > "./advent-of-code-inputs/${year}/day${day_long}.txt" || exit 1
cd ./advent-of-code-inputs || exit 1
git add "${year}/day${day_long}.txt"
git commit --quiet -m "Adding input for ${year}, Day ${day_long}"
git push origin HEAD:main --quiet
cd .. || exit 1
fi
# Example input file
_info "\t- Creating example input file"
if [[ -f "./${year}/src/test/resources/day${day_long}.txt" ]]; then
_warning "\t\t- Example file already exists, skipping"
else
touch "./${year}/src/test/resources/day${day_long}.txt"
fi
# Java source
_info "\t- Creating Java source file"
if [[ -f "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java" ]]; then
_warning "\t\t- Java source file already exists, skipping"
else
if [[ ! -f "./templates/${type_directory}/Day.java" ]]; then
_error "\t\t- Template source file './templates/${type_directory}/Day.java' does not exist"
exit 1
fi
cp "./templates/${type_directory}/Day.java" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
sed -i -e "s|%YEAR%|${year}|g" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
sed -i -e "s|%DAY%|${day}|g" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
sed -i -e "s|%DAY_LONG%|${day_long}|g" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
fi
# Java test
_info "\t- Creating Java test file"
if [[ -f "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java" ]]; then
_warning "\t\t- Java test file already exists, skipping"
else
if [[ ! -f "./templates/${type_directory}/DayTest.java" ]]; then
_error "\t\t- Template test file './templates/${type_directory}/DayTest.java' does not exist"
exit 1
fi
cp "./templates/${type_directory}/DayTest.java" "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java"
sed -i -e "s|%YEAR%|${year}|g" "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java"
sed -i -e "s|%DAY%|${day}|g" "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java"
sed -i -e "s|%DAY_LONG%|${day_long}|g" "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java"
fi
# Title
_info "\t- Retrieving title"
if ! grep -q "%TITLE%" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"; then
_warning "\t\t- Title already exists in file, skipping"
else
# Include '\n' when writing out the HTTP code since it will not be on its own line otherwise
title_output=$(curl \
--user-agent "${USER_AGENT_VALUE}" \
--silent \
--write-out "\n%{http_code}" \
"https://adventofcode.com/${year}/day/${day}")
title_http_status_code=$(echo "${title_output}" | tail -1)
if [[ ${title_http_status_code} != "200" ]]; then
_error "\t\t- Invalid response code for title ${title_http_status_code}"
exit 1;
fi
title=$(echo "${title_output}" | head -n -1 | grep '<h2>' | awk '{split($0, a, "<h2>")} END {print a[2]}' | awk '{split($0, a, "</h2>")} END {print a[1]}' | cut -d ':' -f2 | awk '{split($0, a, "---")} END {print a[1]}' | awk '{$1=$1;print}')
sed -i -e "s|%TITLE%|${title}|g" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
fi
_success "${type_name} template created for ${year}, Day ${day_long}"
}
function _debug() {
# shellcheck disable=SC2059
printf "\033[1;34m$*\033[0m\n"
}
function _info() {
# shellcheck disable=SC2059
printf "$*\n"
}
function _success() {
# shellcheck disable=SC2059
printf "\033[1;32m$*\033[0m\n"
}
function _warning() {
# shellcheck disable=SC2059
printf "\033[1;33m$*\033[0m\n"
}
function _error() {
# shellcheck disable=SC2059
printf "\033[1;31m$*\033[0m\n"
}
function print_usage() {
_warning "Usage:\n\t-y: Year (Optional: 2015-2024)\n\t-d: Day (Optional: 1-25)\n\t-t: Type (Optional: 'basic', 'char_grid', 'int_grid')"
}
# Start script execution
set -euo pipefail
main "$@"