git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
feat: support 'spago run' and 'spago test' (#60)
commit7a3f114fb158a250c6130c93752719b6d69c606a authorKen Naito <fine14r3@gmail.com> dateSun 27 Sep 2020, 11:33 (6 years ago) parentf7e8d1c
2 files changed+32−11
app/Main.hs+29−8 src/CodeGen/IL/Printer.hs+3−3
app/Main.hs+29−8
@@ -43,8 +43,8 @@ import CodeGen.IL.Printer
4343
4444import Tests
4545
46−data Command = Build | Run
47−
46+data Command = Build | Run (Maybe String)
47+
4848parseJson :: Text -> Value
4949parseJson text
5050 | Just fileJson <- decode . L.encodeUtf8 $ L.fromStrict text = fileJson
@@ -56,10 +56,30 @@ jsonToModule value =
5656 Success (_, r) -> r
5757 _ -> error "failed"
5858
59+splitArgs :: [String] -> ([String], Maybe String, [String])
60+splitArgs args =
61+ let (opts, files) = partition (isPrefixOf "--") args
62+ opts' = (map . map) toLower opts
63+ (moduleName, files') =
64+ if null files
65+ then (Nothing, files)
66+ else
67+ if "--run" `elem` opts' && length (files) >= 1
68+ then do
69+ let file0 = head files
70+ files' = tail files
71+ case file0 of
72+ "Main.main" -> (Just "Main", files')
73+ "Test.Main.main" -> (Just "Test.Main", files')
74+ _ -> (Nothing, files)
75+ else (Nothing, files)
76+ in (opts, moduleName, files')
77+
5978main :: IO ()
6079main = do
6180 args <- getArgs
62− let (opts, files) = partition (isPrefixOf "--") args
81+ -- let (opts, files) = partition (isPrefixOf "--") args
82+ let (opts, moduleName, files) = splitArgs args
6383 opts' = (map . map) toLower opts
6484 if "--tests" `elem` opts'
6585 then runTests
@@ -81,7 +101,7 @@ main = do
81101 else
82102 processFiles opts' files
83103 if "--run" `elem` opts
84− then runBuildTool Run
104+ then runBuildTool $ Run moduleName
85105 else when ("--no-build" `notElem` opts) $
86106 runBuildTool Build
87107 return ()
@@ -109,11 +129,12 @@ main = do
109129 return ()
110130 runBuildTool :: Command -> IO ()
111131 runBuildTool cmd = do
112− let command = case cmd of
113− Build -> "build"
114− Run -> "run"
115132 project <- projectEnv
116− callProcess "go" [command, T.unpack project </> modPrefix' </> "Main"]
133+ case cmd of
134+ Build -> do
135+ callProcess "go" ["build", T.unpack project </> modPrefix' </> "Main"]
136+ Run moduleName -> do
137+ callProcess "go" ["run", T.unpack project </> modPrefix' </> fromMaybe "Main" moduleName]
117138 return ()
118139
119140generateCode :: [String] -> FilePath -> FilePath -> IO ()
src/CodeGen/IL/Printer.hs+3−3
@@ -449,9 +449,9 @@ interfaceSource _ _ _ = ""
449449implHeaderSource :: Text -> [Text] -> Text -> Text
450450implHeaderSource mn imports otherPrefix =
451451 "// Code generated by psgo. DO NOT EDIT.\n\n" <>
452− "package " <> (if mn == "Main" then "main" else mn) <> "\n\n" <>
452+ "package " <> (if mn == "Main" || mn == "Test_Main" then "main" else mn) <> "\n\n" <>
453453 "import . \"" <> runtime <> "\"\n" <>
454− (if mn == "Main"
454+ (if mn == "Main" || mn == "Test_Main"
455455 then "import _ \"" <> otherPrefix <> "/" <> ffiLoader <> "\"\n"
456456 else "\n") <>
457457 "import (\n" <>
@@ -482,7 +482,7 @@ implFooterSource mn foreigns =
482482 " })\n" <>
483483 " return " <> valueName name <> "\n" <>
484484 "}\n\n") <$> foreigns))) <>
485− if mn == "Main" then mainSource else "\n"
485+ if mn == "Main" || mn == "Test.Main" then mainSource else "\n"
486486 where
487487 mainSource :: Text
488488 mainSource = "\