forked from neohaskell/NeoHaskell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild.hs
More file actions
95 lines (78 loc) · 2.88 KB
/
Copy pathBuild.hs
File metadata and controls
95 lines (78 loc) · 2.88 KB
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
module Neo.Build (
handle,
Error (..),
) where
import Array qualified
import Directory qualified
import File qualified
import Maybe qualified
import Neo.Build.Templates.AppMain qualified as AppMain
import Neo.Build.Templates.Cabal qualified as Cabal
import Neo.Build.Templates.CabalProject qualified as CabalProject
import Neo.Build.Templates.Nix qualified as Nix
import Neo.Core
import Path qualified
import Subprocess qualified
import Task qualified
import Text qualified
data Error
= NixFileError
| CabalFileError
| CustomError Text
deriving (Show)
handle :: ProjectConfiguration -> Task Error Unit
handle config = do
let haskellExtension = ".hs"
let projectName = config.name
let rootFolder = [path|.|]
let cabalFileName =
[fmt|#{projectName}.cabal|]
|> Path.fromText
|> Maybe.getOrDie -- TODO: Make better error handling here
let cabalProjectFileName = [path|cabal.project|]
let nixFile = Nix.template config
let cabalFilePath =
Array.fromLinkedList [rootFolder, cabalFileName]
|> Path.joinPaths
let targetAppFolder =
Array.fromLinkedList [rootFolder, ".launcher"]
|> Path.joinPaths
let targetAppPath =
Array.fromLinkedList [targetAppFolder, "Main.hs"]
|> Path.joinPaths
filepaths <-
Directory.walk [path|src|]
|> Task.mapError (\_ -> CustomError "WALK ERROR")
let haskellFiles = filepaths |> Array.takeIf (Path.endsWith haskellExtension)
let convertToModule filepath = do
let pathText = Path.toText filepath
let pathWithoutExtension = Text.dropRight (Text.length haskellExtension) pathText
let pathParts = Text.split "/" pathWithoutExtension
pathParts |> Text.joinWith "."
let modules = haskellFiles |> Array.map convertToModule
let cabalFile = Cabal.template config modules
let cabalProjectFile = CabalProject.template
let appMainFile = AppMain.template config
Directory.create targetAppFolder
|> Task.mapError (\_ -> [fmt|Could not create directory #{Path.toText targetAppFolder}|] |> CustomError)
File.writeText cabalFilePath cabalFile
|> Task.mapError (\_ -> CabalFileError)
File.writeText cabalProjectFileName cabalProjectFile
|> Task.mapError (\_ -> CabalFileError)
File.writeText targetAppPath appMainFile
|> Task.mapError (\_ -> CustomError "Could not write app main file")
let buildExpression :: Text =
[fmt|{ pkgs ? import <nixpkgs> {} }:
( (#{nixFile}) { inherit pkgs; } ).package|]
completion <-
Subprocess.openInherit "nix-build" (Array.fromLinkedList ["-E", buildExpression]) rootFolder Subprocess.InheritBOTH
|> Task.mapError (\_ -> CustomError "Failed to run nix-build")
if completion.exitCode != 0
then errorOut completion.stderr
else print completion.stdout
errorOut :: Text -> Task Error _
errorOut err =
[fmt|Oops the build failed:
#{err}|]
|> CustomError
|> Task.throw