Verified Commit 71df457b authored by doshitan's avatar doshitan

Improve metadata generation script and prep for writing metadata to content files

Depending on git info to generate the modified date is kinda convenient
in that I don't have to maintain the date manually, but can be
wrong (e.g., do something in git that touches the file, like moving it,
but doesn't meaningfully change it's content) and means there's a
dynamic element to building the site (generating the site depends on
information outside of the source files), which complicates CI.

So I want to move the modified date into the YAML front matter of the
content itself. Picking up the "published" date as well for good
measure. It's not currently used by pages and posts get it from the file
name, but may be utilized in the future and enables specifying the exact
time I consider a post published (if I want).
parent dfbc4721
......@@ -52,13 +52,20 @@ elm2nix-gen: $(CONTENT_DIR)/assets/elm/elm.json
infra-deploy-lambda:
cd infra && terraform apply -target=aws_lambda_function.cloudfront -target=aws_cloudfront_distribution.site_distribution
metadata_dirs = $(CONTENT_DIR)/{posts,pages,projects}
metadata_script_run = git ls-files -z $(metadata_dirs) | parallel -0 --
metadata-gen: SHELL:=bash
metadata-gen:
git ls-files -z $(CONTENT_DIR)/{posts,pages,projects}/ | parallel -0 -- ./scripts/metadata-gen.sh {}
$(metadata_script_run) ./scripts/metadata-gen.sh {}
metadata-clean: SHELL:=bash
metadata-clean:
rm $(CONTENT_DIR)/{posts,pages,projects}/*.metadata
rm $(metadata_dirs)/*.metadata
metadata-merge: SHELL:=bash
metadata-merge:
$(metadata_script_run) ./scripts/metadata-merge.sh {}
node-gen:
cd nix/node && nix run -f '<nixpkgs>' nodePackages.node2nix -c node2nix --nodejs-10 -i node-packages.json
......
#!/usr/bin/env bash
git log -1 --format="%aI" "$1" | cat <(echo -n "modified: ") - > "$1".metadata
raw_path="$1"
contentless_path=${raw_path#content/}
date_in_name=$(echo "$raw_path" | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}')
get_commit_time() {
git log -1 --format="%aI" "$1"
}
get_first_commit() {
git log --reverse --pretty=format:%H --follow -- "$1" | head -1
}
get_first_commit_time() {
local first_commit_sha
first_commit_sha=$(get_first_commit "$1")
if [ -n "$first_commit_sha" ]; then
get_commit_time "$first_commit_sha"
fi
}
# this commit just rearranged the project, didn't actually make changes to
# content, which is what really matters here
move_sha=c070cb8daf2dbc1c483368937923a2c0f4318a1b
get_latest_meaningful_commit() {
local sha
sha=$(git log -1 --pretty=format:%H --follow -- "$1")
if [ "$sha" == "$move_sha" ]; then
git log -1 --skip=1 --pretty=format:%H -- "${1#content/}"
else
echo "$sha"
fi
}
possible_dates=($(get_first_commit_time "$raw_path") $(get_first_commit_time "$contentless_path") $date_in_name)
earliest_date=$(printf '%s\0' "${possible_dates[@]}" | sort -z | head -z -n 1 | tr -d "\0")
latest_meaningful_date=$(get_commit_time "$(get_latest_meaningful_commit "$raw_path")")
cat <<EOF > "$raw_path.metadata"
published: $earliest_date
modified: $latest_meaningful_date
EOF
#!/usr/bin/env bash
meta=$(cat "$1.metadata")
# super hacky, but just append metadata file content to the end of the YAML
# front matter of file
perl -i -p0e "s/^(---.*)---\$/\\1${meta}\\n---/ms" "$1"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment