Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v0.2.19
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 559 lines (492 sloc) 20.381 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
module Main where

import Control.Applicative
import Control.Monad
import Data.Maybe
import Data.List
import Data.Monoid
        ( Monoid(..) )

-- cabal
import Distribution.Simple.Setup
        ( Flag(..), fromFlag
        , trueArg
        , flagToList
        , optionVerbosity
        )
import Distribution.ReadE ( succeedReadE )
import Distribution.Simple.Command -- commandsRun
import Distribution.Simple.Utils ( die, cabalVersion, warn )
import qualified Distribution.PackageDescription.Parse as Cabal
import qualified Distribution.Package as Cabal
import Distribution.Verbosity (Verbosity, normal)
import Distribution.Text (display, simpleParse)

import Distribution.Client.Types
import Distribution.Client.Update

import qualified Distribution.Client.PackageIndex as Index
import qualified Distribution.Client.IndexUtils as Index

import Hackage (defaultRepo, defaultRepoURI)

import Portage.Overlay as Overlay ( loadLazy, inOverlay )
import Portage.Host as Host ( getInfo, portage_dir )
import Portage.PackageId ( normalizeCabalPackageId )

import Network.URI ( URI(..), parseURI )
import System.Environment ( getArgs, getProgName )
import System.Directory ( doesDirectoryExist )
import System.Exit ( exitFailure )
import System.FilePath ( (</>) )

import Diff
import Error
import Status
import Overlays
import Merge
import DistroMap ( distroMap )

import qualified Paths_cabal_install
import qualified Paths_hackport

-----------------------------------------------------------------------
-- List
-----------------------------------------------------------------------

data ListFlags = ListFlags {
    listVerbosity :: Flag Verbosity
    -- , listOverlayPath :: Flag FilePath
    -- , listServerURI :: Flag String
  }

instance Monoid ListFlags where
  mempty = ListFlags {
    listVerbosity = mempty
    -- , listOverlayPath = mempty
    -- , listServerURI = mempty
  }
  mappend a b = ListFlags {
    listVerbosity = combine listVerbosity
    -- , listOverlayPath = combine listOverlayPath
    -- , listServerURI = combine listServerURI
  }
    where combine field = field a `mappend` field b

defaultListFlags :: ListFlags
defaultListFlags = ListFlags {
    listVerbosity = Flag normal
    -- , listOverlayPath = NoFlag
    -- , listServerURI = Flag defaultHackageServerURI
  }

listCommand :: CommandUI ListFlags
listCommand = CommandUI {
    commandName = "list",
    commandSynopsis = "List packages",
    commandDescription = Just $ \_pname ->
        "TODO: this is the commandDescription for listCommand\n",
    commandUsage = usagePackages "list",
    commandDefaultFlags = defaultListFlags,
    commandOptions = \_showOrParseArgs ->
      [ optionVerbosity listVerbosity (\v flags -> flags { listVerbosity = v })
      {-
, option [] ["overlay"]
"Use cached packages list from specified overlay"
listOverlayPath (\v flags -> flags { listOverlayPath = v })
(reqArgFlag "PATH")
-}
      ]
  }

listAction :: ListFlags -> [String] -> GlobalFlags -> IO ()
listAction flags extraArgs globalFlags = do
  let verbosity = fromFlag (listVerbosity flags)
  overlayPath <- getOverlayPath verbosity (fromFlag $ globalPathToOverlay globalFlags)
  let repo = defaultRepo overlayPath
  index <- fmap packageIndex (Index.getSourcePackages verbosity [ repo ])
  overlay <- Overlay.loadLazy overlayPath
  let pkgs | null extraArgs = Index.allPackages index
           | otherwise = concatMap (concatMap snd . Index.searchByNameSubstring index) extraArgs
      normalized = map (normalizeCabalPackageId . packageInfoId) pkgs
  let decorated = map (\p -> (Overlay.inOverlay overlay p, p)) normalized
  mapM_ (putStrLn . pretty) decorated
  where
  pretty :: (Bool, Cabal.PackageIdentifier) -> String
  pretty (isInOverlay, pkgId) =
      let dec | isInOverlay = " * "
              | otherwise = " "
      in dec ++ display pkgId


-----------------------------------------------------------------------
-- Make Ebuild
-----------------------------------------------------------------------

