Browse Source

refactor: Config setup scripts to support onboard keyboards.

* Use unified config template repo that uses an external
  build matrix YAML file.
* Proper handling for onboard keyboards, including splits, and
 supports for appending the right build matrix once selected.
xmkb
Peter Johanson 3 years ago committed by Pete Johanson
parent
commit
e544d74948
  1. 15
      docs/src/setup-script-generation-plugin/index.js
  2. 94
      docs/src/templates/setup.ps1.mustache
  3. 77
      docs/src/templates/setup.sh.mustache

15
docs/src/setup-script-generation-plugin/index.js

@ -5,6 +5,7 @@
*/ */
var PrebuildPlugin = require("prebuild-webpack-plugin"); var PrebuildPlugin = require("prebuild-webpack-plugin");
const path = require("path");
const fs = require("fs"); const fs = require("fs");
const glob = require("glob"); const glob = require("glob");
const yaml = require("js-yaml"); const yaml = require("js-yaml");
@ -12,9 +13,10 @@ const Mustache = require("mustache");
function generateSetupScripts() { function generateSetupScripts() {
return glob("../app/boards/**/*.zmk.yml", (error, files) => { return glob("../app/boards/**/*.zmk.yml", (error, files) => {
const aggregated = files.flatMap((f) => const aggregated = files.map((f) => ({
yaml.safeLoadAll(fs.readFileSync(f, "utf8")) ...yaml.safeLoadAll(fs.readFileSync(f, "utf8"))[0],
); base_dir: path.basename(path.dirname(f)),
}));
const data = aggregated.reduce( const data = aggregated.reduce(
(agg, item) => { (agg, item) => {
@ -25,7 +27,9 @@ function generateSetupScripts() {
agg.keyboards.push(item); agg.keyboards.push(item);
break; break;
case "board": case "board":
if (!item.features?.includes("keys")) { if (item.features?.includes("keys")) {
agg.keyboards.push(item);
} else {
agg.boards.push(item); agg.boards.push(item);
} }
break; break;
@ -35,6 +39,9 @@ function generateSetupScripts() {
{ keyboards: [], boards: [] } { keyboards: [], boards: [] }
); );
data.keyboards.sort((a, b) => a.name.localeCompare(b.name));
data.boards.sort((a, b) => a.name.localeCompare(b.name));
for (let script_ext of ["sh", "ps1"]) { for (let script_ext of ["sh", "ps1"]) {
const templateBuffer = fs.readFileSync( const templateBuffer = fs.readFileSync(
`src/templates/setup.${script_ext}.mustache`, `src/templates/setup.${script_ext}.mustache`,

94
docs/src/templates/setup.ps1.mustache

@ -84,20 +84,9 @@ if (Test-CommandExists Get-Acl) {
} }
} }
$repo_path = "https://github.com/zmkfirmware/zmk-config-split-template.git" $repo_path = "https://github.com/zmkfirmware/unified-zmk-config-template.git"
$title = "ZMK Config Setup:" $title = "ZMK Config Setup:"
$prompt = "Pick an MCU board"
$options = {{#boards}}"{{{name}}}", {{/boards}} "" | Where-Object { $_ -ne "" }
$boards = {{#boards}}"{{id}}", {{/boards}} "" | Where-Object { $_ -ne "" }
Write-Host "$title"
Write-Host ""
Write-Host "MCU Board Selection:"
$choice = Get-Choice-From-Options -Options $options -Prompt $prompt
$board = $($boards[$choice])
Write-Host "" Write-Host ""
Write-Host "Keyboard Shield Selection:" Write-Host "Keyboard Shield Selection:"
$prompt = "Pick a keyboard" $prompt = "Pick a keyboard"
@ -105,17 +94,41 @@ $prompt = "Pick a keyboard"
# TODO: Add support for "Other" and linking to docs on adding custom shields in user config repos. # TODO: Add support for "Other" and linking to docs on adding custom shields in user config repos.
$options = {{#keyboards}}"{{name}}", {{/keyboards}} "" | Where-Object { $_ -ne "" } $options = {{#keyboards}}"{{name}}", {{/keyboards}} "" | Where-Object { $_ -ne "" }
$names = {{#keyboards}}"{{id}}", {{/keyboards}} "" | Where-Object { $_ -ne "" } $names = {{#keyboards}}"{{id}}", {{/keyboards}} "" | Where-Object { $_ -ne "" }
$types = {{#keyboards}}"{{type}}", {{/keyboards}} "" | Where-Object { $_ -ne "" }
$basedirs = {{#keyboards}}"{{base_dir}}", {{/keyboards}} "" | Where-Object { $_ -ne "" }
$splits = {{#keyboards}}"{{#split}}y{{/split}}{{^split}}n{{/split}}", {{/keyboards}} "" | Where-Object { $_ -ne "" } $splits = {{#keyboards}}"{{#split}}y{{/split}}{{^split}}n{{/split}}", {{/keyboards}} "" | Where-Object { $_ -ne "" }
$arches = {{#keyboards}}"{{arch}}", {{/keyboards}} "REMOVEME" | Where-Object { $_ -ne "REMOVEME" }
$basedirs = {{#keyboards}}"{{base_dir}}", {{/keyboards}} "" | Where-Object { $_ -ne "" }
$sibling_sets = {{#keyboards}}{{#siblings.0}}( {{#siblings}}"{{.}}",{{/siblings}} "" | Where-Object { $_ -ne "" } ){{/siblings.0}}{{^siblings.0}}( "{{id}}" ){{/siblings.0}}, {{/keyboards}} "REMOVEME" | Where-Object { $_ -ne "REMOVEME" }
$choice = Get-Choice-From-Options -Options $options -Prompt $prompt $choice = Get-Choice-From-Options -Options $options -Prompt $prompt
$shield_title = $($options[$choice]) $keyboard_title = $($options[$choice])
$shield = $($names[$choice]) $keyboard = $($names[$choice])
$split = $($splits[$choice]) $basedir = $($basedirs[$choice])
$keyboard_split = $($splits[$choice])
$keyboard_arch = $($arches[$choice])
$keyboard_siblings = $($sibling_sets[$choice])
$keyboard_type = $($types[$choice])
if ($split -eq "n") { if ($keyboard_type -eq "shield") {
$repo_path = "https://github.com/zmkfirmware/zmk-config-template.git" $prompt = "Pick an MCU board"
$options = {{#boards}}"{{{name}}}", {{/boards}} "" | Where-Object { $_ -ne "" }
$boards = {{#boards}}"{{id}}", {{/boards}} "" | Where-Object { $_ -ne "" }
Write-Host "$title"
Write-Host ""
Write-Host "MCU Board Selection:"
$choice = Get-Choice-From-Options -Options $options -Prompt $prompt
$shields = $keyboard_siblings
$board = $($boards[$choice])
$boards = ( $board )
} else {
$boards = ( $keyboard_siblings )
$shields = @( )
} }
$copy_keymap = Read-Host "Copy in the stock keymap for customisation? [Yn]" $copy_keymap = Read-Host "Copy in the stock keymap for customisation? [Yn]"
if ($copy_keymap -eq "" -or $copy_keymap -eq "Y" -or $copy_keymap -eq "y") { if ($copy_keymap -eq "" -or $copy_keymap -eq "Y" -or $copy_keymap -eq "y") {
@ -144,8 +157,12 @@ else {
Write-Host "" Write-Host ""
Write-Host "Preparing a user config for:" Write-Host "Preparing a user config for:"
Write-Host "* MCU Board: ${board}" if ($keyboard_type -eq "shield") {
Write-Host "* Shield: ${shield}" Write-Host "* MCU Board: ${boards}"
Write-Host "* Shield(s): ${shields}"
} else {
Write-Host "* Board(s): ${boards}"
}
if ($copy_keymap -eq "yes") { if ($copy_keymap -eq "yes") {
Write-Host "* Copy Keymap?: Yes" Write-Host "* Copy Keymap?: Yes"
@ -171,24 +188,43 @@ Set-Location "$repo_name"
Push-Location config Push-Location config
Invoke-RestMethod -Uri "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${shield}/${shield}.conf" -OutFile "${shield}.conf" $config_file = "${keyboard}.conf"
$keymap_file = "${keyboard}.keymap"
if ($keyboard_type -eq "shield") {
$config_url = "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${basedir}/${keyboard}.conf"
$keymap_url = "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${basedir}/${keyboard}.keymap"
} else {
$config_url = "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/${keyboard_arch}/${basedir}/${keyboard}.conf"
$keymap_url = "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/${keyboard_arch}/${basedir}/${keyboard}.keymap"
}
Write-Host "Downloading config file (${config_url})"
Try {
Invoke-RestMethod -Uri "${config_url}" -OutFile "${config_file}"
} Catch {
Set-Content -Path $config_file "# Place configuration items here"
}
if ($copy_keymap -eq "yes") { if ($copy_keymap -eq "yes") {
Invoke-RestMethod -Uri "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${shield}/${shield}.keymap" -OutFile "${shield}.keymap" Write-Host "Downloading keymap file (${keymap_url})"
Invoke-RestMethod -Uri "${keymap_url}" -OutFile "${keymap_file}"
} }
Pop-Location Pop-Location
$build_file = (Get-Content .github/workflows/build.yml).replace("BOARD_NAME", $board) Add-Content -Path "build.yaml" -Value "include:"
$build_file = $build_file.replace("SHIELD_NAME", $shield) foreach ($b in ${boards}) {
$build_file = $build_file.replace("KEYBOARD_TITLE", $shield_title) if ($keyboard_type -eq "shield") {
foreach ($s in ${shields}) {
if ($board -eq "proton_c") { Add-Content -Path "build.yaml" -Value " - board: $b"
$build_file = $build_file.replace("uf2", "hex") Add-Content -Path "build.yaml" -Value " shield: $s"
}
} else {
Add-Content -Path "build.yaml" -Value " - board: $b"
}
} }
Set-Content -Path .github/workflows/build.yml -Value $build_file
Remove-Item -Recurse -Force .git Remove-Item -Recurse -Force .git
git init . git init .
git add . git add .

77
docs/src/templates/setup.sh.mustache

@ -51,7 +51,7 @@ if [[ $curl_exists == "true" && $wget_exists == "true" ]]; then
if [[ $force_wget == "true" ]]; then if [[ $force_wget == "true" ]]; then
download_command="wget " download_command="wget "
else else
download_command="curl -O " download_command="curl -fsOL "
fi fi
elif [[ $curl_exists == "true" ]]; then elif [[ $curl_exists == "true" ]]; then
download_command="curl -O " download_command="curl -O "
@ -62,7 +62,7 @@ else
exit 1 exit 1
fi fi
repo_path="https://github.com/zmkfirmware/zmk-config-split-template.git" repo_path="https://github.com/zmkfirmware/unified-zmk-config-template.git"
title="ZMK Config Setup:" title="ZMK Config Setup:"
echo "" echo ""
@ -71,8 +71,17 @@ PS3="Pick a keyboard: "
options=({{#keyboards}}"{{{name}}}" {{/keyboards}}) options=({{#keyboards}}"{{{name}}}" {{/keyboards}})
keyboards_id=({{#keyboards}}"{{id}}" {{/keyboards}}) keyboards_id=({{#keyboards}}"{{id}}" {{/keyboards}})
keyboards_type=({{#keyboards}}"{{type}}" {{/keyboards}}) keyboards_type=({{#keyboards}}"{{type}}" {{/keyboards}})
keyboards_arch=({{#keyboards}}"{{arch}}" {{/keyboards}})
keyboards_basedir=({{#keyboards}}"{{base_dir}}" {{/keyboards}})
keyboards_split=({{#keyboards}}"{{#split}}y{{/split}}{{^split}}n{{/split}}" {{/keyboards}}) keyboards_split=({{#keyboards}}"{{#split}}y{{/split}}{{^split}}n{{/split}}" {{/keyboards}})
keyboards_shield=({{#keyboards}}"{{#compatible}}y{{/compatible}}{{^compatible}}n{{/compatible}}" {{/keyboards}}) keyboards_shield=({{#keyboards}}"{{#compatible}}y{{/compatible}}{{^compatible}}n{{/compatible}}" {{/keyboards}})
{{#keyboards}}
{{#siblings.0}}
{{id}}_siblings=({{#siblings}}"{{.}}" {{/siblings}})
{{/siblings.0}}
{{/keyboards}}
select opt in "${options[@]}" "Quit"; do select opt in "${options[@]}" "Quit"; do
case "$REPLY" in case "$REPLY" in
''|*[!0-9]*) echo "Invalid option. Try another one."; continue;; ''|*[!0-9]*) echo "Invalid option. Try another one."; continue;;
@ -85,9 +94,17 @@ select opt in "${options[@]}" "Quit"; do
fi fi
keyboard_index=$(( $REPLY-1 )) keyboard_index=$(( $REPLY-1 ))
keyboard=${keyboards_id[$keyboard_index]} keyboard=${keyboards_id[$keyboard_index]}
keyboard_arch=${keyboards_arch[$keyboard_index]}
keyboard_basedir=${keyboards_basedir[$keyboard_index]}
keyboard_title=${options[$keyboard_index]} keyboard_title=${options[$keyboard_index]}
keyboard_sibling_var=${keyboard}_siblings[@]
keyboard_sibling_first=${keyboard}_siblings[0]
if [ -n "${!keyboard_sibling_first}" ]; then
keyboard_siblings=${!keyboard_sibling_var}
else
keyboard_siblings=( "${keyboard}" )
fi
split=${keyboards_split[$keyboard_index]} split=${keyboards_split[$keyboard_index]}
type=${keyboards_type[$keyboard_index]}
keyboard_shield=${keyboards_shield[$keyboard_index]} keyboard_shield=${keyboards_shield[$keyboard_index]}
break break
;; ;;
@ -96,6 +113,7 @@ select opt in "${options[@]}" "Quit"; do
done done
if [ "$keyboard_shield" == "y" ]; then if [ "$keyboard_shield" == "y" ]; then
shields=$keyboard_siblings
shield=${keyboard} shield=${keyboard}
shield_title=${keyboard_title} shield_title=${keyboard_title}
@ -119,6 +137,7 @@ if [ "$keyboard_shield" == "y" ]; then
board_index=$(( $REPLY-1 )) board_index=$(( $REPLY-1 ))
board=${board_ids[$board_index]} board=${board_ids[$board_index]}
board_title=${options[$board_index]} board_title=${options[$board_index]}
boards=( "${board}" )
break break
;; ;;
@ -126,12 +145,7 @@ if [ "$keyboard_shield" == "y" ]; then
done done
else else
board=${keyboard} board=${keyboard}
echo "Support for onboard microcontroller keyboards is still a work in progress." boards=$keyboard_siblings
exit 1
fi
if [ "$split" == "n" ]; then
repo_path="https://github.com/zmkfirmware/zmk-config-template.git"
fi fi
read -r -e -p "Copy in the stock keymap for customization? [Yn]: " copy_keymap read -r -e -p "Copy in the stock keymap for customization? [Yn]: " copy_keymap
@ -152,8 +166,12 @@ fi
echo "" echo ""
echo "Preparing a user config for:" echo "Preparing a user config for:"
echo "* MCU Board: ${board}" if [ "$keyboard_shield" == "y" ]; then
echo "* Shield: ${shield}" echo "* MCU Board: ${boards}"
echo "* Shield(s): ${shields}"
else
echo "* Board(s): ${boards}"
fi
if [ "$copy_keymap" == "yes" ]; then if [ "$copy_keymap" == "yes" ]; then
echo "* Copy Keymap?: ✓" echo "* Copy Keymap?: ✓"
@ -178,26 +196,37 @@ cd ${repo_name}
pushd config pushd config
$download_command "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${shield}/${shield}.conf" if [ "$keyboard_shield" == "y" ]; then
config_file="https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${keyboard_basedir}/${shield}.conf"
keymap_file="https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${keyboard_basedir}/${shield}.keymap"
else
config_file="https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/${keyboard_arch}/${keyboard_basedir}/${board}.conf"
keymap_file="https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/${keyboard_arch}/${keyboard_basedir}/${board}.keymap"
fi
echo "Downloading config file (${config_file})"
$download_command "${config_file}" || echo "# Put configuration options here" > "${keyboard}.conf"
if [ "$copy_keymap" == "yes" ]; then if [ "$copy_keymap" == "yes" ]; then
$download_command "https://raw.githubusercontent.com/zmkfirmware/zmk/main/app/boards/shields/${shield}/${shield}.keymap" echo "Downloading keymap file (${keymap_file})"
$download_command "${keymap_file}"
fi fi
popd popd
sed -i'.orig' \ echo "include:" >> build.yaml
-e "s/BOARD_NAME/$board/" \
-e "s/SHIELD_NAME/$shield/" \
-e "s|KEYBOARD_TITLE|$shield_title|" \
.github/workflows/build.yml
if [ "$board" == "proton_c" ]; then for b in ${boards}; do
# Proton-C board still fa if [ -n "${shields}" ];
sed -i'.orig' -e "s/uf2/hex/g" .github/workflows/build.yml then
fi for s in ${shields}; do
echo " - board: ${b}" >> build.yaml
rm .github/workflows/*.yml.orig echo " shield: ${s}" >> build.yaml
done
else
echo " - board: ${b}" >> build.yaml
fi
done
rm -rf .git rm -rf .git
git init . git init .

Loading…
Cancel
Save