git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Make golang module support more robust
commit8c9da9b7aaf39a36117835193ffad72abe53be93 authorAndy Arvanitis <andyarvanitis@gmail.com> dateFri 16 Aug 2019, 21:41 (7 years ago) parent57f696b
3 files changed+40−22
app/Main.hs+29−12 src/CodeGen/IL/Printer.hs+10−5 support/go.mod.working+1−5
app/Main.hs+29−12
@@ -112,9 +112,8 @@ main = do
112112 let command = case cmd of
113113 Build -> "build"
114114 Run -> "run"
115− project <- envVar goproject
116− (_, _, _, p) <- createProcess (proc "go" [command, T.unpack $ project <> modPrefix <> "/Main"])
117− waitForProcess p
115+ project <- projectEnv
116+ callProcess "go" [command, T.unpack project </> modPrefix' </> "Main"]
118117 return ()
119118
120119generateCode :: [String] -> FilePath -> FilePath -> IO ()
@@ -137,7 +136,7 @@ generateCode opts baseOutpath jsonFile = do
137136transpile :: [String] -> FilePath -> FilePath -> IO ()
138137transpile opts baseOutpath jsonFile = do
139138 jsonText <- T.decodeUtf8 <$> B.readFile jsonFile
140− project <- envVar goproject
139+ project <- projectEnv
141140 let module' = jsonToModule $ parseJson jsonText
142141 ((_, foreigns, asts, implHeader, implFooter), _) <- runSupplyT 5 (moduleToIL module' project)
143142 let mn = moduleNameToIL' $ moduleName module'
@@ -152,7 +151,7 @@ writeSupportFiles :: FilePath -> IO ()
152151writeSupportFiles baseOutpath = do
153152 currentDir <- getCurrentDirectory
154153 let outputdir = T.pack $ baseOutpath \\ currentDir
155− ffiOutpath = currentDir </> "purescript-native"
154+ ffiOutpath = currentDir </> subdir
156155 createDirectoryIfMissing True baseOutpath
157156 createDirectoryIfMissing True ffiOutpath
158157 writeModuleFile outputdir currentDir $(embedFile "support/go.mod.working")
@@ -165,10 +164,16 @@ writeSupportFiles baseOutpath = do
165164 let goModSource = path </> "go.mod"
166165 goModSourceExists <- doesFileExist goModSource
167166 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''
167+ currentDir <- getCurrentDirectory
168+ project <- projectEnv
169+ let project' = T.unpack project
170+ outputdir' = tail $ T.unpack outputdir
171+ modText' = T.replace "$PROJECT" project $ T.decodeUtf8 modText
172+ replaceLoader = project' </> ffiLoader' <> "=" <> currentDir </> subdir
173+ replaceOutput = project' </> modPrefix' <> "=" <> currentDir </> outputdir'
174+ B.writeFile goModSource $ T.encodeUtf8 modText'
175+ callProcess "go" ["mod", "edit", "-replace", replaceLoader]
176+ callProcess "go" ["mod", "edit", "-replace", replaceOutput]
172177 writeLoaderFile :: FilePath -> B.ByteString -> IO ()
173178 writeLoaderFile ffiOutpath loaderText = do
174179 let loaderSource = ffiOutpath </> "ffi_loader.go"
@@ -179,9 +184,9 @@ writeSupportFiles baseOutpath = do
179184implFileName :: Text -> FilePath
180185implFileName mn = ((\c -> if c == '.' then '_' else c) <$> T.unpack mn) <> ".go"
181186
182−envVar :: String -> IO Text
183−envVar var = do
184− T.pack . maybe "" (<> "/") <$> lookupEnv var
187+projectEnv :: IO Text
188+projectEnv = do
189+ T.pack . fromMaybe defaultProject <$> lookupEnv goproject
185190
186191help :: String
187192help = "Usage: psgo OPTIONS COREFN-FILES\n\
@@ -205,6 +210,18 @@ goSrc = ".go"
205210goproject :: String
206211goproject = "GOPROJECT"
207212
213+defaultProject :: String
214+defaultProject = "project.localhost"
215+
216+modPrefix' :: String
217+modPrefix' = T.unpack modPrefix
218+
219+ffiLoader' :: String
220+ffiLoader' = T.unpack ffiLoader
221+
222+subdir :: String
223+subdir = T.unpack modLabel
224+
208225errorNoCorefnFiles :: IO ()
209226errorNoCorefnFiles = do
210227 ioError . userError $ "no compiled purescript '" <> corefn <> "' files found –\n" <>
src/CodeGen/IL/Printer.hs+10−5
@@ -7,7 +7,9 @@ module CodeGen.IL.Printer
77 , implHeaderSource
88 , implFooterSource
99 , isLiteral
10+ , modLabel
1011 , modPrefix
12+ , ffiLoader
1113 ) where
1214
1315import Prelude.Compat
@@ -445,14 +447,14 @@ implHeaderSource mn imports otherPrefix =
445447 "package " <> (if mn == "Main" then "main" else mn) <> "\n\n" <>
446448 "import . \"" <> runtime <> "\"\n" <>
447449 (if mn == "Main"
448− then "import _ \"" <> otherPrefix <> ffiLoader <> "\"\n"
450+ then "import _ \"" <> otherPrefix <> "/" <> ffiLoader <> "\"\n"
449451 else "\n") <>
450452 "import (\n" <>
451453 (T.concat $ formatImport <$> imports') <> ")\n\n" <>
452454 "type _ = Any\n\n"
453455 where
454456 formatImport :: Text -> Text
455− formatImport s = " \"" <> otherPrefix <> modPrefix <> "/" <> s <> "\"\n"
457+ formatImport s = " \"" <> otherPrefix <> "/" <> modPrefix <> "/" <> s <> "\"\n"
456458
457459implFooterSource :: Text -> [Ident] -> Text
458460implFooterSource mn foreigns =
@@ -496,14 +498,17 @@ foreignMod = "Foreign"
496498foreignDict :: Text
497499foreignDict = "foreign"
498500
501+modLabel :: Text
502+modLabel = "purescript-native"
503+
499504modPrefix :: Text
500−modPrefix = "purescript-native/output"
505+modPrefix = modLabel <> "/output"
501506
502507runtime :: Text
503−runtime = "github.com/purescript-native/go-runtime"
508+runtime = "github.com/" <> modLabel <> "/go-runtime"
504509
505510ffiLoader :: Text
506−ffiLoader = "purescript-native/ffi-loader"
511+ffiLoader = modLabel <> "/ffi-loader"
507512
508513initName :: Text -> Text
509514initName s = "ₒ" <> s
support/go.mod.working+1−5
@@ -1,5 +1 @@
1−module example.com/example
2−
3−replace $PROJECT/purescript-native/output => ./$OUTPUT/
4−
5−replace $PROJECT/purescript-native/ffi-loader => ./purescript-native/
1+module $PROJECT/example