diff --git a/cli/nhcli.cabal b/cli/nhcli.cabal index ab1d6f8..02491eb 100644 --- a/cli/nhcli.cabal +++ b/cli/nhcli.cabal @@ -54,6 +54,7 @@ library Neo.Build.Templates.AppMain, Neo.Core, Neo.Core.ProjectConfiguration, + Neo.Run, -- other-modules: -- other-extensions: hs-source-dirs: src diff --git a/cli/src/Neo.hs b/cli/src/Neo.hs index bb0aee0..1140fc9 100644 --- a/cli/src/Neo.hs +++ b/cli/src/Neo.hs @@ -11,6 +11,7 @@ import Core import File qualified import Json qualified import Neo.Build qualified as Build +import Neo.Run qualified as Run import Task qualified import Text qualified @@ -23,6 +24,7 @@ data CommonFlags = CommonFlags data NeoCommand = Build CommonFlags + | Run CommonFlags deriving (Show, Eq, Ord) @@ -55,12 +57,19 @@ commandsParser = do let build = Command.CommandOptions { name = "build", - description = "build a file or directory", + description = "build the project", version = Nothing, decoder = buildParser } + let run = + Command.CommandOptions + { name = "run", + description = "run the project", + version = Nothing, + decoder = runParser + } Command.commands - (Array.fromLinkedList [build]) + (Array.fromLinkedList [build, run]) buildParser :: Command.OptionsParser NeoCommand @@ -69,6 +78,12 @@ buildParser = do pure (Build common) +runParser :: Command.OptionsParser NeoCommand +runParser = do + common <- flagsParser + pure (Run common) + + flagsParser :: Command.OptionsParser CommonFlags flagsParser = do projectFilePath <- @@ -85,6 +100,7 @@ flagsParser = do data Error = BuildError Build.Error + | RunError Run.Error | Other deriving (Show) @@ -99,3 +115,12 @@ handleCommand command = Ok config -> Build.handle config |> Task.mapError (\e -> BuildError e) + Run flags -> do + txt <- File.readText flags.projectFile |> Task.mapError (\_ -> Other) + case Json.decodeText txt of + Err err -> panic err + Ok config -> do + Build.handle config + |> Task.mapError (\e -> BuildError e) + Run.handle config + |> Task.mapError (\e -> RunError e) diff --git a/cli/src/Neo/Run.hs b/cli/src/Neo/Run.hs new file mode 100644 index 0000000..d0edcdb --- /dev/null +++ b/cli/src/Neo/Run.hs @@ -0,0 +1,35 @@ +module Neo.Run ( + handle, + Error (..), +) where + +import Array qualified +import Neo.Core +import Subprocess qualified +import Task qualified + + +data Error + = NixFileError + | CabalFileError + | CustomError Text + deriving (Show) + + +handle :: ProjectConfiguration -> Task Error Unit +handle config = do + let projectName = config.name + let rootFolder = [path|nhout|] + completion <- + Subprocess.openInherit [fmt|./result/bin/{projectName}|] (Array.fromLinkedList []) rootFolder Subprocess.InheritBOTH + if completion.exitCode != 0 + then errorOut completion.stderr + else print completion.stdout + + +errorOut :: Text -> Task Error _ +errorOut err = + [fmt|Oops running failed: + {err}|] + |> CustomError + |> Task.throw