|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 The RefValue Project |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <exception> |
| 24 | +#include <filesystem> |
| 25 | +#include <fstream> |
| 26 | + |
| 27 | +#include <essence/abi/json.hpp> |
| 28 | +#include <essence/char8_t_remediation.hpp> |
| 29 | +#include <essence/cli/arg_parser.hpp> |
| 30 | +#include <essence/error_extensions.hpp> |
| 31 | +#include <essence/globalization/compiler.hpp> |
| 32 | +#include <essence/string.hpp> |
| 33 | + |
| 34 | +#include <spdlog/spdlog.h> |
| 35 | + |
| 36 | +namespace essence::tooling { |
| 37 | + namespace { |
| 38 | + void compile_json_file(const std::filesystem::path& input_path, const std::filesystem::path& output_path) { |
| 39 | + using namespace essence::globalization; |
| 40 | + |
| 41 | + spdlog::info(U8("Compiling {} to {}..."), from_u8string(input_path.u8string()), |
| 42 | + from_u8string(output_path.u8string())); |
| 43 | + |
| 44 | + thread_local const auto compiler = make_default_compiler(); |
| 45 | + const auto json = [&] { |
| 46 | + try { |
| 47 | + std::ifstream stream; |
| 48 | + |
| 49 | + stream.exceptions(std::ios_base::badbit); |
| 50 | + stream.open(input_path); |
| 51 | + |
| 52 | + return abi::json::parse(stream, nullptr, true); |
| 53 | + } catch (const std::exception& ex) { |
| 54 | + throw source_code_aware_runtime_error{U8("Path"), from_u8string(input_path.u8string()), |
| 55 | + U8("Message"), U8("Failed to open the JSON file."), U8("Internal"), ex.what()}; |
| 56 | + } |
| 57 | + }(); |
| 58 | + |
| 59 | + compiler.to_file(json, from_u8string(output_path.u8string())); |
| 60 | + } |
| 61 | + |
| 62 | + void compile_all_jsons( |
| 63 | + const std::filesystem::path& input_directory, const std::filesystem::path& output_directory) { |
| 64 | + for (auto&& item : std::filesystem::recursive_directory_iterator{ |
| 65 | + input_directory, std::filesystem::directory_options::skip_permission_denied}) { |
| 66 | + if (const auto extenstion = from_u8string(item.path().extension().u8string()); |
| 67 | + icase_string_comparer{}(extenstion, U8(".json"))) { |
| 68 | + auto output_path = output_directory / item.path().lexically_relative(input_directory); |
| 69 | + const auto output_subdirectory = output_path.parent_path(); |
| 70 | + |
| 71 | + output_path.replace_extension(U8(".lang")); |
| 72 | + |
| 73 | + if (std::error_code code; !std::filesystem::exists(output_subdirectory, code)) { |
| 74 | + std::filesystem::create_directories(output_subdirectory); |
| 75 | + } |
| 76 | + |
| 77 | + compile_json_file(item.path(), output_path); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } // namespace |
| 82 | +} // namespace essence::tooling |
| 83 | + |
| 84 | +int main(int argc, char* argv[]) try { |
| 85 | + using namespace essence; |
| 86 | + using namespace essence::cli; |
| 87 | + using namespace essence::tooling; |
| 88 | + |
| 89 | + if (const arg_parser parser; parser.parse(argc, argv), parser) { |
| 90 | + const auto unmatched = parser.unmatched_args(); |
| 91 | + |
| 92 | + if (unmatched.size() == 2) { |
| 93 | + spdlog::error(U8("Missing an argument: the output directory.")); |
| 94 | + std::exit(-1); |
| 95 | + } |
| 96 | + |
| 97 | + if (unmatched.size() == 1) { |
| 98 | + spdlog::error(U8("Missing an argument: the input directory.")); |
| 99 | + std::exit(-2); |
| 100 | + } |
| 101 | + |
| 102 | + compile_all_jsons(to_u8string(unmatched[1]), to_u8string(unmatched[2])); |
| 103 | + } |
| 104 | +} catch (const std::exception& ex) { |
| 105 | + spdlog::error(ex.what()); |
| 106 | + |
| 107 | + return -99; |
| 108 | +} |
0 commit comments