fix: Always update template list file even if path already registered

This commit is contained in:
j
2026-03-26 21:56:20 +13:00
parent 63fdc2c0cb
commit 74329dd968

View File

@@ -141,56 +141,55 @@ int add_template_handler(const CommandContext& ctx) {
} }
// 6. Check if this path or a parent is already registered // 6. Check if this path or a parent is already registered
bool already_registered = false;
for (const auto& entry : j) { for (const auto& entry : j) {
if (!entry.is_string()) continue; if (!entry.is_string()) continue;
std::string existing = entry_local_path(entry.get<std::string>()); std::string existing = entry_local_path(entry.get<std::string>());
if (is_same_or_child(local_path, existing)) { if (is_same_or_child(local_path, existing)) {
info << "Already covered by existing entry: " << existing << std::endl; info << "Already registered: " << existing << std::endl;
return 0; already_registered = true;
break;
} }
} }
// 7. Auto-detect git remote URL if (!already_registered) {
std::string git_url; // 7. Auto-detect git remote URL
if (!git_root.empty()) { std::string git_url;
std::string cmd = "git -C " + quote(local_path) + " remote get-url origin 2>/dev/null"; if (!git_root.empty()) {
FILE* pipe = popen(cmd.c_str(), "r"); std::string cmd = "git -C " + quote(local_path) + " remote get-url origin 2>/dev/null";
if (pipe) { FILE* pipe = popen(cmd.c_str(), "r");
char buffer[512]; if (pipe) {
while (fgets(buffer, sizeof(buffer), pipe)) char buffer[512];
git_url += buffer; while (fgets(buffer, sizeof(buffer), pipe))
pclose(pipe); git_url += buffer;
while (!git_url.empty() && (git_url.back() == '\n' || git_url.back() == '\r')) pclose(pipe);
git_url.pop_back(); while (!git_url.empty() && (git_url.back() == '\n' || git_url.back() == '\r'))
git_url.pop_back();
}
} }
// 8. Build entry and add
std::string new_entry = local_path;
if (!git_url.empty())
new_entry += ":" + git_url;
j.push_back(new_entry);
// 9. Write template_paths.json
std::filesystem::create_directories(json_path.parent_path());
std::ofstream out(json_path);
if (!out.is_open()) {
error << "Failed to write " << json_path << std::endl;
return 1;
}
out << j.dump(4) << std::endl;
out.close();
info << "Added to " << json_path << ":" << std::endl;
info << " " << new_entry << std::endl;
} }
// 8. Build entry and add // 10. Always update dropshell-templates.list with discovered templates
std::string new_entry = local_path; gTemplateManager().write_list_file(local_path, templates);
if (!git_url.empty())
new_entry += ":" + git_url;
j.push_back(new_entry);
// 9. Write template_paths.json
std::filesystem::create_directories(json_path.parent_path());
std::ofstream out(json_path);
if (!out.is_open()) {
error << "Failed to write " << json_path << std::endl;
return 1;
}
out << j.dump(4) << std::endl;
out.close();
info << "Added to " << json_path << ":" << std::endl;
info << " " << new_entry << std::endl;
// 10. Create dropshell-templates.list if it doesn't exist
std::filesystem::path list_file = dir_path / filenames::dropshell_templates_list;
if (!std::filesystem::exists(list_file)) {
gTemplateManager().write_list_file(local_path, templates);
} else {
info << "Using existing " << list_file << std::endl;
}
info << "Found " << templates.size() << " template(s)" << std::endl; info << "Found " << templates.size() << " template(s)" << std::endl;