1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-20 11:11:03 -04:00
nix/src/libutil
John Ericson bc23a44c54 Make command infra less stateful and more regular
Already, we had classes like `BuiltPathsCommand` and `StorePathsCommand`
which provided alternative `run` virtual functions providing the
implementation with more arguments. This was a very nice and easy way to
make writing command; just fill in the virtual functions and it is
fairly clear what to do.

However, exception to this pattern were `Installable{,s}Command`. These
two classes instead just had a field where the installables would be
stored, and various side-effecting `prepare` and `load` machinery too
fill them in. Command would wish out those fields.

This isn't so clear to use.

What this commit does is make those command classes like the others,
with richer `run` functions.

Not only does this restore the pattern making commands easier to write,
it has a number of other benefits:

- `prepare` and `load` are gone entirely! One command just hands just
  hands off to the next.

- `useDefaultInstallables` because `defaultInstallables`. This takes
  over `prepare` for the one case that needs it, and provides enough
  flexiblity to handle `nix repl`'s idiosyncratic migration.

- We can use `ref` instead of `std::shared_ptr`. The former must be
  initialized (so it is like Rust's `Box` rather than `Option<Box>`,
  This expresses the invariant that the installable are in fact
  initialized much better.

  This is possible because since we just have local variables not
  fields, we can stop worrying about the not-yet-initialized case.

- Fewer lines of code! (Finally I have a large refactor that makes the
  number go down not up...)

- `nix repl` is now implemented in a clearer way.

The last item deserves further mention. `nix repl` is not like the other
installable commands because instead working from once-loaded
installables, it needs to be able to load them again and again.

To properly support this, we make a new superclass
`RawInstallablesCommand`. This class has the argument parsing and
completion logic, but does *not* hand off parsed installables but
instead just the raw string arguments.

This is exactly what `nix repl` needs, and allows us to instead of
having the logic awkwardly split between `prepare`,
`useDefaultInstallables,` and `load`, have everything right next to each
other. I think this will enable future simplifications of that argument
defaulting logic, but I am saving those for a future PR --- best to keep
code motion and more complicated boolean expression rewriting separate
steps.

The "diagnostic ignored `-Woverloaded-virtual`" pragma helps because C++
doesn't like our many `run` methods. In our case, we don't mind the
shadowing it all --- it is *intentional* that the derived class only
provides a `run` method, and doesn't call any of the overridden `run`
methods.

Helps with https://github.com/NixOS/rfcs/pull/134
2023-03-15 16:29:07 -04:00
..
tests url: make percentEncode stricter, expose and unit test it 2023-02-27 15:30:00 +01:00
abstract-setting-to-json.hh reproducibility: hide non-reproducible settings from manual 2021-12-01 17:25:58 +01:00
ansicolor.hh Change warnings from yellow to magenta 2021-09-14 10:42:29 +02:00
archive.cc Remove FormatOrString and remaining uses of format() 2023-03-02 15:57:54 +01:00
archive.hh Trivial changes from the lazy-trees branch 2022-12-07 14:06:34 +01:00
args.cc Completions::add use libutil trim() 2023-02-10 22:17:09 +01:00
args.hh Make command infra less stateful and more regular 2023-03-15 16:29:07 -04:00
callback.hh
canon-path.cc Add CanonPath wrapper to represent canonicalized paths 2022-12-12 19:57:32 +01:00
canon-path.hh Fix CanonPath::dirOf() returning a string_view of a temporary 2022-12-23 15:32:54 +01:00
cgroup.cc Clean up cgroup handling in getMaxCPU() 2022-12-02 12:59:13 +01:00
cgroup.hh Clean up cgroup handling in getMaxCPU() 2022-12-02 12:59:13 +01:00
chunked-vector.hh Fix the darwin build 2022-04-22 10:56:56 +02:00
closure.hh Extract a generic computeClosure function 2021-05-19 11:44:58 +02:00
comparator.hh Recursively substitute the realisations 2021-05-26 18:44:17 +02:00
compression.cc Get rid of std::shared_ptr<std::string> and ref<std::string> 2022-01-18 11:12:30 +01:00
compression.hh Get rid of std::shared_ptr<std::string> and ref<std::string> 2022-01-18 11:12:30 +01:00
compute-levels.cc Add x86_64 compute levels as additional system types 2021-02-22 09:11:15 +01:00
compute-levels.hh Add x86_64 compute levels as additional system types 2021-02-22 09:11:15 +01:00
config.cc Fix C++20 warnings 2022-10-22 15:16:46 +01:00
config.hh Fix C++20 warnings 2022-10-22 15:16:46 +01:00
error.cc Revert "Revert "Merge pull request #6204 from layus/coerce-string"" 2023-01-19 13:23:04 +01:00
error.hh Fix 'destructor called on non-final ...' warning 2023-01-24 16:37:50 +01:00
experimental-features.cc unsafeDiscardReferences 2023-01-03 17:19:16 +01:00
experimental-features.hh unsafeDiscardReferences 2023-01-03 17:19:16 +01:00
filesystem.cc Remove FormatOrString and remaining uses of format() 2023-03-02 15:57:54 +01:00
finally.hh make Finally more local 2022-03-09 00:16:50 +01:00
fmt.hh Remove FormatOrString and remaining uses of format() 2023-03-02 15:57:54 +01:00
git.cc Fix the parsing of the sourcehut refs file 2022-05-04 14:38:59 +02:00
git.hh Fix the parsing of the sourcehut refs file 2022-05-04 14:38:59 +02:00
hash.cc don't assume that rev is a SHA1 hash 2022-04-07 19:49:47 +02:00
hash.hh don't assume that rev is a SHA1 hash 2022-04-07 19:49:47 +02:00
hilite.cc Fix incorrect comment in hiliteMatches 2022-06-05 20:30:18 +02:00
hilite.hh Move hiliteMatches into a separate header 2022-04-21 12:06:29 +02:00
json-impls.hh Remove default constructor from OutputsSpec 2023-01-11 19:08:19 -05:00
json-utils.hh Move json stuff out of util.cc 2022-05-04 11:22:06 +02:00
local.mk Remove the explicit c++fs linkage on darwin 2022-08-08 14:34:22 +02:00
logging.cc Remove FormatOrString and remaining uses of format() 2023-03-02 15:57:54 +01:00
logging.hh Remove FormatOrString and remaining uses of format() 2023-03-02 15:57:54 +01:00
lru-cache.hh
monitor-fd.hh MonitorFdHup: Make it work on macOS again 2023-01-11 10:48:40 -08:00
namespaces.cc Simplify the PID namespace check: just try to mount /proc 2023-02-10 14:38:14 +01:00
namespaces.hh Simplify the PID namespace check: just try to mount /proc 2023-02-10 14:38:14 +01:00
pool.hh
ref.hh Trivial changes from the lazy-trees branch 2022-12-07 14:06:34 +01:00
regex-combinators.hh Test store paths, with property tests 2023-01-23 07:05:50 -05:00
serialise.cc Revert "Disable GC during coroutine execution + test" 2023-03-08 20:47:52 +01:00
serialise.hh Revert "Disable GC during coroutine execution + test" 2023-03-08 20:47:52 +01:00
split.hh
suggestions.cc Implement operator<< for Suggestions 2022-03-07 10:09:10 +01:00
suggestions.hh Merge or-suggestions.hh into suggestions.hh 2022-03-07 17:49:02 +01:00
sync.hh
tarfile.cc tarfile: set directory mode to at least 0500, don't extract fflags 2022-11-01 16:01:38 +01:00
tarfile.hh Revert "TarArchive: Small refactoring" 2022-03-24 22:30:46 +01:00
thread-pool.cc Remove CPU locking 2021-12-22 15:56:25 +01:00
thread-pool.hh
topo-sort.hh Add missing #pragma once 2020-10-06 10:40:07 +02:00
types.hh Move ChunkedVector to its own header 2022-04-22 10:01:02 +02:00
url-parts.hh Allow empty path segments in urls 2022-04-08 16:09:49 +02:00
url.cc url: make percentEncode stricter, expose and unit test it 2023-02-27 15:30:00 +01:00
url.hh url: make percentEncode stricter, expose and unit test it 2023-02-27 15:30:00 +01:00
util.cc Fix macOS warning 2023-03-13 05:31:03 -07:00
util.hh Merge pull request #7918 from zimbatm/fix-empty-nix-store-env 2023-03-03 13:49:56 +01:00
xml-writer.cc xml-writer: Remove std aliases 2022-02-25 16:13:02 +01:00
xml-writer.hh xml-writer: Remove std aliases 2022-02-25 16:13:02 +01:00