Skip to content

Commit

Permalink
first candidate sources
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Dec 31, 2023
1 parent 9b4b1b1 commit 573d565
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 14 deletions.
122 changes: 117 additions & 5 deletions common/nix_candidate_source.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package common

import "runtime"
import (
"fmt"
"os"
"runtime"
"strings"

"go.spiff.io/expand"

g "github.com/zyedidia/generic"
"github.com/zyedidia/generic/hashset"
"mvdan.cc/sh/v3/syntax"
)

type NixCandidateSource struct {
fs Filesystem
sources map[string]*PathSetIn
key string
}

func NewNixCandidateSource(fs Filesystem) CandidateSource {
return &NixCandidateSource{
fs,
map[string]*PathSetIn{},
func NewNixCandidateSource(fs Filesystem, key string) CandidateSource {
res := &NixCandidateSource{
fs: fs,
sources: map[string]*PathSetIn{},
key: key,
}
res.precompute()
return res
}

func (s *NixCandidateSource) WhereSet(somePath string) *PathSetIn {
Expand All @@ -21,3 +36,100 @@ func (s *NixCandidateSource) WhereSet(somePath string) *PathSetIn {
normalizedPath := s.fs.GetAbsolutePath(somePath)
return s.sources[normalizedPath]
}

func (s *NixCandidateSource) precompute() {
ForEachKnownPath(func(originalSource, expandedSource string) {
// try getting the contents
input, err := readAllText(expandedSource)
if err != nil {
return
}

// parse
r := strings.NewReader(input)
f, err := syntax.NewParser().Parse(r, "")
if err != nil {
return
}

syntax.Walk(f, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.Assign:
if s.key == x.Name.Value {
harvestedPaths := s.harvestPaths(input[x.Value.Pos().Offset():x.Value.End().Offset()])
for _, harvestedPath := range harvestedPaths {
foundNormalizedPath := s.fs.GetAbsolutePath(harvestedPath)
sourcesForPath := s.sources[foundNormalizedPath]
if sourcesForPath == nil {
sourcesForPath = &PathSetIn{
What: Location{harvestedPath, foundNormalizedPath},
WhereSet: []Location{},
}
}
sourcesForPath.WhereSet = appendIfNotInSlice(
sourcesForPath.WhereSet,
Location{
originalSource,
expandedSource,
}, func(a, b Location) bool {
return a.Expanded == b.Expanded && a.Original == b.Original
})
s.sources[foundNormalizedPath] = sourcesForPath
}
}
}
return true
})
})
}

// input is some path definition
func (s *NixCandidateSource) harvestPaths(input string) []string {
input = strings.TrimSpace(input)
input = strings.Trim(input, fmt.Sprintf("%v\"'", os.PathListSeparator))
inputExpanded := expand.Expand(input, func(key string) (value string, ok bool) {
// do not expand the desired variable itself
if key == s.key {
return "", false
}
return os.LookupEnv(key)
})
all := strings.Split(inputExpanded, fmt.Sprintf("%c", os.PathListSeparator))
res := hashset.New[string](uint64(len(all)), g.Equals[string], g.HashString)
for _, p := range all {
if p != "" {
res.Put(p)
}
}
r := []string{}
res.Each(func(key string) {
r = append(r, key)
})
return r
}

func readAllText(filename string) (string, error) {
b, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(b), nil
}

func appendIfNotInSlice[T any](input []T, value T, eq func(T, T) bool) []T {
res := input
found := false

for _, v := range res {
if eq(v, value) {
found = true
break
}
}

if !found {
res = append(res, value)
}

return res
}
2 changes: 1 addition & 1 deletion common/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewCustomResultsCalculator(fs Filesystem, source ValueSource, candidateSour
}

func NewResultsCalculator(fs Filesystem, source ValueSource) *ResultsCalculator {
return NewCustomResultsCalculator(fs, source, NewNixCandidateSource(fs))
return NewCustomResultsCalculator(fs, source, NewNixCandidateSource(fs, source.Source()))
}

func (r *ResultsCalculator) CalculateResults() ([]ResultRow, error) {
Expand Down
3 changes: 3 additions & 0 deletions common/value_source.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package common

type ValueSource interface {
// Source is the name/key of the value
Source() string
// Orig is the original string value
Orig() string
// Values is the split list from original value
Values() []string
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ require (
github.com/stretchr/testify v1.8.4
github.com/zyedidia/generic v1.2.1
go.spiff.io/expand v1.1.0
mvdan.cc/sh/v3 v3.7.0
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -29,7 +29,6 @@ require (
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.10.1-0.20230524175051-ec119421bb97 // indirect
github.com/segmentio/fasthash v1.0.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.1 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jedib0t/go-pretty/v6 v6.4.9 h1:vZ6bjGg2eBSrJn365qlxGcaWu09Id+LHtrfDWlB2Usc=
github.com/jedib0t/go-pretty/v6 v6.4.9/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
Expand All @@ -39,17 +38,14 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.1-0.20230524175051-ec119421bb97 h1:3RPlVWzZ/PDqmVuf/FKHARG5EMid/tl7cv54Sw/QRVY=
github.com/rogpeppe/go-internal v1.10.1-0.20230524175051-ec119421bb97/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
Expand Down Expand Up @@ -90,3 +86,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mvdan.cc/sh/v3 v3.7.0 h1:lSTjdP/1xsddtaKfGg7Myu7DnlHItd3/M2tomOcNNBg=
mvdan.cc/sh/v3 v3.7.0/go.mod h1:K2gwkaesF/D7av7Kxl0HbF5kGOd2ArupNTX3X44+8l8=

0 comments on commit 573d565

Please sign in to comment.