generated from nighthawkcoders/portfolio_2025
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Makefile
95 lines (82 loc) · 3.47 KB
/
Makefile
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
# Configuration, override port with usage: make PORT=4200
PORT ?= 4100
REPO_NAME ?= student_2025
LOG_FILE = /tmp/jekyll$(PORT).log
SHELL = /bin/bash -c
.SHELLFLAGS = -e # Exceptions will stop make, works on MacOS
# Phony Targets, makefile housekeeping for below definitions
.PHONY: default server issues convert clean stop
# List all .ipynb files in the _notebooks directory
NOTEBOOK_FILES := $(shell find _notebooks -name '*.ipynb')
# Specify the target directory for the converted Markdown files
DESTINATION_DIRECTORY = _posts
MARKDOWN_FILES := $(patsubst _notebooks/%.ipynb,$(DESTINATION_DIRECTORY)/%_IPYNB_2_.md,$(NOTEBOOK_FILES))
# Call server, then verify and start logging
# ...
# Call server, then verify and start logging
default: server
@echo "Terminal logging starting, watching server..."
@# tail and awk work together to extract Jekyll regeneration messages
@# When a _notebook is detected in the log, call make convert in the background
@# Note: We use the "if ($$0 ~ /_notebooks\/.*\.ipynb/) { system(\"make convert &\") }" to call make convert
@(tail -f $(LOG_FILE) | awk '/Server address: http:\/\/127.0.0.1:$(PORT)\/$(REPO_NAME)\// { serverReady=1 } \
serverReady && /^ *Regenerating:/ { regenerate=1 } \
regenerate { \
if (/^[[:blank:]]*$$/) { regenerate=0 } \
else { \
print; \
if ($$0 ~ /_notebooks\/.*\.ipynb/) { system("make convert &") } \
} \
}') 2>/dev/null &
@# start an infinite loop with timeout to check log status
@for ((COUNTER = 0; ; COUNTER++)); do \
if grep -q "Server address:" $(LOG_FILE); then \
echo "Server started in $$COUNTER seconds"; \
break; \
fi; \
if [ $$COUNTER -eq 60 ]; then \
echo "Server timed out after $$COUNTER seconds."; \
echo "Review errors from $(LOG_FILE)."; \
cat $(LOG_FILE); \
exit 1; \
fi; \
sleep 1; \
done
@# outputs startup log, removes last line ($$d) as ctl-c message is not applicable for background process
@sed '$$d' $(LOG_FILE)
# Start the local web server
server: stop convert
@echo "Starting server..."
@@nohup bundle exec jekyll serve -H 127.0.0.1 -P $(PORT) > $(LOG_FILE) 2>&1 & \
PID=$$!; \
echo "Server PID: $$PID"
@@until [ -f $(LOG_FILE) ]; do sleep 1; done
# Convert .ipynb files to Markdown with front matter
convert: $(MARKDOWN_FILES)
# Convert .ipynb files to Markdown with front matter, preserving directory structure
$(DESTINATION_DIRECTORY)/%_IPYNB_2_.md: _notebooks/%.ipynb
@echo "Converting source $< to destination $@"
@mkdir -p $(@D)
@python -c 'import sys; from scripts.convert_notebooks import convert_single_notebook; convert_single_notebook(sys.argv[1])' "$<"
# Clean up project derived files, to avoid run issues stop is dependency
clean: stop
@echo "Cleaning converted IPYNB files..."
@find _posts -type f -name '*_IPYNB_2_.md' -exec rm {} +
@echo "Cleaning Github Issue files..."
@find _posts -type f -name '*_GithubIssue_.md' -exec rm {} +
@echo "Removing empty directories in _posts..."
@while [ $$(find _posts -type d -empty | wc -l) -gt 0 ]; do \
find _posts -type d -empty -exec rmdir {} +; \
done
@echo "Removing _site directory..."
@rm -rf _site
# Stop the server and kill processes
stop:
@echo "Stopping server..."
@# kills process running on port $(PORT)
@@lsof -ti :$(PORT) | xargs kill >/dev/null 2>&1 || true
@echo "Stopping logging process..."
@# kills previously running logging processes
@@ps aux | awk -v log_file=$(LOG_FILE) '$$0 ~ "tail -f " log_file { print $$2 }' | xargs kill >/dev/null 2>&1 || true
@# removes log
@rm -f $(LOG_FILE)