Skip to content

Add stack-repos and cabal-name executables #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cabal-name/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Main where

import CabalName (doCabalName)
import CabalName.CLI (parseCabalNameArgs)

main :: IO ()
main = parseCabalNameArgs >>= doCabalName
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
packages: .
packages: .

-- Needs https://github.com/input-output-hk/iohk-nix/commit/6a8c29117eff36ce975e02e01efc8b25d93fcb90#diff-6fb0c6517b547a8baf082d5d2d604842
-- to work with the data-dir issues when building components.
Expand Down
13 changes: 13 additions & 0 deletions lib/CabalName.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module CabalName
( doCabalName
) where

import Stack2nix.Project (findCabalFiles)
import Cabal2Nix (cabalFilePkgName)

import CabalName.CLI (Args(..))

doCabalName :: Args -> IO ()
doCabalName args =
findCabalFiles (argHpackUse args) (argPackageDir args)
>>= mapM_ (putStr . cabalFilePkgName) . take 1
29 changes: 29 additions & 0 deletions lib/CabalName/CLI.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module CabalName.CLI
( Args(..)
, HpackUse(..)
, parseCabalNameArgs
) where

import Options.Applicative hiding (option)
import Data.Semigroup ((<>))
import Stack2nix.CLI (HpackUse(..))

--------------------------------------------------------------------------------
-- CLI Arguments
data Args = Args
{ argPackageDir :: FilePath
, argHpackUse :: HpackUse
} deriving Show

-- Argument Parser
args :: Parser Args
args = Args
<$> argument str ( metavar "DIR" <> help "Directory containing the package source" )
<*> flag UsePackageYamlFirst IgnorePackageYaml (long "ignore-package-yaml" <> help "disable hpack run and use only cabal disregarding package.yaml existence")

parseCabalNameArgs :: IO Args
parseCabalNameArgs = execParser opts
where opts = info (args <**> helper)
( fullDesc
<> progDesc "Find the name of the packeage in the specified directory"
<> header "cabal-name - extract the name of a package" )
2 changes: 1 addition & 1 deletion stack2nix/Stack2nix.hs → lib/Stack2nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ packages2nix args pkgs =
writeDoc nixFile =<<
prettyNix <$> cabal2nix True (argDetailLevel args) src cabalFile
return $ fromString pkg $= mkPath False nix
(DVCS (Git url rev) subdirs) ->
(DVCS (Git url rev) _ subdirs) ->
fmap concat . forM subdirs $ \subdir ->
do cacheHits <- liftIO $ cacheHits (argCacheFile args) url rev subdir
case cacheHits of
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion stack2nix/Stack2nix/Stack.hs → lib/Stack2nix/Stack.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ instance Text CabalRev where
data Dependency
= PkgIndex PackageIdentifier (Maybe (Either Sha256 CabalRev)) -- ^ overridden package in the stackage index
| LocalPath String -- ^ Some local package (potentially overriding a package in the index as well)
| DVCS Location [FilePath] -- ^ One or more packages fetched from git or similar.
| DVCS Location (Maybe Sha256) [FilePath] -- ^ One or more packages fetched from git or similar.
-- TODO: Support archives.
-- | Archive ...
deriving (Show)
Expand Down Expand Up @@ -213,6 +213,7 @@ instance FromJSON Dependency where
return . LocalPath . dropTrailingSlash . T.unpack
parseDVCS = withObject "DVCS" $ \o -> DVCS
<$> (o .: "location" <|> parseJSON p)
<*> o .:? "nix-sha256" .!= Nothing
<*> o .:? "subdirs" .!= ["."]

-- drop trailing slashes. Nix doesn't like them much;
Expand Down
46 changes: 46 additions & 0 deletions lib/StackRepos.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}

module StackRepos
( doStackRepos
, stack2nix
) where

import Data.Aeson (ToJSON(..))
import Data.Aeson.Encode.Pretty (encodePretty)
import qualified Data.ByteString.Lazy as LBS (writeFile)
import Data.Yaml (decodeFileEither)

import GHC.Generics (Generic)

import Stack2nix.Stack (Stack(..), Dependency(..), Location(..))
import Stack2nix.External.Resolve

import StackRepos.CLI (Args(..))

