git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
feat: support 'spago run' and 'spago test' (#60)
7a3f114Ken Naito
app/Main.hs+29−8
@@ -43,8 +43,8 @@ import CodeGen.IL.Printer
43
43
44import Tests
44import Tests
45
45
46−data Command = Build | Run
46+data Command = Build | Run (Maybe String)
47−
47+
48parseJson :: Text -> Value
48parseJson :: Text -> Value
49parseJson text
49parseJson text
50 | Just fileJson <- decode . L.encodeUtf8 $ L.fromStrict text = fileJson
50 | Just fileJson <- decode . L.encodeUtf8 $ L.fromStrict text = fileJson
@@ -56,10 +56,30 @@ jsonToModule value =
56 Success (_, r) -> r
56 Success (_, r) -> r
57 _ -> error "failed"
57 _ -> error "failed"
58
58
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+
59main :: IO ()
78main :: IO ()
60main = do
79main = do
61 args <- getArgs
80 args <- getArgs
62− let (opts, files) = partition (isPrefixOf "--") args
81+ -- let (opts, files) = partition (isPrefixOf "--") args
82+ let (opts, moduleName, files) = splitArgs args
63 opts' = (map . map) toLower opts
83 opts' = (map . map) toLower opts
64 if "--tests" `elem` opts'
84 if "--tests" `elem` opts'
65 then runTests
85 then runTests
@@ -81,7 +101,7 @@ main = do
81 else
101 else
82 processFiles opts' files
102 processFiles opts' files
83 if "--run" `elem` opts
103 if "--run" `elem` opts
84− then runBuildTool Run
104+ then runBuildTool $ Run moduleName
85 else when ("--no-build" `notElem` opts) $
105 else when ("--no-build" `notElem` opts) $
86 runBuildTool Build
106 runBuildTool Build
87 return ()
107 return ()
@@ -109,11 +129,12 @@ main = do
109 return ()
129 return ()
110 runBuildTool :: Command -> IO ()
130 runBuildTool :: Command -> IO ()
111 runBuildTool cmd = do
131 runBuildTool cmd = do
112− let command = case cmd of
113− Build -> "build"
114− Run -> "run"
115 project <- projectEnv
132 project <- projectEnv
133+ case cmd of
134+ Build -> do
135+ callProcess "go" ["build", T.unpack project </> modPrefix' </> "Main"]
136+ Run moduleName -> do
116− callProcess "go" [command, T.unpack project </> modPrefix' </> "Main"]
137+ callProcess "go" ["run", T.unpack project </> modPrefix' </> fromMaybe "Main" moduleName]
117 return ()
138 return ()
118
139
119generateCode :: [String] -> FilePath -> FilePath -> IO ()
140generateCode :: [String] -> FilePath -> FilePath -> IO ()
src/CodeGen/IL/Printer.hs+3−3
@@ -449,9 +449,9 @@ interfaceSource _ _ _ = ""
449implHeaderSource :: Text -> [Text] -> Text -> Text
449implHeaderSource :: Text -> [Text] -> Text -> Text
450implHeaderSource mn imports otherPrefix =
450implHeaderSource mn imports otherPrefix =
451 "// Code generated by psgo. DO NOT EDIT.\n\n" <>
451 "// 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" <>
453 "import . \"" <> runtime <> "\"\n" <>
453 "import . \"" <> runtime <> "\"\n" <>
454− (if mn == "Main"
454+ (if mn == "Main" || mn == "Test_Main"
455 then "import _ \"" <> otherPrefix <> "/" <> ffiLoader <> "\"\n"
455 then "import _ \"" <> otherPrefix <> "/" <> ffiLoader <> "\"\n"
456 else "\n") <>
456 else "\n") <>
457 "import (\n" <>
457 "import (\n" <>
@@ -482,7 +482,7 @@ implFooterSource mn foreigns =
482 " })\n" <>
482 " })\n" <>
483 " return " <> valueName name <> "\n" <>
483 " return " <> valueName name <> "\n" <>
484 "}\n\n") <$> foreigns))) <>
484 "}\n\n") <$> foreigns))) <>
485− if mn == "Main" then mainSource else "\n"
485+ if mn == "Main" || mn == "Test.Main" then mainSource else "\n"
486 where
486 where
487 mainSource :: Text
487 mainSource :: Text
488 mainSource = "\
488 mainSource = "\