Skip to content

Commit 7de85f0

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 f759a0d commit 7de85f0

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
@@ -45,6 +45,7 @@ data CmdLine
4545
,haddock :: Maybe FilePath
4646
,debug :: Bool
4747
,language :: Language
48+
,relocatable :: Bool
4849
}
4950
| Server
5051
{port :: Int
@@ -151,6 +152,7 @@ generate = Generate
151152
,count = Nothing &= name "n" &= help "Maximum number of packages to index (defaults to all)"
152153
,haddock = def &= help "Use local haddocks"
153154
,debug = def &= help "Generate debug information"
155+
,relocatable = False &= help "Generate a relocatable database"
154156
} &= help "Generate Hoogle databases"
155157

156158
server = Server

src/Action/Generate.hs

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

124124

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

src/General/Util.hs

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

114114

115115

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

0 commit comments

Comments
 (0)