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 templateDirAttr = cursor->getAttr("path");
auto templateDir = templateDirAttr->getString();
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();
auto templateDirAttr = cursor->getAttr("path")->forceValue();
NixStringContext context;
auto templateDir = evalState->coerceToPath(noPos, templateDirAttr, context, "");
std::vector<fs::path> changedFiles;
std::vector<fs::path> conflictedFiles;
std::function<void(const fs::path & from, const fs::path & to)> copyDir;
copyDir = [&](const fs::path & from, const fs::path & to)
std::function<void(const SourcePath & from, const fs::path & to)> copyDir;
copyDir = [&](const SourcePath & from, const fs::path & to)
{
fs::create_directories(to);
for (auto & entry : fs::directory_iterator{from}) {
for (auto & [name, entry] : from.readDirectory()) {
checkInterrupt();
auto from2 = entry.path();
auto to2 = to / entry.path().filename();
auto st = entry.symlink_status();
auto from2 = from / name;
auto to2 = to / name;
auto st = from2.lstat();
auto to_st = fs::symlink_status(to2);
if (fs::is_directory(st))
if (st.type == SourceAccessor::tDirectory)
copyDir(from2, to2);
else if (fs::is_regular_file(st)) {
auto contents = readFile(from2.string());
else if (st.type == SourceAccessor::tRegular) {
auto contents = from2.readFile();
if (fs::exists(to_st)) {
auto contents2 = readFile(to2.string());
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);
} else {
notice("skipping identical file: %s", from2);
@ -930,18 +925,18 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
} else
writeFile(to2, contents);
}
else if (fs::is_symlink(st)) {
auto target = fs::read_symlink(from2);
else if (st.type == SourceAccessor::tSymlink) {
auto target = from2.readLink();
if (fs::exists(to_st)) {
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);
} else {
notice("skipping identical file: %s", from2);
}
continue;
} else
fs::create_symlink(target, to2);
fs::create_symlink(target, to2);
}
else
throw Error("file '%s' has unsupported type", from2);
@ -957,14 +952,14 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
for (auto & s : changedFiles) args.emplace_back(s.string());
runProgram("git", true, args);
}
auto welcomeText = cursor->maybeGetAttr("welcomeText");
if (welcomeText) {
if (auto welcomeText = cursor->maybeGetAttr("welcomeText")) {
notice("\n");
notice(renderMarkdownToTerminal(welcomeText->getString()));
}
if (!conflictedFiles.empty())
throw Error("Encountered %d conflicts - see above", conflictedFiles.size());
throw Error("encountered %d conflicts - see above", conflictedFiles.size());
}
};