@@ -112,7 +112,8 @@ main = do
112 let command = case cmd of
112 let command = case cmd of
113 Build -> "build"
113 Build -> "build"
114 Run -> "run"
114 Run -> "run"
115+ project <- envVar goproject
115− (_, _, _, p) <- createProcess (proc "go" [command, T.unpack modPrefix <> "/Main"])
116+ (_, _, _, p) <- createProcess (proc "go" [command, T.unpack $ project <> modPrefix <> "/Main"])
116 waitForProcess p
117 waitForProcess p
117 return ()
118 return ()
@@ -136,8 +137,9 @@ generateCode opts baseOutpath jsonFile = do
136transpile :: [String] -> FilePath -> FilePath -> IO ()
137transpile :: [String] -> FilePath -> FilePath -> IO ()
137transpile opts baseOutpath jsonFile = do
138transpile opts baseOutpath jsonFile = do
138 jsonText <- T.decodeUtf8 <$> B.readFile jsonFile
139 jsonText <- T.decodeUtf8 <$> B.readFile jsonFile
140+ project <- envVar goproject
139 let module' = jsonToModule $ parseJson jsonText
141 let module' = jsonToModule $ parseJson jsonText
140− ((_, foreigns, asts, implHeader, implFooter), _) <- runSupplyT 5 (moduleToIL module' Nothing)
142+ ((_, foreigns, asts, implHeader, implFooter), _) <- runSupplyT 5 (moduleToIL module' project)
141 let mn = moduleNameToIL' $ moduleName module'
143 let mn = moduleNameToIL' $ moduleName module'
142 implementation = prettyPrintIL asts
144 implementation = prettyPrintIL asts
143 outpath = joinPath [baseOutpath, T.unpack mn]
145 outpath = joinPath [baseOutpath, T.unpack mn]
@@ -149,36 +151,44 @@ transpile opts baseOutpath jsonFile = do
149writeSupportFiles :: FilePath -> IO ()
151writeSupportFiles :: FilePath -> IO ()
150writeSupportFiles baseOutpath = do
152writeSupportFiles baseOutpath = do
151 currentDir <- getCurrentDirectory
153 currentDir <- getCurrentDirectory
152− let goModSource = currentDir </> "go.mod"
154+ let outputdir = T.pack $ baseOutpath \\ currentDir
153− goModSourceExists <- doesFileExist goModSource
155+ ffiOutpath = currentDir </> "purescript-native"
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'
159 createDirectoryIfMissing True baseOutpath
156 createDirectoryIfMissing True baseOutpath
160− let goModSource = baseOutpath </> "go.mod"
157+ createDirectoryIfMissing True ffiOutpath
161− goModSourceExists <- doesFileExist goModSource
158+ writeModuleFile outputdir currentDir $(embedFile "support/go.mod.working")
162− when (not goModSourceExists) $ do
163− B.writeFile goModSource $(embedFile "support/go.mod.output")
159+ writeModuleFile outputdir baseOutpath $(embedFile "support/go.mod.output")
164− createDirectoryIfMissing True $ currentDir </> "purescript-native"
160+ writeModuleFile outputdir ffiOutpath $(embedFile "support/go.mod.ffi-loader")
165− let goModSource = currentDir </> "purescript-native" </> "go.mod"
161+ writeLoaderFile ffiOutpath $(embedFile "support/ffi-loader.go")
163+ writeModuleFile :: Text -> FilePath -> B.ByteString -> IO ()
164+ writeModuleFile outputdir path modText = do
165+ let goModSource = path </> "go.mod"
166− goModSourceExists <- doesFileExist goModSource
166+ goModSourceExists <- doesFileExist goModSource
167− when (not goModSourceExists) $ do
167+ when (not goModSourceExists) $ do
168+ project <- envVar goproject
169+ let modText' = T.replace "/$OUTPUT/" outputdir $ T.decodeUtf8 modText
170+ modText'' = T.replace "$PROJECT/" project modText'
168− B.writeFile goModSource $(embedFile "support/go.mod.ffi-loader")
171+ B.writeFile goModSource $ T.encodeUtf8 modText''
172+ writeLoaderFile :: FilePath -> B.ByteString -> IO ()
173+ writeLoaderFile ffiOutpath loaderText = do
169− let loaderSource = currentDir </> "purescript-native" </> "ffi_loader.go"
174+ let loaderSource = ffiOutpath </> "ffi_loader.go"
170− loaderSourceExists <- doesFileExist loaderSource
175+ loaderSourceExists <- doesFileExist loaderSource
171− when (not loaderSourceExists) $ do
176+ when (not loaderSourceExists) $ do
172− B.writeFile loaderSource $(embedFile "support/ffi-loader.go")
177+ B.writeFile loaderSource loaderText
174implFileName :: Text -> FilePath
179implFileName :: Text -> FilePath
175implFileName mn = ((\c -> if c == '.' then '_' else c) <$> T.unpack mn) <> ".go"
180implFileName mn = ((\c -> if c == '.' then '_' else c) <$> T.unpack mn) <> ".go"
182+envVar :: String -> IO Text
184+ T.pack . maybe "" (<> "/") <$> lookupEnv var
177help :: String
186help :: String
178help = "Usage: psgo OPTIONS COREFN-FILES\n\
187help = "Usage: psgo OPTIONS COREFN-FILES\n\
179 \ PureScript to native (via go) compiler\n\n\
188 \ PureScript to native (via go) compiler\n\n\
180 \Available options:\n\
189 \Available options:\n\
181 \ --help Show this help text\n\n\
190 \ --help Show this help text\n\n\
191+ \ --version Show the version number\n\n\
182 \ --run Run the generated go code directly, without building an\n\
192 \ --run Run the generated go code directly, without building an\n\
183 \ executable\n\
193 \ executable\n\
184 \ --no-build Generate go source files, but do not build an executable\n\
194 \ --no-build Generate go source files, but do not build an executable\n\
@@ -192,6 +202,9 @@ corefn = "corefn.json"
192goSrc :: String
202goSrc :: String
193goSrc = ".go"
203goSrc = ".go"
206+goproject = "GOPROJECT"
195errorNoCorefnFiles :: IO ()
208errorNoCorefnFiles :: IO ()
196errorNoCorefnFiles = do
209errorNoCorefnFiles = do
197 ioError . userError $ "no compiled purescript '" <> corefn <> "' files found –\n" <>
210 ioError . userError $ "no compiled purescript '" <> corefn <> "' files found –\n" <>