data MakeEbuildFlags = MakeEbuildFlags {
    makeEbuildVerbosity :: Flag Verbosity
  }

instance Monoid MakeEbuildFlags where
  mempty = MakeEbuildFlags {
    makeEbuildVerbosity = mempty
  }
  mappend a b = MakeEbuildFlags {
    makeEbuildVerbosity = combine makeEbuildVerbosity
  }
    where combine field = field a `mappend` field b

defaultMakeEbuildFlags :: MakeEbuildFlags
defaultMakeEbuildFlags = MakeEbuildFlags {
    makeEbuildVerbosity = Flag normal
  }

makeEbuildAction :: MakeEbuildFlags -> [String] -> GlobalFlags -> IO ()
makeEbuildAction flags args globalFlags = do
  (catstr,cabals) <- case args of
                      (category:cabal1:cabaln) -> return (category, cabal1:cabaln)
                      _ -> throwEx (ArgumentError "make-ebuild needs at least two arguments. <category> <cabal-1> <cabal-n>")
  cat <- case simpleParse catstr of
            Just c -> return c
            Nothing -> throwEx (ArgumentError ("could not parse category: " ++ catstr))
  let verbosity = fromFlag (makeEbuildVerbosity flags)
  overlayPath <- getOverlayPath verbosity (fromFlag $ globalPathToOverlay globalFlags)
  forM_ cabals $ \cabalFileName -> do
    pkg <- Cabal.readPackageDescription normal cabalFileName
    mergeGenericPackageDescription verbosity overlayPath cat pkg False

makeEbuildCommand :: CommandUI MakeEbuildFlags
makeEbuildCommand = CommandUI {
    commandName = "make-ebuild",
    commandSynopsis = "Make an ebuild from a .cabal file",
    commandDescription = Just $ \_pname ->
        "TODO: this is the commandDescription for makeEbuildCommand\n",
    commandUsage = \_ -> [],
    commandDefaultFlags = defaultMakeEbuildFlags,
    commandOptions = \_showOrParseArgs ->
      [ optionVerbosity makeEbuildVerbosity (\v flags -> flags { makeEbuildVerbosity = v })
      ]
  }

-----------------------------------------------------------------------
-- Diff
-----------------------------------------------------------------------

data DiffFlags = DiffFlags {
    -- diffMode :: Flag String, -- DiffMode,
    diffVerbosity :: Flag Verbosity
    -- , diffServerURI :: Flag String
  }

instance Monoid DiffFlags where
  mempty = DiffFlags {
    -- diffMode = mempty,
    diffVerbosity = mempty
    -- , diffServerURI = mempty
  }
  mappend a b = DiffFlags {
    -- diffMode = combine diffMode,
    diffVerbosity = combine diffVerbosity
    -- , diffServerURI = combine diffServerURI
  }
    where combine field = field a `mappend` field b

defaultDiffFlags :: DiffFlags
defaultDiffFlags = DiffFlags {
    -- diffMode = Flag "all",
    diffVerbosity = Flag normal
    -- , diffServerURI = Flag defaultHackageServerURI
  }

diffCommand :: CommandUI DiffFlags
diffCommand = CommandUI {
    commandName = "diff",
    commandSynopsis = "Run diff",
    commandDescription = Just $ \_pname ->
        "TODO: this is the commandDescription for diffCommand\n",
    commandUsage = usagePackages "diff",
    commandDefaultFlags = defaultDiffFlags,
    commandOptions = \_showOrParseArgs ->
      [ optionVerbosity diffVerbosity (\v flags -> flags { diffVerbosity = v })
      {-
, option [] ["mode"]
"Diff mode, one of: all, newer, missing, additions, common"
diffMode (\v flags -> flags { diffMode = v })
(reqArgFlag "MODE") -- I don't know how to map it strictly to DiffMode
-}
      ]
  }

diffAction :: DiffFlags -> [String] -> GlobalFlags -> IO ()
diffAction flags args globalFlags = do
  let verbosity = fromFlag (diffVerbosity flags)
      -- dm0 = fromFlag (diffMode flags)
  dm <- case args of
          [] -> return ShowAll
          ["all"] -> return ShowAll
          ["missing"] -> return ShowMissing
          ["additions"] -> return ShowAdditions
          ["newer"] -> return ShowNewer
          ["common"] -> return ShowCommon
          ("package": pkgs) -> return (ShowPackages pkgs)
          -- TODO: ["package",packagePattern] ->
          -- return ShowPackagePattern packagePattern
          _ -> die $ "Unknown mode: " ++ unwords args
  overlayPath <- getOverlayPath verbosity (fromFlag $ globalPathToOverlay globalFlags)
  let repo = defaultRepo overlayPath
  runDiff verbosity overlayPath dm repo

