@@ -10,7 +10,7 @@ import Data.Aeson
10import Data.Aeson.Types
10import Data.Aeson.Types
11import Data.Char
11import Data.Char
12import Data.FileEmbed (embedFile)
12import Data.FileEmbed (embedFile)
13−import Data.List (delete, intercalate, isPrefixOf, nub, partition)
13+import Data.List (delete, intercalate, isPrefixOf, nub, partition, (\\))
14import Data.Maybe
14import Data.Maybe
15import Data.Monoid ((<>))
15import Data.Monoid ((<>))
16import Data.Version
16import Data.Version
@@ -20,7 +20,7 @@ import Text.Printf
21import System.Environment
21import System.Environment
22import System.Directory (createDirectoryIfMissing, doesDirectoryExist, doesFileExist, getCurrentDirectory, getModificationTime)
22import System.Directory (createDirectoryIfMissing, doesDirectoryExist, doesFileExist, getCurrentDirectory, getModificationTime)
23−import System.FilePath ((</>), joinPath, searchPathSeparator, splitDirectories, takeDirectory)
23+import System.FilePath ((</>), joinPath, splitDirectories, takeDirectory)
24import System.FilePath.Find
24import System.FilePath.Find
25import System.Process
25import System.Process
@@ -43,7 +43,7 @@ import CodeGen.IL.Printer
44import Tests
44import Tests
46−data Command = Build | Run
46+data Command = Build | Run (Maybe String)
48parseJson :: Text -> Value
48parseJson :: Text -> Value
49parseJson text
49parseJson text
@@ -54,12 +54,32 @@ jsonToModule :: Value -> Module Ann
54jsonToModule value =
54jsonToModule value =
55 case parse moduleFromJSON value of
55 case parse moduleFromJSON value of
56 Success (_, r) -> r
56 Success (_, r) -> r
57− _ -> error "failed"
57+ _ -> error "Failed parsing corefn. Psgo currently supports purescript <= 0.13.8"
59+splitArgs :: [String] -> ([String], Maybe String, [String])
61+ let (opts, files) = partition (isPrefixOf "--") args
62+ opts' = (map . map) toLower opts
63+ (moduleName, files') =
65+ then (Nothing, files)
67+ if "--run" `elem` opts' && length (files) >= 1
69+ let file0 = head files
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')
59main :: IO ()
78main :: IO ()
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
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 ()
@@ -100,45 +120,32 @@ main = do
100 basePath :: [FilePath] -> FilePath
120 basePath :: [FilePath] -> FilePath
101 basePath files =
121 basePath files =
102 let filepath = takeDirectory (head files) in
122 let filepath = takeDirectory (head files) in
103− joinPath $ (init $ splitDirectories filepath) ++ [outdir]
123+ joinPath $ (init $ splitDirectories filepath)
104 generateFiles :: [String] -> [FilePath] -> IO ()
124 generateFiles :: [String] -> [FilePath] -> IO ()
105 generateFiles opts files = do
125 generateFiles opts files = do
106 let baseOutpath = basePath files
126 let baseOutpath = basePath files
107− writeRuntimeFiles baseOutpath
127+ writeSupportFiles baseOutpath
108 Par.mapM (generateCode opts baseOutpath) files
128 Par.mapM (generateCode opts baseOutpath) files
109 return ()
129 return ()
110 runBuildTool :: Command -> IO ()
130 runBuildTool :: Command -> IO ()
111 runBuildTool cmd = do
131 runBuildTool cmd = do
132+ project <- projectEnv
112− let command = case cmd of
133+ case cmd of
113− Build -> "build"
134+ Build -> do
114− Run -> "run"
135+ callProcess "go" ["build", T.unpack project </> modPrefix' </> "Main"]
115− currentDir <- getCurrentDirectory
136+ Run moduleName -> do
116− files <- find always (extension ==? goSrc) currentDir
137+ callProcess "go" ["run", T.unpack project </> modPrefix' </> fromMaybe "Main" moduleName]
117− let fileDirs = nub $ joinPath . init <$> filter isSrc (getDir <$> files)
118− currentEnv <- getEnvironment
119− let pathDirs = maybe id (:) (lookup "GOPATH" currentEnv) $ fileDirs
120− searchPath = intercalate [searchPathSeparator] pathDirs
121− env = ("GOPATH", searchPath) : currentEnv
122− (_, _, _, p) <- createProcess (proc "go" [command, "Main"]){ env = Just env }
124 return ()
138 return ()
126− getDir :: FilePath -> [FilePath]
128− let filepath = takeDirectory file in
129− init $ splitDirectories filepath
130− isSrc :: [FilePath] -> Bool
131− isSrc components = (last components) == "src"
133generateCode :: [String] -> FilePath -> FilePath -> IO ()
140generateCode :: [String] -> FilePath -> FilePath -> IO ()
134generateCode opts baseOutpath jsonFile = do
141generateCode opts baseOutpath jsonFile = do
135 jsonModTime <- getModificationTime jsonFile
142 jsonModTime <- getModificationTime jsonFile
136 let filepath = takeDirectory jsonFile
143 let filepath = takeDirectory jsonFile
137 dirparts = splitDirectories $ filepath
144 dirparts = splitDirectories $ filepath
138− mname = (\c -> if c == '.' then '_' else c) <$> last dirparts
145+ mname = last dirparts
139 basedir = joinPath $ init dirparts
146 basedir = joinPath $ init dirparts
140− mname' = moduleNameToIL' $ T.pack mname
147+ mname' = T.pack mname
141− possibleFileName = basedir </> outdir </> (T.unpack mname') </> implFileName mname'
148+ possibleFileName = basedir </> (T.unpack mname') </> implFileName mname'
142 exists <- doesFileExist possibleFileName
149 exists <- doesFileExist possibleFileName
143 if exists
150 if exists
@@ -150,9 +157,10 @@ generateCode opts baseOutpath jsonFile = do
150transpile :: [String] -> FilePath -> FilePath -> IO ()
157transpile :: [String] -> FilePath -> FilePath -> IO ()
151transpile opts baseOutpath jsonFile = do
158transpile opts baseOutpath jsonFile = do
152 jsonText <- T.decodeUtf8 <$> B.readFile jsonFile
159 jsonText <- T.decodeUtf8 <$> B.readFile jsonFile
160+ project <- projectEnv
153 let module' = jsonToModule $ parseJson jsonText
161 let module' = jsonToModule $ parseJson jsonText
154− ((_, foreigns, asts, implHeader, implFooter), _) <- runSupplyT 5 (moduleToIL module' Nothing)
162+ ((_, foreigns, asts, implHeader, implFooter), _) <- runSupplyT 5 (moduleToIL module' project)
155− let mn = moduleNameToIL $ moduleName module'
163+ let mn = moduleNameToIL' $ moduleName module'
156 implementation = prettyPrintIL asts
164 implementation = prettyPrintIL asts
157 outpath = joinPath [baseOutpath, T.unpack mn]
165 outpath = joinPath [baseOutpath, T.unpack mn]
158 implPath = outpath </> implFileName mn
166 implPath = outpath </> implFileName mn
@@ -160,25 +168,53 @@ transpile opts baseOutpath jsonFile = do
160 putStrLn implPath
168 putStrLn implPath
161 B.writeFile implPath $ T.encodeUtf8 (implHeader <> implementation <> implFooter)
169 B.writeFile implPath $ T.encodeUtf8 (implHeader <> implementation <> implFooter)
163−writeRuntimeFiles :: FilePath -> IO ()
171+writeSupportFiles :: FilePath -> IO ()
164−writeRuntimeFiles baseOutpath = do
172+writeSupportFiles baseOutpath = do
165− createDirectoryIfMissing True $ baseOutpath </> "purescript"
173+ currentDir <- getCurrentDirectory
166− let runtimeSource = baseOutpath </> "purescript" </> "purescript.go"
174+ let outputdir = T.pack $ baseOutpath \\ currentDir
167− runtimeExists <- doesFileExist runtimeSource
175+ ffiOutpath = currentDir </> subdir
168− when (not runtimeExists) $ do
176+ createDirectoryIfMissing True baseOutpath
169− B.writeFile runtimeSource $(embedFile "runtime/purescript.go")
177+ createDirectoryIfMissing True ffiOutpath
170−
178+ writeModuleFile outputdir currentDir $(embedFile "support/go.mod.working")
171−outdir :: FilePath
179+ writeModuleFile outputdir baseOutpath $(embedFile "support/go.mod.output")
172−outdir = "src"
180+ writeModuleFile outputdir ffiOutpath $(embedFile "support/go.mod.ffi-loader")
181+ writeLoaderFile ffiOutpath $(embedFile "support/ffi-loader.go")
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
174implFileName :: Text -> FilePath
205implFileName :: Text -> FilePath
175−implFileName mn = T.unpack $ mn <> ".go"
206+implFileName mn = ((\c -> if c == '.' then '_' else c) <$> T.unpack mn) <> ".go"
208+projectEnv :: IO Text
210+ T.pack . fromMaybe defaultProject <$> lookupEnv goproject
177help :: String
212help :: String
178help = "Usage: psgo OPTIONS COREFN-FILES\n\
213help = "Usage: psgo OPTIONS COREFN-FILES\n\
179 \ PureScript to native (via go) compiler\n\n\
214 \ PureScript to native (via go) compiler\n\n\
180 \Available options:\n\
215 \Available options:\n\
181 \ --help Show this help text\n\n\
216 \ --help Show this help text\n\n\
217+ \ --version Show the version number\n\n\
182 \ --run Run the generated go code directly, without building an\n\
218 \ --run Run the generated go code directly, without building an\n\
183 \ executable\n\
219 \ executable\n\
184 \ --no-build Generate go source files, but do not build an executable\n\
220 \ --no-build Generate go source files, but do not build an executable\n\
@@ -192,6 +228,21 @@ corefn = "corefn.json"
192goSrc :: String
228goSrc :: String
193goSrc = ".go"
229goSrc = ".go"
232+goproject = "GOPROJECT"
234+defaultProject :: String
235+defaultProject = "project.localhost"
238+modPrefix' = T.unpack modPrefix
241+ffiLoader' = T.unpack ffiLoader
244+subdir = T.unpack modLabel
195errorNoCorefnFiles :: IO ()
246errorNoCorefnFiles :: IO ()
196errorNoCorefnFiles = do
247errorNoCorefnFiles = do
197 ioError . userError $ "no compiled purescript '" <> corefn <> "' files found –\n" <>
248 ioError . userError $ "no compiled purescript '" <> corefn <> "' files found –\n" <>