mirror of
https://github.com/NixOS/nix
synced 2025-06-25 14:51:16 +02:00
refactor: create a new nix formatter run
command alias for nix fmt
This refactor shouldn't change much except add a new `nix formatter run` command. This creates space for the new `nix formatter build` command, which I'll be introducing in the next commit.
This commit is contained in:
parent
9099b7dd87
commit
5ea7b97147
8 changed files with 103 additions and 60 deletions
|
@ -605,8 +605,8 @@
|
||||||
''^tests/functional/flakes/prefetch\.sh$''
|
''^tests/functional/flakes/prefetch\.sh$''
|
||||||
''^tests/functional/flakes/run\.sh$''
|
''^tests/functional/flakes/run\.sh$''
|
||||||
''^tests/functional/flakes/show\.sh$''
|
''^tests/functional/flakes/show\.sh$''
|
||||||
''^tests/functional/fmt\.sh$''
|
''^tests/functional/formatter\.sh$''
|
||||||
''^tests/functional/fmt\.simple\.sh$''
|
''^tests/functional/formatter\.simple\.sh$''
|
||||||
''^tests/functional/gc-auto\.sh$''
|
''^tests/functional/gc-auto\.sh$''
|
||||||
''^tests/functional/gc-concurrent\.builder\.sh$''
|
''^tests/functional/gc-concurrent\.builder\.sh$''
|
||||||
''^tests/functional/gc-concurrent\.sh$''
|
''^tests/functional/gc-concurrent\.sh$''
|
||||||
|
|
|
@ -1,55 +0,0 @@
|
||||||
#include "nix/cmd/command.hh"
|
|
||||||
#include "nix/cmd/installable-value.hh"
|
|
||||||
#include "nix/expr/eval.hh"
|
|
||||||
#include "run.hh"
|
|
||||||
|
|
||||||
using namespace nix;
|
|
||||||
|
|
||||||
struct CmdFmt : SourceExprCommand {
|
|
||||||
std::vector<std::string> args;
|
|
||||||
|
|
||||||
CmdFmt() { expectArgs({.label = "args", .handler = {&args}}); }
|
|
||||||
|
|
||||||
std::string description() override {
|
|
||||||
return "reformat your code in the standard style";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string doc() override {
|
|
||||||
return
|
|
||||||
#include "fmt.md"
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
Category category() override { return catSecondary; }
|
|
||||||
|
|
||||||
Strings getDefaultFlakeAttrPaths() override {
|
|
||||||
return Strings{"formatter." + settings.thisSystem.get()};
|
|
||||||
}
|
|
||||||
|
|
||||||
Strings getDefaultFlakeAttrPathPrefixes() override { return Strings{}; }
|
|
||||||
|
|
||||||
void run(ref<Store> store) override
|
|
||||||
{
|
|
||||||
auto evalState = getEvalState();
|
|
||||||
auto evalStore = getEvalStore();
|
|
||||||
|
|
||||||
auto installable_ = parseInstallable(store, ".");
|
|
||||||
auto & installable = InstallableValue::require(*installable_);
|
|
||||||
auto app = installable.toApp(*evalState).resolve(evalStore, store);
|
|
||||||
|
|
||||||
Strings programArgs{app.program};
|
|
||||||
|
|
||||||
// Propagate arguments from the CLI
|
|
||||||
for (auto &i : args) {
|
|
||||||
programArgs.push_back(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Release our references to eval caches to ensure they are persisted to disk, because
|
|
||||||
// we are about to exec out of this process without running C++ destructors.
|
|
||||||
evalState->evalCaches.clear();
|
|
||||||
|
|
||||||
execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
static auto r2 = registerCommand<CmdFmt>("fmt");
|
|
|
@ -2,7 +2,7 @@ R""(
|
||||||
|
|
||||||
# Description
|
# Description
|
||||||
|
|
||||||
`nix fmt` calls the formatter specified in the flake.
|
`nix fmt` (an alias for `nix formatter run`) calls the formatter specified in the flake.
|
||||||
|
|
||||||
Flags can be forwarded to the formatter by using `--` followed by the flags.
|
Flags can be forwarded to the formatter by using `--` followed by the flags.
|
||||||
|
|
98
src/nix/formatter.cc
Normal file
98
src/nix/formatter.cc
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
#include "nix/cmd/command.hh"
|
||||||
|
#include "nix/cmd/installable-value.hh"
|
||||||
|
#include "nix/expr/eval.hh"
|
||||||
|
#include "run.hh"
|
||||||
|
|
||||||
|
using namespace nix;
|
||||||
|
|
||||||
|
struct CmdFormatter : NixMultiCommand
|
||||||
|
{
|
||||||
|
CmdFormatter()
|
||||||
|
: NixMultiCommand("formatter", RegisterCommand::getCommandsFor({"formatter"}))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string description() override
|
||||||
|
{
|
||||||
|
return "build or run the formatter";
|
||||||
|
}
|
||||||
|
|
||||||
|
Category category() override
|
||||||
|
{
|
||||||
|
return catSecondary;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static auto rCmdFormatter = registerCommand<CmdFormatter>("formatter");
|
||||||
|
|
||||||
|
struct CmdFormatterRun : SourceExprCommand
|
||||||
|
{
|
||||||
|
std::vector<std::string> args;
|
||||||
|
|
||||||
|
CmdFormatterRun()
|
||||||
|
{
|
||||||
|
expectArgs({.label = "args", .handler = {&args}});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string description() override
|
||||||
|
{
|
||||||
|
return "reformat your code in the standard style";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string doc() override
|
||||||
|
{
|
||||||
|
return
|
||||||
|
#include "formatter-run.md"
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
Category category() override
|
||||||
|
{
|
||||||
|
return catSecondary;
|
||||||
|
}
|
||||||
|
|
||||||
|
Strings getDefaultFlakeAttrPaths() override
|
||||||
|
{
|
||||||
|
return Strings{"formatter." + settings.thisSystem.get()};
|
||||||
|
}
|
||||||
|
|
||||||
|
Strings getDefaultFlakeAttrPathPrefixes() override
|
||||||
|
{
|
||||||
|
return Strings{};
|
||||||
|
}
|
||||||
|
|
||||||
|
void run(ref<Store> store) override
|
||||||
|
{
|
||||||
|
auto evalState = getEvalState();
|
||||||
|
auto evalStore = getEvalStore();
|
||||||
|
|
||||||
|
auto installable_ = parseInstallable(store, ".");
|
||||||
|
auto & installable = InstallableValue::require(*installable_);
|
||||||
|
auto app = installable.toApp(*evalState).resolve(evalStore, store);
|
||||||
|
|
||||||
|
Strings programArgs{app.program};
|
||||||
|
|
||||||
|
// Propagate arguments from the CLI
|
||||||
|
for (auto & i : args) {
|
||||||
|
programArgs.push_back(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release our references to eval caches to ensure they are persisted to disk, because
|
||||||
|
// we are about to exec out of this process without running C++ destructors.
|
||||||
|
evalState->evalCaches.clear();
|
||||||
|
|
||||||
|
execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
static auto rFormatterRun = registerCommand2<CmdFormatterRun>({"formatter", "run"});
|
||||||
|
|
||||||
|
struct CmdFmt : CmdFormatterRun
|
||||||
|
{
|
||||||
|
void run(ref<Store> store) override
|
||||||
|
{
|
||||||
|
CmdFormatterRun::run(store);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static auto rFmt = registerCommand<CmdFmt>("fmt");
|
|
@ -78,7 +78,7 @@ nix_sources = [config_priv_h] + files(
|
||||||
'env.cc',
|
'env.cc',
|
||||||
'eval.cc',
|
'eval.cc',
|
||||||
'flake.cc',
|
'flake.cc',
|
||||||
'fmt.cc',
|
'formatter.cc',
|
||||||
'hash.cc',
|
'hash.cc',
|
||||||
'log.cc',
|
'log.cc',
|
||||||
'ls.cc',
|
'ls.cc',
|
||||||
|
|
|
@ -132,7 +132,7 @@ suites = [
|
||||||
'nix-copy-ssh-ng.sh',
|
'nix-copy-ssh-ng.sh',
|
||||||
'post-hook.sh',
|
'post-hook.sh',
|
||||||
'function-trace.sh',
|
'function-trace.sh',
|
||||||
'fmt.sh',
|
'formatter.sh',
|
||||||
'eval-store.sh',
|
'eval-store.sh',
|
||||||
'why-depends.sh',
|
'why-depends.sh',
|
||||||
'derivation-json.sh',
|
'derivation-json.sh',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue