From e544d74948097864a9e0d846694ea16e5a443f22 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Tue, 12 Oct 2021 05:31:56 +0000 Subject: [PATCH] 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. --- .../setup-script-generation-plugin/index.js | 15 ++- docs/src/templates/setup.ps1.mustache | 94 +++++++++++++------ docs/src/templates/setup.sh.mustache | 77 ++++++++++----- 3 files changed, 129 insertions(+), 57 deletions(-) diff --git a/docs/src/setup-script-generation-plugin/index.js b/docs/src/setup-script-generation-plugin/index.js index e97a4d0f..0d768d74 100644 --- a/docs/src/setup-script-generation-plugin/index.js +++ b/docs/src/setup-script-generation-plugin/index.js @@ -5,6 +5,7 @@ */ var PrebuildPlugin = require("prebuild-webpack-plugin"); +const path = require("path"); const fs = require("fs"); const glob = require("glob"); const yaml = require("js-yaml"); @@ -12,9 +13,10 @@ const Mustache = require("mustache"); function generateSetupScripts() { return glob("../app/boards/**/*.zmk.yml", (error, files) => { - const aggregated = files.flatMap((f) => - yaml.safeLoadAll(fs.readFileSync(f, "utf8")) - ); + const aggregated = files.map((f) => ({ + ...yaml.safeLoadAll(fs.readFileSync(f, "utf8"))[0], + base_dir: path.basename(path.dirname(f)), + })); const data = aggregated.reduce( (agg, item) => { @@ -25,7 +27,9 @@ function generateSetupScripts() { agg.keyboards.push(item); break; case "board": - if (!item.features?.includes("keys")) { + if (item.features?.includes("keys")) { + agg.keyboards.push(item); + } else { agg.boards.push(item); } break; @@ -35,6 +39,9 @@ function generateSetupScripts() { { 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"]) { const templateBuffer = fs.readFileSync( `src/templates/setup.${script_ext}.mustache`, diff --git a/docs/src/templates/setup.ps1.mustache b/docs/src/templates/setup.ps1.mustache index ade8d134..d4b92ff4 100644 --- a/docs/src/templates/setup.ps1.mustache +++ b/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:" -$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 "Keyboard Shield Selection:" $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. $options = {{#keyboards}}"{{name}}", {{/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 "" } +$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 -$shield_title = $($options[$choice]) -$shield = $($names[$choice]) -$split = $($splits[$choice]) +$keyboard_title = $($options[$choice]) +$keyboard = $($names[$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") { - $repo_path = "https://github.com/zmkfirmware/zmk-config-template.git" +if ($keyboard_type -eq "shield") { + $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]" if ($copy_keymap -eq "" -or $copy_keymap -eq "Y" -or $copy_keymap -eq "y") { @@ -144,8 +157,12 @@ else { Write-Host "" Write-Host "Preparing a user config for:" -Write-Host "* MCU Board: ${board}" -Write-Host "* Shield: ${shield}" +if ($keyboard_type -eq "shield") { + Write-Host "* MCU Board: ${boards}" + Write-Host "* Shield(s): ${shields}" +} else { + Write-Host "* Board(s): ${boards}" +} if ($copy_keymap -eq "yes") { Write-Host "* Copy Keymap?: Yes" @@ -171,24 +188,43 @@ Set-Location "$repo_name" 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") { - 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 -$build_file = (Get-Content .github/workflows/build.yml).replace("BOARD_NAME", $board) -$build_file = $build_file.replace("SHIELD_NAME", $shield) -$build_file = $build_file.replace("KEYBOARD_TITLE", $shield_title) - -if ($board -eq "proton_c") { - $build_file = $build_file.replace("uf2", "hex") +Add-Content -Path "build.yaml" -Value "include:" +foreach ($b in ${boards}) { + if ($keyboard_type -eq "shield") { + foreach ($s in ${shields}) { + Add-Content -Path "build.yaml" -Value " - board: $b" + 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 git init . git add . diff --git a/docs/src/templates/setup.sh.mustache b/docs/src/templates/setup.sh.mustache index 9dbdcfd6..03df993c 100644 --- a/docs/src/templates/setup.sh.mustache +++ b/docs/src/templates/setup.sh.mustache @@ -51,7 +51,7 @@ if [[ $curl_exists == "true" && $wget_exists == "true" ]]; then if [[ $force_wget == "true" ]]; then download_command="wget " else - download_command="curl -O " + download_command="curl -fsOL " fi elif [[ $curl_exists == "true" ]]; then download_command="curl -O " @@ -62,7 +62,7 @@ else exit 1 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:" echo "" @@ -71,8 +71,17 @@ PS3="Pick a keyboard: " options=({{#keyboards}}"{{{name}}}" {{/keyboards}}) keyboards_id=({{#keyboards}}"{{id}}" {{/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_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 case "$REPLY" in ''|*[!0-9]*) echo "Invalid option. Try another one."; continue;; @@ -85,9 +94,17 @@ select opt in "${options[@]}" "Quit"; do fi keyboard_index=$(( $REPLY-1 )) keyboard=${keyboards_id[$keyboard_index]} + keyboard_arch=${keyboards_arch[$keyboard_index]} + keyboard_basedir=${keyboards_basedir[$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]} - type=${keyboards_type[$keyboard_index]} keyboard_shield=${keyboards_shield[$keyboard_index]} break ;; @@ -96,6 +113,7 @@ select opt in "${options[@]}" "Quit"; do done if [ "$keyboard_shield" == "y" ]; then + shields=$keyboard_siblings shield=${keyboard} shield_title=${keyboard_title} @@ -119,6 +137,7 @@ if [ "$keyboard_shield" == "y" ]; then board_index=$(( $REPLY-1 )) board=${board_ids[$board_index]} board_title=${options[$board_index]} + boards=( "${board}" ) break ;; @@ -126,12 +145,7 @@ if [ "$keyboard_shield" == "y" ]; then done else board=${keyboard} - echo "Support for onboard microcontroller keyboards is still a work in progress." - exit 1 -fi - -if [ "$split" == "n" ]; then - repo_path="https://github.com/zmkfirmware/zmk-config-template.git" + boards=$keyboard_siblings fi read -r -e -p "Copy in the stock keymap for customization? [Yn]: " copy_keymap @@ -152,8 +166,12 @@ fi echo "" echo "Preparing a user config for:" -echo "* MCU Board: ${board}" -echo "* Shield: ${shield}" +if [ "$keyboard_shield" == "y" ]; then + echo "* MCU Board: ${boards}" + echo "* Shield(s): ${shields}" +else + echo "* Board(s): ${boards}" +fi if [ "$copy_keymap" == "yes" ]; then echo "* Copy Keymap?: ✓" @@ -178,26 +196,37 @@ cd ${repo_name} 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 - $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 popd -sed -i'.orig' \ - -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 - # Proton-C board still fa - sed -i'.orig' -e "s/uf2/hex/g" .github/workflows/build.yml -fi +echo "include:" >> build.yaml -rm .github/workflows/*.yml.orig +for b in ${boards}; do + if [ -n "${shields}" ]; + then + for s in ${shields}; do + echo " - board: ${b}" >> build.yaml + echo " shield: ${s}" >> build.yaml + done + else + echo " - board: ${b}" >> build.yaml + fi +done rm -rf .git git init .