]> gitweb @ CieloNegro.org - pkg-find-outdated-cabal.git/blob - Main.hs
extract PackageIdentifier from Makefile's
[pkg-find-outdated-cabal.git] / Main.hs
1 module Main (main) where
2
3 import Control.Monad
4 import qualified Data.ByteString.Lazy as Lazy
5 import Data.List.Utils
6 import Data.Version
7 import Distribution.Package
8 import Distribution.PackageDescription
9 import Distribution.Query
10 import HSH
11 import System.Directory
12 import System.FilePath
13 import System.FilePath.Glob
14 import Text.ParserCombinators.ReadP
15
16 indexURL :: String
17 indexURL = "http://hackage.haskell.org/packages/archive/00-index.tar.gz"
18
19 pkgsrcPath :: String
20 pkgsrcPath = "/usr/pkgsrc" -- FIXME!
21
22 main :: IO ()
23 main = do appDir <- getAppUserDataDirectory "pkg-find-outdated-cabal"
24           createDirectoryIfMissing False appDir
25
26           let indexFile = appDir </> "00-index.tar.gz"
27
28           putStrLn "Downloading the Hackage index..."
29           cwd <- pwd
30           cd appDir
31           runIO ("wget", ["-N", indexURL])
32           cd cwd
33           putStrLn "Done."
34
35           indexBin <- Lazy.readFile indexFile
36           let runQuery q = queryIndex q indexBin
37
38           makefiles <- namesMatching (pkgsrcPath </> "*" </> "*" </> "Makefile")
39           scanPkgs runQuery makefiles
40
41 scanPkgs :: (Query -> [PackageDescription]) -> [FilePath] -> IO ()
42 scanPkgs runQuery = mapM_ scanPkg
43     where
44       scanPkg :: FilePath -> IO ()
45       scanPkg makPath
46           = do isCabalPkg <- run ("fgrep", ["-q", "mk/haskell.mk", makPath])
47                when isCabalPkg
48                     $ checkPkg makPath
49
50       checkPkg :: FilePath -> IO ()
51       checkPkg makPath
52           = do mak <- readFile makPath
53                case grep "DISTNAME=" (lines mak) of
54                  [l] -> do let line     = (trd ' ' . trd '\t') l
55                                distname = cut 1 '=' line
56                                pkgName  = extractPkgName distname
57                                pkgVer   = extractPkgVersion distname
58                                pkgId    = PackageIdentifier pkgName pkgVer
59                            putStrLn (distname ++ ": " ++ show pkgId)
60                  _   -> return ()
61
62 extractPkgName :: String -> PackageName
63 extractPkgName = PackageName . head . split "-"
64
65 extractPkgVersion :: String -> Version
66 extractPkgVersion =  take' . readP_to_S parseVersion . last . split "-"
67     where
68       take' ((v, ""):_ ) = v
69       take' (_      :xs) = take' xs