-----------------------------------------------------------------------
-- Update
-----------------------------------------------------------------------

data UpdateFlags = UpdateFlags {
    updateVerbosity :: Flag Verbosity
    -- , updateServerURI :: Flag String
  }

instance Monoid UpdateFlags where
  mempty = UpdateFlags {
    updateVerbosity = mempty
    -- , updateServerURI = mempty
  }
  mappend a b = UpdateFlags {
    updateVerbosity = combine updateVerbosity
    -- , updateServerURI = combine updateServerURI
  }
    where combine field = field a `mappend` field b

defaultUpdateFlags :: UpdateFlags
defaultUpdateFlags = UpdateFlags {
    updateVerbosity = Flag normal
    -- , updateServerURI = Flag defaultHackageServerURI
  }

updateCommand :: CommandUI UpdateFlags
updateCommand = CommandUI {
    commandName = "update",
    commandSynopsis = "Update the local cache",
    commandDescription = Just $ \_pname ->
        "TODO: this is the commandDescription for updateCommand\n",
    commandUsage = usageFlags "update",
    commandDefaultFlags = defaultUpdateFlags,
    commandOptions = \_ ->
      [ optionVerbosity updateVerbosity (\v flags -> flags { updateVerbosity = v })

      {-
, option [] ["server"]
"Set the server you'd like to update the cache from"
updateServerURI (\v flags -> flags { updateServerURI = v} )
(reqArgFlag "SERVER")
-}
      ]
  }

updateAction :: UpdateFlags -> [String] -> GlobalFlags -> IO ()
updateAction flags extraArgs globalFlags = do
  unless (null extraArgs) $
    die $ "'update' doesn't take any extra arguments: " ++ unwords extraArgs
  let verbosity = fromFlag (updateVerbosity flags)
  overlayPath <- getOverlayPath verbosity (fromFlag $ globalPathToOverlay globalFlags)
  update verbosity [ defaultRepo overlayPath ]
  

-----------------------------------------------------------------------
-- Status
-----------------------------------------------------------------------

data StatusFlags = StatusFlags {
    statusVerbosity :: Flag Verbosity,
    statusDirection :: Flag StatusDirection
  }

defaultStatusFlags :: StatusFlags
defaultStatusFlags = StatusFlags {
    statusVerbosity = Flag normal,
    statusDirection = Flag PortagePlusOverlay
  }

statusCommand :: CommandUI StatusFlags
statusCommand = CommandUI {
    commandName = "status",
    commandSynopsis = "Show status(??)",
    commandDescription = Just $ \_pname ->
        "TODO: this is the commandDescription for statusCommand\n",
    commandUsage = usagePackages "status",
    commandDefaultFlags = defaultStatusFlags,
    commandOptions = \_ ->
      [ optionVerbosity statusVerbosity (\v flags -> flags { statusVerbosity = v })
      , option [] ["to-portage"]
          "Print only packages likely to be interesting to move to the portage tree."
          statusDirection (\v flags -> flags { statusDirection = v })
          (noArg (Flag OverlayToPortage))
      , option [] ["from-hackage"]
          "Print only packages likely to be interesting to move from hackage tree."
          statusDirection (\v flags -> flags { statusDirection = v })
          (noArg (Flag HackageToOverlay))
      ]
  }

statusAction :: StatusFlags -> [String] -> GlobalFlags -> IO ()
statusAction flags args globalFlags = do
  let verbosity = fromFlag (statusVerbosity flags)
      direction = fromFlag (statusDirection flags)
  portagePath <- getPortageDir verbosity globalFlags
  overlayPath <- getOverlayPath verbosity (fromFlag $ globalPathToOverlay globalFlags)
  runStatus verbosity portagePath overlayPath direction args

-----------------------------------------------------------------------
-- Merge
-----------------------------------------------------------------------

data MergeFlags = MergeFlags {
    mergeVerbosity :: Flag Verbosity
    -- , mergeServerURI :: Flag String
  }

