aboutsummaryrefslogtreecommitdiff
path: root/infra/libkookie/nixpkgs/pkgs/development/interpreters/clojurescript/lumo/default.nix
blob: 61fd94c09bf4fe958cfb4433b648b6266752a8a8 (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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{ stdenv
, lib
, fetchurl
, clojure
, gnutar
, nodejs
, jre
, unzip
, nodePackages
, xcbuild
, python
, openssl
, pkgs
, fetchgit
, darwin
}:
let
  version = "1.10.1";
  nodeVersion = "11.13.0";
  nodeSources = fetchurl {
    url = "https://nodejs.org/dist/v${nodeVersion}/node-v${nodeVersion}.tar.gz";
    sha256 = "1cjzjbshxnysxkvbf41p3m8298cnhs9kfvdczgvvvlp6w16x4aac";
  };
  lumo-internal-classpath = "LUMO__INTERNAL__CLASSPATH";

  # as found in cljs/snapshot/lumo/repl.cljs
  requireDeps = '' \
      cljs.analyzer \
      cljs.compiler \
      cljs.env \
      cljs.js \
      cljs.reader \
      cljs.repl \
      cljs.source-map \
      cljs.source-map.base64 \
      cljs.source-map.base64-vlq \
      cljs.spec.alpha \
      cljs.spec.gen.alpha \
      cljs.tagged-literals \
      cljs.tools.reader \
      cljs.tools.reader.reader-types \
      cljs.tools.reader.impl.commons \
      cljs.tools.reader.impl.utils \
      clojure.core.rrb-vector \
      clojure.core.rrb-vector.interop \
      clojure.core.rrb-vector.nodes \
      clojure.core.rrb-vector.protocols \
      clojure.core.rrb-vector.rrbt \
      clojure.core.rrb-vector.transients \
      clojure.core.rrb-vector.trees \
      clojure.string \
      clojure.set \
      clojure.walk \
      cognitect.transit \
      fipp.visit \
      fipp.engine \
      fipp.deque \
      lazy-map.core \
      lumo.pprint.data \
      lumo.repl \
      lumo.repl-resources \
      lumo.js-deps \
      lumo.common '';

  compileClojurescript = (simple: ''
    (require '[cljs.build.api :as cljs])
    (cljs/build \"src/cljs/snapshot\"
      {:optimizations      ${if simple then ":simple" else ":none"}
       :main               'lumo.core
       :cache-analysis     true
       :source-map         false
       :dump-core          false
       :static-fns         true
       :optimize-constants false
       :npm-deps           false
       :verbose            true
       :closure-defines    {'cljs.core/*target*       \"nodejs\"
                            'lumo.core/*lumo-version* \"${version}\"}
       :compiler-stats     true
       :process-shim       false
       :fn-invoke-direct   true
       :parallel-build     false
       :browser-repl       false
       :target             :nodejs
       :hashbang           false
       ;; :libs               [ \"src/cljs/bundled\" \"src/js\" ]
       :output-dir         ${if simple
  then ''\"cljstmp\"''
  else ''\"target\"''}
       :output-to          ${if simple
  then ''\"cljstmp/main.js\"''
  else ''\"target/deleteme.js\"'' }})
  ''
  );


  cacheToJsons = ''
    (import [java.io ByteArrayOutputStream FileInputStream])
    (require '[cognitect.transit :as transit]
             '[clojure.edn :as edn]
             '[clojure.string :as str])

    (defn write-transit-json [cache]
      (let [out (ByteArrayOutputStream. 1000000)
            writer (transit/writer out :json)]
        (transit/write writer cache)
        (.toString out)))

    (defn process-caches []
      (let [cache-aot-path      \"target/cljs/core.cljs.cache.aot.edn\"
            cache-aot-edn       (edn/read-string (slurp cache-aot-path))
            cache-macros-path   \"target/cljs/core\$macros.cljc.cache.json\"
            cache-macros-stream (FileInputStream. cache-macros-path)
            cache-macros-edn    (transit/read (transit/reader cache-macros-stream :json))
            caches              [[cache-aot-path cache-aot-edn]
                                 [cache-macros-path cache-macros-edn]]]
        (doseq [[path cache-edn] caches]
          (doseq [key (keys cache-edn)]
            (let [out-path (str/replace path #\"(\.json|\.edn)\$\"
                             (str \".\" (munge key) \".json\"))
                  tr-json  (write-transit-json (key cache-edn))]
              (spit out-path tr-json))))))

    (process-caches)
  '';

  trimMainJsEnd = ''
    (let [string (slurp \"target/main.js\")]
      (spit \"target/main.js\"
        (subs string  0 (.indexOf string \"cljs.nodejs={};\"))))
  '';


  cljdeps = import ./deps.nix { inherit pkgs; };
  classp = cljdeps.makeClasspaths {
    extraClasspaths = [ "src/js" "src/cljs/bundled" "src/cljs/snapshot" ];
  };


  getJarPath = jarName: (lib.findFirst (p: p.name == jarName) null cljdeps.packages).path.jar;
in
stdenv.mkDerivation {
  inherit version;
  pname = "lumo";

  src = fetchgit {
    url = "https://github.com/anmonteiro/lumo.git";
    rev = version;
    sha256 = "12agi6bacqic2wq6q3l28283badzamspajmajzqm7fbdl2aq1a4p";
  };

  buildInputs = [
    nodejs
    clojure
    jre
    unzip
    python
    openssl
    gnutar
    nodePackages."lumo-build-deps-../interpreters/clojurescript/lumo"
  ]
  ++ lib.optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [
    ApplicationServices
    xcbuild
  ]
  );

  patches = [ ./no_mangle.patch ./mkdir_promise.patch ];

  postPatch = ''
    substituteInPlace $NIX_BUILD_TOP/lumo/vendor/nexe/exe.js \
      --replace 'glob.sync(dir + "/*")' 'glob.sync(dir + "/../*")'
  '';

  buildPhase = ''
       # Copy over lumo-build-deps environment
    rm yarn.lock
    cp -rf ${nodePackages."lumo-build-deps-../interpreters/clojurescript/lumo"}/lib/node_modules/lumo-build-deps/* ./

    # configure clojure-cli
    mkdir ./.cpcache
    export CLJ_CONFIG=`pwd`
    export CLJ_CACHE=`pwd`/.cpcache

    # require more namespaces for cljs-bundle
    sed -i "s!ns lumo.core! \
              ns lumo.core  \
               (:require ${requireDeps}) \
               (:require-macros [clojure.template :as temp] \
                                [cljs.test :as test])!g" \
              ./src/cljs/snapshot/lumo/core.cljs

    # Step 1: compile clojurescript with :none and :simple
    ${clojure}/bin/clojure -Scp ${classp} -e "${compileClojurescript true}"
    ${clojure}/bin/clojure -Scp ${classp} -e "${compileClojurescript false}"
    cp -f cljstmp/main.js target/main.js
    ${clojure}/bin/clojure -Scp ${classp} -e "${trimMainJsEnd}"

    # Step 2: sift files
    unzip -o ${getJarPath "org.clojure/clojurescript"} -d ./target
    unzip -j ${getJarPath "org.clojure/clojure"} "clojure/template.clj" -d ./target/clojure
    unzip -o ${getJarPath "org.clojure/google-closure-library"} -d ./target
    unzip -o ${getJarPath "org.clojure/google-closure-library-third-party"} -d ./target
    unzip -o ${getJarPath "org.clojure/tools.reader"} -d ./target
    unzip -o ${getJarPath "org.clojure/test.check"} -d ./target
    cp -rf ./src/cljs/bundled/lumo/* ./target/lumo/
    cp -rf ./src/cljs/snapshot/lumo/repl.clj ./target/lumo/
    # cleanup
    mv ./target/main.js ./target/main
    rm ./target/*\.js
    mv ./target/main ./target/main.js
    rm ./target/AUTHORS
    rm ./target/LICENSE
    rm ./target/*.edn
    rm ./target/*.md
    rm -rf ./target/css
    rm -rf ./target/META-INF
    rm -rf ./target/com
    rm -rf ./target/cljs/build
    rm -rf ./target/cljs/repl
    rm  ./target/cljs/core\.cljs\.cache.aot\.json
    rm  ./target/cljs/source_map\.clj
    rm  ./target/cljs/repl\.cljc
    rm  ./target/cljs/externs\.clj
    rm  ./target/cljs/closure\.clj
    rm  ./target/cljs/util\.cljc
    rm  ./target/cljs/js_deps\.cljc
    rm  ./target/cljs/analyzer/utils\.clj
    rm  ./target/cljs/core/macros\.clj
    rm  ./target/cljs/compiler/api.clj
    rm  ./target/goog/test_module*
    rm  ./target/goog/transpile\.js
    rm  ./target/goog/base_*
    find ./target -type f -name '*.class' -delete
    find ./target -type d -empty -delete

    # Step 3: generate munged cache jsons
    ${clojure}/bin/clojure -Scp ${classp} -e "${cacheToJsons}"
    rm  ./target/cljs/core\$macros\.cljc\.cache\.json


    # Step 4: Bunde javascript
    NODE_ENV=production node scripts/bundle.js
    node scripts/bundleForeign.js

    # Step 5: Backup resources
    cp -R target resources_bak

    # Step 6: Package executeable 1st time
    # fetch node sources and copy to palce that nexe will find
    mkdir -p tmp/node/${nodeVersion}
    cp ${nodeSources} tmp/node/${nodeVersion}/node-${nodeVersion}.tar.gz
    tar -C ./tmp/node/${nodeVersion} -xf ${nodeSources} --warning=no-unknown-keyword
    mv ./tmp/node/${nodeVersion}/node-v${nodeVersion}/* ./tmp/node/${nodeVersion}/
    rm -rf ${lumo-internal-classpath}
    cp -rf target ${lumo-internal-classpath}
    node scripts/package.js ${nodeVersion}
    rm -rf target
    mv ${lumo-internal-classpath} target

    # Step 7: AOT Macros
    sh scripts/aot-bundle-macros.sh

    # Step 8: Package executeable 2nd time
    node scripts/package.js ${nodeVersion}
  '';

  dontStrip = true;

  installPhase = ''
    mkdir -p $out/bin
    cp build/lumo $out/bin
  '';

  meta = {
    description = "Fast, cross-platform, standalone ClojureScript environment";
    longDescription = ''
      Lumo is a fast, standalone ClojureScript REPL that runs on Node.js and V8.
      Thanks to V8's custom startup snapshots, Lumo starts up instantaneously,
      making it the fastest Clojure REPL in existence.
    '';
    homepage = "https://github.com/anmonteiro/lumo";
    license = stdenv.lib.licenses.epl10;
    maintainers = [ stdenv.lib.maintainers.hlolli ];
    platforms = stdenv.lib.platforms.linux ++ stdenv.lib.platforms.darwin;
  };
}