Skip to content

Commit fb911cc

Browse files
NickSeagullNick Seagull
and
Nick Seagull
authored
feat: neo run (#139)
Co-authored-by: Nick Seagull <[email protected]>
1 parent 2d56986 commit fb911cc

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

cli/nhcli.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ library
5454
Neo.Build.Templates.AppMain,
5555
Neo.Core,
5656
Neo.Core.ProjectConfiguration,
57+
Neo.Run,
5758
-- other-modules:
5859
-- other-extensions:
5960
hs-source-dirs: src

cli/src/Neo.hs

+27-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Core
1111
import File qualified
1212
import Json qualified
1313
import Neo.Build qualified as Build
14+
import Neo.Run qualified as Run
1415
import Task qualified
1516
import Text qualified
1617

@@ -23,6 +24,7 @@ data CommonFlags = CommonFlags
2324

2425
data NeoCommand
2526
= Build CommonFlags
27+
| Run CommonFlags
2628
deriving (Show, Eq, Ord)
2729

2830

@@ -55,12 +57,19 @@ commandsParser = do
5557
let build =
5658
Command.CommandOptions
5759
{ name = "build",
58-
description = "build a file or directory",
60+
description = "build the project",
5961
version = Nothing,
6062
decoder = buildParser
6163
}
64+
let run =
65+
Command.CommandOptions
66+
{ name = "run",
67+
description = "run the project",
68+
version = Nothing,
69+
decoder = runParser
70+
}
6271
Command.commands
63-
(Array.fromLinkedList [build])
72+
(Array.fromLinkedList [build, run])
6473

6574

6675
buildParser :: Command.OptionsParser NeoCommand
@@ -69,6 +78,12 @@ buildParser = do
6978
pure (Build common)
7079

7180

81+
runParser :: Command.OptionsParser NeoCommand
82+
runParser = do
83+
common <- flagsParser
84+
pure (Run common)
85+
86+
7287
flagsParser :: Command.OptionsParser CommonFlags
7388
flagsParser = do
7489
projectFilePath <-
@@ -85,6 +100,7 @@ flagsParser = do
85100

86101
data Error
87102
= BuildError Build.Error
103+
| RunError Run.Error
88104
| Other
89105
deriving (Show)
90106

@@ -99,3 +115,12 @@ handleCommand command =
99115
Ok config ->
100116
Build.handle config
101117
|> Task.mapError (\e -> BuildError e)
118+
Run flags -> do
119+
txt <- File.readText flags.projectFile |> Task.mapError (\_ -> Other)
120+
case Json.decodeText txt of
121+
Err err -> panic err
122+
Ok config -> do
123+
Build.handle config
124+
|> Task.mapError (\e -> BuildError e)
125+
Run.handle config
126+
|> Task.mapError (\e -> RunError e)

cli/src/Neo/Run.hs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Neo.Run (
2+
handle,
3+
Error (..),
4+
) where
5+
6+
import Array qualified
7+
import Neo.Core
8+
import Subprocess qualified
9+
import Task qualified
10+
11+
12+
data Error
13+
= NixFileError
14+
| CabalFileError
15+
| CustomError Text
16+
deriving (Show)
17+
18+
19+
handle :: ProjectConfiguration -> Task Error Unit
20+
handle config = do
21+
let projectName = config.name
22+
let rootFolder = [path|nhout|]
23+
completion <-
24+
Subprocess.openInherit [fmt|./result/bin/{projectName}|] (Array.fromLinkedList []) rootFolder Subprocess.InheritBOTH
25+
if completion.exitCode != 0
26+
then errorOut completion.stderr
27+
else print completion.stdout
28+
29+
30+
errorOut :: Text -> Task Error _
31+
errorOut err =
32+
[fmt|Oops running failed:
33+
{err}|]
34+
|> CustomError
35+
|> Task.throw

0 commit comments

Comments
 (0)