From 11a515ed0e887eef081e514b51f29589cf6693ca Mon Sep 17 00:00:00 2001 From: Daniel Gröber Date: Fri, 1 May 2020 23:00:21 +0200 Subject: Move CabalVersion and related types into a new module --- src/CabalHelper/Compiletime/Cabal.hs | 44 ++-------------- src/CabalHelper/Compiletime/Compile.hs | 1 + .../Compiletime/Program/CabalInstall.hs | 4 +- src/CabalHelper/Compiletime/Program/GHC.hs | 2 +- src/CabalHelper/Compiletime/Types.hs | 1 + src/CabalHelper/Compiletime/Types/Cabal.hs | 59 ++++++++++++++++++++++ 6 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 src/CabalHelper/Compiletime/Types/Cabal.hs (limited to 'src/CabalHelper/Compiletime') diff --git a/src/CabalHelper/Compiletime/Cabal.hs b/src/CabalHelper/Compiletime/Cabal.hs index 1c4efa5..aad004c 100644 --- a/src/CabalHelper/Compiletime/Cabal.hs +++ b/src/CabalHelper/Compiletime/Cabal.hs @@ -15,7 +15,8 @@ Description : Cabal library source unpacking License : Apache-2.0 -} -{-# LANGUAGE DeriveFunctor, ViewPatterns, OverloadedStrings, CPP #-} +{-# LANGUAGE DeriveFunctor, ViewPatterns, OverloadedStrings #-} +{-# LANGUAGE CPP #-} -- for VERSION_Cabal module CabalHelper.Compiletime.Cabal where @@ -37,43 +38,10 @@ import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as BS8 import CabalHelper.Compiletime.Types +import CabalHelper.Compiletime.Types.Cabal import CabalHelper.Compiletime.Process import CabalHelper.Shared.Common (replace, parseVer, parseVerMay, parsePkgIdBS, panicIO) -type UnpackedCabalVersion = CabalVersion' (CommitId, CabalSourceDir) -type ResolvedCabalVersion = CabalVersion' CommitId -type CabalVersion = CabalVersion' () - -unpackedToResolvedCabalVersion :: UnpackedCabalVersion -> ResolvedCabalVersion -unpackedToResolvedCabalVersion (CabalHEAD (commit, _)) = CabalHEAD commit -unpackedToResolvedCabalVersion (CabalVersion ver) = CabalVersion ver - --- | Cabal library version we're compiling the helper exe against. -data CabalVersion' a - = CabalHEAD a - | CabalVersion { cvVersion :: Version } - deriving (Eq, Ord, Functor) - -newtype CommitId = CommitId { unCommitId :: String } - -showUnpackedCabalVersion :: UnpackedCabalVersion -> String -showUnpackedCabalVersion (CabalHEAD (commitid, _)) = - "HEAD-" ++ unCommitId commitid -showUnpackedCabalVersion CabalVersion {cvVersion} = - showVersion cvVersion - -showResolvedCabalVersion :: ResolvedCabalVersion -> String -showResolvedCabalVersion (CabalHEAD commitid) = - "HEAD-" ++ unCommitId commitid -showResolvedCabalVersion CabalVersion {cvVersion} = - showVersion cvVersion - -showCabalVersion :: CabalVersion -> String -showCabalVersion (CabalHEAD ()) = - "HEAD" -showCabalVersion CabalVersion {cvVersion} = - showVersion cvVersion - data CabalPatchDescription = CabalPatchDescription { cpdVersions :: [Version] , cpdUnpackVariant :: UnpackCabalVariant @@ -164,8 +132,6 @@ unpackCabal (CabalHEAD ()) tmpdir = do (commit, csdir) <- unpackCabalHEAD tmpdir return $ CabalHEAD (commit, csdir) -data UnpackCabalVariant = Pristine | LatestRevision -newtype CabalSourceDir = CabalSourceDir { unCabalSourceDir :: FilePath } unpackCabalHackage :: (Verbose, Progs) => Version @@ -254,8 +220,8 @@ complainIfNoCabalFile _ (Just cabal_file) = return cabal_file complainIfNoCabalFile pkgdir Nothing = panicIO $ "No cabal file found in package-dir: '"++pkgdir++"'" -bultinCabalVersion :: Version -bultinCabalVersion = parseVer VERSION_Cabal +bultinCabalVersion :: CabalVersion +bultinCabalVersion = CabalVersion $ parseVer VERSION_Cabal readSetupConfigHeader :: FilePath -> IO (Maybe UnitHeader) readSetupConfigHeader file = bracket (openFile file ReadMode) hClose $ \h -> do diff --git a/src/CabalHelper/Compiletime/Compile.hs b/src/CabalHelper/Compiletime/Compile.hs index d2886e8..2993906 100644 --- a/src/CabalHelper/Compiletime/Compile.hs +++ b/src/CabalHelper/Compiletime/Compile.hs @@ -58,6 +58,7 @@ import CabalHelper.Compiletime.Program.CabalInstall import CabalHelper.Compiletime.Sandbox ( getSandboxPkgDb ) import CabalHelper.Compiletime.Types +import CabalHelper.Compiletime.Types.Cabal import CabalHelper.Shared.Common diff --git a/src/CabalHelper/Compiletime/Program/CabalInstall.hs b/src/CabalHelper/Compiletime/Program/CabalInstall.hs index d5ed15e..9823c50 100644 --- a/src/CabalHelper/Compiletime/Program/CabalInstall.hs +++ b/src/CabalHelper/Compiletime/Program/CabalInstall.hs @@ -43,8 +43,10 @@ import qualified CabalHelper.Compiletime.Cabal as Cabal import CabalHelper.Compiletime.Types import CabalHelper.Compiletime.Program.GHC ( GhcVersion(..), createPkgDb ) +import CabalHelper.Compiletime.Types.Cabal + ( CabalSourceDir(..), UnpackedCabalVersion, CabalVersion'(..) ) import CabalHelper.Compiletime.Cabal - ( CabalSourceDir(..), UnpackedCabalVersion, CabalVersion'(..), unpackCabalV1 ) + ( unpackCabalV1 ) import CabalHelper.Compiletime.Process import CabalHelper.Shared.InterfaceTypes ( ChComponentName(..), ChLibraryName(..) ) diff --git a/src/CabalHelper/Compiletime/Program/GHC.hs b/src/CabalHelper/Compiletime/Program/GHC.hs index 95293fb..9ab0b33 100644 --- a/src/CabalHelper/Compiletime/Program/GHC.hs +++ b/src/CabalHelper/Compiletime/Program/GHC.hs @@ -31,7 +31,7 @@ import System.Directory import CabalHelper.Shared.Common (parseVer, trim, appCacheDir, parsePkgId) import CabalHelper.Compiletime.Types -import CabalHelper.Compiletime.Cabal +import CabalHelper.Compiletime.Types.Cabal ( ResolvedCabalVersion, showResolvedCabalVersion, UnpackedCabalVersion , unpackedToResolvedCabalVersion, CabalVersion'(..) ) import CabalHelper.Compiletime.Process diff --git a/src/CabalHelper/Compiletime/Types.hs b/src/CabalHelper/Compiletime/Types.hs index ab84178..b96101a 100644 --- a/src/CabalHelper/Compiletime/Types.hs +++ b/src/CabalHelper/Compiletime/Types.hs @@ -30,6 +30,7 @@ import GHC.Generics import System.FilePath (takeDirectory) import System.Posix.Types import CabalHelper.Compiletime.Types.RelativePath +import CabalHelper.Compiletime.Types.Cabal import CabalHelper.Shared.InterfaceTypes import Data.List.NonEmpty (NonEmpty) diff --git a/src/CabalHelper/Compiletime/Types/Cabal.hs b/src/CabalHelper/Compiletime/Types/Cabal.hs new file mode 100644 index 0000000..90ee975 --- /dev/null +++ b/src/CabalHelper/Compiletime/Types/Cabal.hs @@ -0,0 +1,59 @@ +-- cabal-helper: Simple interface to Cabal's configuration state +-- Copyright (C) 2020 Daniel Gröber +-- +-- SPDX-License-Identifier: Apache-2.0 +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 + +{-| +Module : CabalHelper.Compiletime.Types.Cabal +License : Apache-2.0 +-} + +{-# LANGUAGE DeriveFunctor #-} + +module CabalHelper.Compiletime.Types.Cabal where + +import Data.Version + +-- | Cabal library version we're compiling the helper exe against. +data CabalVersion' a + = CabalHEAD a + | CabalVersion { cvVersion :: Version } + deriving (Eq, Ord, Functor) + +newtype CommitId = CommitId { unCommitId :: String } + +type UnpackedCabalVersion = CabalVersion' (CommitId, CabalSourceDir) +type ResolvedCabalVersion = CabalVersion' CommitId +type CabalVersion = CabalVersion' () + +data UnpackCabalVariant = Pristine | LatestRevision +newtype CabalSourceDir = CabalSourceDir { unCabalSourceDir :: FilePath } + + +unpackedToResolvedCabalVersion :: UnpackedCabalVersion -> ResolvedCabalVersion +unpackedToResolvedCabalVersion (CabalHEAD (commit, _)) = CabalHEAD commit +unpackedToResolvedCabalVersion (CabalVersion ver) = CabalVersion ver + +showUnpackedCabalVersion :: UnpackedCabalVersion -> String +showUnpackedCabalVersion (CabalHEAD (commitid, _)) = + "HEAD-" ++ unCommitId commitid +showUnpackedCabalVersion CabalVersion {cvVersion} = + showVersion cvVersion + +showResolvedCabalVersion :: ResolvedCabalVersion -> String +showResolvedCabalVersion (CabalHEAD commitid) = + "HEAD-" ++ unCommitId commitid +showResolvedCabalVersion CabalVersion {cvVersion} = + showVersion cvVersion + +showCabalVersion :: CabalVersion -> String +showCabalVersion (CabalHEAD ()) = + "HEAD" +showCabalVersion CabalVersion {cvVersion} = + showVersion cvVersion -- cgit v1.2.3