Skip to content

Commit

Permalink
Version compiled FW with hash
Browse files Browse the repository at this point in the history
Auto generates a macro to type out version info, local and github compilation now includes commit hash and timestamp in final uf2 files
  • Loading branch information
ReFil committed Nov 15, 2023
1 parent 44a42af commit 99436c3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Get version data
id: get_info
run: |
timestamp=$(date +"%Y%m%d%H%M")
commit=$(echo "${{ github.sha }}" | cut -c1-7)
file_prefix=$timestamp-$commit
branch_name=$(echo "${{ github.ref }}" | awk -F'/' '{print $3}' | cut -c1-4)
echo "::set-output name=file_prefix::$file_prefix"
bin/get_version.sh $branch_name $commit
- name: Cache west modules
uses: actions/cache@v3
env:
Expand Down Expand Up @@ -42,11 +51,11 @@ jobs:
- name: Adv360 Right Kconfig file
run: grep -vE '(^#|^$)' build/right/zephyr/.config
- name: Rename zmk.uf2
run: cp build/left/zephyr/zmk.uf2 left.uf2 && cp build/right/zephyr/zmk.uf2 right.uf2
run: cp build/left/zephyr/zmk.uf2 ${{ steps.get_info.outputs.file_prefix }}-left.uf2 && cp build/right/zephyr/zmk.uf2 ${{ steps.get_info.outputs.file_prefix }}-right.uf2
- name: Archive (Adv360)
uses: actions/upload-artifact@v3
with:
name: firmware
path: |
left.uf2
right.uf2
${{ steps.get_info.outputs.file_prefix }}-left.uf2
${{ steps.get_info.outputs.file_prefix }}-right.uf2
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DOCKER := $(shell { command -v podman || command -v docker; })
TIMESTAMP := $(shell date -u +"%Y%m%d%H%M%S")
TIMESTAMP := $(shell date -u +"%Y%m%d%H%M")
COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null)
detected_OS := $(shell uname) # Classify UNIX OS
ifeq ($(strip $(detected_OS)),Darwin) #We only care if it's OS X
SELINUX1 :=
Expand All @@ -12,11 +13,13 @@ endif
.PHONY: all clean

all:
$(shell bin/get_version.sh)
$(DOCKER) build --tag zmk --file Dockerfile .
$(DOCKER) run --rm -it --name zmk \
-v $(PWD)/firmware:/app/firmware$(SELINUX1) \
-v $(PWD)/config:/app/config:ro$(SELINUX2) \
-e TIMESTAMP=$(TIMESTAMP) \
-e COMMIT=$(COMMIT) \
zmk

clean:
Expand Down
5 changes: 3 additions & 2 deletions bin/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
set -eu

PWD=$(pwd)
TIMESTAMP="${TIMESTAMP:-$(date -u +"%Y%m%d%H%M%S")}"
TIMESTAMP="${TIMESTAMP:-$(date -u +"%Y%m%d%H%M")}"
COMMIT="${COMMIT:-$(echo xxxxxx)}"

# West Build (left)
west build -s zmk/app -d build/left -b adv360_left -- -DZMK_CONFIG="${PWD}/config"
Expand All @@ -14,4 +15,4 @@ west build -s zmk/app -d build/right -b adv360_right -- -DZMK_CONFIG="${PWD}/con
# Adv360 Right Kconfig file
grep -vE '(^#|^$)' build/right/zephyr/.config
# Rename zmk.uf2
cp build/left/zephyr/zmk.uf2 "./firmware/${TIMESTAMP}-left.uf2" && cp build/right/zephyr/zmk.uf2 "./firmware/${TIMESTAMP}-right.uf2"
cp build/left/zephyr/zmk.uf2 "./firmware/${TIMESTAMP}-${COMMIT}-left.uf2" && cp build/right/zephyr/zmk.uf2 "./firmware/${TIMESTAMP}-${COMMIT}-right.uf2"
57 changes: 57 additions & 0 deletions bin/get_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# Get the date, first 4 chars of branch name and short commit hash
date=$(date -u +"%Y%m%d")
branch=${1:-(git rev-parse --abbrev-ref HEAD | cut -c1-4)}
commit=${2:-(git rev-parse --short HEAD)}

