Skip to content

Commit 2727994

Browse files
authored
add: preserve dependency order and keep entries in single line (#1031)
- Preserve table orders when adding dependencies to the manifest - Keep dependency entries as a single line table
1 parent 67a74bf commit 2727994

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Cmd/Add.cc

+15-6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ handleDependency(
6666
static std::string
6767
getDependencyGitUrl(const std::string_view dep) {
6868
if (dep.find("://") == std::string_view::npos) {
69-
// check if atleast in "user/repo" format
69+
// Check if at least in "user/repo" format.
7070
if (dep.find('/') == std::string_view::npos) {
7171
logger::error("Invalid dependency: " + std::string(dep));
7272
return "";
@@ -88,9 +88,9 @@ getDependencyName(const std::string_view dep) {
8888
);
8989
}
9090

91-
// Remove trailing '.git' if it exists
91+
// Remove trailing '.git' if it exists.
9292
if (name.ends_with(".git")) {
93-
name = name.substr(0, name.size() - 4);
93+
name = name.substr(0, name.size() - ".git"sv.size());
9494
}
9595

9696
return name;
@@ -103,6 +103,9 @@ addDependencyToManifest(
103103
std::string& rev, std::string& branch
104104
) {
105105
toml::value depData = toml::table{};
106+
// Set the formatting for the dependency data table to be on a single line.
107+
// e.g. dep = { git = "https://github.com/user/repo.git", tag = "v1.0.0" }
108+
depData.as_table_fmt().fmt = toml::table_format::oneline;
106109

107110
if (isSystemDependency) {
108111
if (version.empty()) {
@@ -124,8 +127,14 @@ addDependencyToManifest(
124127
}
125128
}
126129

127-
auto data = toml::parse(getManifestPath());
128-
auto& deps = toml::find<toml::table>(data, "dependencies");
130+
// Keep the order of the tables.
131+
auto data = toml::parse<toml::ordered_type_config>(getManifestPath());
132+
133+
// Check if the dependencies table exists, if not create it.
134+
if (data["dependencies"].is_empty()) {
135+
data["dependencies"] = toml::table{};
136+
}
137+
auto& deps = data["dependencies"];
129138

130139
for (const auto& dep : newDeps) {
131140

@@ -145,7 +154,7 @@ addDependencyToManifest(
145154
}
146155

147156
std::ofstream ofs(getManifestPath());
148-
ofs << toml::format(data);
157+
ofs << data;
149158

150159
logger::info("Added", "to the poac.toml");
151160
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)