move some sed operations to Haskell

This commit is contained in:
Ryan Mulligan 2018-04-19 08:20:25 -07:00
parent 0989b25633
commit ecdc3cf16c
3 changed files with 36 additions and 25 deletions

View File

@ -20,6 +20,7 @@ import Shelly
import qualified Text.Regex.Applicative as RE
import Text.Regex.Applicative (RE, (=~))
import Utils (Version, canFail, setupNixpkgs, succeded)
import qualified File
default (T.Text)
@ -50,21 +51,18 @@ fixSrcUrl packageName oldVersion newVersion derivationFile attrPath oldSrcUrl =
cmd "grep" "-q" ("name = \"" <> newDerivationName <> "\"") derivationFile) $
-- Separate name and version
do
cmd
"sed"
"-i"
("s|" <> newDerivationName <> "|" <> name <> "-${version}|")
derivationFile
cmd "grep" "-q" ("name = \"" <> name <> "-${version}\"") derivationFile
cmd
"sed"
"-i"
("s|^\\([ ]*\\)\\(name = \"" <> name <>
"-${version}\";\\)|\\1\\2\n\\1version = \"" <>
newVersion <>
"\";|")
derivationFile
cmd "grep" "-q" ("version = \"" <> newVersion <> "\";") derivationFile
let newName = name <> "-${version}"
File.replace newDerivationName newName derivationFile
cmd "grep" "-q" ("name = \"" <> newName <> "\"") derivationFile
cmd
"sed"
"-i"
("s|^\\([ ]*\\)\\(name = \"" <> name <>
"-${version}\";\\)|\\1\\2\n\\1version = \"" <>
newVersion <>
"\";|")
derivationFile
cmd "grep" "-q" ("version = \"" <> newVersion <> "\";") derivationFile
-- Obtain download URLs from repology
-- TODO: use repology-api package
downloads <-
@ -91,8 +89,7 @@ fixSrcUrl packageName oldVersion newVersion derivationFile attrPath oldSrcUrl =
newVersion
"${version}"
(T.replace newDerivationName "${name}" downloadUrl)
lift $
cmd "sed" "-i" ("s|" <> oldUrl <> "|" <> newUrl <> "|") derivationFile
lift $ File.replace oldUrl newUrl derivationFile
lift $ cmd "grep" "-q" ("url = \"" <> newUrl <> "\";") derivationFile
whenM
(lift $

19
src/File.hs Normal file
View File

@ -0,0 +1,19 @@
module File
( replace
) where
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.IO as T
import qualified Shelly
import Shelly (liftIO, toTextIgnore, Sh)
replaceIO :: Text -> Text -> FilePath -> IO ()
replaceIO find r file = do
contents <- T.readFile file
T.writeFile file (T.replace find r contents)
replace :: Text -> Text -> Shelly.FilePath -> Sh ()
replace find r file = liftIO $ replaceIO find r f
where
f = (T.unpack . toTextIgnore) file

View File

@ -19,6 +19,7 @@ import qualified Data.Text as T
import Data.Text (Text)
import Data.Time.Clock (getCurrentTime)
import Data.Time.Format (defaultTimeLocale, formatTime, iso8601DateFormat)
import qualified File
import Git
( autoUpdateBranchExists
, checkoutAtMergeBase
@ -229,12 +230,7 @@ updatePackage options log packageName oldVersion newVersion = do
rawEval
("(let pkgs = import ./. {}; in builtins.elemAt pkgs." <> attrPath <>
".src.drvAttrs.urls 0)")
cmd
"sed"
"-i"
("s/" <> (T.replace "." "\\." oldVersion) <> "/" <> newVersion <> "/g")
derivationFile `orElse`
errorExit "Could not replace oldVersion with newVersion."
File.replace oldVersion newVersion derivationFile
newSrcUrl <-
rawEval
("(let pkgs = import ./. {}; in builtins.elemAt pkgs." <> attrPath <>
@ -251,8 +247,7 @@ updatePackage options log packageName oldVersion newVersion = do
oldSrcUrl `orElse`
errorExit "Could not prefetch new version URL."
when (oldHash == newHash) $ errorExit "Hashes equal; no update necessary"
cmd "sed" "-i" ("s/" <> oldHash <> "/" <> newHash <> "/g") derivationFile `orElse`
errorExit "Could not replace oldHash with newHash."
File.replace oldHash newHash derivationFile
cmd "rm" "-f" "result*"
cmd "nix" "build" "-f" "." attrPath `orElse` do
buildLog <-