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.Option
import GF.Data.ErrM
import GF.System.Directory
import GF.System.Catch
import Data.Maybe
import Data.Binary
@@ -23,7 +25,7 @@ import qualified Data.ByteString as BSS
import qualified Data.ByteString.Lazy as BSL
import System.FilePath
import System.IO
import Control.Exception
import Control.Exception(bracket)
import Control.Monad(unless,forM_)
mainGFC :: Options -> [FilePath] -> IO ()
@@ -63,16 +65,28 @@ compileCFFiles opts fs =
writeOutputs opts pgf
unionPGFFiles :: Options -> [FilePath] -> IOE ()
unionPGFFiles opts fs =
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
where readPGFVerbose f = putPointE Normal opts ("Reading " ++ f ++ "...") $ liftIO $ readPGF f
unionPGFFiles opts fs = maybe doIt checkFirst (flag optName opts)
where
checkFirst name =
do let pgfFile = name <.> "pgf"
sourceTime <- liftIO $ maximum `fmap` mapM getModificationTime fs
targetTime <- maybeIO $ getModificationTime pgfFile
if targetTime >= Just sourceTime
then putIfVerb opts $ pgfFile ++ " is up-to-date."
else doIt
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 opts pgf = do