about summary refs log tree commit diff
path: root/external
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-12T23·26+0000
committerVincent Ambo <mail@tazj.in>2019-12-13T00·39+0000
commit875628a0978937d708ce9365c9cc2061f39b0bca (patch)
tree09f167567bc52f62cac38fe474f67462608b9b04 /external
parentf5e3183de14e73ab6ff0ada0a6d2e9408a5280be (diff)
feat(external): Switch between packages & programs automatically
Diffstat (limited to 'external')
-rw-r--r--external/default.nix28
-rw-r--r--external/main.go2
2 files changed, 21 insertions, 9 deletions
diff --git a/external/default.nix b/external/default.nix
index a065ab3331aa..5854d4f0277f 100644
--- a/external/default.nix
+++ b/external/default.nix
@@ -3,7 +3,7 @@
 { pkgs, program, package }:
 
 let
-  inherit (builtins) foldl'fromJSON head readFile replaceStrings tail throw;
+  inherit (builtins) elemAt foldl' fromJSON head length readFile replaceStrings tail throw;
   inherit (pkgs) lib runCommand go jq ripgrep;
 
   pathToName = p: replaceStrings ["/"] ["_"] (toString p);
@@ -36,14 +36,24 @@ let
     if path == [] then { gopkg = value; }
     else { "${head path}" = mkset (tail path) value; };
 
-  toPackage = self: src: path: entry: package {
-    name = pathToName entry.name entry.name;
-    path = lib.concatStringsSep "/" ([ path ] ++ entry.locator);
-    srcs = map (f: src + ("/" + f)) entry.files;
-    deps = map (d: lib.attrByPath (d ++ [ "gopkg" ]) (
-      throw "missing local dependency '${lib.concatStringsSep "." d}' in '${path}'"
-    ) self) entry.localDeps;
-  };
+  last = l: elemAt l ((length l) - 1);
+
+  toPackage = self: src: path: entry:
+    let
+      args = {
+        srcs = map (f: src + ("/" + f)) entry.files;
+        deps = map (d: lib.attrByPath (d ++ [ "gopkg" ]) (
+          throw "missing local dependency '${lib.concatStringsSep "." d}' in '${path}'"
+        ) self) entry.localDeps;
+      };
+      libArgs = args // {
+        name = pathToName entry.name;
+        path = lib.concatStringsSep "/" ([ path ] ++ entry.locator);
+      };
+      binArgs = args // {
+        name = last ([ path ] ++ entry.locator);
+      };
+    in if entry.isCommand then (program binArgs) else (package libArgs);
 
 in { src, path, deps ? [] }: let
   name = pathToName path;
diff --git a/external/main.go b/external/main.go
index aac966172b34..23fd53326ed0 100644
--- a/external/main.go
+++ b/external/main.go
@@ -34,6 +34,7 @@ type pkg struct {
 	Files       []string   `json:"files"`
 	LocalDeps   [][]string `json:"localDeps"`
 	ForeignDeps []string   `json:"foreignDeps"`
+	IsCommand   bool       `json:"isCommand"`
 }
 
 // findGoDirs returns a filepath.WalkFunc that identifies all
@@ -116,6 +117,7 @@ func analysePackage(root, source, importpath string, stdlib map[string]bool) (pk
 		Files:       files,
 		LocalDeps:   local,
 		ForeignDeps: foreign,
+		IsCommand:   p.IsCommand(),
 	}, nil
 }