nix flake init: Operate on a SourcePath

Cherry-picked from lazy-trees.
This commit is contained in:
Eelco Dolstra 2024-11-19 16:59:38 +01:00
parent 3b76d01f3b
commit 868b4d37ea

View File

@ -891,37 +891,32 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
auto cursor = installable.getCursor(*evalState); auto cursor = installable.getCursor(*evalState);
auto templateDirAttr = cursor->getAttr("path"); auto templateDirAttr = cursor->getAttr("path")->forceValue();
auto templateDir = templateDirAttr->getString(); NixStringContext context;
auto templateDir = evalState->coerceToPath(noPos, templateDirAttr, context, "");
if (!store->isInStore(templateDir))
evalState->error<TypeError>(
"'%s' was not found in the Nix store\n"
"If you've set '%s' to a string, try using a path instead.",
templateDir, templateDirAttr->getAttrPathStr()).debugThrow();
std::vector<fs::path> changedFiles; std::vector<fs::path> changedFiles;
std::vector<fs::path> conflictedFiles; std::vector<fs::path> conflictedFiles;
std::function<void(const fs::path & from, const fs::path & to)> copyDir; std::function<void(const SourcePath & from, const fs::path & to)> copyDir;
copyDir = [&](const fs::path & from, const fs::path & to) copyDir = [&](const SourcePath & from, const fs::path & to)
{ {
fs::create_directories(to); fs::create_directories(to);
for (auto & entry : fs::directory_iterator{from}) { for (auto & [name, entry] : from.readDirectory()) {
checkInterrupt(); checkInterrupt();
auto from2 = entry.path(); auto from2 = from / name;
auto to2 = to / entry.path().filename(); auto to2 = to / name;
auto st = entry.symlink_status(); auto st = from2.lstat();
auto to_st = fs::symlink_status(to2); auto to_st = fs::symlink_status(to2);
if (fs::is_directory(st)) if (st.type == SourceAccessor::tDirectory)
copyDir(from2, to2); copyDir(from2, to2);
else if (fs::is_regular_file(st)) { else if (st.type == SourceAccessor::tRegular) {
auto contents = readFile(from2.string()); auto contents = from2.readFile();
if (fs::exists(to_st)) { if (fs::exists(to_st)) {
auto contents2 = readFile(to2.string()); auto contents2 = readFile(to2.string());
if (contents != contents2) { if (contents != contents2) {
printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2.string()); printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2);
conflictedFiles.push_back(to2); conflictedFiles.push_back(to2);
} else { } else {
notice("skipping identical file: %s", from2); notice("skipping identical file: %s", from2);
@ -930,11 +925,11 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
} else } else
writeFile(to2, contents); writeFile(to2, contents);
} }
else if (fs::is_symlink(st)) { else if (st.type == SourceAccessor::tSymlink) {
auto target = fs::read_symlink(from2); auto target = from2.readLink();
if (fs::exists(to_st)) { if (fs::exists(to_st)) {
if (fs::read_symlink(to2) != target) { if (fs::read_symlink(to2) != target) {
printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2.string()); printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2);
conflictedFiles.push_back(to2); conflictedFiles.push_back(to2);
} else { } else {
notice("skipping identical file: %s", from2); notice("skipping identical file: %s", from2);
@ -957,14 +952,14 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
for (auto & s : changedFiles) args.emplace_back(s.string()); for (auto & s : changedFiles) args.emplace_back(s.string());
runProgram("git", true, args); runProgram("git", true, args);
} }
auto welcomeText = cursor->maybeGetAttr("welcomeText");
if (welcomeText) { if (auto welcomeText = cursor->maybeGetAttr("welcomeText")) {
notice("\n"); notice("\n");
notice(renderMarkdownToTerminal(welcomeText->getString())); notice(renderMarkdownToTerminal(welcomeText->getString()));
} }
if (!conflictedFiles.empty()) if (!conflictedFiles.empty())
throw Error("Encountered %d conflicts - see above", conflictedFiles.size()); throw Error("encountered %d conflicts - see above", conflictedFiles.size());
} }
}; };