about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-12T23·00+0000
committerVincent Ambo <mail@tazj.in>2019-12-13T00·39+0000
commitc5373a69fe440bada29f7e2fc41882890f7d0872 (patch)
treebdb04f08d03f285cd2a76f56f97c094499be3567
parentbbf3a418a5a119e2ab6aaaf1e04f42caa1b683a8 (diff)
feat(external): Implement builder function for externals
Implements a builder function that calls the analysis tool on the
provided source and builds up the required attribute set, including
local dependencies.
-rw-r--r--external/default.nix31
1 files changed, 29 insertions, 2 deletions
diff --git a/external/default.nix b/external/default.nix
index 79d559c05498..a065ab3331aa 100644
--- a/external/default.nix
+++ b/external/default.nix
@@ -1,8 +1,13 @@
 # Copyright 2019 Google LLC.
 # SPDX-License-Identifier: Apache-2.0
-{ runCommand, go, jq, ripgrep, program }:
+{ pkgs, program, package }:
 
 let
+  inherit (builtins) foldl'fromJSON head readFile replaceStrings tail throw;
+  inherit (pkgs) lib runCommand go jq ripgrep;
+
+  pathToName = p: replaceStrings ["/"] ["_"] (toString p);
+
   # Collect all non-vendored dependencies from the Go standard library
   # into a file that can be used to filter them out when processing
   # dependencies.
@@ -26,4 +31,26 @@ let
       "main.stdlibList" = "${stdlibPackages}";
     };
   };
-in analyser
+
+  mkset = path: value:
+    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;
+  };
+
+in { src, path, deps ? [] }: let
+  name = pathToName path;
+  analysisOutput = runCommand "${name}-structure.json" {} ''
+    ${analyser}/bin/analyser -path ${path} -source ${src} > $out
+  '';
+  analysis = fromJSON (readFile analysisOutput);
+in lib.fix(self: foldl' lib.recursiveUpdate {} (
+  map (entry: mkset entry.locator (toPackage self src path entry)) analysis
+))