From 866b8260e743d3833f06279f7b8d7f50a490629b Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Fri, 15 Jul 2022 07:42:54 -0500 Subject: [PATCH] Remove requirement that module name be main (#285) * Remove requirement that module name be main * Add changelog --- CHANGELOG.md | 1 + server/Main.hs | 45 +++++++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25a4f37d..6bc78acb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ New features: Bugfixes: Other improvements: +- Drop requirement that module name be `Main` (#285 by @JordanMartinez) ## [v2022-07-12.1](https://github.com/purescript/trypurescript/releases/tag/v2022-07-12.1) diff --git a/server/Main.hs b/server/Main.hs index 981b0b7f..9f317e6d 100644 --- a/server/Main.hs +++ b/server/Main.hs @@ -25,6 +25,8 @@ import qualified Data.IORef as IORef import Data.List (nubBy) import qualified Data.List.NonEmpty as NE import qualified Data.Map as M +import Data.Set (Set) +import qualified Data.Set as Set import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.Encoding as T @@ -118,10 +120,11 @@ buildMakeActions codegenRef = outputPrimDocs :: Make.Make () outputPrimDocs = pure () -server :: [P.ExternsFile] -> P.Env -> P.Environment -> Int -> IO () -server externs initNamesEnv initEnv port = do +server :: [N.ModuleName] -> [P.ExternsFile] -> P.Env -> P.Environment -> Int -> IO () +server allModuleNames externs initNamesEnv initEnv port = do codegenRef <- IORef.newIORef Nothing let makeActions = buildMakeActions codegenRef + let modNames = Set.fromList allModuleNames let compile :: Text -> IO (Either Error ([P.JSONError], JS)) compile input | T.length input > 20000 = return $ Left $ OtherError "Please limit your input to 20000 characters" @@ -134,20 +137,24 @@ server externs initNamesEnv initEnv port = do (_, Left parserErrors) -> return $ Left $ toCompilerErrors parserErrors - (parserWarnings, Right m) | P.getModuleName m == P.ModuleName "Main" -> do - (makeResult, warnings) <- Make.runMake P.defaultOptions $ Make.rebuildModule' makeActions initNamesEnv externs m - codegenResult <- IORef.readIORef codegenRef - return $ case makeResult of - Left errors -> - Left $ CompilerErrors $ toJsonErrors errors - Right _ | Just js <- codegenResult -> do - let ws = warnings <> CST.toMultipleWarnings "" parserWarnings - Right (toJsonErrors ws, js) - Right _ -> - Left $ OtherError "Failed to read the results of codegen." - - (_, Right _) -> - return $ Left $ OtherError "The name of the main module should be Main." + (parserWarnings, Right m) + | Set.notMember (P.getModuleName m) modNames -> do + (makeResult, warnings) <- Make.runMake P.defaultOptions $ Make.rebuildModule' makeActions initNamesEnv externs m + codegenResult <- IORef.readIORef codegenRef + return $ case makeResult of + Left errors -> + Left $ CompilerErrors $ toJsonErrors errors + Right _ | Just js <- codegenResult -> do + let ws = warnings <> CST.toMultipleWarnings "" parserWarnings + Right (toJsonErrors ws, js) + Right _ -> + Left $ OtherError "Failed to read the results of codegen." + + | otherwise -> do + let + modName = N.runModuleName $ P.getModuleName m + return $ Left $ OtherError $ + "The name of the module you defined " <> modName <> " clashes with another module in the package set. Rename the module to something else (e.g. 'Main')." scottyOpts (getOpts port) $ do get "/" $ @@ -242,7 +249,9 @@ main = do modules <- ExceptT $ I.loadAllModules inputFiles (exts, env) <- ExceptT . I.runMake . I.make $ map (second CST.pureResult) modules namesEnv <- fmap fst . runWriterT $ foldM P.externsEnv P.primEnv exts - pure (exts, namesEnv, env) + let + allModuleNames = fmap (P.getModuleName . snd) modules + pure (allModuleNames, exts, namesEnv, env) case e of Left err -> print err >> exitFailure - Right (exts, namesEnv, env) -> server exts namesEnv env port + Right (allModuleNames, exts, namesEnv, env) -> server allModuleNames exts namesEnv env port