diff --git a/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_move_the_masd_pages_and_templates.org b/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_move_the_masd_pages_and_templates.org index 1a9692e8..d8ff853a 100644 --- a/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_move_the_masd_pages_and_templates.org +++ b/doc/agile/versions/v0/sprint_01/own_the_masd_meta_model/task_move_the_masd_pages_and_templates.org @@ -33,9 +33,9 @@ model lives in one repository. |--------------+----------------------------------------| | State | STARTED | | Parent story | [[id:E3C81059-FE00-4403-AA56-190FDBB9904A][Own the MASD meta model and generate compass with it]] | -| Now | The pages and families the investigation classes MASD are copied into compass in #213: 23 modeling pages and 24 template files. ORE Studio is not changed. The layered library the readers need now resolves from one shared place: @compass under the project's directories, a later layer winning. | +| Now | The pages and families the investigation classes MASD are copied into compass in #213: 23 modeling pages and 24 template files. ORE Studio is not changed. The layered library resolves from one shared place, and physical_inventory is the first reader that takes it. | | Waiting on | The product owner's sign-off of compass, before ORE Studio's copies are deleted. | -| Next | Rewrite the copied pages that still carry ORE Studio text (see "Copied, still needs rewriting"). Split the mixed pages and families, =ores.cpp.component.test_main= among them. Give the copied space nodes' facet tables an owner. Have =physical_inventory=, =tangle_check= and =check_inventory= read the layered library. After the sign-off, delete ORE Studio's copies in the change that layers =@compass= under its library. | +| Next | Give =tangle_check= and =check_inventory= the same layered option. Rewrite the copied pages that still carry ORE Studio text (see "Copied, still needs rewriting"). Split the mixed pages and families, =ores.cpp.component.test_main= among them. Give the copied space nodes' facet tables an owner. After the sign-off, delete ORE Studio's copies in the change that layers =@compass= under its library. | | Last touched | 2026-11-29 | * Acceptance @@ -123,6 +123,16 @@ task and name that file in the Command column. read the layers outside the checkout, which =physical_inventory=, =tangle_check= and =check_inventory= need before they can take the same option; today they see only the checkout. +- *The readers take the library option.* =physical_inventory= now reads the + pages of every library layer, the codegen option =@compass,=, each + library's =templates/= as the investigation recommends, a later page + replacing an earlier one of the same title. It writes back only the + checkout's own =directory= pages, and its =environment= digest covers every + file of a library outside the checkout, so a page that changes runs it + again. The filesystem half of the reader, =files_below= and + =read_disk_file=, moved to =compass.platform= so =compass.docs= and + =compass.codegen= share it. =tangle_check= and =check_inventory= still + read one directory. - *The overlay shape.* The product owner confirmed it on 2026-12-05: the generic MASD stays in compass and a project's own additions layer over it from the project's library, so ORE Studio's =ores.*= addresses and its diff --git a/projects/compass.codegen/src/ops/library.cpp b/projects/compass.codegen/src/ops/library.cpp index 65f427a1..1700779c 100644 --- a/projects/compass.codegen/src/ops/library.cpp +++ b/projects/compass.codegen/src/ops/library.cpp @@ -36,33 +36,6 @@ namespace { namespace fs = std::filesystem; -std::optional read_disk(const fs::path& path) { - std::ifstream in(path, std::ios::binary); - if (!in) - return std::nullopt; - return std::string(std::istreambuf_iterator(in), - std::istreambuf_iterator()); -} - -/** - * @brief The regular files below @p directory, in generic form, sorted; - * nothing when it is not a directory. - */ -std::vector files_below(const fs::path& directory) { - std::vector out; - std::error_code ec; - if (!fs::is_directory(directory, ec)) - return out; - for (auto it = fs::recursive_directory_iterator(directory, ec); - !ec && it != fs::recursive_directory_iterator(); - it.increment(ec)) { - if (it->is_regular_file(ec)) - out.push_back(it->path().generic_string()); - } - std::ranges::sort(out); - return out; -} - /** * @brief The paths with_files_outside listed, which the views it returns * point into for as long as the reader lives. @@ -108,7 +81,8 @@ checkout_reader with_files_outside(checkout_reader checkout) { ? prefix : prefix.substr(0, slash); std::vector out; const std::lock_guard lock(state->mutex); - for (auto& path : files_below(fs::path(directory))) { + for (auto& path : compass::platform::filesystem::files_below( + fs::path(directory))) { if (!path.starts_with(prefix) || !path.ends_with(suffix)) continue; out.emplace_back(state->paths.emplace_back(std::move(path))); @@ -116,7 +90,8 @@ checkout_reader with_files_outside(checkout_reader checkout) { return out; }; r.read = [read = checkout.read](std::string_view path) { - return outside_the_checkout(path) ? read_disk(fs::path(path)) + return outside_the_checkout(path) + ? compass::platform::filesystem::read_disk_file(fs::path(path)) : read(path); }; r.exists = [exists = checkout.exists](std::string_view path) { @@ -135,8 +110,10 @@ std::string outside_files_digest(const std::vector& directories) { continue; out += directory; out.push_back('\0'); - for (const auto& path : files_below(fs::path(directory))) { - const auto bytes = read_disk(fs::path(path)); + for (const auto& path : compass::platform::filesystem::files_below( + fs::path(directory))) { + const auto bytes = + compass::platform::filesystem::read_disk_file(fs::path(path)); out += path; out.push_back('\0'); out += bytes ? compass::store::ops::to_hex( diff --git a/projects/compass.docs/src/codecs/physical_inventory.cpp b/projects/compass.docs/src/codecs/physical_inventory.cpp index 51754ade..c64ca104 100644 --- a/projects/compass.docs/src/codecs/physical_inventory.cpp +++ b/projects/compass.docs/src/codecs/physical_inventory.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -30,7 +31,10 @@ #include "compass.codec/types/corpus_view.hpp" #include "compass.cpp/ops/paths.hpp" #include "compass.cpp/ops/text.hpp" +#include "compass.platform/filesystem/library.hpp" +#include "compass.platform/process/executable.hpp" #include "compass.platform/text/regex.hpp" +#include "compass.store/ops/blob_hash.hpp" namespace compass::docs::codecs { @@ -38,11 +42,18 @@ namespace { namespace ct = compass::codec::types; namespace co = compass::cpp::ops; +namespace pf = compass::platform::filesystem; namespace pt = compass::platform::text; -constexpr std::array options{{ - {"directory", "", "The directory of the physical-space documents, " - "relative to the checkout."}, +constexpr std::array options{{ + {"library", "", "The directories of the codegen library, " + "comma-separated, in the order they layer; @compass is the library " + "installed with compass. Each library's templates/ holds " + "physical-space pages, and a later layer's page replaces an earlier " + "one of the same title."}, + {"directory", "", "The directory of the checkout's physical-space " + "documents, relative to the checkout, read after the library's and " + "written back."}, {"product", "", "The project's product: the title of the root " "technical space, and the first part of every address under it."}}}; @@ -60,6 +71,7 @@ std::string required(const ct::corpus_view& v, std::string_view key) { struct node { std::string path; std::string id; + bool writable = false; std::map> keywords; [[nodiscard]] std::string_view keyword(std::string_view key) const { @@ -98,6 +110,72 @@ node parse(std::string_view path, std::string_view text) { return n; } +/** + * @brief The pages directly under @p directory, which is a checkout path + * read through @p v or, outside the checkout, read from disk. + */ +std::vector> pages_below( + const ct::corpus_view& v, std::string_view directory) { + std::string prefix(directory); + if (!prefix.ends_with('/')) + prefix.push_back('/'); + std::vector> out; + if (pf::outside_the_checkout(directory)) { + for (const auto& path : pf::files_below(std::filesystem::path( + directory))) { + if (!path.starts_with(prefix) || !path.ends_with(".org") || + path.find('/', prefix.size()) != std::string::npos) + continue; + out.emplace_back(path, pf::read_disk_file(path).value_or("")); + } + return out; + } + for (const auto path : v.list_files(prefix, ".org")) { + if (path.find('/', prefix.size()) != std::string::npos) + continue; + out.emplace_back(path, v.read_file(path).value_or("")); + } + return out; +} + +/** + * @brief The physical-space pages of the library's layers and then the + * checkout's directory, a later page replacing an earlier one of the same + * title; only the checkout's own pages are written back. + * @throws std::runtime_error when neither a library nor a directory is + * set. + */ +std::vector pages_of(const ct::corpus_view& v) { + std::vector out; + std::map> by_title; + const auto add = [&](node n) { + const auto title = std::string(n.title()); + if (const auto it = by_title.find(title); it != by_title.end()) { + out[it->second] = std::move(n); + return; + } + by_title.emplace(title, out.size()); + out.push_back(std::move(n)); + }; + const std::string library(v.option("library")); + if (!library.empty()) + for (const auto& directory : pf::layered_directories(library, + compass::platform::process::executable_path())) + for (auto& [path, text] : pages_below(v, directory + "/templates")) + add(parse(path, text)); + const std::string directory(v.option("directory")); + if (!directory.empty()) + for (auto& [path, text] : pages_below(v, directory)) { + auto n = parse(path, text); + n.writable = true; + add(std::move(n)); + } + if (out.empty()) + throw std::runtime_error("physical_inventory: set the option " + "library or directory"); + return out; +} + std::string build_table(const std::vector& headers, const std::vector>& rows) { std::string out = "|"; @@ -165,16 +243,9 @@ std::size_t count_of(const group& g, std::string_view key) { } void inventories(const ct::corpus_view& v, ct::file_output& out) { - auto templates_dir = required(v, "directory"); - if (!templates_dir.ends_with('/')) - templates_dir.push_back('/'); const auto product = required(v, "product"); const auto prefix = product + "."; - std::vector docs; - for (const auto path : v.list_files(templates_dir, ".org")) { - if (path.find('/', templates_dir.size()) == std::string_view::npos) - docs.push_back(parse(path, v.read_file(path).value_or(""))); - } + const auto docs = pages_of(v); std::vector tspaces; std::vector facets; group facets_by_ts; @@ -243,20 +314,53 @@ void inventories(const ct::corpus_view& v, ct::file_output& out) { it->second = replace_section_table(it->second, e.heading, e.table); } for (const auto& d : docs) - if (const auto it = texts.find(&d); it != texts.end()) - out.files.add(d.path, it->second); + if (d.writable) + if (const auto it = texts.find(&d); it != texts.end()) + out.files.add(d.path, it->second); +} + +/** + * @brief Every file of the libraries outside the checkout, which no run + * records as an input, so a change to one runs every unit again. + */ +std::string environment(const ct::option_values& o) { + std::vector directories; + try { + directories = pf::layered_directories(o["library"], + compass::platform::process::executable_path()); + } catch (const std::runtime_error&) { + return {}; + } + std::string bytes; + for (const auto& directory : directories) { + if (!pf::outside_the_checkout(directory)) + continue; + bytes += directory; + bytes.push_back('\0'); + for (const auto& path : pf::files_below( + std::filesystem::path(directory))) { + bytes += path; + bytes.push_back('\0'); + const auto content = pf::read_disk_file(path); + bytes += content ? compass::store::ops::to_hex( + compass::store::ops::blob_id_of(*content)) : std::string(); + bytes.push_back('\n'); + } + } + return bytes; } } const ct::descriptor physical_inventory{ .name = "physical_inventory", - .version = 2, + .version = 3, .summary = "Writes the child tables of the physical-space documents.", .reads = ct::inputs::of_types({}), .options = options, .rules = {}, .enabled_by_default = false, - .run = ct::corpus_function{inventories}}; + .run = ct::corpus_function{inventories}, + .environment = environment}; } diff --git a/projects/compass.docs/tests/codecs_physical_inventory_tests.cpp b/projects/compass.docs/tests/codecs_physical_inventory_tests.cpp index 55a16185..dca530e1 100644 --- a/projects/compass.docs/tests/codecs_physical_inventory_tests.cpp +++ b/projects/compass.docs/tests/codecs_physical_inventory_tests.cpp @@ -20,6 +20,8 @@ #include #include +#include +#include #include #include "compass.docs/codecs/physical_inventory.hpp" #include "test_tree.hpp" @@ -91,11 +93,51 @@ TEST_CASE("the product names the root and the addresses under it", CHECK_FALSE(out.contains(lib + "/ores.cpp.org")); } -TEST_CASE("the directory and the product are configuration", +TEST_CASE("a library's pages layer under the checkout's, which replace " + "them by title and are the only ones written", "[codecs][physical_inventory]") { + namespace fs = std::filesystem; + const auto root = fs::temp_directory_path() / + "compass-physical-inventory-library"; + std::error_code ec; + fs::remove_all(root, ec); + fs::create_directories(root / "templates", ec); + std::ofstream(root / "templates" / "ores.org") << node("L", "ores", + "technical_space", "#+description: From the library\n", + "\n* Technical spaces\n"); + std::ofstream(root / "templates" / "ores.sql.org") << node("S", + "ores.sql", "technical_space", + "#+description: SQL from the library\n", "\n* Facets\n"); + std::ofstream(root / "templates" / "ores.cpp.org") << node("C", + "ores.cpp", "technical_space", + "#+description: C++ from the library\n", "\n* Facets\n"); + const std::string modelling = "projects/modeling/"; + const auto out = tests::generate(codecs::physical_inventory, { + {modelling + "ores.org", node("R", "ores", "technical_space", + "#+description: From the checkout\n", "\n* Technical spaces\n")}, + {modelling + "ores.cpp.org", node("C2", "ores.cpp", + "technical_space", "#+description: C++ from the checkout\n", + "\n* Facets\n")}}, + {{"library", root.generic_string()}, {"directory", modelling}, + {"product", "ores"}}); + + CHECK_FALSE(out.contains((root / "templates" / "ores.org").generic_string())); + CHECK_FALSE(out.contains((root / "templates" / "ores.sql.org").generic_string())); + CHECK(out.at(modelling + "ores.org").find( + "| [[id:S][ores.sql]] | 0 | SQL from the library |") != + std::string::npos); + CHECK(out.at(modelling + "ores.org").find( + "| [[id:C2][ores.cpp]] | 0 | C++ from the checkout |") != + std::string::npos); + CHECK(out.at(modelling + "ores.org").find("C++ from the library") == + std::string::npos); + fs::remove_all(root, ec); +} + +TEST_CASE("the directory, the library and the product are configuration", "[codecs][physical_inventory]") { CHECK_THROWS_WITH(tests::generate(codecs::physical_inventory, {}, {{"product", "ores"}}), - "physical_inventory: set the option directory"); + "physical_inventory: set the option library or directory"); CHECK_THROWS_WITH(tests::generate(codecs::physical_inventory, {}, {{"directory", dir}}), "physical_inventory: set the option product"); diff --git a/projects/compass.platform/include/compass.platform/filesystem/library.hpp b/projects/compass.platform/include/compass.platform/filesystem/library.hpp index 8afb6956..bc034f55 100644 --- a/projects/compass.platform/include/compass.platform/filesystem/library.hpp +++ b/projects/compass.platform/include/compass.platform/filesystem/library.hpp @@ -21,6 +21,7 @@ #define COMPASS_PLATFORM_FILESYSTEM_LIBRARY_HPP #include +#include #include #include #include @@ -59,6 +60,20 @@ inline constexpr std::string_view compass_library = "@compass"; */ [[nodiscard]] bool outside_the_checkout(std::string_view directory); +/** + * @brief The regular files below @p directory, in generic form, sorted; + * nothing when it is not a directory. + */ +[[nodiscard]] std::vector files_below( + const std::filesystem::path& directory); + +/** + * @brief The bytes of the file at @p path, or nothing when it cannot be + * read. + */ +[[nodiscard]] std::optional read_disk_file( + const std::filesystem::path& path); + } #endif diff --git a/projects/compass.platform/src/filesystem/library.cpp b/projects/compass.platform/src/filesystem/library.cpp index 5ae144c0..b242c81c 100644 --- a/projects/compass.platform/src/filesystem/library.cpp +++ b/projects/compass.platform/src/filesystem/library.cpp @@ -19,7 +19,11 @@ */ #include "compass.platform/filesystem/library.hpp" +#include +#include +#include #include +#include namespace compass::platform::filesystem { @@ -77,4 +81,27 @@ bool outside_the_checkout(std::string_view directory) { return !directory.empty() && fs::path(directory).is_absolute(); } +std::vector files_below(const fs::path& directory) { + std::vector out; + std::error_code ec; + if (!fs::is_directory(directory, ec)) + return out; + for (auto it = fs::recursive_directory_iterator(directory, ec); + !ec && it != fs::recursive_directory_iterator(); + it.increment(ec)) { + if (it->is_regular_file(ec)) + out.push_back(it->path().generic_string()); + } + std::ranges::sort(out); + return out; +} + +std::optional read_disk_file(const fs::path& path) { + std::ifstream in(path, std::ios::binary); + if (!in) + return std::nullopt; + return std::string(std::istreambuf_iterator(in), + std::istreambuf_iterator()); +} + } diff --git a/projects/compass.platform/tests/filesystem_library_tests.cpp b/projects/compass.platform/tests/filesystem_library_tests.cpp index b8bbc266..69e63a02 100644 --- a/projects/compass.platform/tests/filesystem_library_tests.cpp +++ b/projects/compass.platform/tests/filesystem_library_tests.cpp @@ -19,6 +19,8 @@ */ #include "compass.platform/filesystem/library.hpp" +#include +#include #include #include #include @@ -28,7 +30,9 @@ namespace { using compass::platform::filesystem::layered_directories; +using compass::platform::filesystem::files_below; using compass::platform::filesystem::outside_the_checkout; +using compass::platform::filesystem::read_disk_file; } @@ -54,3 +58,23 @@ TEST_CASE("a directory is outside the checkout when it is absolute", CHECK(outside_the_checkout("/opt/compass/share/compass/masd")); CHECK_FALSE(outside_the_checkout("projects/ores.codegen/library")); } + +TEST_CASE("files_below lists the regular files of a directory by path", + "[platform][filesystem][library]") { + namespace fs = std::filesystem; + const auto root = fs::temp_directory_path() / + "compass-platform-files-below"; + std::error_code ec; + fs::remove_all(root, ec); + fs::create_directories(root / "templates" / "nested", ec); + std::ofstream(root / "templates" / "b.org") << "b"; + std::ofstream(root / "templates" / "nested" / "a.org") << "a"; + CHECK(files_below(root) == std::vector{ + (root / "templates" / "b.org").generic_string(), + (root / "templates" / "nested" / "a.org").generic_string()}); + CHECK(read_disk_file(root / "templates" / "b.org") == + std::optional{"b"}); + CHECK_FALSE(read_disk_file(root / "missing.org")); + CHECK(files_below(root / "missing").empty()); + fs::remove_all(root, ec); +}