Skip to content

Instantly share code, notes, and snippets.

@tobozo
Last active April 28, 2022 09:06
Show Gist options
  • Save tobozo/f13f050c5c131da0e42b859eea810d9e to your computer and use it in GitHub Desktop.
Save tobozo/f13f050c5c131da0e42b859eea810d9e to your computer and use it in GitHub Desktop.
Json Package splitter for Arduino IDE and espressif esp32 SDK
<?php
/*\
Json Package splitter for Arduino IDE and espressif esp32 SDK.
Copyleft tobozo 2022
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This php script allows different package versions to coexist in the
~/.arduino15/packages/ folder and in the Arduino boards menu.
See https://twitter.com/TobozoTagada/status/1490015504051937283
This php script will create one package_esp32_XXX_index.json file limited to only one
version for each platform entry from the official package_esp32_dev_index.json file.
Just pick those you're interested it, give them a URL (create a gist or host them
yourself), then add the URL in the Additional Boards Manager URLs from the preferences
page.
The additional SDKs can then be found and downloaded from the boards manager along with
the official version.
\*/
$main_package_json = file_get_contents("https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json");
if(!is_dir("packages")) {
mkdir("packages");
}
// retrieve the JSON
$package_json = json_decode( $main_package_json );
// get the packages array
$packages_arr = $package_json->packages;
if( empty($packages_arr) || !isset($packages_arr[0]) ) {
die("No package in json file");
}
$packages = $packages_arr[0];
if( !isset( $packages->platforms ) ) {
die("No platform in this package");
}
if( !isset( $packages->name ) ) {
die("One package has no name");
}
$json_platforms = [];
// loop once to memoize platforms
foreach( $packages->platforms as $pos => $platform ) {
if( !isset( $platform->version ) ) {
continue;
}
$name = sprintf("esp32-%s", $platform->version ); // e.g esp32-2.0.1-rc1
$json_platforms[$name] = $platform;
unset($packages->platforms[$pos]);
}
// loop to generate separate package files
foreach( $json_platforms as $name => $platform ) {
$json_file = sprintf("packages/package_esp32_%s_index.json", $name );
echo "Generating $json_file...\n";
// rename some variables to allow various packages coexistence in Arduino IDE
$packages->name = $name;
$platform->name = $name;
foreach( $platform->toolsDependencies as $pos => $tool ) {
$platform->toolsDependencies[$pos]->packager = $name;
}
// reinject in original json (this is lazy)
$packages->platforms[0] = $platform;
$package_json->packages = array($packages);
// try to keep the same formatting as the original, then save
$json_indent_4 = json_encode( $package_json, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
$json_indent_2 = preg_replace('/^( +?)\\1(?=[^ ])/m', '$1', $json_indent_4);
file_put_contents( $json_file, $json_indent_2 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment