Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/tar'
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Jan 20, 2024
2 parents f6cf4cb + 856e48a commit 6ae312c
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 113 deletions.
1 change: 0 additions & 1 deletion app/ghcup/BrickMain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import Brick.Widgets.Center ( center, centerLayer )
import qualified Brick.Widgets.List as L
import Brick.Focus (FocusRing)
import qualified Brick.Focus as F
import Codec.Archive
import Control.Applicative
import Control.Exception.Safe
#if !MIN_VERSION_base(4,13,0)
Expand Down
10 changes: 9 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ packages: ./ghcup.cabal
optional-packages: ./vendored/*/*.cabal

package ghcup
flags: +tui
flags: +tui +tar

constraints: http-io-streams -brotli,
any.aeson >= 2.0.1.0
Expand All @@ -13,6 +13,11 @@ source-repository-package
location: https://github.com/fosskers/versions.git
tag: 7bc3355348aac3510771d4622aff09ac38c9924d

source-repository-package
type: git
location: https://github.com/haskell/tar.git
tag: d94a988be4311b830149a9f8fc16739927e5fc1c

package libarchive
flags: -system-libarchive

Expand All @@ -30,3 +35,6 @@ package streamly

package *
test-show-details: direct

allow-newer: cabal-install-parsers:tar

2 changes: 1 addition & 1 deletion cabal.project.release
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ optional-packages: ./vendored/*/*.cabal
optimization: 2

package ghcup
flags: +tui
flags: +tui -tar

if os(linux)
if arch(x86_64) || arch(i386)
Expand Down
27 changes: 25 additions & 2 deletions ghcup.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ flag no-exe
default: False
manual: True

flag tar
description: Use haskell tar instead of libarchive.
default: False
manual: True

common app-common-depends
build-depends:
, aeson >=1.4
Expand All @@ -68,7 +73,6 @@ common app-common-depends
, filepath ^>=1.4.2.1
, haskus-utils-types ^>=1.5
, haskus-utils-variant ^>=3.2.1
, libarchive ^>=3.0.3.0
, megaparsec >=8.0.0 && <9.3
, mtl ^>=2.2
, optparse-applicative >=0.15.1.0 && <0.18
Expand All @@ -90,6 +94,15 @@ common app-common-depends
, versions >=6.0.3 && <6.1
, yaml-streamly ^>=0.12.0

if flag(tar)
cpp-options: -DTAR
build-depends:
tar ^>=0.6.0.0
, zip ^>=2.0.0

else
build-depends: libarchive ^>=3.0.3.0

library
exposed-modules:
GHCup
Expand Down Expand Up @@ -122,6 +135,8 @@ library
GHCup.Types.Stack
GHCup.Utils
GHCup.Utils.Dirs
GHCup.Utils.Tar
GHCup.Utils.Tar.Types
GHCup.Version

hs-source-dirs: lib
Expand Down Expand Up @@ -166,7 +181,6 @@ library
, filepath ^>=1.4.2.1
, haskus-utils-types ^>=1.5
, haskus-utils-variant ^>=3.2.1
, libarchive ^>=3.0.3.0
, lzma-static ^>=5.2.5.3
, megaparsec >=8.0.0 && <9.3
, mtl ^>=2.2
Expand Down Expand Up @@ -196,6 +210,15 @@ library
, yaml-streamly ^>=0.12.0
, zlib ^>=0.6.2.2

if flag(tar)
cpp-options: -DTAR
build-depends:
tar ^>=0.6.0.0
, zip ^>=2.0.0

else
build-depends: libarchive ^>=3.0.3.0

if (flag(internal-downloader) && !os(windows))
exposed-modules: GHCup.Download.IOStreams
cpp-options: -DINTERNAL_DOWNLOADER
Expand Down
1 change: 0 additions & 1 deletion lib-opt/GHCup/OptParse/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import GHCup.OptParse.Common
#if !MIN_VERSION_base(4,13,0)
import Control.Monad.Fail ( MonadFail )
#endif
import Codec.Archive ( ArchiveResult )
import Control.Concurrent (threadDelay)
import Control.Monad.Reader
import Control.Monad.Trans.Resource
Expand Down
1 change: 0 additions & 1 deletion lib-opt/GHCup/OptParse/Install.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import GHCup.Prelude
import GHCup.Prelude.Logger
import GHCup.Prelude.String.QQ

import Codec.Archive
#if !MIN_VERSION_base(4,13,0)
import Control.Monad.Fail ( MonadFail )
#endif
Expand Down
1 change: 0 additions & 1 deletion lib-opt/GHCup/OptParse/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import Control.Exception.Safe ( MonadMask, MonadCatch )
#if !MIN_VERSION_base(4,13,0)
import Control.Monad.Fail ( MonadFail )
#endif
import Codec.Archive
import Control.Monad.Reader
import Control.Monad.Trans.Resource
import Data.Functor
Expand Down
1 change: 0 additions & 1 deletion lib-opt/GHCup/OptParse/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import GHCup.Utils.Dirs
import GHCup.Prelude.Logger
import GHCup.Prelude.String.QQ

