git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Support 'project' prefix for generated code modules
commit01be38e8bdae1355f6d0fde975accf63b4044b81 authorAndy Arvanitis <andyarvanitis@gmail.com> dateFri 9 Aug 2019, 18:50 (7 years ago) parentdcfe31e
6 files changed+45−32
app/Main.hs+35−22 src/CodeGen/IL.hs+3−3 src/CodeGen/IL/Printer.hs+3−3 support/go.mod.ffi-loader+1−1 support/go.mod.output+1−1 support/go.mod.working+2−2
app/Main.hs+35−22
@@ -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" <>
src/CodeGen/IL.hs+3−3
@@ -48,9 +48,9 @@ moduleToIL
4848 -- . (Monad m, MonadReader Options m, MonadSupply m, MonadError MultipleErrors m)
4949 . (Monad m, MonadSupply m)
5050 => Module Ann
51− -> Maybe AST
51+ -> Text
5252 -> m (Text, [Text], [AST], Text, Text)
53−moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
53+moduleToIL (Module _ coms mn _ imps _ foreigns decls) project =
5454 do
5555 let usedNames = concatMap getNames decls
5656 let mnLookup = renameImports usedNames imps
@@ -62,7 +62,7 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
6262 foreigns' = moduleIdentToIL <$> foreigns
6363 interface = interfaceSource modName values foreigns
6464 imports = nub . concat $ importToIL <$> optimized'
65− implHeader = implHeaderSource modName imports ""
65+ implHeader = implHeaderSource modName imports project
6666 implFooter = implFooterSource (runModuleName mn) foreigns
6767 return $ (interface, foreigns', optimized', implHeader, implFooter)
6868 where
src/CodeGen/IL/Printer.hs+3−3
@@ -439,20 +439,20 @@ interfaceSource :: Text -> [(Text,Bool)] -> [Ident] -> Text
439439interfaceSource _ _ _ = ""
440440
441441implHeaderSource :: Text -> [Text] -> Text -> Text
442−implHeaderSource mn imports _ =
442+implHeaderSource mn imports otherPrefix =
443443 let imports' = T.replace "_" "." <$> imports in
444444 "// Code generated by psgo. DO NOT EDIT.\n\n" <>
445445 "package " <> (if mn == "Main" then "main" else mn) <> "\n\n" <>
446446 "import . \"" <> runtime <> "\"\n" <>
447447 (if mn == "Main"
448− then "import _ \"" <> ffiLoader <> "\"\n"
448+ then "import _ \"" <> otherPrefix <> ffiLoader <> "\"\n"
449449 else "\n") <>
450450 "import (\n" <>
451451 (T.concat $ formatImport <$> imports') <> ")\n\n" <>
452452 "type _ = Any\n\n"
453453 where
454454 formatImport :: Text -> Text
455− formatImport s = " \"" <> modPrefix <> "/" <> s <> "\"\n"
455+ formatImport s = " \"" <> otherPrefix <> modPrefix <> "/" <> s <> "\"\n"
456456
457457implFooterSource :: Text -> [Ident] -> Text
458458implFooterSource mn foreigns =
support/go.mod.ffi-loader+1−1
@@ -1,2 +1,2 @@
1−module purescript-native/ffi-loader
1+module $PROJECT/purescript-native/ffi-loader
22
support/go.mod.output+1−1
@@ -1,2 +1,2 @@
1−module purescript-native/output
1+module $PROJECT/purescript-native/output
22
support/go.mod.working+2−2
@@ -1,5 +1,5 @@
11module example.com/example
22
3−replace purescript-native/output => ./$OUTPUT/
3+replace $PROJECT/purescript-native/output => ./$OUTPUT/
44
5−replace purescript-native/ffi-loader => ./purescript-native/
5+replace $PROJECT/purescript-native/ffi-loader => ./purescript-native/