-- | -- Module : Commands.Update -- Copyright : (C) 2014-2016 Jens Petersen -- -- Maintainer : Jens Petersen -- Stability : alpha -- -- Explanation: update spec file to a new package version -- 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.Update ( update ) where import Commands.Spec (createSpecFile) import Distro (detectDistro, Distro(..)) import FileUtils (withTempDirectory) import PackageUtils (PackageData (..), bringTarball, isGitDir, latestPkg, packageName, packageVersion, prepare, removePrefix) import Setup (RpmFlags (..)) import SysCmd (cmd_, cmdBool, cmdIgnoreErr, (+-+)) import Control.Applicative ((<$>)) import Control.Monad (when) import Distribution.PackageDescription (PackageDescription (..)) import Distribution.Simple.Utils (die) import Data.Maybe (fromMaybe) import System.Directory (createDirectory, getCurrentDirectory, setCurrentDirectory) update :: PackageData -> RpmFlags -> IO () update pkgdata flags = case specFilename pkgdata of Nothing -> die "No (unique) .spec file in directory." Just spec -> do let pkg = package $ packageDesc pkgdata name = packageName pkg ver = packageVersion pkg current = name ++ "-" ++ ver latest <- latestPkg name if current == latest then error $ current +-+ "is latest version." else do bringTarball latest gitDir <- getCurrentDirectory >>= isGitDir rwGit <- if gitDir then cmdBool "grep -q 'url = ssh://' .git/config" else return False when rwGit $ cmd_ "fedpkg" ["new-sources", latest ++ ".tar.gz"] withTempDirectory $ \cwd -> do curspec <- createSpecVersion current spec newspec <- createSpecVersion latest spec patch <- cmdIgnoreErr "diff" ["-u2", "-I - spec file generated by cabal-rpm", "-I Fedora Haskell SIG ", curspec, newspec] "" putStrLn patch out <- cmdIgnoreErr "patch" ["-d", cwd, "-p1" ] patch putStrLn out setCurrentDirectory cwd distro <- fromMaybe detectDistro (return <$> rpmDistribution flags) let suffix = if distro == SUSE then "" else "%{?dist}" cmd_ "sed" ["-i", "-e s/^\\(Release: \\).*/\\10" ++ suffix ++ "/", spec] let newver = removePrefix (name ++ "-") latest if distro == SUSE then cmd_ "sed" ["-i", "-e s/^\\(Version: \\).*/\\1" ++ newver ++ "/", spec] else cmd_ "rpmdev-bumpspec" ["-c", "update to" +-+ newver, spec] when rwGit $ cmd_ "git" ["commit", "-a", "-m", "update to" +-+ newver] where createSpecVersion :: String -> String -> IO FilePath createSpecVersion ver spec = do pkgdata' <- prepare (Just ver) flags let pkgdata'' = pkgdata' { specFilename = Just spec } createDirectory ver createSpecFile pkgdata'' flags (Just ver)