@@ -112,7 +112,8 @@ main = do
112112 let command = case cmd of
113113 Build -> "build"
114114 Run -> "run"
115− (_, _, _, p) <- createProcess (proc "go" [command, T.unpack modPrefix <> "/Main"])
115+ project <- envVar goproject
116+ (_, _, _, p) <- createProcess (proc "go" [command, T.unpack $ project <> modPrefix <> "/Main"])
116117 waitForProcess p
117118 return ()
118119
@@ -136,8 +137,9 @@ generateCode opts baseOutpath jsonFile = do
136137transpile :: [String] -> FilePath -> FilePath -> IO ()
137138transpile opts baseOutpath jsonFile = do
138139 jsonText <- T.decodeUtf8 <$> B.readFile jsonFile
140+ project <- envVar goproject
139141 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)
141143 let mn = moduleNameToIL' $ moduleName module'
142144 implementation = prettyPrintIL asts
143145 outpath = joinPath [baseOutpath, T.unpack mn]
@@ -149,36 +151,44 @@ transpile opts baseOutpath jsonFile = do
149151writeSupportFiles :: FilePath -> IO ()
150152writeSupportFiles baseOutpath = do
151153 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'
154+ let outputdir = T.pack $ baseOutpath \\ currentDir
155+ ffiOutpath = currentDir </> "purescript-native"
159156 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")
157+ createDirectoryIfMissing True ffiOutpath
158+ writeModuleFile outputdir currentDir $(embedFile "support/go.mod.working")
159+ writeModuleFile outputdir baseOutpath $(embedFile "support/go.mod.output")
160+ writeModuleFile outputdir ffiOutpath $(embedFile "support/go.mod.ffi-loader")
161+ writeLoaderFile ffiOutpath $(embedFile "support/ffi-loader.go")
162+ where
163+ writeModuleFile :: Text -> FilePath -> B.ByteString -> IO ()
164+ writeModuleFile outputdir path modText = do
165+ let goModSource = path </> "go.mod"
166+ goModSourceExists <- doesFileExist goModSource
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'
171+ B.writeFile goModSource $ T.encodeUtf8 modText''
172+ writeLoaderFile :: FilePath -> B.ByteString -> IO ()
173+ writeLoaderFile ffiOutpath loaderText = do
174+ let loaderSource = ffiOutpath </> "ffi_loader.go"
175+ loaderSourceExists <- doesFileExist loaderSource
176+ when (not loaderSourceExists) $ do
177+ B.writeFile loaderSource loaderText
173178
174179implFileName :: Text -> FilePath
175180implFileName mn = ((\c -> if c == '.' then '_' else c) <$> T.unpack mn) <> ".go"
176181
182+envVar :: String -> IO Text
183+envVar var = do
184+ T.pack . maybe "" (<> "/") <$> lookupEnv var
185+
177186help :: String
178187help = "Usage: psgo OPTIONS COREFN-FILES\n\
179188 \ PureScript to native (via go) compiler\n\n\
180189 \Available options:\n\
181190 \ --help Show this help text\n\n\
191+ \ --version Show the version number\n\n\
182192 \ --run Run the generated go code directly, without building an\n\
183193 \ executable\n\
184194 \ --no-build Generate go source files, but do not build an executable\n\
@@ -192,6 +202,9 @@ corefn = "corefn.json"
192202goSrc :: String
193203goSrc = ".go"
194204
205+goproject :: String
206+goproject = "GOPROJECT"
207+
195208errorNoCorefnFiles :: IO ()
196209errorNoCorefnFiles = do
197210 ioError . userError $ "no compiled purescript '" <> corefn <> "' files found –\n" <>