Skip to content

Commit

Permalink
Move parsing functions into pantry
Browse files Browse the repository at this point in the history
  • Loading branch information
qrilka committed Aug 23, 2018
1 parent 3211dd5 commit 7cfef74
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 58 deletions.
1 change: 0 additions & 1 deletion src/Stack/Ghci.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import Stack.Types.Compiler
import Stack.Types.Config
import Stack.Types.NamedComponent
import Stack.Types.Package
import Stack.Types.PackageName
import Stack.Types.Runner
import System.IO (putStrLn)
import System.IO.Temp (getCanonicalTemporaryDirectory)
Expand Down
1 change: 0 additions & 1 deletion src/Stack/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ import Stack.Types.Config
import Stack.Types.GhcPkgId
import Stack.Types.NamedComponent
import Stack.Types.Package
import Stack.Types.PackageName
import Stack.Types.Runner
import Stack.Types.Version
import qualified System.Directory as D
Expand Down
2 changes: 0 additions & 2 deletions src/Stack/PackageDump.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ import Stack.GhcPkg
import Stack.Types.Compiler
import Stack.Types.GhcPkgId
import Stack.Types.PackageDump
import Stack.Types.PackageName
import Stack.Types.Version
import System.Directory (getDirectoryContents, doesFileExist)
import System.Process (readProcess) -- FIXME confirm that this is correct
import RIO.Process hiding (readProcess)
Expand Down
1 change: 0 additions & 1 deletion src/Stack/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Stack.Runners
import Stack.Types.BuildPlan
import Stack.Types.Compiler
import Stack.Types.Config
import Stack.Types.PackageName
import System.FilePath (dropExtension, replaceExtension)
import RIO.Process
import qualified RIO.Text as T
Expand Down
1 change: 0 additions & 1 deletion src/Stack/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ import Stack.Types.Compiler
import Stack.Types.CompilerBuild
import Stack.Types.Config
import Stack.Types.Docker
import Stack.Types.PackageName
import Stack.Types.Runner
import Stack.Types.Version
import qualified System.Directory as D
Expand Down
1 change: 0 additions & 1 deletion src/Stack/Setup/Installed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import Path
import Path.IO
import Stack.Types.Compiler
import Stack.Types.Config
import Stack.Types.Version
import RIO.Process

data Tool
Expand Down
37 changes: 2 additions & 35 deletions src/Stack/Types/PackageName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,12 @@
-- | Names for packages.

module Stack.Types.PackageName
(parsePackageNameThrowing
,parsePackageNameFromFilePath
,packageNameArgument)
where
( packageNameArgument
) where

import Stack.Prelude
import qualified Data.Text as T
import qualified Options.Applicative as O
import Path

-- | A parse fail.
data PackageNameParseFail
= PackageNameParseFail Text
| CabalFileNameParseFail FilePath
| CabalFileNameInvalidPackageName FilePath
deriving (Typeable)
instance Exception PackageNameParseFail
instance Show PackageNameParseFail where
show (PackageNameParseFail bs) = "Invalid package name: " ++ show bs
show (CabalFileNameParseFail fp) = "Invalid file path for cabal file, must have a .cabal extension: " ++ fp
show (CabalFileNameInvalidPackageName fp) = "cabal file names must use valid package names followed by a .cabal extension, the following is invalid: " ++ fp

-- | Parse a package name from a 'String'.
parsePackageNameThrowing :: MonadThrow m => String -> m PackageName
parsePackageNameThrowing str =
case parsePackageName str of
Nothing -> throwM $ PackageNameParseFail $ T.pack str
Just pn -> pure pn

-- | Parse a package name from a file path.
parsePackageNameFromFilePath :: MonadThrow m => Path a File -> m PackageName
parsePackageNameFromFilePath fp = do
base <- clean $ toFilePath $ filename fp
case parsePackageName base of
Nothing -> throwM $ CabalFileNameInvalidPackageName $ toFilePath fp
Just x -> return x
where clean = liftM reverse . strip . reverse
strip ('l':'a':'b':'a':'c':'.':xs) = return xs
strip _ = throwM (CabalFileNameParseFail (toFilePath fp))

-- | An argument which accepts a template name of the format
-- @foo.hsfiles@.
Expand Down
15 changes: 0 additions & 15 deletions src/Stack/Types/Version.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module Stack.Types.Version
,Cabal.VersionRange -- TODO in the future should have a newtype wrapper
,IntersectingVersionRange(..)
,VersionCheck(..)
,parseVersionThrowing
,versionRangeText
,withinRange
,Stack.Types.Version.intersectVersionRanges
Expand All @@ -40,13 +39,6 @@ import Distribution.Version (Version, versionNumbers, withinRange)
import qualified Paths_stack as Meta
import Text.PrettyPrint (render)