data SourceRepos = SourceRepos
{ url :: String
, rev :: String
, sha256 :: Maybe String
, subdirs :: [FilePath]
} deriving (Show, Eq, Ord, Generic)

instance ToJSON SourceRepos

doStackRepos :: Args -> IO ()
doStackRepos args = do
evalue <- decodeFileEither (argStackYaml args)
case evalue of
Left e -> error (show e)
Right value -> stack2nix args
=<< resolveSnapshot (argStackYaml args) value

stack2nix :: Args -> Stack -> IO ()
stack2nix args (Stack _ _ pkgs _ _) =
LBS.writeFile "repos.json" $ encodePretty (
pkgs >>= (\case
(DVCS (Git url rev) sha256 subdirs) ->
[SourceRepos { url, rev, sha256, subdirs }]
_ -> []))
27 changes: 27 additions & 0 deletions lib/StackRepos/CLI.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module StackRepos.CLI
( Args(..)
, HpackUse(..)
, parseStackReposArgs
) where

import Options.Applicative hiding (option)
import Data.Semigroup ((<>))
import Stack2nix.CLI (HpackUse(..))

--------------------------------------------------------------------------------
-- CLI Arguments
newtype Args = Args
{ argStackYaml :: FilePath
} deriving Show

-- Argument Parser
args :: Parser Args
args = Args
<$> strOption ( long "stack-yaml" <> value "stack.yaml" <> showDefault <> metavar "FILE" <> help "Override project stack.yaml" )

parseStackReposArgs :: IO Args
parseStackReposArgs = execParser opts
where opts = info (args <**> helper)
( fullDesc
<> progDesc "Collect information about remote source packages used by Stack"
<> header "stack-repos - extract the details of remote repos" )
83 changes: 46 additions & 37 deletions nix-tools.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,42 @@ library
exposed-modules: Cabal2Nix
, Cabal2Nix.Util
, Cabal2Nix.Plan
, CabalName
, CabalName.CLI
, Distribution.Nixpkgs.Fetch
, StackRepos
, StackRepos.CLI
, Stack2nix
, Stack2nix.Cache
, Stack2nix.CLI
, Stack2nix.External.Resolve
, Stack2nix.Project
, Stack2nix.Stack
build-depends: base >=4 && <4.13
, hnix
, aeson
, unordered-containers
, process
, deepseq
, transformers
, data-fix
, Cabal >= 2.4
, text
, filepath
, directory
, aeson
, aeson-pretty
, base16-bytestring
, bytestring
, cryptohash-sha256
, base16-bytestring
, data-fix
, deepseq
, directory
, extra
, filepath
, hnix
, hpack
, http-client
, http-client-tls
, http-types
, optparse-applicative
, prettyprinter
, process
, text
, transformers
, unordered-containers
, yaml

hs-source-dirs: lib
default-language: Haskell2010

Expand Down Expand Up @@ -139,34 +158,8 @@ executable lts-to-nix
executable stack-to-nix
ghc-options: -Wall
main-is: Main.hs
other-modules: Stack2nix
, Stack2nix.Cache
, Stack2nix.CLI
, Stack2nix.External.Resolve
, Stack2nix.Project
, Stack2nix.Stack
build-depends: base >=4 && <4.13
, nix-tools
, transformers
, hnix
, yaml
, aeson
, microlens
, microlens-aeson
, text
, Cabal
, vector
, prettyprinter
, directory
, filepath
, extra
, hpack
, bytestring
, optparse-applicative
, http-client-tls
, http-client
, http-types
, unordered-containers
hs-source-dirs: stack2nix
default-language: Haskell2010

Expand All @@ -182,3 +175,19 @@ executable truncate-index
, time
hs-source-dirs: truncate-index
default-language: Haskell2010

executable stack-repos
ghc-options: -Wall
main-is: Main.hs
build-depends: base >=4 && <4.13
, nix-tools
hs-source-dirs: stack-repos
default-language: Haskell2010

executable cabal-name
ghc-options: -Wall
main-is: Main.hs
build-depends: base >=4 && <4.13
, nix-tools
hs-source-dirs: cabal-name
default-language: Haskell2010
7 changes: 7 additions & 0 deletions stack-repos/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Main where

import StackRepos (doStackRepos)
import StackRepos.CLI (parseStackReposArgs)

main :: IO ()
main = parseStackReposArgs >>= doStackRepos