[SOLVED] Build tools #5
Replies: 2 comments
-
EDIT: A new python script has been added that does the job of compressing the html and outputting the C++ array for direct pasting into the source code. Hi, For the website code in PrettyOTA.cpp: This is the code for the tool. It uses the clip library to access the clipboard. //
// main.cpp
// WebsiteCompressor
//
// Created by Marc Schöndorf on 23.02.2025.
// License: Do what you want with it
//
#include <iostream>
#include <fstream>
#include <filesystem>
#include "clip.h"
#define COMPRESS
#define COPY_OUTPUT_TO_CLIPBOARD
//#define WRITE_OUTPUT_TO_FILE
int main(int argc, const char* argv[])
{
const std::string outputFilename = "array_out.txt";
const std::string inputFilename = argv[1];
// Determine array name
std::string arrayName;
if(inputFilename == "login.html" || inputFilename == "login_minify.html")
arrayName = "PRETTY_OTA_LOGIN_DATA";
else
arrayName = "PRETTY_OTA_WEBSITE_DATA";
std::cout << "Using file: " << inputFilename << std::endl;
#ifdef COMPRESS
const std::string gzFilename = inputFilename + ".gz";
// Compress input file (gzip)
std::cout << "Compressing file..." << std::endl;
system(("gzip -kf \"" + inputFilename + "\"").c_str());
// Get compressed file size
const uint64_t fileSize = std::filesystem::file_size(gzFilename);
// Read compressed file into vector
std::ifstream fileIn(gzFilename, std::ios::binary);
std::vector<uint8_t> compressedData(fileSize);
fileIn.read(reinterpret_cast<char*>(compressedData.data()), fileSize);
fileIn.close();
// Delete compressed file
system(("rm -f " + gzFilename).c_str());
// Assemble array code
std::string arrayCode = "const uint8_t PrettyOTA::" + arrayName + "[" + std::to_string(fileSize) + "] = {\n ";
for(uint64_t i = 0; i < compressedData.size(); i++)
{
arrayCode += std::to_string(static_cast<unsigned int>(compressedData[i]));
if(i != compressedData.size() - 1) // Only write comma when not the last integer
arrayCode += ", ";
if(i % 40 == 0 && i > 0)
arrayCode += "\n ";
}
arrayCode += "\n};";
#else
const uint64_t fileSize = std::filesystem::file_size(inputFilename);
std::ifstream fileIn(inputFilename, std::ios::binary);
std::vector<uint8_t> data(fileSize);
fileIn.read(reinterpret_cast<char*>(data.data()), fileSize);
fileIn.close();
// Assemble array code
std::string arrayCode = "const uint8_t PrettyOTA::PRETTY_OTA_WEBSITE_DATA[" + std::to_string(fileSize) + "] = {\n ";
for(uint64_t i = 0; i < data.size(); i++)
{
arrayCode += std::to_string(static_cast<unsigned int>(data[i]));
if(i != data.size() - 1) // Only write comma when not the last integer
arrayCode += ", ";
if(i % 40 == 0 && i > 0)
arrayCode += "\n ";
}
arrayCode += "\n};";
#endif
#ifdef WRITE_OUTPUT_TO_FILE
// Write array code to file
std::cout << "Writing array file..." << std::endl;
std::ofstream fileOut(outputFilename);
fileOut << arrayCode;
fileOut.close();
#endif
#ifdef COPY_OUTPUT_TO_CLIPBOARD
clip::set_text(arrayCode);
std::cout << "Array has been copied to clipboard" << std::endl;
#endif
std::cout << std::endl << "Done" << std::endl;
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
I added a new python script that does the trick now. It compresses the html file and outputs the full C++ array that can just be pasted into the source code of PrettyOTA. @MarcelBolten Check it out if you want. It's under the |
Beta Was this translation helpful? Give feedback.
-
Hi @LostInCompilation
Do you mind adding your build tools to the repository? More concrete, can you add information on how you minify the html and convert it to the hex strings that are included in the cpp files?
Beta Was this translation helpful? Give feedback.
All reactions