Skip to content

Commit

Permalink
Allow running build
Browse files Browse the repository at this point in the history
  • Loading branch information
NickSeagull committed Mar 9, 2025
1 parent 32592f2 commit e1dcb93
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ cabal.project.local~
# hie cradle generated automatically
hie.yaml

.neo
nhout
sandbox/*.cabal
sandbox/*.nix

Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"type": "process",
"command": "cabal",
"args": ["build", "all"],
"args": ["--enable-nix", "build", "all"],
"label": "cabal: build all",
"presentation": {
"echo": true,
Expand Down
1 change: 1 addition & 0 deletions cli/nhcli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ library
Neo.Build,
Neo.Build.Templates.Cabal,
Neo.Build.Templates.Nix,
Neo.Build.Templates.AppMain,
Neo.Core,
Neo.Core.ProjectConfiguration,
-- other-modules:
Expand Down
24 changes: 20 additions & 4 deletions cli/src/Neo/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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.Nix qualified as Nix
import Neo.Core
Expand All @@ -28,7 +29,7 @@ handle :: ProjectConfiguration -> Task Error Unit
handle config = do
let haskellExtension = ".hs"
let projectName = config.name
let rootFolder = [path|.neohaskell|]
let rootFolder = [path|nhout|]
let nixFileName = [path|default.nix|]
let cabalFileName =
[fmt|{projectName}.cabal|]
Expand All @@ -41,10 +42,19 @@ handle config = do
let cabalFilePath =
Array.fromLinkedList [rootFolder, cabalFileName]
|> Path.joinPaths
let targetAppFolder =
Array.fromLinkedList [rootFolder, "app"]
|> Path.joinPaths
let targetAppPath =
Array.fromLinkedList [targetAppFolder, "Main.hs"]
|> Path.joinPaths
let targetSrcFolder =
Array.fromLinkedList [rootFolder, "src"]
|> Path.joinPaths

-- TODO: Remember to copy using https://hackage.haskell.org/package/directory-1.3.8.1/docs/System-Directory.html#v:copyFileWithMetadata
-- I mean into .neohaskell
Directory.copy [path|src|] rootFolder
Directory.copy [path|src|] targetSrcFolder
|> Task.mapError (\e -> CustomError (toText e))

filepaths <-
Expand All @@ -67,20 +77,26 @@ handle config = do

let modules = haskellFiles |> Array.map convertToModule
let cabalFile = Cabal.template config modules
let appMainFile = AppMain.template config

Directory.create rootFolder
|> Task.mapError (\_ -> [fmt|Could not create directory {Path.toText rootFolder}|] |> CustomError)

Directory.create targetAppFolder
|> Task.mapError (\_ -> [fmt|Could not create directory {Path.toText targetAppFolder}|] |> CustomError)

File.writeText nixFilePath nixFile
|> Task.mapError (\_ -> NixFileError)

File.writeText cabalFilePath cabalFile
|> Task.mapError (\_ -> CabalFileError)

File.writeText targetAppPath appMainFile
|> Task.mapError (\_ -> CustomError "Could not write app main file")

-- FIXME: Create another thread that renders the output of the build via streaming.
-- As right now there's no output at all
completion <- Subprocess.open "nix-build" (Array.fromLinkedList []) rootFolder
-- completion <- Subprocess.open "nix-build" (Array.fromLinkedList ["-E", nixFile]) rootFolder
completion <- Subprocess.openInherit "nix-build" (Array.fromLinkedList []) rootFolder Subprocess.InheritBOTH
if completion.exitCode != 0
then errorOut completion.stderr
else print completion.stdout
Expand Down
20 changes: 20 additions & 0 deletions cli/src/Neo/Build/Templates/AppMain.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Neo.Build.Templates.AppMain where

import Core
import Text qualified
import Neo.Core


template :: ProjectConfiguration -> Text
template
ProjectConfiguration {name} = do
let mainModuleName = Text.toPascalCase name
[fmt|module Main where

import Core
import Task qualified
import {mainModuleName} qualified

main :: IO ()
main = Task.runOrPanic {mainModuleName}.run
|]
16 changes: 2 additions & 14 deletions cli/src/Neo/Build/Templates/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ template :: ProjectConfiguration -> Array Text -> Text
template
ProjectConfiguration {name, version, description, license, author, dependencies}
modules = do
let execName = Text.toKebabCase name
-- FIXME: Move onto a separate version handling module
let vText = Version.toText version
let makeDep (k, v)
Expand All @@ -30,10 +31,6 @@ template
[fmt|cabal-version: 3.4
-- THIS CABAL FILE IS AUTOGENERATED BY `neo`, THE NEOHASKELL CLI TOOL.
-- YOU SHOULD NOT MODIFY IT, AS IT WILL BE REGENERATED NEXT TIME `neo` RUNS.
-- THE REASON FOR THIS FILE TO EXIST IS THAT
-- CURRENT HASKELL IDE TOOLING CAN PICK UP
-- THE NEOHASKELL PROJECT AS IF IT WAS A
-- REGULAR HASKELL PROJECT.
--
-- IF YOU HAVE A SPECIFIC NEED TO MODIFY THIS
-- FILE, PLEASE STATE SO EITHER IN A GITHUB
Expand Down Expand Up @@ -79,22 +76,13 @@ library
import: common_cfg
exposed-modules:
{mods}
-- other-modules:
-- other-extensions:
hs-source-dirs: src

executable neo
executable {execName}
import: common_cfg
main-is: Main.hs
build-depends:
{name}
hs-source-dirs: app

test-suite {name}-test
import: common_cfg
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
build-depends:
{name}
|]
8 changes: 5 additions & 3 deletions cli/src/Neo/Build/Templates/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pinnedNixpkgs =


template :: ProjectConfiguration -> Text
template _ =
template ProjectConfiguration {name} =
{-
https://github.com/NixOS/nixpkgs/blob/777a9707e72e6dbbbdf9033c44f237154c64e9f7/pkgs/development/haskell-modules/make-package-set.nix#L227-L254
Expand All @@ -32,12 +32,14 @@ template _ =
[fmt|{{ pkgs ? ({pinnedNixpkgs}) {{}} }}:
let
neoHaskellGitHub = builtins.fetchTarball
"https://github.com/NeoHaskell/NeoHaskell/archive/refs/heads/dev.tar.gz";
"https://github.com/NeoHaskell/NeoHaskell/archive/refs/heads/main.tar.gz";
in
pkgs.haskellPackages.developPackage {{
name = "{name}";
root = ./.;
returnShellEnv = false;
source-overrides = {{
nhcore = "${{neoHaskellGitHub}}/core";
}};
}}
}} // {{ name = "{name}"; }}
|]
33 changes: 33 additions & 0 deletions core/core/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ module Text (
toBytes,
fromBytes,
convert,

-- * Casing Conversions
toPascalCase,
toCamelCase,
toTitleCase,
toSnakeCase,
toKebabCase,
) where

import Array (Array)
Expand All @@ -86,6 +93,7 @@ import Bytes qualified
import Char (Char)
import Data.Text qualified
import Data.Text.Encoding qualified
import Data.Text.Manipulate qualified as Manipulate
import LinkedList (LinkedList)
import Maybe (Maybe)
import Text.Read qualified
Expand Down Expand Up @@ -574,3 +582,28 @@ fromBytes (Bytes.INTERNAL_CORE_BYTES_CONSTRUCTOR bytes) =

convert :: (IsString s) => Text -> s
convert text = Data.Text.unpack text |> fromString


toPascalCase :: Text -> Text
toPascalCase txt =
Manipulate.toPascal txt


toCamelCase :: Text -> Text
toCamelCase txt =
Manipulate.toCamel txt


toTitleCase :: Text -> Text
toTitleCase txt =
Manipulate.toTitle txt


toSnakeCase :: Text -> Text
toSnakeCase txt =
Manipulate.toSnake txt


toKebabCase :: Text -> Text
toKebabCase txt =
Manipulate.toSpinal txt
1 change: 1 addition & 0 deletions core/nhcore.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ common common_cfg
text,
vector,
directory,
text-manipulate,
transformers,
http-conduit,
containers,
Expand Down
40 changes: 32 additions & 8 deletions core/system/Subprocess.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Subprocess (
OpenOptions (..),
Completion (..),
InheritStream (..),
open,
openInherit,
) where

import Array (Array)
Expand Down Expand Up @@ -35,8 +37,21 @@ data OpenOptions = OpenOptions
deriving (Eq, Ord, Show)


open :: Text -> Array Text -> Path -> Task _ Completion
open executable arguments directory = do
data InheritStream
= InheritSTDOUT
| InheritSTDERR
| InheritBOTH
| InheritNONE
deriving (Eq, Ord, Show)


openInherit :: Text -> Array Text -> Path -> InheritStream -> Task _ Completion
openInherit executable arguments directory inheritStream = do
let (stdoutStream, stderrStream) = case inheritStream of
InheritSTDOUT -> (System.Process.Inherit, System.Process.CreatePipe)
InheritSTDERR -> (System.Process.CreatePipe, System.Process.Inherit)
InheritBOTH -> (System.Process.Inherit, System.Process.Inherit)
InheritNONE -> (System.Process.CreatePipe, System.Process.CreatePipe)
let exec = Text.toLinkedList executable
let args = Array.map Text.toLinkedList arguments |> Array.toLinkedList
let processToExecute =
Expand All @@ -47,14 +62,23 @@ open executable arguments directory = do
{ System.Process.cwd =
directory
|> Path.toLinkedList
|> Maybe.Just
|> Maybe.Just,
System.Process.std_out = stdoutStream,
System.Process.std_err = stderrStream
}
(ec, out, err) <-
System.Process.readCreateProcessWithExitCode processToExecute ""
(_, _, _, ph) <-
-- System.Process.readCreateProcessWithExitCode processToExecute ""
System.Process.createProcess processToExecute
|> Task.fromIO
ec <- System.Process.waitForProcess ph |> Task.fromIO
let exitCode = case ec of
System.Exit.ExitSuccess -> 0
System.Exit.ExitFailure code -> code
let stdout = Text.fromLinkedList out
let stderr = Text.fromLinkedList err
Task.yield Completion {exitCode, stdout, stderr}
let stdout = ""
let stderr = ""
Task.yield Completion {exitCode, stdout, stderr}


open :: Text -> Array Text -> Path -> Task _ Completion
open executable arguments directory =
openInherit executable arguments directory InheritNONE
5 changes: 2 additions & 3 deletions sandbox/neo.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "example-package",
"name": "sandbox",
"version": "1.0.0",
"description": "An example package.json file",
"description": "An example neo.json file",
"author": "Your Name",
"license": "ISC",
"dependencies": {
"brick": "^2.5"
}
}
9 changes: 9 additions & 0 deletions sandbox/src/Sandbox.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Sandbox where

import Core
import Console qualified


run :: Task Text ()
run = do
Console.print "Hello, NeoHaskell!"

0 comments on commit e1dcb93

Please sign in to comment.