Skip to content

Commit c3e4508

Browse files
committed
sh and make
1 parent c151c75 commit c3e4508

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

Makefile

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
SCRIPT_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
2+
3+
.PHONY: help run test new create
4+
5+
help:
6+
@echo "Usage: make <target> [variables]"
7+
@echo
8+
@echo "Targets:"
9+
@echo " run Run the main console application."
10+
@echo " test Run the unit tests."
11+
@echo " new DAY=<num> Create a new day with template files."
12+
@echo
13+
@echo "Examples:"
14+
@echo " make run"
15+
@echo " make test"
16+
@echo " make new DAY=5"
17+
18+
run:
19+
dotnet run -c Debug --project "$(SCRIPT_ROOT)src/adventofcode2024.console/adventofcode2024.console.csproj"
20+
21+
test:
22+
dotnet run -c Debug --project "$(SCRIPT_ROOT)tests/adventofcode2024.UnitTests/adventofcode2024.UnitTests.csproj" --disable-logo
23+
24+
new:
25+
@if [ -z "$(DAY)" ]; then \
26+
echo "Error: 'DAY' variable must be set (e.g. make new DAY=5)"; \
27+
exit 1; \
28+
fi
29+
30+
@expr "$(DAY)" + 1 >/dev/null 2>&1 || { \
31+
echo "Error: 'DAY' must be an integer"; \
32+
exit 1; \
33+
}
34+
35+
$(eval DAY_PAD = $(shell printf "%02d" $(DAY)))
36+
37+
$(MAKE) create \
38+
TEMPLATE_PATH="$(SCRIPT_ROOT)src/adventofcode2024.console/input/Day00.txt" \
39+
DAY="$(DAY_PAD)"
40+
41+
$(MAKE) create \
42+
TEMPLATE_PATH="$(SCRIPT_ROOT)src/adventofcode2024/Day00.cs" \
43+
DAY="$(DAY_PAD)"
44+
45+
$(MAKE) create \
46+
TEMPLATE_PATH="$(SCRIPT_ROOT)tests/adventofcode2024.UnitTests/Day00Tests.cs" \
47+
DAY="$(DAY_PAD)"
48+
49+
create:
50+
@if [ -z "$(TEMPLATE_PATH)" ]; then \
51+
echo "Error: 'TEMPLATE_PATH' must be set"; \
52+
exit 1; \
53+
fi
54+
@if [ -z "$(DAY)" ]; then \
55+
echo "Error: 'DAY' must be set"; \
56+
exit 1; \
57+
fi
58+
59+
@target_path="$$(echo "$(TEMPLATE_PATH)" | sed "s/Day00/Day$(DAY)/")"; \
60+
if [ -e "$$target_path" ]; then \
61+
echo "$$target_path already exists."; \
62+
else \
63+
if [ ! -e "$(TEMPLATE_PATH)" ]; then \
64+
echo "Error: Template file '$(TEMPLATE_PATH)' does not exist."; \
65+
exit 1; \
66+
fi; \
67+
cp "$(TEMPLATE_PATH)" "$$target_path"; \
68+
sed -i "s/Day00/Day$(DAY)/g" "$$target_path"; \
69+
echo "Created $$target_path"; \
70+
fi

aoc.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
script_root="$(dirname "$0")"
6+
7+
echoerr() {
8+
printf "%s\n" "$*" >&2
9+
}
10+
11+
usage() {
12+
echo "Usage: $(basename "$0") <command> [args]"
13+
echo
14+
echo "Commands:"
15+
echo " run Run the main console application."
16+
echo " test Run the unit tests."
17+
echo " new <day> Create a new day with template files."
18+
echo
19+
echo "Examples:"
20+
echo " $(basename "$0") run"
21+
echo " $(basename "$0") test"
22+
echo " $(basename "$0") new 5"
23+
}
24+
25+
invoke_run() {
26+
dotnet run \
27+
-c Debug \
28+
--project "${script_root}/src/adventofcode2024.console/adventofcode2024.console.csproj"
29+
}
30+
31+
invoke_test() {
32+
dotnet run \
33+
-c Debug \
34+
--project "${script_root}/tests/adventofcode2024.UnitTests/adventofcode2024.UnitTests.csproj" \
35+
--disable-logo
36+
}
37+
38+
invoke_create_from_template() {
39+
local template_path="$1"
40+
local day="$2"
41+
local target_path
42+
43+
target_path="$(echo "${template_path}" | sed "s/Day00/Day${day}/")"
44+
45+
if [[ -e "${target_path}" ]]; then
46+
echo "${target_path} already exists."
47+
return
48+
fi
49+
50+
if [[ ! -e "${template_path}" ]]; then
51+
echoerr "${template_path} does not exist."
52+
exit 1
53+
fi
54+
55+
cp "${template_path}" "${target_path}"
56+
sed -i "s/Day00/Day${day}/g" "${target_path}"
57+
echo "Created ${target_path}"
58+
}
59+
60+
invoke_new_day() {
61+
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
62+
echoerr "The day argument must be a positive integer."
63+
exit 1
64+
fi
65+
66+
local day="$(printf "%02d" "$1")"
67+
local input_path="${script_root}/src/adventofcode2024.console/input/Day00.txt"
68+
local day_path="${script_root}/src/adventofcode2024/Day00.cs"
69+
local test_path="${script_root}/tests/adventofcode2024.UnitTests/Day00Tests.cs"
70+
71+
invoke_create_from_template "${input_path}" "${day}"
72+
invoke_create_from_template "${day_path}" "${day}"
73+
invoke_create_from_template "${test_path}" "${day}"
74+
}
75+
76+
command="${1:-}"
77+
shift || true
78+
79+
case "${command}" in
80+
run)
81+
invoke_run
82+
;;
83+
test)
84+
invoke_test
85+
;;
86+
new)
87+
invoke_new_day "$1"
88+
;;
89+
*)
90+
echoerr "Invalid or missing command. See usage below."
91+
usage
92+
exit 1
93+
;;
94+
esac

0 commit comments

Comments
 (0)