Commits

Stefan Saasen committed f067323

Add simple build.hs script to extract the command list overview from the man pages

build.hs will:

* Extract the command name/short description from the main/git-* man pages.
* Replace the "Commands" section in the README file with a properly formatted
list of commands

Run manually:

$> runhaskell build.hs

This can be executed as part of a post-commit script to automatically update
the README file.

  • Participants
  • Parent commits 0730ea8

Comments (0)

Files changed (1)

+module Main where
+
+import System.Directory
+import Control.Monad
+import Data.List
+
+extractCommandName :: String -> String
+extractCommandName input = dropWhile (not . isPrefixOf ".SH NAME") (lines input) !! 1
+
+-- | Extract the command name and short description from
+-- the man pages
+extractCommandOverview :: FilePath -> IO [String]
+extractCommandOverview path = do
+    entries <- getDirectoryContents path
+    let files = map (\e -> path ++ "/" ++ e) $ filter (isPrefixOf "git-") entries
+    mapM (fmap extractCommandName . readFile) files
+
+replaceCommandsIn :: FilePath -> [String] -> IO String
+replaceCommandsIn path content = do
+    existing <- readFile path
+    let new = break (== separator) $ lines existing
+    let clean = unwords . filter (/= "\\-") . words
+    let format e = "* `" ++ unwords (take 2 $ words e) ++ "` -> " ++ unwords (drop 2 $ words e)
+    return $ unlines $ fst new ++ [separator, "=========", ""] ++ map (format . clean) content
+
+separator = "Commands"
+
+main = do
+    let readme = "README.md"
+    let tmp = "README2.md"
+    commandList <- extractCommandOverview "man"
+    new <- replaceCommandsIn readme commandList
+    writeFile tmp new
+    renameFile tmp readme