instance Monoid MergeFlags where
  mempty = MergeFlags {
    mergeVerbosity = mempty
    -- , mergeServerURI = mempty
  }
  mappend a b = MergeFlags {
    mergeVerbosity = combine mergeVerbosity
    -- , mergeServerURI = combine mergeServerURI
  }
    where combine field = field a `mappend` field b

defaultMergeFlags :: MergeFlags
defaultMergeFlags = MergeFlags {
    mergeVerbosity = Flag normal
    -- , mergeServerURI = Flag defaultHackageServerURI
  }

mergeCommand :: CommandUI MergeFlags
mergeCommand = CommandUI {
    commandName = "merge",
    commandSynopsis = "Make an ebuild out of hackage package",
    commandDescription = Just $ \_pname ->
      "TODO: this is the commandDescription for mergeCommand\n",
    commandUsage = usagePackages "merge",
    commandDefaultFlags = defaultMergeFlags,
    commandOptions = \_showOrParseArgs ->
      [ optionVerbosity mergeVerbosity (\v flags -> flags { mergeVerbosity = v })

      {-
, option [] ["server"]
"Set the server you'd like to update the cache from"
mergeServerURI (\v flags -> flags { mergeServerURI = v} )
(reqArgFlag "SERVER")
-}
      ]
  }

mergeAction :: MergeFlags -> [String] -> GlobalFlags -> IO ()
mergeAction flags extraArgs globalFlags = do
  let verbosity = fromFlag (mergeVerbosity flags)
  overlayPath <- getOverlayPath verbosity (fromFlag $ globalPathToOverlay globalFlags)
  let repo = defaultRepo overlayPath
  merge verbosity repo (defaultRepoURI overlayPath) extraArgs overlayPath

-----------------------------------------------------------------------
-- DistroMap
-----------------------------------------------------------------------

data DistroMapFlags = DistroMapFlags {
    distroMapVerbosity :: Flag Verbosity
  }

instance Monoid DistroMapFlags where
  mempty = DistroMapFlags {
    distroMapVerbosity = mempty
    -- , mergeServerURI = mempty
  }
  mappend a b = DistroMapFlags {
    distroMapVerbosity = combine distroMapVerbosity
  }
    where combine field = field a `mappend` field b

defaultDistroMapFlags :: DistroMapFlags
defaultDistroMapFlags = DistroMapFlags {
    distroMapVerbosity = Flag normal
  }

distroMapCommand :: CommandUI DistroMapFlags
distroMapCommand = CommandUI {
    commandName = "distromap",
    commandSynopsis = "Build a distromap file",
    commandDescription = Just $ \_pname ->
      "TODO: this is the commandDescription for distroMapCommand\n",
    commandUsage = usagePackages "distromap",
    commandDefaultFlags = defaultDistroMapFlags,
    commandOptions = \_showOrParseArgs ->
      [ optionVerbosity distroMapVerbosity (\v flags -> flags { distroMapVerbosity = v })
      ]
  }

distroMapAction :: DistroMapFlags-> [String] -> GlobalFlags -> IO ()
distroMapAction flags extraArgs globalFlags = do
  let verbosity = fromFlag (distroMapVerbosity flags)
  overlayPath <- getOverlayPath verbosity (fromFlag $ globalPathToOverlay globalFlags)
  let repo = defaultRepo overlayPath
  portagePath <- getPortageDir verbosity globalFlags
  distroMap verbosity repo portagePath overlayPath extraArgs

-----------------------------------------------------------------------
-- Utils
-----------------------------------------------------------------------

getServerURI :: String -> IO URI
getServerURI str =
  case parseURI str of
    Just uri -> return uri
    Nothing -> throwEx (InvalidServer str)

reqArgFlag :: ArgPlaceHolder -> SFlags -> LFlags -> Description ->
              (b -> Flag String) -> (Flag String -> b -> b) -> OptDescr b
reqArgFlag ad = reqArg ad (succeedReadE Flag) flagToList

usagePackages :: String -> String -> String
usagePackages op_name pname =
  "Usage: " ++ pname ++ " " ++ op_name ++ " [FLAGS] [PACKAGE]\n\n"
  ++ "Flags for " ++ op_name ++ ":"

usageFlags :: String -> String -> String
usageFlags flag_name pname =
      "Usage: " ++ pname ++ " " ++ flag_name ++ " [FLAGS]\n\n"
      ++ "Flags for " ++ flag_name ++ ":"

