Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,<project>=, 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
Expand Down
39 changes: 8 additions & 31 deletions projects/compass.codegen/src/ops/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,6 @@ namespace {

namespace fs = std::filesystem;

std::optional<std::string> read_disk(const fs::path& path) {
std::ifstream in(path, std::ios::binary);
if (!in)
return std::nullopt;
return std::string(std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>());
}

/**
* @brief The regular files below @p directory, in generic form, sorted;
* nothing when it is not a directory.
*/
std::vector<std::string> files_below(const fs::path& directory) {
std::vector<std::string> 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.
Expand Down Expand Up @@ -108,15 +81,17 @@ checkout_reader with_files_outside(checkout_reader checkout) {
? prefix : prefix.substr(0, slash);
std::vector<std::string_view> 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)));
}
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) {
Expand All @@ -135,8 +110,10 @@ std::string outside_files_digest(const std::vector<std::string>& 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(
Expand Down
134 changes: 119 additions & 15 deletions projects/compass.docs/src/codecs/physical_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <algorithm>
#include <array>
#include <cctype>
#include <filesystem>
#include <map>
#include <stdexcept>
#include <string>
Expand All @@ -30,19 +31,29 @@
#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 {

namespace {

namespace ct = compass::codec::types;
namespace co = compass::cpp::ops;
namespace pf = compass::platform::filesystem;
namespace pt = compass::platform::text;

constexpr std::array<ct::option, 2> options{{
{"directory", "", "The directory of the physical-space documents, "
"relative to the checkout."},
constexpr std::array<ct::option, 3> 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."}}};

Expand All @@ -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<std::string, std::string, std::less<>> keywords;

[[nodiscard]] std::string_view keyword(std::string_view key) const {
Expand Down Expand Up @@ -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<std::pair<std::string, std::string>> pages_below(
const ct::corpus_view& v, std::string_view directory) {
std::string prefix(directory);
if (!prefix.ends_with('/'))
prefix.push_back('/');
std::vector<std::pair<std::string, std::string>> 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<node> pages_of(const ct::corpus_view& v) {
std::vector<node> out;
std::map<std::string, std::size_t, std::less<>> 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<std::string_view>& headers,
const std::vector<std::vector<std::string>>& rows) {
std::string out = "|";
Expand Down Expand Up @@ -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<node> 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<const node*> tspaces;
std::vector<const node*> facets;
group facets_by_ts;
Expand Down Expand Up @@ -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<std::string> 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<ct::file_output>{inventories}};
.run = ct::corpus_function<ct::file_output>{inventories},
.environment = environment};

}
46 changes: 44 additions & 2 deletions projects/compass.docs/tests/codecs_physical_inventory_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

#include <filesystem>
#include <fstream>
#include <string>
#include "compass.docs/codecs/physical_inventory.hpp"
#include "test_tree.hpp"
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define COMPASS_PLATFORM_FILESYSTEM_LIBRARY_HPP

#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -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<std::string> 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<std::string> read_disk_file(
const std::filesystem::path& path);

}

#endif
Loading
Loading