git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #583 from purescript/557

Prefix output with a 'Generated by psc version X.Y.Z' comment #557

commit94e0c349efa523da48be26235408461c8303502c authorPhil Freeman <paf31@cantab.net> dateThu 28 Aug 2014, 16:27 (12 years ago) parent26c33ff147a460
6 files changed+41−23
psc-make/Main.hs+15−7 psc/Main.hs+11−4 psci/Main.hs+3−3 src/Language/PureScript.hs+10−8 src/Language/PureScript/Options.hs+1 tests/Main.hs+1−1
psc-make/Main.hs+15−7
@@ -66,24 +66,28 @@ instance P.MonadMake Make where
6666 liftError = either throwError return
6767 progress = makeIO . U.putStrLn
6868
69−compile :: FilePath -> [FilePath] -> FilePath -> P.Options -> IO ()
70−compile prelude input outputDir opts = do
69+compile :: FilePath -> [FilePath] -> FilePath -> P.Options -> Bool -> IO ()
70+compile prelude input outputDir opts usePrefix = do
7171 modules <- readInput allInputFiles
7272 case modules of
7373 Left err -> do
7474 U.print err
7575 exitFailure
7676 Right ms -> do
77− e <- runMake $ P.make outputDir opts ms
77+ e <- runMake $ P.make outputDir opts ms prefix
7878 case e of
7979 Left err -> do
8080 U.putStrLn err
8181 exitFailure
8282 Right _ -> do
8383 exitSuccess
84− where
85− allInputFiles | P.optionsNoPrelude opts = input
86− | otherwise = prelude : input
84+ where
85+ prefix = if usePrefix
86+ then ["Generated by psc-make version " ++ showVersion Paths.version]
87+ else []
88+
89+ allInputFiles | P.optionsNoPrelude opts = input
90+ | otherwise = prelude : input
8791
8892mkdirp :: FilePath -> IO ()
8993mkdirp = createDirectoryIfMissing True . takeDirectory
@@ -123,8 +127,12 @@ verboseErrors = value $ flag $ (optInfo [ "v", "verbose-errors" ])
123127options :: Term P.Options
124128options = P.Options <$> noPrelude <*> noTco <*> performRuntimeTypeChecks <*> noMagicDo <*> pure Nothing <*> noOpts <*> pure Nothing <*> pure [] <*> pure [] <*> verboseErrors
125129
130+noPrefix :: Term Bool
131+noPrefix = value $ flag $ (optInfo ["p", "no-prefix" ])
132+ { optDoc = "Do not include comment header"}
133+
126134term :: FilePath -> Term (IO ())
127−term prelude = compile prelude <$> inputFiles <*> outputDirectory <*> options
135+term prelude = compile prelude <$> inputFiles <*> outputDirectory <*> options <*> (not <$> noPrefix)
128136
129137termInfo :: TermInfo
130138termInfo = defTI
psc/Main.hs+11−4
@@ -44,15 +44,15 @@ readInput (Just input) = fmap collect $ forM input $ \inputFile -> do
4444 collect :: [(FilePath, Either ParseError [P.Module])] -> Either ParseError [(FilePath, P.Module)]
4545 collect = fmap concat . sequence . map (\(fp, e) -> fmap (map ((,) fp)) e)
4646
47−compile :: FilePath -> P.Options -> Bool -> [FilePath] -> Maybe FilePath -> Maybe FilePath -> IO ()
48−compile prelude opts stdin input output externs = do
47+compile :: FilePath -> P.Options -> Bool -> [FilePath] -> Maybe FilePath -> Maybe FilePath -> Bool -> IO ()
48+compile prelude opts stdin input output externs usePrefix = do
4949 modules <- readInput stdInOrInputFiles
5050 case modules of
5151 Left err -> do
5252 U.hPutStr stderr $ show err
5353 exitFailure
5454 Right ms -> do
55− case P.compile opts (map snd ms) of
55+ case P.compile opts (map snd ms) prefix of
5656 Left err -> do
5757 U.hPutStrLn stderr err
5858 exitFailure
@@ -69,6 +69,9 @@ compile prelude opts stdin input output externs = do
6969 stdInOrInputFiles | stdin = Nothing
7070 | P.optionsNoPrelude opts = Just input
7171 | otherwise = Just $ prelude : input
72+ prefix = if usePrefix
73+ then ["Generated by psc version " ++ showVersion Paths.version]
74+ else []
7275
7376mkdirp :: FilePath -> IO ()
7477mkdirp = createDirectoryIfMissing True . takeDirectory
@@ -129,11 +132,15 @@ verboseErrors :: Term Bool
129132verboseErrors = value $ flag $ (optInfo [ "v", "verbose-errors" ])
130133 { optDoc = "Display verbose error messages" }
131134
135+noPrefix :: Term Bool
136+noPrefix = value $ flag $ (optInfo ["no-prefix" ])
137+ { optDoc = "Do not include comment header"}
138+
132139options :: Term P.Options
133140options = P.Options <$> noPrelude <*> noTco <*> performRuntimeTypeChecks <*> noMagicDo <*> runMain <*> noOpts <*> (Just <$> browserNamespace) <*> dceModules <*> codeGenModules <*> verboseErrors
134141
135142term :: FilePath -> Term (IO ())
136−term prelude = compile prelude <$> options <*> useStdIn <*> inputFiles <*> outputFile <*> externsFile
143+term prelude = compile prelude <$> options <*> useStdIn <*> inputFiles <*> outputFile <*> externsFile <*> (not <$> noPrefix)
137144
138145termInfo :: TermInfo
139146termInfo = defTI
psci/Main.hs+3−3
@@ -276,7 +276,7 @@ handleDeclaration :: P.Expr -> PSCI ()
276276handleDeclaration value = do
277277 st <- PSCI $ lift get
278278 let m = createTemporaryModule True st value
279− e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)])
279+ e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
280280 case e of
281281 Left err -> PSCI $ outputStrLn err
282282 Right _ -> do
@@ -295,7 +295,7 @@ handleTypeOf :: P.Expr -> PSCI ()
295295handleTypeOf value = do
296296 st <- PSCI $ lift get
297297 let m = createTemporaryModule False st value
298− e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)])
298+ e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
299299 case e of
300300 Left err -> PSCI $ outputStrLn err
301301 Right env' ->
@@ -311,7 +311,7 @@ handleKindOf typ = do
311311 st <- PSCI $ lift get
312312 let m = createTemporaryModuleForKind st typ
313313 mName = P.ModuleName [P.ProperName "$PSCI"]
314− e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)])
314+ e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
315315 case e of
316316 Left err -> PSCI $ outputStrLn err
317317 Right env' ->
src/Language/PureScript.hs+10−8
@@ -67,11 +67,11 @@ import System.FilePath (pathSeparator)
6767--
6868-- * Pretty-print the generated Javascript
6969--
70−compile :: Options -> [Module] -> Either String (String, String, Environment)
70+compile :: Options -> [Module] -> [String] -> Either String (String, String, Environment)
7171compile = compile' initEnvironment
7272
73−compile' :: Environment -> Options -> [Module] -> Either String (String, String, Environment)
74−compile' env opts ms = do
73+compile' :: Environment -> Options -> [Module] -> [String] -> Either String (String, String, Environment)
74+compile' env opts ms prefix = do
7575 (sorted, _) <- sortModules $ map importPrim $ if optionsNoPrelude opts then ms else (map importPrelude ms)
7676 (desugared, nextVar) <- stringifyErrorStack True $ runSupplyT 0 $ desugar sorted
7777 (elaborated, env') <- runCheck' opts env $ forM desugared $ typeCheckModule mainModuleIdent
@@ -84,7 +84,8 @@ compile' env opts ms = do
8484 let js = evalSupply nextVar $ concat <$> mapM (\m -> moduleToJs Globals opts m env') modulesToCodeGen
8585 let exts = intercalate "\n" . map (`moduleToPs` env') $ modulesToCodeGen
8686 js' <- generateMain env' opts js
87− return (prettyPrintJS js', exts, env')
87+ let pjs = unlines $ map ("// " ++) prefix ++ [prettyPrintJS js']
88+ return (pjs, exts, env')
8889 where
8990 mainModuleIdent = moduleNameFromString <$> optionsMain opts
9091
@@ -169,8 +170,8 @@ class MonadMake m where
169170-- If timestamps have not changed, the externs file can be used to provide the module's types without
170171-- having to typecheck the module again.
171172--
172−make :: (Functor m, Applicative m, Monad m, MonadMake m) => FilePath -> Options -> [(FilePath, Module)] -> m Environment
173−make outputDir opts ms = do
173+make :: (Functor m, Applicative m, Monad m, MonadMake m) => FilePath -> Options -> [(FilePath, Module)] -> [String] -> m Environment
174+make outputDir opts ms prefix = do
174175 let filePathMap = M.fromList (map (\(fp, Module mn _ _) -> (mn, fp)) ms)
175176
176177 (sorted, graph) <- liftError $ sortModules $ map importPrim $ if optionsNoPrelude opts then map snd ms else (map (importPrelude . snd) ms)
@@ -217,8 +218,9 @@ make outputDir opts ms = do
217218 let mod' = Module moduleName' regrouped exps
218219 let [renamed] = renameInModules [mod']
219220
220− js <- prettyPrintJS <$> moduleToJs CommonJS opts renamed env'
221− let exts = moduleToPs renamed env'
221+ pjs <- prettyPrintJS <$> moduleToJs CommonJS opts renamed env'
222+ let js = unlines $ map ("// " ++) prefix ++ [pjs]
223+ let exts = unlines $ map ("-- " ++ ) prefix ++ [moduleToPs renamed env']
222224
223225 lift $ writeTextFile jsFile js
224226 lift $ writeTextFile externsFile exts
src/Language/PureScript/Options.hs+1
@@ -61,6 +61,7 @@ data Options = Options {
6161 -- Verbose error message
6262 --
6363 , optionsVerboseErrors :: Bool
64+
6465 } deriving Show
6566
6667-- |
tests/Main.hs+1−1
@@ -40,7 +40,7 @@ compile opts inputFiles = do
4040 case modules of
4141 Left parseError ->
4242 return (Left $ show parseError)
43− Right ms -> return $ P.compile opts ms
43+ Right ms -> return $ P.compile opts ms []
4444
4545assert :: FilePath -> P.Options -> FilePath -> (Either String (String, String, P.Environment) -> IO (Maybe String)) -> IO ()
4646assert preludeExterns opts inputFile f = do