|
10 | 10 | #include <fstream> |
11 | 11 | #include <iomanip> |
12 | 12 | #include <new> |
13 | | -#include <nlohmann/json.hpp> |
14 | 13 | #include <regex> |
15 | 14 | #include <sstream> |
16 | 15 | #include <stdexcept> |
@@ -164,7 +163,7 @@ struct Command { |
164 | 163 | } |
165 | 164 | } |
166 | 165 |
|
167 | | - std::string quote(const std::string &str) { |
| 166 | + static std::string quote(const std::string &str) { |
168 | 167 | // Don't quote arguments that don't need quoting |
169 | 168 | if (str.find(' ') == std::string::npos && str.find('\"') == std::string::npos && str.find('/') == std::string::npos && |
170 | 169 | str.find(';') == std::string::npos) { |
@@ -582,24 +581,47 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { |
582 | 581 | endl(); |
583 | 582 | // clang-format on |
584 | 583 |
|
585 | | - // Generate vcpkg.json |
586 | | - using namespace nlohmann; |
587 | | - json j; |
588 | | - j["$cmkr"] = "This file is automatically generated from cmake.toml - DO NOT EDIT"; |
589 | | - j["$cmkr-url"] = cmkr_url; |
590 | | - j["$schema"] = "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json"; |
591 | | - j["name"] = vcpkg_escape_identifier(project.project_name); |
592 | | - j["version-string"] = project.project_version; |
593 | | - j["dependencies"] = project.vcpkg.packages; |
594 | | - if (!project.project_description.empty()) { |
595 | | - j["description"] = project.project_description; |
596 | | - } |
597 | | - |
| 584 | + // Generate vcpkg.json (sorry for the ugly string handling, nlohmann compiles very slowly) |
598 | 585 | std::ofstream ofs("vcpkg.json", std::ios::binary); |
599 | 586 | if (!ofs) { |
600 | 587 | throw std::runtime_error("Failed to create a vcpkg.json manifest file!"); |
601 | 588 | } |
602 | | - ofs << std::setw(2) << j << std::endl; |
| 589 | + ofs << R"({ |
| 590 | + "$cmkr": "This file is automatically generated from cmake.toml - DO NOT EDIT", |
| 591 | + "$cmkr-url": "https://github.com/build-cpp/cmkr", |
| 592 | + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", |
| 593 | + "dependencies": [ |
| 594 | +)"; |
| 595 | + |
| 596 | + const auto &packages = project.vcpkg.packages; |
| 597 | + for (size_t i = 0; i < packages.size(); i++) { |
| 598 | + const auto &package = packages[i]; |
| 599 | + if (!vcpkg_valid_identifier(package)) { |
| 600 | + throw std::runtime_error("Invalid vcpkg package name '" + package + "'"); |
| 601 | + } |
| 602 | + ofs << " \"" << package << '\"'; |
| 603 | + if (i + 1 < packages.size()) { |
| 604 | + ofs << ','; |
| 605 | + } |
| 606 | + ofs << '\n'; |
| 607 | + } |
| 608 | + |
| 609 | + auto escape = [](const std::string &str) { |
| 610 | + std::string result; |
| 611 | + for (auto ch : str) { |
| 612 | + if (ch == '\\' || ch == '\"') { |
| 613 | + result += '\\'; |
| 614 | + } |
| 615 | + result += ch; |
| 616 | + } |
| 617 | + return result; |
| 618 | + }; |
| 619 | + |
| 620 | + ofs << " ],\n"; |
| 621 | + ofs << " \"description\": \"" << escape(project.project_description) << "\",\n"; |
| 622 | + ofs << " \"name\": \"" << escape(project.project_name) << "\",\n"; |
| 623 | + ofs << R"( "version-string": "")" << '\n'; |
| 624 | + ofs << "}\n"; |
603 | 625 | } |
604 | 626 |
|
605 | 627 | if (!project.packages.empty()) { |
|
0 commit comments