getPortageDir :: Verbosity -> GlobalFlags -> IO FilePath
getPortageDir verbosity globalFlags = do
  let portagePathM = fromFlag (globalPathToPortage globalFlags)
  portagePath <- case portagePathM of
                   Nothing -> Host.portage_dir <$> Host.getInfo
                   Just path -> return path
  exists <- doesDirectoryExist $ portagePath </> "dev-haskell"
  when (not exists) $
    warn verbosity $ "Looks like an invalid portage directory: " ++ portagePath
  return portagePath

-----------------------------------------------------------------------
-- Main
-----------------------------------------------------------------------

data GlobalFlags =
    GlobalFlags { globalVersion :: Flag Bool
                , globalNumericVersion :: Flag Bool
                , globalPathToOverlay :: Flag (Maybe FilePath)
                , globalPathToPortage :: Flag (Maybe FilePath)
                }

defaultGlobalFlags :: GlobalFlags
defaultGlobalFlags =
    GlobalFlags { globalVersion = Flag False
                , globalNumericVersion = Flag False
                , globalPathToOverlay = Flag Nothing
                , globalPathToPortage = Flag Nothing
                }

globalCommand :: CommandUI GlobalFlags
globalCommand = CommandUI {
    commandName = "",
    commandSynopsis = "",
    commandDescription = Just $ \_pname ->
        "TODO: this is the commandDescription for globalCommand\n",
    commandUsage = \_ -> [],
    commandDefaultFlags = defaultGlobalFlags,
    commandOptions = \_showOrParseArgs ->
        [ option ['V'] ["version"]
            "Print version information"
            globalVersion (\v flags -> flags { globalVersion = v })
            trueArg
        , option [] ["numeric-version"]
            "Print just the version number"
            globalNumericVersion (\v flags -> flags { globalNumericVersion = v })
            trueArg
        , option ['p'] ["overlay-path"]
            "Override search path list where .hackport/ lives (default list: ['.', paludis-ovls or emerge-ovls])"
            globalPathToOverlay (\ovrl_path flags -> flags { globalPathToOverlay = ovrl_path })
            (reqArg' "PATH" (Flag . Just) (\(Flag ms) -> catMaybes [ms]))
        , option [] ["portage-path"]
            "Override path to your portage tree"
            globalPathToPortage (\port_path flags -> flags { globalPathToPortage = port_path })
            (reqArg' "PATH" (Flag . Just) (\(Flag ms) -> catMaybes [ms]))
        ]
    }

mainWorker :: [String] -> IO ()
mainWorker args =
  case commandsRun globalCommand commands args of
    CommandHelp help -> printHelp help
    CommandList opts -> printOptionsList opts
    CommandErrors errs -> printErrors errs
    CommandReadyToGo (globalflags, commandParse) -> do
      case commandParse of
        _ | fromFlag (globalVersion globalflags) -> printVersion
          | fromFlag (globalNumericVersion globalflags) -> printNumericVersion
        CommandHelp help -> printHelp help
        CommandList opts -> printOptionsList opts
        CommandErrors errs -> printErrors errs
        CommandReadyToGo action -> catchEx (action globalflags) errorHandler
    where
    printHelp help = getProgName >>= putStr . help
    printOptionsList = putStr . unlines
    printErrors errs = do
      putStr (concat (intersperse "\n" errs))
      exitFailure
    printNumericVersion = putStrLn $ display Paths_hackport.version
    printVersion = putStrLn $ "hackport version "
                                  ++ display Paths_hackport.version
                                  ++ "\nusing cabal-install "
                                  ++ display Paths_cabal_install.version
                                  ++ " and the Cabal library version "
                                  ++ display cabalVersion
    errorHandler :: HackPortError -> IO ()
    errorHandler e = do
      putStrLn (hackPortShowError e)
    commands =
      [ listCommand `commandAddAction` listAction
      , makeEbuildCommand `commandAddAction` makeEbuildAction
      , statusCommand `commandAddAction` statusAction
      , diffCommand `commandAddAction` diffAction
      , updateCommand `commandAddAction` updateAction
      , mergeCommand `commandAddAction` mergeAction
      , distroMapCommand `commandAddAction` distroMapAction
      ]

main :: IO ()
main = getArgs >>= mainWorker
Something went wrong with that request. Please try again.