aboutsummaryrefslogtreecommitdiff
path: root/vendor/gen-vendor.py
blob: a38da61dbb4cb5600e9d6165b955aad44d6de6eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
import subprocess
from os.path import exists
import re

# tee the output of the given command
def run(cmd):
    proc = subprocess.Popen(cmd,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE, encoding='utf-8')

    outp = ""
    while proc.poll() is None:
        line = proc.stdout.readline()
        if line:
            print(line.strip())
            outp += line
    return outp

pkgs = run(["raco", "pkg", "show", "-a", "--scope" ,"user"]).split("\n")
catalog_pkg_names = [l.split(" ")[0].replace("*", "") for l in pkgs if "catalog" in l]

print("Found %d packages" % len(catalog_pkg_names))

infos = []
for pkg in catalog_pkg_names:
    info = run(["raco", "pkg", "catalog-show", pkg])
    m = re.search(r'Source: (.*)', info)
    url = m.group(1)
    fname = url.split("/")[-1]
    if not exists(fname):
        run(["curl", "-OL", url])
    infos.append((pkg, url, run(["nix", "hash", "file", fname]).strip()))

# fetch files and install packages
configurePhase = "mkdir -p $out\n" + "\n".join([
    "raco pkg install --scope-dir $out -t dir -n %s --no-setup --deps force ${fetchurl { url = \"%s\"; hash = \"%s\"; }}" % (name, url, hash)
    for (name, url, hash) in infos
])

# do building
buildPhase = "raco pkg install --scope-dir $out --skip-installed " + " ".join(catalog_pkg_names)

with open("./default.nix", "w") as f:
    f.write("""
{pkgs ? import <nixpkgs> {}, ...}: let
  inherit (pkgs) fetchurl;
  collection = pkgs.stdenv.mkDerivation {
    name = "raco-catalog";
    buildInputs = [pkgs.racket-minimal];
    dontUnpack = true;

    configurePhase = ''
      %s
    '';
    buildPhase = "%s";
  }; in
    pkgs.stdenv.mkDerivation {
      name = "racket-wrapped";
      installPhase = ''
        mkdir -p $out/bin
        ln -s $out/pkgs ${collection};
        mkWrapper ${pkgs.racket-minimal}/bin/racket \
                  $out/bin/racket \
                  --set PLTADDONDIR $out/pkgs;
      '';
    }
    """ % (configurePhase, buildPhase))