hopefully the last revision of the relative paths handling algorithm

This commit is contained in:
krasimir
2009-10-06 10:27:34 +00:00
parent 332dbf7b9b
commit cbcdae9148
8 changed files with 43 additions and 41 deletions

View File

@@ -7,7 +7,7 @@ module GF.Infra.Option
SISRFormat(..), Optimization(..), CFGTransform(..), HaskellOption(..),
Dump(..), Printer(..), Recomp(..), BuildParser(..),
-- * Option parsing
parseOptions, parseModuleOptions,
parseOptions, parseModuleOptions, fixRelativeLibPaths,
-- * Option pretty-printing
optionsGFO,
optionsPGF,
@@ -186,29 +186,27 @@ instance Show Options where
-- Option parsing
parseOptions :: FilePath -- ^ if there are relative file paths they will be interpreted as relative to this path
-> [String] -- ^ list of string arguments
parseOptions :: [String] -- ^ list of string arguments
-> Err (Options, [FilePath])
parseOptions root args
parseOptions args
| not (null errs) = errors errs
| otherwise = do opts <- liftM concatOptions $ sequence optss
return (fixRelativeLibPaths opts, files)
return (opts, files)
where
(optss, files, errs) = getOpt RequireOrder optDescr args
fixRelativeLibPaths (Options o) = Options (fixPathFlags . o)
where
fixPathFlags f@(Flags{optLibraryPath=path}) = f{optLibraryPath=map (root </>) path}
parseModuleOptions :: FilePath -- ^ if there are relative file paths they will be interpreted as relative to this path
-> [String] -- ^ list of string arguments
parseModuleOptions :: [String] -- ^ list of string arguments
-> Err Options
parseModuleOptions root args = do
(opts,nonopts) <- parseOptions root args
parseModuleOptions args = do
(opts,nonopts) <- parseOptions args
if null nonopts
then return opts
else errors $ map ("Non-option among module options: " ++) nonopts
fixRelativeLibPaths curr_dir lib_dir (Options o) = Options (fixPathFlags . o)
where
fixPathFlags f@(Flags{optLibraryPath=path}) = f{optLibraryPath=concatMap (\dir -> [curr_dir </> dir, lib_dir </> dir]) path}
-- Showing options
-- | Pretty-print the options that are preserved in .gfo files.

View File

@@ -57,8 +57,8 @@ type FullPath = String
gfLibraryPath = "GF_LIB_PATH"
gfGrammarPathVar = "GF_GRAMMAR_PATH"
getLibraryPath :: Options -> IO FilePath
getLibraryPath opts =
getLibraryDirectory :: Options -> IO FilePath
getLibraryDirectory opts =
case flag optGFLibPath opts of
Just path -> return path
Nothing -> catch
@@ -66,19 +66,19 @@ getLibraryPath opts =
(\ex -> getDataDir >>= \path -> return (path </> "lib"))
getGrammarPath :: FilePath -> IO [FilePath]
getGrammarPath lib_path = do
catch (fmap splitSearchPath $ getEnv gfGrammarPathVar) (\_ -> return [lib_path </> "prelude"]) -- e.g. GF_GRAMMAR_PATH
getGrammarPath lib_dir = do
catch (fmap splitSearchPath $ getEnv gfGrammarPathVar) (\_ -> return [lib_dir </> "prelude"]) -- e.g. GF_GRAMMAR_PATH
-- | extends the search path with the
-- 'gfLibraryPath' and 'gfGrammarPathVar'
-- environment variables. Returns only existing paths.
extendPathEnv :: Options -> FilePath -> IO [FilePath]
extendPathEnv opts fdir = do
opt_paths <- return $ flag optLibraryPath opts -- e.g. paths given as options
lib_path <- getLibraryPath opts -- e.g. GF_LIB_PATH
grm_paths <- getGrammarPath lib_path -- e.g. GF_GRAMMAR_PATH
let paths = opt_paths ++ [lib_path] ++ grm_paths
ps <- liftM (nub . concat) $ mapM allSubdirs paths
extendPathEnv :: Options -> IO [FilePath]
extendPathEnv opts = do
opt_path <- return $ flag optLibraryPath opts -- e.g. paths given as options
lib_dir <- getLibraryDirectory opts -- e.g. GF_LIB_PATH
grm_path <- getGrammarPath lib_dir -- e.g. GF_GRAMMAR_PATH
let paths = opt_path ++ [lib_dir] ++ grm_path
ps <- liftM concat $ mapM allSubdirs paths
mapM canonicalizePath ps
where
allSubdirs :: FilePath -> IO [FilePath]