Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support 'spago run' and 'spago test' #60

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import CodeGen.IL.Printer

import Tests

data Command = Build | Run

data Command = Build | Run (Maybe String)
parseJson :: Text -> Value
parseJson text
| Just fileJson <- decode . L.encodeUtf8 $ L.fromStrict text = fileJson
Expand All @@ -56,10 +56,30 @@ jsonToModule value =
Success (_, r) -> r
_ -> error "failed"

splitArgs :: [String] -> ([String], Maybe String, [String])
splitArgs args =
let (opts, files) = partition (isPrefixOf "--") args
opts' = (map . map) toLower opts
(moduleName, files') =
if null files
then (Nothing, files)
else
if "--run" `elem` opts' && length (files) >= 1
then do
let file0 = head files
files' = tail files
case file0 of
"Main.main" -> (Just "Main", files')
"Test.Main.main" -> (Just "Test.Main", files')
_ -> (Nothing, files)
else (Nothing, files)
in (opts, moduleName, files')

main :: IO ()
main = do
args <- getArgs
let (opts, files) = partition (isPrefixOf "--") args
-- let (opts, files) = partition (isPrefixOf "--") args
let (opts, moduleName, files) = splitArgs args
opts' = (map . map) toLower opts
if "--tests" `elem` opts'
then runTests
Expand All @@ -81,7 +101,7 @@ main = do
else
processFiles opts' files
if "--run" `elem` opts
then runBuildTool Run
then runBuildTool $ Run moduleName
else when ("--no-build" `notElem` opts) $
runBuildTool Build
return ()
Expand Down Expand Up @@ -109,11 +129,12 @@ main = do
return ()
runBuildTool :: Command -> IO ()
runBuildTool cmd = do
let command = case cmd of
Build -> "build"
Run -> "run"
project <- projectEnv
callProcess "go" [command, T.unpack project </> modPrefix' </> "Main"]
case cmd of
Build -> do
callProcess "go" ["build", T.unpack project </> modPrefix' </> "Main"]
Run moduleName -> do
callProcess "go" ["run", T.unpack project </> modPrefix' </> fromMaybe "Main" moduleName]
return ()

generateCode :: [String] -> FilePath -> FilePath -> IO ()
Expand Down
6 changes: 3 additions & 3 deletions src/CodeGen/IL/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ interfaceSource _ _ _ = ""
implHeaderSource :: Text -> [Text] -> Text -> Text
implHeaderSource mn imports otherPrefix =
"// Code generated by psgo. DO NOT EDIT.\n\n" <>
"package " <> (if mn == "Main" then "main" else mn) <> "\n\n" <>
"package " <> (if mn == "Main" || mn == "Test_Main" then "main" else mn) <> "\n\n" <>
"import . \"" <> runtime <> "\"\n" <>
(if mn == "Main"
(if mn == "Main" || mn == "Test_Main"
then "import _ \"" <> otherPrefix <> "/" <> ffiLoader <> "\"\n"
else "\n") <>
"import (\n" <>
Expand Down Expand Up @@ -482,7 +482,7 @@ implFooterSource mn foreigns =
" })\n" <>
" return " <> valueName name <> "\n" <>
"}\n\n") <$> foreigns))) <>
if mn == "Main" then mainSource else "\n"
if mn == "Main" || mn == "Test.Main" then mainSource else "\n"
where
mainSource :: Text
mainSource = "\
Expand Down