aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/command/build.lux
blob: c0f9566a8817d44f55e8c983c89782f39dd64290 (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
(.module:
  [library
   [lux (#- Name)
    [abstract
     [monad (#+ do)]]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]
     ["." io (#+ IO)]
     [concurrency
      ["." promise (#+ Promise) ("#\." monad)]]]
    [data
     ["." product]
     ["." maybe]
     ["." text ("#\." equivalence)
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." functor)]
      ["." dictionary]
      ["." set]]]
    [math
     [number
      ["i" int]]]
    [world
     ["." program (#+ Program)]
     ["." file (#+ Path)]
     ["." shell (#+ Exit Process Shell)]
     ["." console (#+ Console)]
     [net
      ["." uri]]]]]
  ["." /// #_
   ["#" profile]
   ["#." action]
   ["#." command (#+ Command)]
   ["#." local]
   ["#." repository]
   ["#." runtime]
   ["#." dependency (#+ Dependency)
    ["#/." resolution (#+ Resolution)]]
   ["#." artifact (#+ Group Name Artifact)
    ["#/." type]]])

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

(def: (dependency_finder group name)
  (-> Group Name Finder)
  (|>> dictionary.entries
       (list.one (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: #export lux_group
  Group
  "com.github.luxlang")

(template [<dependency> <definition>]
  [(def: #export <definition>
     Name
     <dependency>)]

  ["lux-jvm" jvm_compiler_name]
  ["lux-js" js_compiler_name]
  ["lux-python" python_compiler_name]
  ["lux-lua" lua_compiler_name]
  ["lux-ruby" ruby_compiler_name]
  )

(exception: #export no_available_compiler)
(exception: #export no_specified_program)

(type: #export Compiler
  (#JVM Dependency)
  (#JS Dependency)
  (#Python Dependency)
  (#Lua Dependency)
  (#Ruby Dependency))

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

(def: (compiler resolution compiler_dependency)
  (-> Resolution Dependency (Try [Resolution Compiler]))
  (let [[[compiler_group compiler_name compiler_version] compiler_type] compiler_dependency]
    (case (..dependency_finder compiler_group compiler_name resolution)
      (#.Some dependency)
      (case compiler_name
        (^template [<tag> <name>]
          [(^ (static <name>))
           (#try.Success [(..remove_dependency dependency resolution)
                          (<tag> dependency)])])
        ([#JVM ..jvm_compiler_name]
         [#JS ..js_compiler_name]
         [#Python ..python_compiler_name]
         [#Lua ..lua_compiler_name]
         [#Ruby ..ruby_compiler_name])
        
        _
        (exception.throw ..no_available_compiler []))
      
      _
      (exception.throw ..no_available_compiler []))))

(def: (path fs home dependency)
  (All [!] (-> (file.System !) Path Dependency Path))
  (let [/ (\ fs separator)
        artifact (get@ #///dependency.artifact dependency)]
    (|> artifact
        (///local.uri (get@ #///artifact.version artifact))
        (text.replace_all uri.separator /)
        (format home /)
        (text.suffix (format "." (get@ #///dependency.type dependency))))))

(def: (libraries fs home)
  (All [!] (-> (file.System !) Path Resolution (List Path)))
  (|>> dictionary.keys
       (list.filter (|>> (get@ #///dependency.type) (text\= ///artifact/type.lux_library)))
       (list\map (..path fs home))))

(def: (singular name)
  (-> Text Text (List Text))
  (|>> (list name)))

(def: (plural name)
  (-> Text (List Text) (List Text))
  (|>> (list\map (|>> (list name))) list.concat))

(def: #export start "[BUILD STARTED]")
(def: #export success "[BUILD ENDED]")
(def: #export failure "[BUILD FAILED]")

(template [<name> <capability>]
  [(def: #export (<name> console process)
     (-> (Console Promise) (Process Promise) (Promise (Try Any)))
     ## This is a very odd way of implementing this function.
     ## But it's written this way because the more straightforward way (i.e. by using (try.with promise.monad))
     ## eventually led to the function hanging/freezing.
     ## I'm not sure why it happened, but I got this weirder implementation to work.
     (let [[read! write!] (: [(Promise (Try Any))
                              (promise.Resolver (Try Any))]
                             (promise.promise []))
           _ (|> (\ process <capability> [])
                 (promise.await (function (recur ?line)
                                  (case ?line
                                    (#try.Failure error)
                                    (if (exception.match? shell.no_more_output error)
                                      (write! (#try.Success []))
                                      (promise.await write! (console.write_line error console)))
                                    
                                    (#try.Success line)
                                    (promise.await (function (_ outcome)
                                                     (case outcome
                                                       (#try.Failure error)
                                                       (write! (#try.Failure error))

                                                       (#try.Success _)
                                                       (promise.await recur
                                                                      (\ process <capability> []))))
                                                   (console.write_line line console)))))
                 io.run)]
       read!))]

  [log_output! read]
  [log_error! error]
  )

(def: #export (do! console program fs shell resolution)
  (-> (Console Promise) (Program Promise) (file.System Promise) (Shell Promise) Resolution (Command [Exit Compiler Path]))
  (function (_ profile)
    (let [target (get@ #///.target profile)]
      (case (get@ #///.program profile)
        #.None
        (promise\wrap (exception.throw ..no_specified_program []))

        (#.Some program_module)
        (do promise.monad
          [environment (program.environment promise.monad program)
           #let [home (\ program home)
                 working_directory (\ program directory)]]
          (do ///action.monad
            [[resolution compiler] (promise\wrap (..compiler resolution (get@ #///.compiler profile)))
             #let [[[command compiler_params] output] (case compiler
                                                        (^template [<tag> <runtime> <program>]
                                                          [(<tag> dependency)
                                                           [(///runtime.for (get@ <runtime> profile)
                                                                            (..path fs home dependency))
                                                            <program>]])
                                                        ([#JVM #///.java "program.jar"]
                                                         [#JS #///.js "program.js"]
                                                         [#Python #///.java "program.py"]
                                                         [#Lua #///.java "program.lua"]
                                                         [#Ruby #///.java "program.rb"]))
                   / (\ fs separator)
                   cache_directory (format working_directory / target)]
             _ (console.write_line ..start console)
             process (\ shell execute [environment
                                       working_directory
                                       command
                                       (list.concat (list compiler_params
                                                          (list "build")
                                                          (..plural "--library" (..libraries fs home resolution))
                                                          (..plural "--source" (set.to_list (get@ #///.sources profile)))
                                                          (..singular "--target" cache_directory)
                                                          (..singular "--module" program_module)))])
             _ (..log_output! console process)
             _ (..log_error! console process)
             exit (\ process await [])
             _ (console.write_line (if (i.= shell.normal exit)
                                     ..success
                                     ..failure)
                                   console)]
            (wrap [exit
                   compiler
                   (format cache_directory / output)])))))))