import Codec.Archive
#if !MIN_VERSION_base(4,13,0)
import Control.Monad.Fail ( MonadFail )
#endif
Expand Down
1 change: 0 additions & 1 deletion lib/GHCup/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import GHCup.Prelude
import GHCup.Prelude.File
import GHCup.Prelude.Logger

import Codec.Archive ( ArchiveResult )
import Control.Applicative
import Control.Exception.Safe
import Control.Monad
Expand Down
1 change: 0 additions & 1 deletion lib/GHCup/Errors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ module GHCup.Errors where

import GHCup.Types

import Codec.Archive
import Control.Exception.Safe
import Data.ByteString ( ByteString )
import Data.CaseInsensitive ( CI )
Expand Down
1 change: 0 additions & 1 deletion lib/GHCup/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import GHCup.Prelude.String.QQ
import GHCup.Prelude.Version.QQ
import GHCup.Prelude.MegaParsec

import Codec.Archive ( ArchiveResult )
import Control.Applicative
import Control.Concurrent ( threadDelay )
import Control.Exception.Safe
Expand Down
1 change: 0 additions & 1 deletion lib/GHCup/HLS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import GHCup.Prelude.Logger
import GHCup.Prelude.Process
import GHCup.Prelude.String.QQ

import Codec.Archive ( ArchiveResult )
import Control.Applicative
import Control.Exception.Safe
import Control.Monad
Expand Down
1 change: 0 additions & 1 deletion lib/GHCup/Stack.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import GHCup.Prelude
import GHCup.Prelude.File
import GHCup.Prelude.Logger

import Codec.Archive ( ArchiveResult )
import Control.Applicative
import Control.Exception.Safe
import Control.Monad
Expand Down
3 changes: 2 additions & 1 deletion lib/GHCup/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ module GHCup.Types
, Key(..)
, Modifier(..)
#endif
, ArchiveResult(..)
)
where

import GHCup.Types.Stack ( SetupInfo )
import GHCup.Utils.Tar.Types ( ArchiveResult(..) )
import {-# SOURCE #-} GHCup.Utils.Dirs ( fromGHCupPath, GHCupPath )

import Control.DeepSeq ( NFData, rnf )
Expand Down Expand Up @@ -775,4 +777,3 @@ instance Pretty ToolVersion where
data BuildSystem = Hadrian
| Make
deriving (Show, Eq)

122 changes: 24 additions & 98 deletions lib/GHCup/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ installation and introspection of files/versions etc.
-}
module GHCup.Utils
( module GHCup.Utils.Dirs
, module GHCup.Utils.Tar
, module GHCup.Utils
#if defined(IS_WINDOWS)
, module GHCup.Prelude.Windows
Expand All @@ -42,14 +43,14 @@ import GHCup.Types
import GHCup.Types.Optics
import GHCup.Types.JSON ( )
import GHCup.Utils.Dirs
import GHCup.Utils.Tar
import GHCup.Version
import GHCup.Prelude
import GHCup.Prelude.File
import GHCup.Prelude.Logger.Internal
import GHCup.Prelude.MegaParsec
import GHCup.Prelude.Process
import GHCup.Prelude.String.QQ
import Codec.Archive hiding ( Directory )
import Control.Applicative
import Control.Exception.Safe
import Control.Monad
Expand Down Expand Up @@ -79,10 +80,6 @@ import Text.Regex.Posix
import Text.PrettyPrint.HughesPJClass (prettyShow)
import URI.ByteString

import qualified Codec.Compression.BZip as BZip
import qualified Codec.Compression.GZip as GZip
import qualified Codec.Compression.Lzma as Lzma
import qualified Data.ByteString.Lazy as BL
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Data.Text.Encoding as E
Expand Down Expand Up @@ -783,99 +780,6 @@ getLatestToolFor tool target pvpIn dls = do





-----------------
--[ Unpacking ]--
-----------------



-- | Unpack an archive to a temporary directory and return that path.
unpackToDir :: (MonadReader env m, HasLog env, MonadIO m, MonadThrow m)
=> FilePath -- ^ destination dir
-> FilePath -- ^ archive path
-> Excepts '[UnknownArchive
, ArchiveResult
] m ()
unpackToDir dfp av = do
let fn = takeFileName av
lift $ logInfo $ "Unpacking: " <> T.pack fn <> " to " <> T.pack dfp

let untar :: MonadIO m => BL.ByteString -> Excepts '[ArchiveResult] m ()
untar = lEM . liftIO . runArchiveM . unpackToDirLazy dfp

