Skip to content

Commit

Permalink
chore: add local preview make target
Browse files Browse the repository at this point in the history
  • Loading branch information
kilianc committed May 20, 2024
1 parent 73fb38b commit 9eb4409
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
*.out
cover.txt
cover-test
cover.html
cover.txt
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
LOCAL_COVER_DIR := cover-test

.PHONY: release
release:
git tag -d v1
git tag v1 HEAD
git push -f origin v1

$(LOCAL_COVER_DIR)/revisions:
@mkdir -p "$(LOCAL_COVER_DIR)/revisions"

$(LOCAL_COVER_DIR)/revisions/local.html: $(LOCAL_COVER_DIR)/revisions
@cd go-test-app-01; make test
@cd go-test-app-01; go tool cover -html=cover.out -o "../$(LOCAL_COVER_DIR)/revisions/local.html"
@cp $(LOCAL_COVER_DIR)/revisions/local.html $(LOCAL_COVER_DIR)/revisions/local-inc.html
@for file in assets/*; do ln -s "$$PWD/$$file" "$(LOCAL_COVER_DIR)/$(shell basename "$$file")"; done
@cd $(LOCAL_COVER_DIR); REVISION=local ../scripts/beautify-html.sh "$$html_file"

preview: clean $(LOCAL_COVER_DIR)/revisions/local.html
@echo ""
@echo preview live at: http://localhost:8000?hash=local
@echo " ctrl+c to stop"
@echo ""
@cd $(LOCAL_COVER_DIR); python3 -m http.server 8000 > /dev/null

clean:
@rm -rf "$(LOCAL_COVER_DIR)"
@cd go-test-app-01; make clean
4 changes: 2 additions & 2 deletions assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ function main() {

configureCodeBlocks()
configureSyntaxHighlight('pre .code .editor')
addCoverageSpans('pre .coverage span')
addCoverageSpans('pre .coverage .editor')
addLineNumbers()

// setup complete, restore the page visibility
document.documentElement.style.setProperty('opacity', '1')
}

function addCoverageSpans(cssSelector) {
let spans = Array.from(document.querySelectorAll(cssSelector))
let spans = Array.from(document.querySelectorAll(`${cssSelector} span`))

spans.forEach((span) => {
let html = span.innerHTML
Expand Down
24 changes: 24 additions & 0 deletions scripts/beautify-html.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

if [ "${REVISION}" = "local" ]; then
set -eo pipefail
else
set -xeo pipefail
fi

if [ -z "${REVISION}" ]; then
echo "REVISION is not set"
exit 1
fi

# this is useful for browser caching
hash=$(cat index.css index.js | md5sum | awk '{print $1}')

for file in "revisions/${REVISION}.html" "revisions/${REVISION}-inc.html"; do
ex -sc '%s/\n\t\t<style>\_.\{-}<\/style>//' -c 'x' "${file}"
ex -sc '%s/\n\t<script>\_.\{-}<\/script>//' -c 'x' "${file}"
ex -sc '%s/<title>/<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" \/>\r\t\t<title>/' -c 'x' "${file}"
ex -sc '%s/<title>/<meta http-equiv="Pragma" content="no-cache" \/>\r\t\t<title>/' -c 'x' "${file}"
ex -sc '%s/<title>/<meta http-equiv="Expires" content="0" \/>\r\t\t<title>/' -c 'x' "${file}"
ex -sc '%s/<\/title>/<\/title>\r\t\t<script src="..\/index.js?'"${hash}"'"><\/script>/' -c 'x' "${file}"
done
20 changes: 4 additions & 16 deletions scripts/push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,20 @@ cp cover.out "${cover_dir}/revisions/${REVISION}.out"

echo "mode: set" > incremental.out
# grep exits with 1 if no lines are found, so we need to ignore that
# incremental = cover - head
grep -F -v -x -f "${cover_dir}/head/head.out" cover.out >> incremental.out || true
go tool cover -html=incremental.out -o "${cover_dir}/revisions/${REVISION}-inc.html"
go tool cover -func=incremental.out -o "${cover_dir}/revisions/${REVISION}-inc.txt"
cp incremental.out "${cover_dir}/revisions/${REVISION}-inc.out"

cd "${cover_dir}"

# copy assets
# prepare assets

cp "${GITHUB_ACTION_PATH}"/assets/* .
./"${GITHUB_ACTION_PATH}"/scripts/beautify-html.sh

# beautify html

# this is useful for browser caching
hash=$(cat index.css index.js | md5sum | awk '{print $1}')

for file in "revisions/${REVISION}.html" "revisions/${REVISION}-inc.html"; do
ex -sc '%s/\n\t\t<style>\_.\{-}<\/style>//' -c 'x' "${file}"
ex -sc '%s/\n\t<script>\_.\{-}<\/script>//' -c 'x' "${file}"
ex -sc '%s/<title>/<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" \/>\r\t\t<title>/' -c 'x' "${file}"
ex -sc '%s/<title>/<meta http-equiv="Pragma" content="no-cache" \/>\r\t\t<title>/' -c 'x' "${file}"
ex -sc '%s/<title>/<meta http-equiv="Expires" content="0" \/>\r\t\t<title>/' -c 'x' "${file}"
ex -sc '%s/<\/title>/<\/title>\r\t\t<script src="..\/index.js?'"${hash}"'"><\/script>/' -c 'x' "${file}"
done

# if we are on the main branch, copy files to main.*
# if we are on the main branch, copy files to head.*

if [ "${REF_NAME}" = "main" ]; then
cp "revisions/${REVISION}.html" "${cover_dir}/head/head.html"
Expand Down

0 comments on commit 9eb4409

Please sign in to comment.