diff --git a/instantiated.go b/instantiated.go index 3014eb7..6b64b01 100644 --- a/instantiated.go +++ b/instantiated.go @@ -108,12 +108,10 @@ func (d *InstantiatedDecoder) Drv() iter.Seq[string] { } } -// DecodeInstantiated interprets the verbose output of `nix build` and returns a slice of all instantiated derivations. -func DecodeInstantiated(r io.Reader) ([]string, error) { - decoder := NewInstantiatedDecoder(r) - +// Decode collects and deduplicates derivations emitted by [InstantiatedDecoder]. +func (d *InstantiatedDecoder) Decode() ([]string, error) { instantiated := make([]string, 0, instantiatedInitialCap) - for drv := range decoder.Drv() { + for drv := range d.Drv() { if len(instantiated) == cap(instantiated) { // grow the buffer exponentially to minimise copies instantiated = slices.Grow(instantiated, cap(instantiated)<<1) @@ -122,12 +120,11 @@ func DecodeInstantiated(r io.Reader) ([]string, error) { } slices.Sort(instantiated) - return slices.Compact(instantiated), decoder.Err() - + return slices.Compact(instantiated), d.Err() } // EvalInstantiated evaluates the installable and returns all derivations instantiated during the evaluation. -// This relies on verbose output of `nix build` and is certainly not the correct way to do it, but so far it works +// This interprets verbose output of `nix build` and is certainly not the correct way to do it, but so far it works // significantly better than `nix-store -qR` and there does not appear to be a better way. func EvalInstantiated(ctx context.Context, installable string) ([]string, error) { c, cancel := context.WithCancel(ctx) @@ -153,7 +150,7 @@ func EvalInstantiated(ctx context.Context, installable string) ([]string, error) return nil, err } - instantiated, decodeErr := DecodeInstantiated(stderr) + instantiated, decodeErr := NewInstantiatedDecoder(stderr).Decode() waitErr := cmd.Wait() return instantiated, errors.Join(decodeErr, waitErr) } diff --git a/instantiated_test.go b/instantiated_test.go index accc054..ecb4c36 100644 --- a/instantiated_test.go +++ b/instantiated_test.go @@ -50,7 +50,7 @@ func TestDecodeInstantiated(t *testing.T) { } stderr := strings.NewReader(tc.out) - got, err := nixbuild.DecodeInstantiated(stderr) + got, err := nixbuild.NewInstantiatedDecoder(stderr).Decode() if !errors.Is(err, tc.wantErr) { t.Fatalf("DecodeInstantiated: error = %v, want %v", err, tc.wantErr) } @@ -146,7 +146,7 @@ func BenchmarkDecodeInstantiated(b *testing.B) { stderr := strings.NewReader(pluiedevPappardelleOut) var v []string for i := 0; i < b.N; i++ { - v, _ = nixbuild.DecodeInstantiated(stderr) + v, _ = nixbuild.NewInstantiatedDecoder(stderr).Decode() runtime.KeepAlive(v) } } @@ -155,7 +155,7 @@ func BenchmarkDecodeInstantiatedCopy(b *testing.B) { stderr := strings.NewReader(getchooAtlasOut) var v []string for i := 0; i < b.N; i++ { - v, _ = nixbuild.DecodeInstantiated(stderr) + v, _ = nixbuild.NewInstantiatedDecoder(stderr).Decode() runtime.KeepAlive(v) } }