Skip to content

Commit 4e333ef

Browse files
committed
Implement force version
Doesn't quite work because srcDir is unique by url, so if you have the same url with different versions, things will collide. This is a bug that might affect patching too.
1 parent 7942fa1 commit 4e333ef

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

app/Foliage/CmdBuild.hs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ cmdBuild
6969
getSourceMeta <- addOracle $ \(GetSourceMeta PackageId {pkgName, pkgVersion}) ->
7070
readSourceMeta' $ inputDir </> pkgName </> pkgVersion </> "meta.toml"
7171

72-
getSourceDir <- addOracle $ \(GetSourceDir pkgId@PackageId {pkgName, pkgVersion}) -> do
73-
SourceMeta {sourceUrl, sourceSubdir} <- getSourceMeta (GetSourceMeta pkgId)
72+
getSourceDir <- addOracle $ \(GetSourceDir pkgId) -> do
73+
SourceMeta {sourceUrl, sourceSubdir, sourceForceVersion} <- getSourceMeta (GetSourceMeta pkgId)
7474
let urlDir = "_cache" </> urlToFileName sourceUrl
7575

7676
need [urlDir </> ".downloaded"]
@@ -86,15 +86,10 @@ cmdBuild
8686
Just s -> urlDir </> s
8787
Nothing -> urlDir
8888

89-
let patchesDir = inputDir </> pkgName </> pkgVersion </> "patches"
90-
hasPatches <- doesDirectoryExist patchesDir
89+
when sourceForceVersion $
90+
forcePackageVersion srcDir pkgId
9191

92-
when hasPatches $ do
93-
patches <- getDirectoryFiles (inputDir </> pkgName </> pkgVersion </> "patches") ["*.patch"]
94-
for_ patches $ \patch -> do
95-
let patchfile = inputDir </> pkgName </> pkgVersion </> "patches" </> patch
96-
putInfo $ "Applying patch: " <> patch
97-
cmd_ Shell (Cwd srcDir) (FileStdin patchfile) "patch --backup -p1"
92+
applyPatches inputDir srcDir pkgId
9893

9994
return srcDir
10095

@@ -421,3 +416,33 @@ mkTarEntry filePath indexPath timestamp = do
421416
Tar.groupId = 0
422417
}
423418
}
419+
420+
applyPatches :: FilePath -> FilePath -> PackageId -> Action ()
421+
applyPatches inputDir srcDir PackageId {pkgName, pkgVersion} = do
422+
let patchesDir = inputDir </> pkgName </> pkgVersion </> "patches"
423+
hasPatches <- doesDirectoryExist patchesDir
424+
425+
when hasPatches $ do
426+
patches <- getDirectoryFiles (inputDir </> pkgName </> pkgVersion </> "patches") ["*.patch"]
427+
for_ patches $ \patch -> do
428+
let patchfile = inputDir </> pkgName </> pkgVersion </> "patches" </> patch
429+
putInfo $ "Applying patch: " <> patch
430+
cmd_ Shell (Cwd srcDir) (FileStdin patchfile) "patch --backup -p1"
431+
432+
forcePackageVersion :: FilePath -> PackageId -> Action ()
433+
forcePackageVersion srcDir PackageId {pkgName, pkgVersion} = do
434+
let cabalFilePath = srcDir </> pkgName <.> "cabal"
435+
cabalFile <- readFile' cabalFilePath
436+
writeFile' cabalFilePath (replaceVersion pkgVersion cabalFile)
437+
438+
replaceVersion :: String -> String -> String
439+
replaceVersion version = unlines . map f . lines
440+
where
441+
f line
442+
| "version" `isPrefixOf` line =
443+
unlines
444+
[ "-- version field replaced by foliage",
445+
"--" <> line,
446+
"version:\t" ++ version
447+
]
448+
f line = line

app/Foliage/CmdImportHackage.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ importIndex f (Tar.Next e es) m =
5959
{ sourceUrl = pkgIdToHackageUrl pkgId,
6060
sourceTimestamp = Just time,
6161
sourceSubdir = Nothing,
62-
sourceRevisions = []
62+
sourceRevisions = [],
63+
sourceForceVersion = False
6364
}
6465
-- Existing package, new revision
6566
Just sm -> do

app/Foliage/Meta.hs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Foliage.Meta
1111
sourceUrl,
1212
sourceSubdir,
1313
sourceRevisions,
14+
sourceForceVersion,
1415
readSourceMeta,
1516
writeSourceMeta,
1617
RevisionMeta,
@@ -24,6 +25,7 @@ where
2425

2526
import Control.Monad (void)
2627
import Data.Coerce (coerce)
28+
import Data.Maybe (fromMaybe)
2729
import Data.Time.Format.ISO8601
2830
import Data.Time.LocalTime (utc, utcToZonedTime, zonedTimeToUTC)
2931
import Development.Shake.Classes
@@ -32,15 +34,26 @@ import GHC.Generics
3234
import Toml (TomlCodec, (.=))
3335
import Toml qualified
3436

35-
data SourceMeta = SourceMeta' (Maybe WrapUTCTime) String (Maybe String) [RevisionMeta]
37+
data SourceMeta
38+
= SourceMeta'
39+
(Maybe WrapUTCTime)
40+
-- ^ timestamp
41+
String
42+
-- ^ url
43+
(Maybe String)
44+
-- ^ subdir
45+
[RevisionMeta]
46+
-- ^ revisions
47+
Bool
48+
-- ^ force version
3649
deriving (Show, Eq, Generic)
3750
deriving anyclass (Binary, Hashable, NFData)
3851

39-
pattern SourceMeta :: Maybe UTCTime -> String -> Maybe String -> [RevisionMeta] -> SourceMeta
40-
pattern SourceMeta {sourceTimestamp, sourceUrl, sourceSubdir, sourceRevisions} <-
41-
SourceMeta' (coerce -> sourceTimestamp) sourceUrl sourceSubdir sourceRevisions
52+
pattern SourceMeta :: Maybe UTCTime -> String -> Maybe String -> [RevisionMeta] -> Bool -> SourceMeta
53+
pattern SourceMeta {sourceTimestamp, sourceUrl, sourceSubdir, sourceRevisions, sourceForceVersion} <-
54+
SourceMeta' (coerce -> sourceTimestamp) sourceUrl sourceSubdir sourceRevisions sourceForceVersion
4255
where
43-
SourceMeta timestamp url subdir revisions = SourceMeta' (coerce timestamp) url subdir revisions
56+
SourceMeta timestamp url subdir revisions forceversion = SourceMeta' (coerce timestamp) url subdir revisions forceversion
4457

4558
sourceMetaCodec :: TomlCodec SourceMeta
4659
sourceMetaCodec =
@@ -49,6 +62,12 @@ sourceMetaCodec =
4962
<*> Toml.string "url" .= sourceUrl
5063
<*> Toml.dioptional (Toml.string "subdir") .= sourceSubdir
5164
<*> Toml.list revisionMetaCodec "revisions" .= sourceRevisions
65+
<*> withDefault False (Toml.bool "force-version") .= sourceForceVersion
66+
67+
withDefault :: Eq a => a -> TomlCodec a -> TomlCodec a
68+
withDefault d c = (fromMaybe d <$> Toml.dioptional c) .= f
69+
where
70+
f a = if a == d then Nothing else Just a
5271

5372
readSourceMeta :: FilePath -> IO SourceMeta
5473
readSourceMeta = Toml.decodeFile sourceMetaCodec

0 commit comments

Comments
 (0)