Skip to content

Commit

Permalink
initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Mar 7, 2011
0 parents commit 1345f26
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
dist/
87 changes: 87 additions & 0 deletions Commands.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,87 @@
module Commands where

data Command = Sync
| Install
| Uninstall
| Installed
| Configure
| Build
| Info
| Help
deriving (Eq,Show)

data Route = RouteFunc ([String] -> IO ())
| RouteProc String [String]

data CommandSpec = CommandSpec {
command :: Command
, commandNames :: [String]
, document :: String
, routing :: Route
}

type CommandDB = [CommandSpec]

commandDB :: CommandDB
commandDB = [
CommandSpec {
command = Sync
, commandNames = ["sync", "update"]
, document = "Fetchinwg the latest package index"
, routing = RouteProc "cabal" ["update"]
}
, CommandSpec {
command = Install
, commandNames = ["install"]
, document = "Install packages"
, routing = RouteProc "cabal" ["install"]
}
, CommandSpec {
command = Uninstall
, commandNames = ["uninstall"]
, document = "Uninstalling packages"
, routing = RouteProc "ghc-pkg" ["unregister"]
}
, CommandSpec {
command = Installed
, commandNames = ["installed"]
, document = "Listing installed packages"
, routing = RouteProc "ghc-pkg" ["list"]
}
, CommandSpec {
command = Configure
, commandNames = ["configure"]
, document = "Configuring a cabal package"
, routing = RouteProc "cabal" ["configure"]
}
, CommandSpec {
command = Build
, commandNames = ["build"]
, document = "Building a cabal package"
, routing = RouteProc "cabal" ["build"]
}
, CommandSpec {
command = Info
, commandNames = ["info"]
, document = "Display information of a package"
, routing = RouteProc "cabal" ["info"]
}
, CommandSpec {
command = Help
, commandNames = ["help"]
, document = undefined
, routing = undefined
}
]

getCommand :: String -> CommandDB -> Command
getCommand _ [] = Help
getCommand x (ent:ents)
| x `elem` commandNames ent = command ent
| otherwise = getCommand x ents

commandSpecByCommand :: Command -> CommandDB -> Maybe CommandSpec
commandSpecByCommand _ [] = Nothing
commandSpecByCommand x (ent:ents)
| x == command ent = Just ent
| otherwise = commandSpecByCommand x ents
31 changes: 31 additions & 0 deletions Help.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,31 @@
module Help where

import Commands
import Data.List
import Program
import Data.Maybe

displayHelp :: [String] -> IO ()
displayHelp [] = displayHelpAll
displayHelp (x:_) = case getCommand x commandDB of
Help -> displayHelpAll
com -> displayHelpCommand com

displayHelpAll :: IO ()
displayHelpAll = do
putStrLn $ programName ++ " " ++ " -- " ++ description
putStrLn ""
putStrLn $ "Version: " ++ version
putStrLn "Usage:"
putStrLn $ "\t" ++ programName ++ " help"
putStrLn $ "\t" ++ programName ++ " <command> [args...]"
putStrLn ""
putStrLn $ "\t <command> = " ++ extractCommands commandDB
where
extractCommands = concat . intersperse "," . map head . map commandNames
-- FIXME take 5 or so

displayHelpCommand :: Command -> IO ()
displayHelpCommand com = putStrLn $ document spec
where
spec = fromJust $ commandSpecByCommand com commandDB
43 changes: 43 additions & 0 deletions Main.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,43 @@
module Main where

import Commands
import Control.Exception
import Data.List
import Data.Maybe
import Help
import Options
import System.Cmd
import System.Console.GetOpt
import System.Environment (getArgs)

parseArgs :: [OptDescr (Options -> Options)] -> [String] -> (Options, [String])
parseArgs spec argv
= case getOpt Permute spec argv of
(o,n,[] ) -> (foldl (flip id) defaultOptions o, n)
(_,_,errs) -> error "XXX" -- FIXME

analyzeArgs :: Options -> [String] -> (Command, [String], Options)
analyzeArgs opt [] = (Help, [], opt)
analyzeArgs opt xs
| help opt = (Help, xs, opt)
analyzeArgs opt (x:xs) = (getCommand x commandDB, xs, opt)

main :: IO ()
main = flip catches handlers $ do
args <- getArgs
let (cmd, params, opts) = uncurry analyzeArgs $ parseArgs argSpec args
if cmd == Help
then displayHelp params
else do
let spec = fromJust $ commandSpecByCommand cmd commandDB
route = routing spec
case route of
RouteFunc func -> func params
RouteProc subcmd subargs -> callProcess subcmd subargs params opts
where
handlers = undefined

callProcess :: String -> [String] -> [String] -> Options -> IO ()
callProcess cmd args0 args1 _ = system script >> return ()
where
script = concat . intersperse " " $ cmd : args0 ++ args1
19 changes: 19 additions & 0 deletions Options.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
module Options where

import System.Console.GetOpt

data Options = Options {
help :: Bool
}

defaultOptions :: Options
defaultOptions = Options {
help = False
}

argSpec :: [OptDescr (Options -> Options)]
argSpec = [ Option "h" ["help"]
(NoArg (\opts -> opts { help = True }))
"print help messages"
]

10 changes: 10 additions & 0 deletions Program.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
module Program where

version :: String
version = "0.0.0"

programName :: String
programName = "cab"

description :: String
description = "A maintenance command of Haskell cabal packages"
4 changes: 4 additions & 0 deletions TODO
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
cabal-delete
https://github.com/iquiw/cabal-delete
cabal-dev
https://github.com/creswick/cabal-dev

0 comments on commit 1345f26

Please sign in to comment.