-- | A parse fail.
newtype VersionParseFail = VersionParseFail Text
deriving (Typeable)
instance Exception VersionParseFail
instance Show VersionParseFail where
show (VersionParseFail bs) = "Invalid version: " ++ show bs

-- | A Package upgrade; Latest or a specific version.
data UpgradeTo = Specific Version | Latest deriving (Show)

Expand All @@ -62,13 +54,6 @@ instance Monoid IntersectingVersionRange where
mempty = IntersectingVersionRange Cabal.anyVersion
mappend = (<>)

-- | Convenient way to parse a package version from a 'String'.
parseVersionThrowing :: MonadThrow m => String -> m Version
parseVersionThrowing str =
case parseVersion str of
Nothing -> throwM $ VersionParseFail $ T.pack str
Just v -> pure v

-- | Display a version range
versionRangeText :: Cabal.VersionRange -> Text
versionRangeText = T.pack . render . disp
Expand Down
1 change: 0 additions & 1 deletion src/test/Stack/NixSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Stack.Prelude
import Stack.Types.Config
import Stack.Types.Nix
import Stack.Types.Runner
import Stack.Types.Version
import System.Directory
import System.Environment
import Test.Hspec
Expand Down
3 changes: 3 additions & 0 deletions subs/pantry/src/Pantry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ module Pantry
-- ** Cabal values
, parsePackageIdentifier
, parsePackageName
, parsePackageNameThrowing
, parsePackageNameFromFilePath
, parseFlagName
, parseVersion
, parseVersionThrowing

-- * Stackage snapshots
, ltsSnapshotLocation
Expand Down
47 changes: 47 additions & 0 deletions subs/pantry/src/Pantry/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ module Pantry.Types
, RepoType (..)
, parsePackageIdentifier
, parsePackageName
, parsePackageNameThrowing
, parsePackageNameFromFilePath
, parseFlagName
, parseVersion
, parseVersionThrowing
, packageIdentifierString
, packageNameString
, flagNameString
Expand Down Expand Up @@ -1002,12 +1005,56 @@ parsePackageIdentifier str =
parsePackageName :: String -> Maybe PackageName
parsePackageName = Distribution.Text.simpleParse

-- | A package name parse fail.
data PackageNameParseFail
= PackageNameParseFail Text
| CabalFileNameParseFail FilePath
| CabalFileNameInvalidPackageName FilePath
deriving (Typeable)
instance Exception PackageNameParseFail
instance Show PackageNameParseFail where
show (PackageNameParseFail bs) = "Invalid package name: " ++ show bs
show (CabalFileNameParseFail fp) = "Invalid file path for cabal file, must have a .cabal extension: " ++ fp
show (CabalFileNameInvalidPackageName fp) = "cabal file names must use valid package names followed by a .cabal extension, the following is invalid: " ++ fp

-- | Parse a package name from a 'String'.
parsePackageNameThrowing :: MonadThrow m => String -> m PackageName
parsePackageNameThrowing str =
case parsePackageName str of
Nothing -> throwM $ PackageNameParseFail $ T.pack str
Just pn -> pure pn

-- | Parse a package name from a file path.
parsePackageNameFromFilePath :: MonadThrow m => Path a File -> m PackageName
parsePackageNameFromFilePath fp = do
base <- clean $ toFilePath $ filename fp
case parsePackageName base of
Nothing -> throwM $ CabalFileNameInvalidPackageName $ toFilePath fp
Just x -> return x
where clean = liftM reverse . strip . reverse
strip ('l':'a':'b':'a':'c':'.':xs) = return xs
strip _ = throwM (CabalFileNameParseFail (toFilePath fp))

-- | Parse a version from a 'String'.
--
-- @since 0.1.0.0
parseVersion :: String -> Maybe Version
parseVersion = Distribution.Text.simpleParse

-- | A parse fail.
newtype VersionParseFail = VersionParseFail Text
deriving (Typeable)
instance Exception VersionParseFail
instance Show VersionParseFail where
show (VersionParseFail bs) = "Invalid version: " ++ show bs

-- | Convenient way to parse a package version from a 'String'.
parseVersionThrowing :: MonadThrow m => String -> m Version
parseVersionThrowing str =
case parseVersion str of
Nothing -> throwM $ VersionParseFail $ T.pack str
Just v -> pure v

-- | Parse a version range from a 'String'.
--
-- @since 0.1.0.0
Expand Down

0 comments on commit 7cfef74

Please sign in to comment.