summary refs log tree commit diff
path: root/pkgs/development/compilers/graalvm/community-edition/update.nix
blob: e9d32cf7bdeb5564f8fe2dcfbfe5599fa5b7b166 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{ javaVersion
, graalVersion
, config
, sourcesFilename
, name
, lib
, writeScript
, jq
, runtimeShell
, gawk
}:

/*
  How to  use:
  run `nix-shell maintainers/scripts/update.nix --argstr package graalvmXX-ce`
  to update the graalvmXX-ce-sources.json file.
  E.g: nix-shell maintainers/scripts/update.nix --argstr package graalvm17-ce

  Basic idea:
  If we know the platform, product, javaVersion and graalVersion
  we can create the url. This leads to the following json structure:
  {
  "platform/arch1": {
  "product1|javaVersion|graalVersion": { "sha256": "...", "url": "..."},
  "product2|javaVersion|graalVersion": { "sha256": "...", "url": "..."},
  ...
  },
  "platform/arch2": {
  ...
  }
  }
*/

let
  productJavaVersionGraalVersionSep = "|";

  # isDev :: String -> Boolean
  isDev = version:
    lib.hasInfix "dev" version;

  # getLatestVersion :: String -> String
  getLatestVersion = currentVersion:
    let
      dev = if isDev currentVersion then "dev-" else "";
      url = "https://api.github.com/repos/graalvm/graalvm-ce-${dev}builds/releases/latest";
      file = builtins.fetchurl url;
      json = builtins.fromJSON (builtins.readFile file);
    in
    lib.removePrefix "vm-" json.tag_name;

  # getArchString :: String -> String
  getArchString = nixArchString:
    {
      "aarch64-linux" = "linux-aarch64";
      "x86_64-linux" = "linux-amd64";
      "x86_64-darwin" = "darwin-amd64";
    }.${nixArchString};


  # getProductSuffix :: String -> String
  getProductSuffix = productName:
    {
      "graalvm-ce" = ".tar.gz";
      "native-image-installable-svm" = ".jar";
      "ruby-installable-svm" = ".jar";
      "wasm-installable-svm" = ".jar";
      "python-installable-svm" = ".jar";
    }.${productName};

  # getProductSuffix :: String -> String
  getProductBaseUrl = productName:
    {
      "graalvm-ce" = "https://github.com/graalvm/graalvm-ce-builds/releases/download";
      "native-image-installable-svm" = "https://github.com/graalvm/graalvm-ce-builds/releases/download";
      "ruby-installable-svm" = "https://github.com/oracle/truffleruby/releases/download";
      "wasm-installable-svm" = "https://github.com/graalvm/graalvm-ce-builds/releases/download";
      "python-installable-svm" = "https://github.com/graalvm/graalpython/releases/download";
    }.${productName};

  # getDevUrl :: String
  getDevUrl = { arch, graalVersion, product, javaVersion }:
    let
      baseUrl = https://github.com/graalvm/graalvm-ce-dev-builds/releases/download;
    in
    "${baseUrl}/${graalVersion}/${product}-${javaVersion}-${arch}-dev${getProductSuffix product}";

  # getReleaseUrl :: AttrSet -> String
  getReleaseUrl = { arch, graalVersion, product, javaVersion }:
    let baseUrl = getProductBaseUrl product;
    in
    "${baseUrl}/vm-${graalVersion}/${product}-${javaVersion}-${arch}-${graalVersion}${getProductSuffix product}";

  # getUrl :: AttrSet -> String
  getUrl = args@{ arch, graalVersion, product, javaVersion }:
    if isDev graalVersion
    then getDevUrl args
    else getReleaseUrl args;

  # computeSha256 :: String -> String
  computeSha256 = url:
    builtins.hashFile "sha256" (builtins.fetchurl url);

  # downloadSha256 :: String -> String
  downloadSha256 = url:
    let sha256Url = url + ".sha256";
    in
    builtins.readFile (builtins.fetchurl sha256Url);

  # getSha256 :: String -> String -> String
  getSha256 = graalVersion: url:
    if isDev graalVersion
    then computeSha256 url
    else downloadSha256 url;

  # cartesianZipListsWith :: (a -> b -> c) -> [a] -> [b] -> [c]
  cartesianZipListsWith = f: fst: snd:
    let
      cartesianProduct = lib.cartesianProductOfSets { a = fst; b = snd; };
      fst' = builtins.catAttrs "a" cartesianProduct;
      snd' = builtins.catAttrs "b" cartesianProduct;
    in
    lib.zipListsWith f fst' snd';

  # zipListsToAttrs :: [a] -> [b] -> AttrSet
  zipListsToAttrs = names: values:
    lib.listToAttrs (
      lib.zipListsWith (name: value: { inherit name value; }) names values
    );

  # genProductJavaVersionGraalVersionAttrSet :: String -> AttrSet
  genProductJavaVersionGraalVersionAttrSet = product_javaVersion_graalVersion:
    let
      attrNames = [ "product" "javaVersion" "graalVersion" ];
      attrValues = lib.splitString productJavaVersionGraalVersionSep product_javaVersion_graalVersion;
    in
    zipListsToAttrs attrNames attrValues;

  # genUrlAndSha256 :: String -> String -> AttrSet
  genUrlAndSha256 = arch: product_javaVersion_graalVersion:
    let
      productJavaVersionGraalVersion =
        (genProductJavaVersionGraalVersionAttrSet product_javaVersion_graalVersion)
        // { inherit arch; };
      url = getUrl productJavaVersionGraalVersion;
      sha256 = getSha256 productJavaVersionGraalVersion.graalVersion url;
    in
    {
      ${arch} = {
        ${product_javaVersion_graalVersion} = {
          inherit sha256 url;
        };
      };
    };

  # genArchProductVersionPairs :: String -> AttrSet -> [AttrSet]
  genArchProductVersionList = javaGraalVersion: archProducts:
    let
      arch = archProducts.arch;
      products = archProducts.products;
      productJavaGraalVersionList =
        cartesianZipListsWith (a: b: a + productJavaVersionGraalVersionSep + b)
          products [ javaGraalVersion ];
    in
    cartesianZipListsWith (genUrlAndSha256) [ arch ] productJavaGraalVersionList;


  # genSources :: String -> String -> AttrSet -> Path String
  genSources = graalVersion: javaVersion: config:
    let
      javaGraalVersion = javaVersion + productJavaVersionGraalVersionSep + graalVersion;
      archProducts = builtins.attrValues config;
      sourcesList = builtins.concatMap (genArchProductVersionList javaGraalVersion) archProducts;
      sourcesAttr = builtins.foldl' (lib.recursiveUpdate) { } sourcesList;
    in
    builtins.toFile "sources.json" (builtins.toJSON sourcesAttr);

  # isNew :: String -> String -> Boolean
  isNew = newVersion: currentVersion:
    {
      "-1" = false;
      "0" = false;
      "1" = true;
    }.${builtins.toString (builtins.compareVersions newVersion currentVersion)};

  newVersion = getLatestVersion graalVersion;
  sourcesJson = genSources newVersion javaVersion config;
  sourcesJsonPath = lib.strings.escapeShellArg ././${sourcesFilename};
  defaultNixPath = lib.strings.escapeShellArg ././default.nix;

  /*
    updateScriptText :: String -> String -> String
    Writes the json file, finds the line number of the current derivation
    name, which wants to update with awk and replace the first version match
    after that line.
  */
  updateScriptText = newVersion: currentVersion:
    if isNew newVersion currentVersion
    then
      ''
        echo "New version found. Updating ${currentVersion} -> ${newVersion}".
        export PATH="${lib.makeBinPath [ jq gawk ]}:$PATH"
        jq . ${sourcesJson} > ${sourcesJsonPath}
        drvName=$(awk '/${name}/{ print NR; exit }' ${defaultNixPath})
        awk -v drvName="$drvName" -i inplace \
        'NR>drvName {sub(/${graalVersion}/, "${newVersion}")} 1' ${defaultNixPath}
      ''
    else ''echo "No new version found. Skip updating."'';

in
writeScript "update-graal.sh" ''
  #!${runtimeShell}
  set -o errexit
  set -o nounset
  set -o pipefail

  ${updateScriptText newVersion graalVersion}
''