@@ -43,7 +43,7 @@ import CodeGen.IL.Printer
4343
4444import Tests
4545
46−data Command = Build | Run
46+data Command = Build | Run (Maybe String)
4747
4848parseJson :: Text -> Value
4949parseJson text
@@ -54,12 +54,32 @@ jsonToModule :: Value -> Module Ann
5454jsonToModule value =
5555 case parse moduleFromJSON value of
5656 Success (_, r) -> r
57− _ -> error "failed"
57+ _ -> error "Failed parsing corefn. Psgo currently supports purescript <= 0.13.8"
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')
5877
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"
115− (_, _, _, p) <- createProcess (proc "go" [command, T.unpack modPrefix <> "/Main"])
116− waitForProcess p
132+ project <- projectEnv
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 ()
@@ -136,8 +157,9 @@ generateCode opts baseOutpath jsonFile = do
136157transpile :: [String] -> FilePath -> FilePath -> IO ()
137158transpile opts baseOutpath jsonFile = do
138159 jsonText <- T.decodeUtf8 <$> B.readFile jsonFile
160+ project <- projectEnv
139161 let module' = jsonToModule $ parseJson jsonText
140− ((_, foreigns, asts, implHeader, implFooter), _) <- runSupplyT 5 (moduleToIL module' Nothing)
162+ ((_, foreigns, asts, implHeader, implFooter), _) <- runSupplyT 5 (moduleToIL module' project)
141163 let mn = moduleNameToIL' $ moduleName module'
142164 implementation = prettyPrintIL asts
143165 outpath = joinPath [baseOutpath, T.unpack mn]
@@ -149,36 +171,50 @@ transpile opts baseOutpath jsonFile = do
149171writeSupportFiles :: FilePath -> IO ()
150172writeSupportFiles baseOutpath = do
151173 currentDir <- getCurrentDirectory
152− let goModSource = currentDir </> "go.mod"
153− goModSourceExists <- doesFileExist goModSource
154− when (not goModSourceExists) $ do
155− let modText = $(embedFile "support/go.mod.working")
156− subdir = baseOutpath \\ currentDir
157− modText' = T.replace "/$OUTPUT" (T.pack subdir) $ T.decodeUtf8 modText
158− B.writeFile goModSource $ T.encodeUtf8 modText'
174+ let outputdir = T.pack $ baseOutpath \\ currentDir
175+ ffiOutpath = currentDir </> subdir
159176 createDirectoryIfMissing True baseOutpath
160− let goModSource = baseOutpath </> "go.mod"
161− goModSourceExists <- doesFileExist goModSource
162− when (not goModSourceExists) $ do
163− B.writeFile goModSource $(embedFile "support/go.mod.output")
164− createDirectoryIfMissing True $ currentDir </> "purescript-native"
165− let goModSource = currentDir </> "purescript-native" </> "go.mod"
166− goModSourceExists <- doesFileExist goModSource
167− when (not goModSourceExists) $ do
168− B.writeFile goModSource $(embedFile "support/go.mod.ffi-loader")
169− let loaderSource = currentDir </> "purescript-native" </> "ffi_loader.go"
170− loaderSourceExists <- doesFileExist loaderSource
171− when (not loaderSourceExists) $ do
172− B.writeFile loaderSource $(embedFile "support/ffi-loader.go")
177+ createDirectoryIfMissing True ffiOutpath
178+ writeModuleFile outputdir currentDir $(embedFile "support/go.mod.working")
179+ writeModuleFile outputdir baseOutpath $(embedFile "support/go.mod.output")
180+ writeModuleFile outputdir ffiOutpath $(embedFile "support/go.mod.ffi-loader")
181+ writeLoaderFile ffiOutpath $(embedFile "support/ffi-loader.go")
182+ where
183+ writeModuleFile :: Text -> FilePath -> B.ByteString -> IO ()
184+ writeModuleFile outputdir path modText = do
185+ let goModSource = path </> "go.mod"
186+ goModSourceExists <- doesFileExist goModSource
187+ when (not goModSourceExists) $ do
188+ project <- projectEnv
189+ let project' = T.unpack project
190+ outputdir' = tail $ T.unpack outputdir
191+ modText' = T.replace "$PROJECT" project $ T.decodeUtf8 modText
192+ replaceLoader = project' </> ffiLoader' <> "=." </> subdir
193+ replaceOutput = project' </> modPrefix' <> "=." </> outputdir'
194+ B.writeFile goModSource $ T.encodeUtf8 modText'
195+ callProcess "go" ["mod", "edit", "-replace", replaceLoader]
196+ callProcess "go" ["mod", "edit", "-replace", replaceOutput]
197+ callProcess "go" ["clean", "-modcache"]
198+ writeLoaderFile :: FilePath -> B.ByteString -> IO ()
199+ writeLoaderFile ffiOutpath loaderText = do
200+ let loaderSource = ffiOutpath </> "ffi_loader.go"
201+ loaderSourceExists <- doesFileExist loaderSource
202+ when (not loaderSourceExists) $ do
203+ B.writeFile loaderSource loaderText
173204
174205implFileName :: Text -> FilePath
175206implFileName mn = ((\c -> if c == '.' then '_' else c) <$> T.unpack mn) <> ".go"
176207
208+projectEnv :: IO Text
209+projectEnv = do
210+ T.pack . fromMaybe defaultProject <$> lookupEnv goproject
211+
177212help :: String
178213help = "Usage: psgo OPTIONS COREFN-FILES\n\
179214 \ PureScript to native (via go) compiler\n\n\
180215 \Available options:\n\
181216 \ --help Show this help text\n\n\
217+ \ --version Show the version number\n\n\
182218 \ --run Run the generated go code directly, without building an\n\
183219 \ executable\n\
184220 \ --no-build Generate go source files, but do not build an executable\n\
@@ -192,6 +228,21 @@ corefn = "corefn.json"
192228goSrc :: String
193229goSrc = ".go"
194230
231+goproject :: String
232+goproject = "GOPROJECT"
233+
234+defaultProject :: String
235+defaultProject = "project.localhost"
236+
237+modPrefix' :: String
238+modPrefix' = T.unpack modPrefix
239+
240+ffiLoader' :: String
241+ffiLoader' = T.unpack ffiLoader
242+
243+subdir :: String
244+subdir = T.unpack modLabel
245+
195246errorNoCorefnFiles :: IO ()
196247errorNoCorefnFiles = do
197248 ioError . userError $ "no compiled purescript '" <> corefn <> "' files found –\n" <>