Skip to content

Commit

Permalink
Manifest: move findManifest into Manifest class (#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui authored Jan 31, 2025
1 parent 9265493 commit bbf1096
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Cmd/Add.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ addDependencyToManifest(
}

// Keep the order of the tables.
const fs::path manifestPath = Try(findManifest());
const fs::path manifestPath = Try(Manifest::findPath());
auto data = toml::parse<toml::ordered_type_config>(manifestPath);

// Check if the dependencies table exists, if not create it.
Expand Down
2 changes: 1 addition & 1 deletion src/Cmd/Clean.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Subcmd CLEAN_CMD = //
static Result<void>
cleanMain(CliArgsView args) noexcept {
// TODO: share across sources
fs::path outDir = Try(findManifest()).parent_path() / "cabin-out";
fs::path outDir = Try(Manifest::findPath()).parent_path() / "cabin-out";

// Parse args
for (auto itr = args.begin(); itr != args.end(); ++itr) {
Expand Down
2 changes: 1 addition & 1 deletion src/Cmd/Remove.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ removeMain(const CliArgsView args) {
Ensure(!args.empty(), "`cabin remove` requires at least one argument");

std::vector<std::string_view> removedDeps = {};
const fs::path manifestPath = Try(findManifest());
const fs::path manifestPath = Try(Manifest::findPath());
auto data = toml::parse<toml::ordered_type_config>(manifestPath);
auto& deps = data["dependencies"];

Expand Down
51 changes: 24 additions & 27 deletions src/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,37 +465,12 @@ SystemDependency::install() const {
return Ok(DepMetadata{ .includes = cflags, .libs = libs });
}

Result<fs::path>
findManifest(fs::path candidateDir) noexcept {
const fs::path origCandDir = candidateDir;
while (true) {
const fs::path configPath = candidateDir / Manifest::NAME;
logger::trace("Finding manifest: {}", configPath.string());
if (fs::exists(configPath)) {
return Ok(configPath);
}

const fs::path parentPath = candidateDir.parent_path();
if (candidateDir.has_parent_path()
&& parentPath != candidateDir.root_directory()) {
candidateDir = parentPath;
} else {
break;
}
}

Bail(
"{} not find in `{}` and its parents", Manifest::NAME,
origCandDir.string()
);
}

Result<Manifest>
Manifest::tryParse(fs::path path, const bool findParents) noexcept {
if (findParents) {
path = Try(findManifest(path.parent_path()));
path = Try(findPath(path.parent_path()));
}
return Manifest::tryFromToml(toml::parse(path), path);
return tryFromToml(toml::parse(path), path);
}

Result<Manifest>
Expand All @@ -514,6 +489,28 @@ Manifest::tryFromToml(const toml::value& data, fs::path path) noexcept {
));
}

Result<fs::path>
Manifest::findPath(fs::path candidateDir) noexcept {
const fs::path origCandDir = candidateDir;
while (true) {
const fs::path configPath = candidateDir / FILE_NAME;
logger::trace("Finding manifest: {}", configPath.string());
if (fs::exists(configPath)) {
return Ok(configPath);
}

const fs::path parentPath = candidateDir.parent_path();
if (candidateDir.has_parent_path()
&& parentPath != candidateDir.root_directory()) {
candidateDir = parentPath;
} else {
break;
}
}

Bail("{} not find in `{}` and its parents", FILE_NAME, origCandDir.string());
}

Result<std::vector<DepMetadata>>
Manifest::installDeps(const bool includeDevDeps) const {
std::vector<DepMetadata> installed;
Expand Down
9 changes: 5 additions & 4 deletions src/Manifest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ using Dependency =

class Manifest {
public:
static constexpr const char* NAME = "cabin.toml";
static constexpr const char* FILE_NAME = "cabin.toml";

const fs::path path;
const Package package;
Expand All @@ -200,11 +200,14 @@ class Manifest {
const Lint lint;

static Result<Manifest> tryParse(
fs::path path = fs::current_path() / NAME, bool findParents = true
fs::path path = fs::current_path() / FILE_NAME, bool findParents = true
) noexcept;
static Result<Manifest>
tryFromToml(const toml::value& data, fs::path path = "unknown") noexcept;

static Result<fs::path>
findPath(fs::path candidateDir = fs::current_path()) noexcept;

Result<std::vector<DepMetadata>> installDeps(bool includeDevDeps) const;

private:
Expand All @@ -219,8 +222,6 @@ class Manifest {
profiles(std::move(profiles)), lint(std::move(lint)) {}
};

Result<fs::path>
findManifest(fs::path candidateDir = fs::current_path()) noexcept;
Result<void> validatePackageName(std::string_view name) noexcept;

} // namespace cabin
Expand Down

0 comments on commit bbf1096

Please sign in to comment.