aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/build.lux
blob: 74f64cb59c14d3f791091d297c594d59468f7cb0 (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
(.module:
  [lux (#- Name)
   ["." host (#+ import:)]
   [abstract
    [monad (#+ Monad do)]]
   [control
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]
    ["." io (#+ IO)]
    [concurrency
     ["." promise (#+ Promise) ("#@." monad)]]]
   [data
    ["." product]
    ["." maybe]
    ["." text ("#@." equivalence)
     ["%" format (#+ format)]]
    [collection
     ["." list ("#@." functor)]
     ["." dictionary]]]
   [world
    ["." file (#+ Path)]]]
  ["." // #_
   ["#" project]
   ["#." action (#+ Action)]
   ["#." local]
   ["#." artifact (#+ Group Name Artifact)]
   ["#." dependency (#+ Dependency Resolution)]
   ["#." shell]])

(type: #export (Command a)
  (-> //.Project (Action a)))

(type: Finder
  (-> Resolution (Maybe Dependency)))

(def: (dependency-finder group name)
  (-> Group Name Finder)
  (|>> dictionary.entries
       (list.search (function (_ [dependency package])
                      (if (and (text@= group (get@ [#//dependency.artifact #//artifact.group] dependency))
                               (text@= name (get@ [#//dependency.artifact #//artifact.name] dependency)))
                        (#.Some dependency)
                        #.None)))))

(def: lux-group
  Group
  "com.github.luxlang")

(template [<name> <finder>]
  [(def: <finder>
     Finder
     (..dependency-finder ..lux-group <name>))]

  ["lux-jvm" jvm-compiler]
  ["lux-js" js-compiler]
  )

(exception: #export no-available-compiler)
(exception: #export no-specified-program)

(type: Compiler
  (#JVM Artifact)
  (#JS Artifact))

(def: (remove-dependency dependency)
  (-> Dependency (-> Resolution Resolution))
  (|>> dictionary.entries
       (list.filter (|>> product.left (is? dependency) not))
       (dictionary.from-list //dependency.hash)))

(def: (compiler resolution)
  (-> Resolution (Try [Resolution Compiler]))
  (case [(..jvm-compiler resolution)
         (..js-compiler resolution)]
    [(#.Some dependency) _]
    (#try.Success [(..remove-dependency dependency resolution)
                   (#JVM (get@ #//dependency.artifact dependency))])
    
    [_ (#.Some dependency)]
    (#try.Success [(..remove-dependency dependency resolution)
                   (#JS (get@ #//dependency.artifact dependency))])

    _
    (exception.throw ..no-available-compiler [])))

(def: libraries
  (-> Resolution (List Path))
  (|>> dictionary.keys
       (list.filter (|>> (get@ #//dependency.type) (text@= //dependency.lux-library)))
       (list@map (|>> (get@ #//dependency.artifact) (//local.path file.system)))))

(import: #long java/lang/String)

## https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
(import: #long java/lang/System
  (#static getProperty [java/lang/String] #io #? java/lang/String))

(def: working-directory
  (IO (Try Text))
  (do io.monad
    [?value (java/lang/System::getProperty "user.dir")]
    (wrap (#try.Success (maybe.default "~" ?value)))))

(def: (singular-parameter name value)
  (-> Text Text Text)
  (format name " " value))

(def: (plural-parameter name values)
  (-> Text (List Text) Text)
  (|> values (list@map (|>> (format name " "))) (text.join-with " ")))

(def: #export (do! project)
  (Command Any)
  (case (get@ #//.program project)
    (#.Some program)
    (do //action.monad
      [cache (//local.all-cached (file.async file.system)
                                 (get@ #//.dependencies project)
                                 //dependency.empty)
       resolution (promise.future
                   (//dependency.resolve-all (get@ #//.repositories project)
                                             (get@ #//.dependencies project)
                                             cache))
       _ (//local.cache-all (file.async file.system)
                            resolution)
       [resolution compiler] (promise@wrap (..compiler resolution))
       working-directory (promise.future ..working-directory)
       #let [libraries (..libraries resolution)
             prefix (case compiler
                      (#JVM artifact) (format "java -jar " (//local.path file.system artifact))
                      (#JS artifact) (format "node --stack_size=8192 " (//local.path file.system artifact)))
             cache-directory (format working-directory (:: file.system separator) (get@ #//.target project))
             command (format prefix " build"
                             " " (..plural-parameter "--library" libraries)
                             " " (..plural-parameter "--source" (get@ #//.sources project))
                             " " (..singular-parameter "--target" cache-directory)
                             " " (..singular-parameter "--module" program))]
       #let [_ (log! "[BUILD STARTED]")]
       outcome (//shell.execute command working-directory)
       #let [_ (log! "[BUILD END]")]]
      (wrap []))

    #.None
    (promise@wrap (exception.throw ..no-specified-program []))))