Skip to content

Commit cc3a6f4

Browse files
authored
Added "--dest PATH" flag (#321)
* Error message should reference "grc" rather than "gr". * Added the "--dest PATH" flag. PATH is the location where all generated Haskell files will be written to. These changes preserve the original behavior in that if "--dest" is not specified then the original behavior is used. Thus, this is completely backward compatible. * Fixing an accidental change.
1 parent 130893e commit cc3a6f4

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

compiler/app/Language/Granule/Compiler.hs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import Control.Exception (SomeException, displayException, try)
99
import Control.Monad ((<=<), forM_, when)
1010
import Development.GitRev
1111
import Data.Char (isSpace)
12-
import Data.List (isPrefixOf, stripPrefix)
12+
import Data.List (stripPrefix)
1313
import Data.Maybe (fromMaybe)
1414
import Data.Version (showVersion)
1515

1616
import System.Directory (getAppUserDataDirectory, getCurrentDirectory)
17-
import System.FilePath (takeFileName)
17+
import System.FilePath ((</>), splitFileName)
1818
import "Glob" System.FilePath.Glob (glob)
1919
import Options.Applicative
2020
import qualified Options.Applicative.Help.Pretty as OA
@@ -42,20 +42,23 @@ compileGrOnFiles globPatterns config = let ?globals = grGlobals config in do
4242
pwd <- getCurrentDirectory
4343
forM_ globPatterns $ \pat -> do
4444
paths <- glob pat
45+
debugM "Glob paths: " $ show paths
4546
case paths of
4647
[] -> error "No matching files"
47-
_ -> forM_ paths $ \path -> do
48-
let fileName = if pwd `isPrefixOf` path then takeFileName path else path
48+
_ -> forM_ paths $ \inPath -> do
49+
let (cwd, fileName) = splitFileName inPath
4950
let ?globals = ?globals{ globalsSourceFilePath = Just fileName } in do
5051
printInfo $ "Checking " <> fileName <> "..."
5152
src <- preprocess
5253
(rewriter config)
5354
(keepBackup config)
54-
path
55+
inPath
5556
(literateEnvName config)
5657
hsCode <- compile config src
57-
debugM "Code: " hsCode
58-
let outPath = changeFileExtension path
58+
debugM "Code: " hsCode
59+
let destPath = fromMaybe cwd $ grWriteDest config
60+
debugM "destPath: " destPath
61+
let outPath = changeFileExtension $ destPath </> fileName
5962
printSuccess $ "Writing " ++ outPath
6063
writeFile outPath hsCode
6164

@@ -115,6 +118,7 @@ data GrConfig = GrConfig
115118
, grLiterateEnvName :: Maybe String
116119
, grShowVersion :: Bool
117120
, grGlobals :: Globals
121+
, grWriteDest :: Maybe FilePath
118122
}
119123

120124
rewriter :: GrConfig -> Maybe Rewriter
@@ -133,15 +137,17 @@ instance Semigroup GrConfig where
133137
, grLiterateEnvName = grLiterateEnvName c1 <|> grLiterateEnvName c2
134138
, grGlobals = grGlobals c1 <> grGlobals c2
135139
, grShowVersion = grShowVersion c1 || grShowVersion c2
140+
, grWriteDest = grWriteDest c1 <|> grWriteDest c2
136141
}
137142

138143
instance Monoid GrConfig where
139144
mempty = GrConfig
140-
{ grRewriter = Nothing
145+
{ grRewriter = Nothing
141146
, grKeepBackup = Nothing
142147
, grLiterateEnvName = Nothing
143148
, grGlobals = mempty
144149
, grShowVersion = False
150+
, grWriteDest = Nothing
145151
}
146152

147153
getGrConfig :: IO ([FilePath], GrConfig)
@@ -166,7 +172,7 @@ getGrConfig = do
166172
Right Nothing -> do
167173
printInfo . red . unlines $
168174
[ "Couldn't parse granule configuration file at " <> configFile
169-
, "Run `gr --help` to see a list of accepted flags."
175+
, "Run `grc --help` to see a list of accepted flags."
170176
]
171177
pure mempty
172178
Right (Just config) -> pure config
@@ -358,6 +364,12 @@ parseGrConfig = info (go <**> helper) $ briefDesc
358364
flag Nothing (Just True)
359365
$ long "raw-data"
360366
<> help "Show raw data of benchmarking data for synthesis."
367+
368+
grWriteDest <-
369+
optional $ strOption
370+
$ long "dest"
371+
<> help "Path to the location to write generated Haskell files."
372+
<> metavar "PATH"
361373

362374
pure
363375
( globPatterns
@@ -395,6 +407,7 @@ parseGrConfig = info (go <**> helper) $ briefDesc
395407
, globalsExtensions = []
396408
, globalsDocMode = Nothing
397409
}
410+
, grWriteDest
398411
}
399412
)
400413
where

0 commit comments

Comments
 (0)