/
MassChange.hs
53 lines (46 loc) · 1.73 KB
/
MassChange.hs
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
{-# LANGUAGE OverloadedStrings #-}
module MassChange where
import Control.Applicative
import Control.Monad
import qualified Data.Text as DT
import qualified Data.Text.IO as DTI
import System.Directory
import System.FilePath.Posix
import System.IO
import Text.Printf
hasString :: DT.Text -> DT.Text -> Bool
hasString str = not . null . filter (str `DT.isInfixOf`) . DT.lines
withLines :: ([DT.Text] -> [DT.Text]) -> DT.Text -> DT.Text
withLines f = DT.unlines . f . DT.lines
bumpRevision :: DT.Text -> DT.Text
bumpRevision input
| hasString "PORTREVISION" input = withLines (map f) input
| hasString "PORTVERSION" input = withLines insertRevision input
| otherwise = input
where
f line
| "PORTREVISION" `DT.isInfixOf` line = DT.concat [pre, value', post]
| otherwise = line
where
(pre,line') = DT.break (`elem` ['0'..'9']) line
(value,post) = DT.break (`elem` "\t ") line'
value'
| not (DT.null value) =
DT.pack $ show ((read (DT.unpack value) :: Integer) + 1)
| otherwise = value
insertRevision xs = ys1 ++ [y, revisionLine] ++ ys2
where
(ys1,y:ys2) = break (hasString "PORTVERSION") xs
revisionLine = "PORTREVISION=\t1"
changeMakefile :: (DT.Text -> DT.Text) -> FilePath -> IO ()
changeMakefile f path = do
let makefile = path </> "Makefile"
exist <- doesFileExist makefile
if exist
then do
contents <- DTI.readFile makefile
forM_ [(makefile <.> "orig", contents), (makefile, f contents)]
(uncurry DTI.writeFile)
else hPutStrLn stderr $ printf "Warning: %s cannot be found." makefile
on :: (DT.Text -> DT.Text) -> [FilePath] -> IO ()
action `on` ports = forM_ ports (changeMakefile action)