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
bool already_registered = false;
for (const auto& entry : j) {
if (!entry.is_string()) continue;
std::string existing = entry_local_path(entry.get<std::string>());
if (is_same_or_child(local_path, existing)) {
info << "Already covered by existing entry: " << existing << std::endl;
return 0;
info << "Already registered: " << existing << std::endl;
already_registered = true;
break;
}
}
// 7. Auto-detect git remote URL
std::string git_url;
if (!git_root.empty()) {
std::string cmd = "git -C " + quote(local_path) + " remote get-url origin 2>/dev/null";
FILE* pipe = popen(cmd.c_str(), "r");
if (pipe) {
char buffer[512];
while (fgets(buffer, sizeof(buffer), pipe))
git_url += buffer;
pclose(pipe);
while (!git_url.empty() && (git_url.back() == '\n' || git_url.back() == '\r'))
git_url.pop_back();
if (!already_registered) {
// 7. Auto-detect git remote URL
std::string git_url;
if (!git_root.empty()) {
std::string cmd = "git -C " + quote(local_path) + " remote get-url origin 2>/dev/null";
FILE* pipe = popen(cmd.c_str(), "r");
if (pipe) {
char buffer[512];
while (fgets(buffer, sizeof(buffer), pipe))
git_url += buffer;
pclose(pipe);
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
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;
// 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;
}
// 10. Always update dropshell-templates.list with discovered templates
gTemplateManager().write_list_file(local_path, templates);
info << "Found " << templates.size() << " template(s)" << std::endl;