cabal-rpm-0.11: src/Commands/Refresh.hs
-- |
-- Module : Commands.Refresh
-- Copyright : (C) 2016 Jens Petersen
--
-- Maintainer : Jens Petersen <petersen@fedoraproject.org>
--
-- Explanation: refresh spec file to newer cabal-rpm
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
module Commands.Refresh (
refresh
) where
import Commands.Spec (createSpecFile)
import FileUtils (withTempDirectory)
import Options (RpmFlags (..))
import PackageUtils (PackageData (..), removePrefix)
import SysCmd (cmd_, optionalProgram, shell, (+-+))
#if (defined(MIN_VERSION_base) && MIN_VERSION_base(4,8,2))
#else
import Control.Applicative ((<$>))
#endif
import Control.Monad (unless)
import Data.List (isPrefixOf)
import Distribution.Simple.Utils (die)
import System.Directory (copyFile, createDirectoryIfMissing, doesFileExist,
setCurrentDirectory)
import System.Environment (getEnv)
import System.FilePath ((</>))
refresh :: PackageData -> RpmFlags -> IO ()
refresh pkgdata flags =
case specFilename pkgdata of
Nothing -> die "No (unique) .spec file in directory."
Just spec -> do
first <- head . lines <$> readFile spec
if "# generated by cabal-rpm-" `isPrefixOf` first
then do
let cblrpmver = removePrefix "# generated by cabal-rpm-" first
oldspec <- createOldSpec cblrpmver spec
newspec <- createSpecFile pkgdata flags Nothing
shell $ "diff -u2 -I \"- spec file generated by cabal-rpm\" -I \"Fedora Haskell SIG <haskell@lists.fedoraproject.org>\"" +-+ oldspec +-+ newspec +-+ "| sed -e 's/.cblrpm//' | patch" +-+ "|| :"
else putStrLn $ "No cabal-rpm header line in" +-+ spec
-- setCurrentDirectory cwd
-- when rwGit $
-- cmd_ "git" ["commit", "-a", "-m", "update to" +-+ newver]
where
createOldSpec :: String -> FilePath -> IO FilePath
createOldSpec crVer spec = do
cblrpmVersion crVer
let backup = spec ++ ".cblrpm"
backup' = backup ++ "-" ++ crVer
cmd_ "mv" [backup, backup']
return backup'
cblrpmVersion :: String -> IO ()
cblrpmVersion crver = do
let cblrpmver = "cabal-rpm-" ++ crver
inpath <- optionalProgram cblrpmver
if inpath
then cmd_ cblrpmver ["spec"]
else do
home <- getEnv "HOME"
let bindir = home </> ".cblrpm/versions/"
haveExe <- doesFileExist $ bindir </> cblrpmver
unless haveExe $
withTempDirectory $ \cwd -> do
cmd_ "cabal" ["unpack", cblrpmver]
setCurrentDirectory cblrpmver
cmd_ "cabal" ["configure"]
cmd_ "cabal" ["build"]
createDirectoryIfMissing True bindir
let bin = "dist/build/cabal-rpm/cabal-rpm"
cmd_ "strip" [bin]
copyFile bin $ bindir </> cblrpmver
setCurrentDirectory cwd
cmd_ (bindir </> cblrpmver) ["spec"]