rf :: MonadIO m => FilePath -> Excepts '[ArchiveResult] m BL.ByteString
rf = liftIO . BL.readFile

-- extract, depending on file extension
if
| ".tar.gz" `isSuffixOf` fn -> liftE
(untar . GZip.decompress =<< rf av)
| ".tar.xz" `isSuffixOf` fn -> do
filecontents <- liftE $ rf av
let decompressed = Lzma.decompressWith (Lzma.defaultDecompressParams { Lzma.decompressAutoDecoder= True }) filecontents
liftE $ untar decompressed
| ".tar.bz2" `isSuffixOf` fn ->
liftE (untar . BZip.decompress =<< rf av)
| ".tar" `isSuffixOf` fn -> liftE (untar =<< rf av)
| ".zip" `isSuffixOf` fn -> liftE (untar =<< rf av)
| otherwise -> throwE $ UnknownArchive fn


getArchiveFiles :: (MonadReader env m, HasLog env, MonadIO m, MonadThrow m)
=> FilePath -- ^ archive path
-> Excepts '[UnknownArchive
, ArchiveResult
] m [FilePath]
getArchiveFiles av = do
let fn = takeFileName av

let entries :: Monad m => BL.ByteString -> Excepts '[ArchiveResult] m [FilePath]
entries = (fmap . fmap) filepath . lE . readArchiveBSL

rf :: MonadIO m => FilePath -> Excepts '[ArchiveResult] m BL.ByteString
rf = liftIO . BL.readFile

-- extract, depending on file extension
if
| ".tar.gz" `isSuffixOf` fn -> liftE
(entries . GZip.decompress =<< rf av)
| ".tar.xz" `isSuffixOf` fn -> do
filecontents <- liftE $ rf av
let decompressed = Lzma.decompressWith (Lzma.defaultDecompressParams { Lzma.decompressAutoDecoder= True }) filecontents
liftE $ entries decompressed
| ".tar.bz2" `isSuffixOf` fn ->
liftE (entries . BZip.decompress =<< rf av)
| ".tar" `isSuffixOf` fn -> liftE (entries =<< rf av)
| ".zip" `isSuffixOf` fn -> liftE (entries =<< rf av)
| otherwise -> throwE $ UnknownArchive fn


intoSubdir :: (MonadReader env m, HasLog env, MonadIO m, MonadThrow m, MonadCatch m)
=> GHCupPath -- ^ unpacked tar dir
-> TarDir -- ^ how to descend
-> Excepts '[TarDirDoesNotExist] m GHCupPath
intoSubdir bdir tardir = case tardir of
RealDir pr -> do
whenM (fmap not . liftIO . doesDirectoryExist $ fromGHCupPath (bdir `appendGHCupPath` pr))
(throwE $ TarDirDoesNotExist tardir)
pure (bdir `appendGHCupPath` pr)
RegexDir r -> do
let rs = split (`elem` pathSeparators) r
foldlM
(\y x ->
(handleIO (\_ -> pure []) . liftIO . findFiles (fromGHCupPath y) . regex $ x) >>= (\case
[] -> throwE $ TarDirDoesNotExist tardir
(p : _) -> pure (y `appendGHCupPath` p)) . sort
)
bdir
rs
where regex = makeRegexOpts compIgnoreCase execBlank




------------
--[ Tags ]--
------------
Expand Down Expand Up @@ -929,6 +833,28 @@ getLatestBaseVersion av pvpVer =
--[ Other ]--
-------------


intoSubdir :: (MonadReader env m, HasLog env, MonadIO m, MonadThrow m, MonadCatch m)
=> GHCupPath -- ^ unpacked tar dir
-> TarDir -- ^ how to descend
-> Excepts '[TarDirDoesNotExist] m GHCupPath
intoSubdir bdir tardir = case tardir of
RealDir pr -> do
whenM (fmap not . liftIO . doesDirectoryExist $ fromGHCupPath (bdir `appendGHCupPath` pr))
(throwE $ TarDirDoesNotExist tardir)
pure (bdir `appendGHCupPath` pr)
RegexDir r -> do
let rs = split (`elem` pathSeparators) r
foldlM
(\y x ->
(handleIO (\_ -> pure []) . liftIO . findFiles (fromGHCupPath y) . regex $ x) >>= (\case
[] -> throwE $ TarDirDoesNotExist tardir
(p : _) -> pure (y `appendGHCupPath` p)) . sort
)
bdir
rs
where regex = makeRegexOpts compIgnoreCase execBlank

-- | Usually @~\/.ghcup\/ghc\/\<ver\>\/bin\/@
ghcInternalBinDir :: (MonadReader env m, HasDirs env, MonadThrow m, MonadFail m, MonadIO m)
=> GHCTargetVersion
Expand Down
Loading

0 comments on commit 6ae312c

Please sign in to comment.