about summary refs log tree commit diff
path: root/external/main.go
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-12T03·46+0000
committerVincent Ambo <mail@tazj.in>2019-12-13T00·39+0000
commita5473293e78fc0b01def5c4c3f7e11854d88d8d0 (patch)
tree22321ca3b78495698738387cdfcdcfc03ba901af /external/main.go
parentfb4dd761461de12ccbc276431ea8aa37dcefa9b0 (diff)
feat(external): Return references in more useable format for Nix
Sub-packages of external dependencies are traversed by Nix as a tree
of attribute sets which need to be accessed by "path". To make this
easier, the dependency analyser now returns "paths" as string lists.
Diffstat (limited to 'external/main.go')
-rw-r--r--external/main.go36
1 files changed, 20 insertions, 16 deletions
diff --git a/external/main.go b/external/main.go
index 0f8834fc99c9..2e5ee4900b37 100644
--- a/external/main.go
+++ b/external/main.go
@@ -13,6 +13,7 @@ import (
 	"io/ioutil"
 	"log"
 	"os"
+	"path"
 	"path/filepath"
 	"strings"
 )
@@ -28,11 +29,10 @@ var stdlibList string
 // Return information includes the local (relative from project root)
 // and external (none-stdlib) dependencies of this package.
 type pkg struct {
-	Name        string   `json:"name"`
-	Source      string   `json:"source"`
-	Files       []string `json:"files"`
-	LocalDeps   []string `json:"localDeps"`
-	ForeignDeps []string `json:"foreignDeps"`
+	Name        []string   `json:"name"`
+	Files       []string   `json:"files"`
+	LocalDeps   [][]string `json:"localDeps"`
+	ForeignDeps []string   `json:"foreignDeps"`
 }
 
 // findGoDirs returns a filepath.WalkFunc that identifies all
@@ -80,7 +80,7 @@ func findGoDirs(at string) ([]string, error) {
 // analysePackage loads and analyses the imports of a single Go
 // package, returning the data that is required by the Nix code to
 // generate a derivation for this package.
-func analysePackage(root, source, path string, stdlib map[string]bool) (pkg, error) {
+func analysePackage(root, source, importpath string, stdlib map[string]bool) (pkg, error) {
 	ctx := build.Default
 
 	p, err := ctx.ImportDir(source, build.IgnoreVendor)
@@ -88,7 +88,7 @@ func analysePackage(root, source, path string, stdlib map[string]bool) (pkg, err
 		return pkg{}, err
 	}
 
-	local := []string{}
+	local := [][]string{}
 	foreign := []string{}
 
 	for _, i := range p.Imports {
@@ -96,17 +96,22 @@ func analysePackage(root, source, path string, stdlib map[string]bool) (pkg, err
 			continue
 		}
 
-		if strings.HasPrefix(i, path) {
-			local = append(local, i)
+		if strings.HasPrefix(i, importpath) {
+			local = append(local, strings.Split(strings.TrimPrefix(i, importpath+"/"), "/"))
 		} else {
 			foreign = append(foreign, i)
 		}
 	}
 
+	prefix := strings.TrimPrefix(source, root+"/")
+	files := []string{}
+	for _, f := range p.GoFiles {
+		files = append(files, path.Join(prefix, f))
+	}
+
 	analysed := pkg{
-		Name:        strings.TrimPrefix(source, root+"/"),
-		Source:      source,
-		Files:       p.GoFiles,
+		Name:        strings.Split(prefix, "/"),
+		Files:       files,
 		LocalDeps:   local,
 		ForeignDeps: foreign,
 	}
@@ -125,9 +130,8 @@ func loadStdlibPkgs(from string) (pkgs map[string]bool, err error) {
 }
 
 func main() {
-	// TODO(tazjin): Remove default values
-	source := flag.String("source", "/nix/store/fzp67ris29zg5zfs65z0q245x0fahgll-source", "path to directory with sources to process")
-	path := flag.String("path", "github.com/golang/protobuf", "import path for the package")
+	source := flag.String("source", "", "path to directory with sources to process")
+	path := flag.String("path", "", "import path for the package")
 
 	flag.Parse()
 
@@ -154,6 +158,6 @@ func main() {
 		all = append(all, analysed)
 	}
 
-	j, _ := json.MarshalIndent(all, "", "  ") // TODO: no indent
+	j, _ := json.Marshal(all)
 	fmt.Println(string(j))
 }