1-- | Create a bundle to be uploaded to Stackage Server.
2{-# LANGUAGE NoImplicitPrelude #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE RecordWildCards #-}
5module Stackage.ServerBundle
18import qualified Codec.Archive.Tar as Tar
19import qualified Codec.Archive.Tar.Entry as Tar
20import qualified Codec.Compression.GZip as GZip
21import qualified Data.Map as M
22import Data.Aeson (ToJSON (..), (.=), object, FromJSON (..), (.:), withObject)
23import System.IO.Temp (withTempDirectory)
24import qualified Data.Yaml as Y
25import Filesystem (isFile, getWorkingDirectory, listDirectory, isDirectory, canonicalizePath)
26import Foreign.C.Types (CTime (CTime))
27import Stackage.BuildConstraints
28import Stackage.BuildPlan
29import Stackage.Prelude
30import System.IO.Temp (withTempDirectory)
31import qualified System.PosixCompat.Time as PC
32import qualified Text.XML as X
34import System.PosixCompat.Files (createSymbolicLink)
37epochTime :: IO Tar.EpochTime
38epochTime = (\(CTime t) -> fromIntegral t) <$> PC.epochTime
40-- | All package/versions in a build plan, including core packages.
42-- Note that this may include packages not available on Hackage.
43bpAllPackages :: BuildPlan -> Map PackageName Version
44bpAllPackages BuildPlan {..} =
45 siCorePackages bpSystemInfo ++ map ppVersion bpPackages
47serverBundle :: Tar.EpochTime
52serverBundle time title slug bp@BuildPlan {..} = GZip.compress $ Tar.write
53 [ fe "build-plan.yaml" (fromStrict $ Y.encode bp)
54 , fe "hackage" hackage
55 , fe "slug" (fromStrict $ encodeUtf8 slug)
56 , fe "desc" (fromStrict $ encodeUtf8 title)
57 , fe "core" corePackagesList
61 case Tar.toTarPath False name of
63 Right name' -> (Tar.fileEntry name' contents)
64 { Tar.entryTime = time
66 hackage = builderToLazy $ foldMap goPair $ mapToList packageMap
68 -- need to remove some packages that don't exist on Hackage
69 packageMap = foldr deleteMap (bpAllPackages bp) $ map PackageName
75 goPair (name, version) =
76 toBuilder (display name) ++
77 toBuilder (asText "-") ++
78 toBuilder (display version) ++
79 toBuilder (asText "\n")
82 builderToLazy $ toBuilder $ unlines $
83 map (\(PackageName name) -> name)
84 (M.keys $ siCorePackages bpSystemInfo)
86-- | Package name is key
87type DocMap = Map Text PackageDocs
88data PackageDocs = PackageDocs
90 , pdModules :: Map Text [Text]
91 -- ^ module name, path
93instance ToJSON PackageDocs where
94 toJSON PackageDocs {..} = object
95 [ "version" .= pdVersion
96 , "modules" .= pdModules
98instance FromJSON PackageDocs where
99 parseJSON = withObject "PackageDocs" $ \o -> PackageDocs
103docsListing :: BuildPlan
104 -> FilePath -- ^ docs directory
106docsListing bp docsDir =
107 fmap fold $ mapM go $ mapToList $ bpAllPackages bp
109 go :: (PackageName, Version) -> IO DocMap
110 go (package, version) = do -- handleAny (const $ return mempty) $ do
111 let dirname = fpFromText (concat
116 indexFP = (docsDir </> dirname </> "index.html")
120 doc <- flip X.readFile indexFP X.def
121 { X.psDecodeEntities = X.decodeHtmlEntities
123 let cursor = fromDocument doc
124 getPair x = take 1 $ do
125 href <- attribute "href" x
126 let name = concat $ x $// content
127 guard $ not $ null name
129 pairs = cursor $// attributeIs "class" "module"
130 &/ laxElement "a" >=> getPair
131 m <- fmap fold $ forM pairs $ \(href, name) -> do
132 let suffix = dirname </> fpFromText href
133 e <- isFile $ docsDir </> suffix
135 then asMap $ singletonMap name [fpToText dirname, href]
137 return $ singletonMap (display package) $ PackageDocs
138 { pdVersion = display version
143data SnapshotType = STNightly
144 | STLTS !Int !Int -- ^ major, minor
145 deriving (Show, Read, Eq, Ord)
147instance ToJSON SnapshotType where
148 toJSON STNightly = object
149 [ "type" .= asText "nightly"
151 toJSON (STLTS major minor) = object
152 [ "type" .= asText "lts"
156instance FromJSON SnapshotType where
157 parseJSON = withObject "SnapshotType" $ \o -> do
160 "nightly" -> return STNightly
164 _ -> fail $ "Unknown type for SnapshotType: " ++ unpack t
166data CreateBundleV2 = CreateBundleV2
167 { cb2Plan :: BuildPlan
168 , cb2Type :: SnapshotType
169 , cb2DocsDir :: FilePath
170 , cb2Dest :: FilePath
173-- | Create a V2 bundle, which contains the build plan, metadata, docs, and doc
175createBundleV2 :: CreateBundleV2 -> IO ()
176createBundleV2 CreateBundleV2 {..} = do
177 docsDir <- canonicalizePath cb2DocsDir
178 docMap <- docsListing cb2Plan cb2DocsDir
180 Y.encodeFile (fpToString $ docsDir </> "build-plan.yaml") cb2Plan
181 Y.encodeFile (fpToString $ docsDir </> "build-type.yaml") cb2Type
182 Y.encodeFile (fpToString $ docsDir </> "docs-map.yaml") docMap
183 void $ writeIndexStyle Nothing cb2DocsDir
185 currentDir <- getWorkingDirectory
186 files <- listDirectory docsDir
189 : fpToString (currentDir </> cb2Dest)
191 : map (fpToString . filename) files
192 cp = (proc "tar" args) { cwd = Just $ fpToString docsDir }
193 withCheckedProcess cp $ \ClosedStream Inherited Inherited -> return ()
195writeIndexStyle :: Maybe Text -- ^ snapshot id
196 -> FilePath -- ^ docs dir
198writeIndexStyle msnapid dir = do
201 $ sourceDirectory dir
202 $$ filterMC (liftIO . isDirectory)
203 =$ mapC (fpToString . filename)
205 writeFile (dir </> "index.html") $ mkIndex
208 writeFile (dir </> "style.css") styleCss
211mkIndex :: Maybe String -> [String] -> String
212mkIndex msnapid dirs = concat
213 [ "<!DOCTYPE html>\n<html lang='en'><head><title>Haddocks index</title>"
214 , "<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>"
215 , "<link rel='stylesheet' href='style.css'>"
216 , "<link rel='shortcut icon' href='http://www.stackage.org/static/img/favicon.ico' />"
218 , "<body><div class='container'>"
219 , "<div class='row'><div class='span12 col-md-12'>"
220 , "<h1>Haddock documentation index</h1>"
221 , flip foldMap msnapid $ \snapid -> concat
222 [ "<p class='return'><a href=\"http://www.stackage.org/stackage/"
224 , "\">Return to snapshot</a></p>"
227 , concatMap toLI dirs
228 , "</ul></div></div></div></body></html>"
241 [ "@media (min-width: 530px) {"
242 , "ul { -webkit-column-count: 2; -moz-column-count: 2; column-count: 2 }"
244 , "@media (min-width: 760px) {"
245 , "ul { -webkit-column-count: 3; -moz-column-count: 3; column-count: 3 }"
249 , " padding-left: 0;"
250 , " list-style-type: none;"
253 , " background: #f0f0f0;"
254 , " font-family: 'Lato', sans-serif;"
255 , " text-shadow: 1px 1px 1px #ffffff;"
256 , " font-size: 20px;"
257 , " line-height: 30px;"
258 , " padding-bottom: 5em;"
261 , " font-weight: normal;"
263 , " font-size: 45px;"
267 , " font-style: italic;"
270 , " margin-bottom: 1em;"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list