Skip to content

Commit e44d045

Browse files
committed
Action/Generate: Add relocatable option
This is useful for example if you generate the haddocks and the index in CI and deploy them to another machine at a different path.
1 parent e83646e commit e44d045

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

docs/Install.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ Run `hoogle generate base filepath` to generate an index for only the `base` and
2525

2626
Run `hoogle generate --local` to query `ghc-pkg` and generate links for all packages which have documentation and Hoogle input files generated. By editing your Cabal config file you can have Cabal automatically generate such files when packages are installed. Links to the results will point at your local file system.
2727

28-
### Index a directory
28+
### Index one or more directories
2929

30-
Run `hoogle generate --local=mydir` to generate an index for the packages in `mydir`, which must contain `foo.txt` Hoogle input files. Links to the results will default to Hackage, but if `@url` directives are in the `.txt` files they can override the link destination.
30+
Run `hoogle generate --local=mydir1 --local=mydir2` to generate an index for the packages in `mydir1` and `mydir2`, which must contain `foo.txt` Hoogle input files. Links to the results will default to Hackage, but if `@url` directives are in the `.txt` files they can override the link destination.
31+
32+
### Index a directory, producing a relocatable database
33+
34+
Run `hoogle generate --relocatable --local=mydir` to generate an index that supports moving the Haddock directory to a diffent path without breaking the Haddock links.
35+
This mode only supports one `--local` directory.
3136

3237
## Searching a Hoogle database
3338

src/Action/CmdLine.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ data CmdLine
4444
,haddock :: Maybe FilePath
4545
,debug :: Bool
4646
,language :: Language
47+
,relocatable :: Bool
4748
}
4849
| Server
4950
{port :: Int
@@ -132,6 +133,7 @@ generate = Generate
132133
,count = Nothing &= name "n" &= help "Maximum number of packages to index (defaults to all)"
133134
,haddock = def &= help "Use local haddocks"
134135
,debug = def &= help "Generate debug information"
136+
,relocatable = False &= help "Generate a relocatable database"
135137
} &= help "Generate Hoogle databases"
136138

137139
server = Server

src/Action/Generate.hs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ readHaskellOnline timing settings download = do
121121
pure (cbl, want, source)
122122

123123

124-
readHaskellDirs :: Timing -> Settings -> [FilePath] -> IO (Map.Map PkgName Package, Set.Set PkgName, ConduitT () (PkgName, URL, LBStr) IO ())
125-
readHaskellDirs timing settings dirs = do
124+
readHaskellDirs
125+
:: Timing
126+
-> Settings
127+
-> Maybe FilePath
128+
-> [FilePath] -- ^ Prefix to remove from URLs to make the DB relocatable
129+
-> IO (Map.Map PkgName Package, Set.Set PkgName, ConduitT () (PkgName, URL, LBStr) IO ())
130+
readHaskellDirs timing settings prefixToRemove dirs = do
126131
files <- concatMapM listFilesRecursive dirs
127132
-- We reverse/sort the list because of #206
128133
-- Two identical package names with different versions might be foo-2.0 and foo-1.0
@@ -134,7 +139,9 @@ readHaskellDirs timing settings dirs = do
134139
let source = forM_ packages $ \(name, file) -> do
135140
src <- liftIO $ bstrReadFile file
136141
dir <- liftIO $ canonicalizePath $ takeDirectory file
137-
let url = "file://" ++ ['/' | not $ "/" `isPrefixOf` dir] ++ replace "\\" "/" dir ++ "/"
142+
let url = case prefixToRemove of
143+
Just prefix -> makeRelative prefix $ replace "\\" "/" dir ++ "/"
144+
Nothing -> "file://" ++ ['/' | not $ "/" `isPrefixOf` dir] ++ replace "\\" "/" dir ++ "/"
138145
yield (name, url, lbstrFromChunks [src])
139146
pure (Map.union
140147
(Map.fromList cabals)
@@ -237,12 +244,18 @@ actionGenerate g@Generate{..} = withTiming (if debug then Just $ replaceExtensio
237244
Haskell | Just dir <- haddock -> do
238245
warnFlagIgnored "--haddock" "set" (local_ /= []) "--local"
239246
warnFlagIgnored "--haddock" "set" (isJust download) "--download"
247+
warnFlagIgnored "--haddock" "set" relocatable "--relocatable"
240248
readHaskellHaddock timing settings dir
241249
| [""] <- local_ -> do
242250
warnFlagIgnored "--local" "used as flag (no paths)" (isJust download) "--download"
243251
readHaskellGhcpkg timing settings
244252
| [] <- local_ -> do readHaskellOnline timing settings doDownload
245-
| otherwise -> readHaskellDirs timing settings local_
253+
| relocatable, _:_:_ <- local_ ->
254+
exitFail "Error: --relocatable needs exactly one --local, or the paths will be ambiguous"
255+
| relocatable -> do
256+
prefix <- traverse canonicalizePath $ listToMaybe local_
257+
readHaskellDirs timing settings prefix local_
258+
| otherwise -> readHaskellDirs timing settings Nothing local_
246259
Frege | [] <- local_ -> readFregeOnline timing doDownload
247260
| otherwise -> errorIO "No support for local Frege databases"
248261
(cblErrs, popularity) <- evaluate $ packagePopularity cbl

src/General/Util.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ getStatsDebug = do
112112

113113

114114

115-
exitFail :: String -> IO ()
115+
exitFail :: String -> IO a
116116
exitFail msg = do
117117
hPutStrLn stderr msg
118118
exitFailure

0 commit comments

Comments
 (0)