# Function to transform characters to ZMK key behaviours
transform_char() {
local char=$1
if [[ $char =~ [A-Za-z] ]]; then
echo "<&kp ${char^^}>, "
elif [[ $char =~ [0-9] ]]; then
echo "<&kp N${char}>, "
elif [ "$char" = "." ]; then
echo "<&kp DOT>, "
fi
}

# Iterate over the date and format characters
formatted_date=""
for ((i = 0; i < ${#date}; i++)); do
formatted_date+=$(transform_char "${date:$i:1}")
done

# Insert separator between date and branch
formatted_date+="<&kp MINUS>, "

# Iterate over the branch and format characters
formatted_branch=""
for ((i = 0; i < ${#branch}; i++)); do
formatted_branch+=$(transform_char "${branch:$i:1}")
done

# Insert separator between branch and commit hash
formatted_branch+="<&kp MINUS>, "

# Iterate over the commit hash and format characters
formatted_commit=""
for ((i = 0; i < ${#commit}; i++)); do
formatted_commit+=$(transform_char "${commit:$i:1}")
done

# Combine the formatted string, add trailing carriage return
formatted_result="$formatted_date$formatted_branch$formatted_commit"
formatted_result+="<&kp RET>"

echo $formatted_result
# Create new macro to define version, overwrite previous one

echo '#define VERSION_MACRO' > "config/version.dtsi"
echo 'macro_ver: macro_ver {' >> "config/version.dtsi"
echo 'compatible = "zmk,behavior-macro";' >> "config/version.dtsi"
echo 'label = "macro_ver";' >> "config/version.dtsi"
echo '#binding-cells = <0>;' >> "config/version.dtsi"
echo "bindings = $formatted_result;" >> "config/version.dtsi"
echo '};' >> "config/version.dtsi"
13 changes: 11 additions & 2 deletions config/adv360.keymap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
/ {
behaviors {
#include "macros.dtsi"
#include "version.dtsi"
#ifndef VERSION_MACRO
macro_ver: macro_ver {
compatible = "zmk,behavior-macro";
label = "macro_version";
#binding-cells = <0>;
bindings = <&kp RET>;
};
#endif

hm: homerow_mods {
compatible = "zmk,behavior-hold-tap";
Expand All @@ -27,7 +36,7 @@
&kp EQUAL &kp N1 &kp N2 &kp N3 &kp N4 &kp N5 &tog 1 &mo 3 &kp N6 &kp N7 &kp N8 &kp N9 &kp N0 &kp MINUS
&kp TAB &kp Q &kp W &kp E &kp R &kp T &none &none &kp Y &kp U &kp I &kp O &kp P &kp BSLH
&kp ESC &kp A &kp S &kp D &kp F &kp G &none &kp LCTRL &kp LALT &kp LGUI &kp RCTRL &none &kp H &kp J &kp K &kp L &kp SEMI &kp SQT
&kp LSHFT &kp Z &kp X &kp C &kp V &kp B &kp HOME &kp PG_UP &kp N &kp M &kp COMMA &kp DOT &kp FSLH &kp RSHFT
&kp LSHFT &kp Z &kp X &kp C &kp V &kp B &kp HOME &macro_ver &kp N &kp M &kp COMMA &kp DOT &kp FSLH &kp RSHFT
&mo 2 &kp GRAVE &kp CAPS &kp LEFT &kp RIGHT &kp BSPC &kp DEL &kp END &kp PG_DN &kp ENTER &kp SPACE &kp UP &kp DOWN &kp LBKT &kp RBKT &mo 2
>;
};
Expand All @@ -54,7 +63,7 @@
&none &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &bt BT_SEL 3 &bt BT_SEL 4 &none &trans &none &none &none &none &none &none
&none &none &none &none &none &none &bootloader &bootloader &none &none &none &none &none &none
&none &none &none &none &none &none &none &none &none &bt BT_CLR &none &rgb_ug RGB_MEFS_CMD 5 &none &none &none &none &none &none
&none &none &none &none &none &none &none &none &none &none &none &none &none &none
&none &none &none &none &macro_ver &none &none &none &none &none &none &none &none &none
&none &none &none &none &none &none &none &none &none &bl BL_TOG &rgb_ug RGB_TOG &bl BL_INC &bl BL_DEC &none &none &none
>;
};
Expand Down
Empty file added config/version.dtsi
Empty file.

0 comments on commit 99436c3

Please sign in to comment.