Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabedini committed Apr 20, 2023
1 parent 59d1c77 commit 2e27d6b
Showing 1 changed file with 2 additions and 30 deletions.
32 changes: 2 additions & 30 deletions app/Foliage/CmdBuild.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ import Data.Aeson qualified as Aeson
import Data.Bifunctor (second)
import Data.ByteString.Char8 qualified as BS
import Data.ByteString.Lazy.Char8 qualified as BL
import Data.Function ((&))
import Data.List (sortOn)
import Data.List.NonEmpty qualified as NE
import Data.Map qualified as Map
import Data.Maybe (fromMaybe)
import Data.Text qualified as T
import Data.Traversable (for)
import Development.Shake
import Development.Shake.FilePath
import Distribution.Client.IndexUtils (PreferredVersionsParseError (PreferredVersionsParseError))
import Distribution.Compat.Lens (_1)
import Distribution.Package
import Distribution.Pretty (prettyShow)
import Distribution.Version
Expand All @@ -40,6 +36,7 @@ import Foliage.Time qualified as Time
import Hackage.Security.Util.Path (castRoot, toFilePath)
import Network.URI (URI (uriPath, uriQuery, uriScheme), nullURI)
import System.Directory (createDirectoryIfMissing)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)

cmdBuild :: BuildOptions -> IO ()
cmdBuild buildOptions = do
Expand Down Expand Up @@ -164,7 +161,7 @@ buildAction
NE.tail $
NE.scanl
(\(_, range) (ts, change) -> (ts, simplifyVersionRange $ change range))
(0, anyVersion)
(posixSecondsToUTCTime 0, anyVersion)
deprecationChanges
in map
( \(ts, effectiveRange) ->
Expand Down Expand Up @@ -331,31 +328,6 @@ prepareIndexPkgMetadata expiryTime PreparedPackageVersion {pkgId, sdistPath} = d
targetsDelegations = Nothing
}

prepareIndexPkgExtraEntries :: UTCTime -> [PreparedPackageVersion] -> [Tar.Entry]
prepareIndexPkgExtraEntries defaultTimestamp packageVersions =
map buildTarEntry $ sortOn (fst . snd) $ Map.toList $ getPreferredVersions packageVersions
where
buildTarEntry (pkgName, (maybeTs, vr)) =
mkTarEntry content path (fromMaybe defaultTimestamp maybeTs)
where
path = prettyShow pkgName </> "preferred-versions"
content = generatePreferredVersionsFileContent vr

getPreferredVersions :: [PreparedPackageVersion] -> Map.Map PackageName (Maybe UTCTime, VersionRange)
getPreferredVersions packageVersions =
Map.fromListWith combineValues [(getKey p, getValue p) | p <- packageVersions]
where
getKey PreparedPackageVersion {pkgId = PackageIdentifier {pkgName}} = pkgName
getValue PreparedPackageVersion {pkgTimestamp, pkgVersionIsDeprecated, pkgId = PackageIdentifier {pkgVersion}}
| not pkgVersionIsDeprecated = (pkgTimestamp, thisVersion pkgVersion)
| otherwise = (pkgTimestamp, noVersion)
combineValues (ts1, vr1) (ts2, vr2) =
(max ts1 ts2, unionVersionRanges vr1 vr2)

generatePreferredVersionsFileContent :: VersionRange -> BL.ByteString
generatePreferredVersionsFileContent versionRange =
BL.fromStrict . BS.pack $ prettyShow versionRange

mkTarEntry :: BL.ByteString -> IndexFile dec -> UTCTime -> Tar.Entry
mkTarEntry contents indexFile timestamp =
(Tar.fileEntry tarPath contents)
Expand Down

0 comments on commit 2e27d6b

Please sign in to comment.