Check file datestamps before unioning PGF files

When running a command like

 gf -make -name=T L_1.pgf ... L_n.pgf

gf now checks if T.pgf exists and is up-to-date before reading and computing
the union of the L_i.pgf files.

The name (T) of the target PGF file has to be given explicitly for this to work,
since otherwise the name is not known until the union has been computed.
If the functions for reading PGF files and computing the union were lazier,
this would not be necessary...
This commit is contained in:
hallgren
2014-01-09 14:18:21 +00:00
parent 60c981d414
commit 40b8c5fb99

View File

@@ -15,6 +15,8 @@ import GF.Infra.Ident(identS)
import GF.Infra.UseIO import GF.Infra.UseIO
import GF.Infra.Option import GF.Infra.Option
import GF.Data.ErrM import GF.Data.ErrM
import GF.System.Directory
import GF.System.Catch
import Data.Maybe import Data.Maybe
import Data.Binary import Data.Binary
@@ -23,7 +25,7 @@ import qualified Data.ByteString as BSS
import qualified Data.ByteString.Lazy as BSL import qualified Data.ByteString.Lazy as BSL
import System.FilePath import System.FilePath
import System.IO import System.IO
import Control.Exception import Control.Exception(bracket)
import Control.Monad(unless,forM_) import Control.Monad(unless,forM_)
mainGFC :: Options -> [FilePath] -> IO () mainGFC :: Options -> [FilePath] -> IO ()
@@ -63,16 +65,28 @@ compileCFFiles opts fs =
writeOutputs opts pgf writeOutputs opts pgf
unionPGFFiles :: Options -> [FilePath] -> IOE () unionPGFFiles :: Options -> [FilePath] -> IOE ()
unionPGFFiles opts fs = unionPGFFiles opts fs = maybe doIt checkFirst (flag optName opts)
do pgfs <- mapM readPGFVerbose fs where
let pgf0 = foldl1 unionPGF pgfs checkFirst name =
pgf = if flag optOptimizePGF opts then optimizePGF pgf0 else pgf0 do let pgfFile = name <.> "pgf"
pgfFile = grammarName opts pgf <.> "pgf" sourceTime <- liftIO $ maximum `fmap` mapM getModificationTime fs
if pgfFile `elem` fs targetTime <- maybeIO $ getModificationTime pgfFile
then putStrLnE $ "Refusing to overwrite " ++ pgfFile if targetTime >= Just sourceTime
else writePGF opts pgf then putIfVerb opts $ pgfFile ++ " is up-to-date."
writeOutputs opts pgf else doIt
where readPGFVerbose f = putPointE Normal opts ("Reading " ++ f ++ "...") $ liftIO $ readPGF f
doIt =
do pgfs <- mapM readPGFVerbose fs
let pgf0 = foldl1 unionPGF pgfs
pgf = if flag optOptimizePGF opts then optimizePGF pgf0 else pgf0
pgfFile = grammarName opts pgf <.> "pgf"
if pgfFile `elem` fs
then putStrLnE $ "Refusing to overwrite " ++ pgfFile
else writePGF opts pgf
writeOutputs opts pgf
readPGFVerbose f =
putPointE Normal opts ("Reading " ++ f ++ "...") $ liftIO $ readPGF f
writeOutputs :: Options -> PGF -> IOE () writeOutputs :: Options -> PGF -> IOE ()
writeOutputs opts pgf = do writeOutputs opts pgf = do