1
1
mirror of https://github.com/nmattia/niv.git synced 2024-09-19 11:27:40 +03:00

Run Ormolu

This commit is contained in:
Nicolas Mattia 2020-07-23 16:24:16 +02:00
parent 1edb6856ad
commit e0bfb5d007
18 changed files with 1482 additions and 1289 deletions

View File

@ -11,7 +11,8 @@ import qualified Data.ByteString.Lazy as BL
encodeFilePretty :: (ToJSON a) => FilePath -> a -> IO ()
encodeFilePretty fp = BL.writeFile fp . AesonPretty.encodePretty' config
where
config = AesonPretty.defConfig {
AesonPretty.confTrailingNewline = True,
config =
AesonPretty.defConfig
{ AesonPretty.confTrailingNewline = True,
AesonPretty.confCompare = compare
}

View File

@ -1,40 +1,39 @@
module Data.HashMap.Strict.Extended where
import Control.Monad
import Data.Hashable (Hashable)
import qualified Data.HashMap.Strict as HMS
import Data.Hashable (Hashable)
--- HashMap
forWithKeyM
:: (Eq k, Hashable k, Monad m)
=> HMS.HashMap k v1
-> (k -> v1 -> m v2)
-> m (HMS.HashMap k v2)
forWithKeyM ::
(Eq k, Hashable k, Monad m) =>
HMS.HashMap k v1 ->
(k -> v1 -> m v2) ->
m (HMS.HashMap k v2)
forWithKeyM = flip mapWithKeyM
forWithKeyM_
:: (Eq k, Hashable k, Monad m)
=> HMS.HashMap k v1
-> (k -> v1 -> m ())
-> m ()
forWithKeyM_ ::
(Eq k, Hashable k, Monad m) =>
HMS.HashMap k v1 ->
(k -> v1 -> m ()) ->
m ()
forWithKeyM_ = flip mapWithKeyM_
mapWithKeyM
:: (Eq k, Hashable k, Monad m)
=> (k -> v1 -> m v2)
-> HMS.HashMap k v1
-> m (HMS.HashMap k v2)
mapWithKeyM ::
(Eq k, Hashable k, Monad m) =>
(k -> v1 -> m v2) ->
HMS.HashMap k v1 ->
m (HMS.HashMap k v2)
mapWithKeyM f m = do
fmap mconcat $ forM (HMS.toList m) $ \(k, v) ->
HMS.singleton k <$> f k v
mapWithKeyM_
:: (Eq k, Hashable k, Monad m)
=> (k -> v1 -> m ())
-> HMS.HashMap k v1
-> m ()
mapWithKeyM_ ::
(Eq k, Hashable k, Monad m) =>
(k -> v1 -> m ()) ->
HMS.HashMap k v1 ->
m ()
mapWithKeyM_ f m = do
forM_ (HMS.toList m) $ \(k, v) ->
HMS.singleton k <$> f k v

View File

@ -2,10 +2,10 @@
module Data.Text.Extended where
import Niv.Logger
import UnliftIO
import System.Exit (exitFailure)
import qualified Data.Text as T
import Niv.Logger
import System.Exit (exitFailure)
import UnliftIO
tshow :: Show a => a -> T.Text
tshow = T.pack . show
@ -13,5 +13,5 @@ tshow = T.pack . show
-- not quite the perfect place for this
abort :: MonadIO io => T.Text -> io a
abort msg = do
tsay $ T.unwords [ tbold $ tred "FATAL:", msg ]
tsay $ T.unwords [tbold $ tred "FATAL:", msg]
liftIO exitFailure

View File

@ -12,34 +12,33 @@ import Control.Applicative
import Control.Monad
import Control.Monad.Reader
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Data.Char (isSpace)
import qualified Data.HashMap.Strict as HMS
import Data.HashMap.Strict.Extended
import Data.Hashable (Hashable)
import qualified Data.Text as T
import Data.Text.Extended
import Data.Version (showVersion)
import Niv.Cmd
import Niv.Git.Cmd
import Niv.Local.Cmd
import Niv.GitHub.Cmd
import Niv.Local.Cmd
import Niv.Logger
import Niv.Sources
import Niv.Update
import qualified Options.Applicative as Opts
import qualified Options.Applicative.Help.Pretty as Opts
-- I died a little
import Paths_niv (version)
import qualified System.Directory as Dir
import System.Environment (getArgs)
import System.FilePath (takeDirectory)
import UnliftIO
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import qualified Data.HashMap.Strict as HMS
import qualified Data.Text as T
import qualified Options.Applicative as Opts
import qualified Options.Applicative.Help.Pretty as Opts
import qualified System.Directory as Dir
-- I died a little
import Paths_niv (version)
newtype NIO a = NIO { runNIO :: ReaderT FindSourcesJson IO a }
newtype NIO a = NIO {runNIO :: ReaderT FindSourcesJson IO a}
deriving (Functor, Applicative, Monad, MonadIO, MonadReader FindSourcesJson)
instance MonadUnliftIO NIO where
@ -53,42 +52,48 @@ li = liftIO
cli :: IO ()
cli = do
(fsj, nio) <- execParserPure' Opts.defaultPrefs opts <$> getArgs
(fsj, nio) <-
execParserPure' Opts.defaultPrefs opts <$> getArgs
>>= Opts.handleParseResult
runReaderT (runNIO nio) fsj
where
execParserPure' pprefs pinfo [] = Opts.Failure $
execParserPure' pprefs pinfo [] =
Opts.Failure $
Opts.parserFailure pprefs pinfo Opts.ShowHelpText mempty
execParserPure' pprefs pinfo args = Opts.execParserPure pprefs pinfo args
opts = Opts.info ((,) <$> parseFindSourcesJson <*> (parseCommand <**> Opts.helper)) $ mconcat desc
desc =
[ Opts.fullDesc
, Opts.headerDoc $ Just $
"niv - dependency manager for Nix projects" Opts.<$$>
"" Opts.<$$>
"version:" Opts.<+> Opts.text (showVersion version)
[ Opts.fullDesc,
Opts.headerDoc $ Just $
"niv - dependency manager for Nix projects"
Opts.<$$> ""
Opts.<$$> "version:" Opts.<+> Opts.text (showVersion version)
]
parseFindSourcesJson =
AtPath <$> Opts.strOption (
Opts.long "sources-file" <>
Opts.short 's' <>
Opts.metavar "FILE" <>
Opts.help "Use FILE instead of nix/sources.json"
) <|> pure Auto
AtPath
<$> Opts.strOption
( Opts.long "sources-file"
<> Opts.short 's'
<> Opts.metavar "FILE"
<> Opts.help "Use FILE instead of nix/sources.json"
)
<|> pure Auto
parseCommand :: Opts.Parser (NIO ())
parseCommand = Opts.subparser (
Opts.command "init" parseCmdInit <>
Opts.command "add" parseCmdAdd <>
Opts.command "show" parseCmdShow <>
Opts.command "update" parseCmdUpdate <>
Opts.command "modify" parseCmdModify <>
Opts.command "drop" parseCmdDrop )
parseCommand =
Opts.subparser
( Opts.command "init" parseCmdInit
<> Opts.command "add" parseCmdAdd
<> Opts.command "show" parseCmdShow
<> Opts.command "update" parseCmdUpdate
<> Opts.command "modify" parseCmdModify
<> Opts.command "drop" parseCmdDrop
)
parsePackageName :: Opts.Parser PackageName
parsePackageName = PackageName <$>
Opts.argument Opts.str (Opts.metavar "PACKAGE")
parsePackageName =
PackageName
<$> Opts.argument Opts.str (Opts.metavar "PACKAGE")
parsePackage :: Opts.Parser (PackageName, PackageSpec)
parsePackage = (,) <$> parsePackageName <*> (parsePackageSpec githubCmd)
@ -120,31 +125,32 @@ parseCmdInit = Opts.info (cmdInit <$> parseNixpkgs <**> Opts.helper) $ mconcat d
[owner, reponame] -> Just (Nixpkgs owner reponame)
_ -> Nothing
parseNixpkgs =
Opts.flag' NoNixpkgs
(
Opts.long "no-nixpkgs" <>
Opts.help "Don't add a nixpkgs entry to sources.json."
) <|>
(YesNixpkgs <$>
(Opts.strOption
(
Opts.long "nixpkgs-branch" <>
Opts.short 'b' <>
Opts.help "The nixpkgs branch to use." <>
Opts.showDefault <>
Opts.value defaultNixpkgsBranch
Opts.flag'
NoNixpkgs
( Opts.long "no-nixpkgs"
<> Opts.help "Don't add a nixpkgs entry to sources.json."
)
<|> ( YesNixpkgs
<$> ( Opts.strOption
( Opts.long "nixpkgs-branch"
<> Opts.short 'b'
<> Opts.help "The nixpkgs branch to use."
<> Opts.showDefault
<> Opts.value defaultNixpkgsBranch
)
)
<*> Opts.option
customNixpkgsReader
( Opts.long "nixpkgs"
<> Opts.showDefault
<> Opts.help "Use a custom nixpkgs repository from GitHub."
<> Opts.metavar "OWNER/REPO"
<> Opts.value (Nixpkgs defaultNixpkgsUser defaultNixpkgsRepo)
)
)
) <*> Opts.option customNixpkgsReader
(
Opts.long "nixpkgs" <>
Opts.showDefault <>
Opts.help "Use a custom nixpkgs repository from GitHub." <>
Opts.metavar "OWNER/REPO" <>
Opts.value (Nixpkgs defaultNixpkgsUser defaultNixpkgsRepo)
))
desc =
[ Opts.fullDesc
, Opts.progDesc
[ Opts.fullDesc,
Opts.progDesc
"Initialize a Nix project. Existing files won't be modified."
]
@ -152,28 +158,30 @@ cmdInit :: FetchNixpkgs -> NIO ()
cmdInit nixpkgs = do
job "Initializing" $ do
fsj <- getFindSourcesJson
-- Writes all the default files
-- a path, a "create" function and an update function for each file.
forM_
[ ( pathNixSourcesNix
, (`createFile` initNixSourcesNixContent)
, \path content -> do
[ ( pathNixSourcesNix,
(`createFile` initNixSourcesNixContent),
\path content -> do
if shouldUpdateNixSourcesNix content
then do
say "Updating sources.nix"
li $ B.writeFile path initNixSourcesNixContent
else say "Not updating sources.nix"
)
, ( pathNixSourcesJson fsj
, \path -> do
),
( pathNixSourcesJson fsj,
\path -> do
createFile path initNixSourcesJsonContent
-- Imports @niv@ and @nixpkgs@
say "Importing 'niv' ..."
cmdAdd (updateCmd githubCmd) (PackageName "niv")
(specToFreeAttrs $ PackageSpec $ HMS.fromList
[ "owner" .= ("nmattia" :: T.Text)
, "repo" .= ("niv" :: T.Text)
cmdAdd
(updateCmd githubCmd)
(PackageName "niv")
( specToFreeAttrs $ PackageSpec $
HMS.fromList
[ "owner" .= ("nmattia" :: T.Text),
"repo" .= ("niv" :: T.Text)
]
)
case nixpkgs of
@ -181,34 +189,41 @@ cmdInit nixpkgs = do
YesNixpkgs branch nixpkgs' -> do
say "Importing 'nixpkgs' ..."
let (owner, repo) = case nixpkgs' of
Nixpkgs o r -> (o,r)
cmdAdd (updateCmd githubCmd) (PackageName "nixpkgs")
(specToFreeAttrs $ PackageSpec $ HMS.fromList
[ "owner" .= owner
, "repo" .= repo
, "branch" .= branch
Nixpkgs o r -> (o, r)
cmdAdd
(updateCmd githubCmd)
(PackageName "nixpkgs")
( specToFreeAttrs $ PackageSpec $
HMS.fromList
[ "owner" .= owner,
"repo" .= repo,
"branch" .= branch
]
),
\path _content -> dontCreateFile path
)
, \path _content -> dontCreateFile path)
] $ \(path, onCreate, onUpdate) -> do
]
$ \(path, onCreate, onUpdate) -> do
exists <- li $ Dir.doesFileExist path
if exists then li (B.readFile path) >>= onUpdate path else onCreate path
case fsj of
Auto -> pure ()
AtPath fp ->
tsay $ T.unlines
tsay $
T.unlines
[ T.unwords
[ tbold $ tblue "INFO:"
, "You are using a custom path for sources.json."
[ tbold $ tblue "INFO:",
"You are using a custom path for sources.json."
],
" You need to configure the sources.nix to use " <> tbold (T.pack fp) <> ":",
tbold " import sources.nix { sourcesFile = PATH ; }; ",
T.unwords
[ " where",
tbold "PATH",
"is the relative path from sources.nix to",
tbold (T.pack fp) <> "."
]
, " You need to configure the sources.nix to use " <> tbold (T.pack fp) <> ":"
, tbold " import sources.nix { sourcesFile = PATH ; }; "
, T.unwords
[ " where", tbold "PATH", "is the relative path from sources.nix to"
, tbold (T.pack fp) <> "." ]
]
where
createFile :: FilePath -> B.ByteString -> NIO ()
createFile path content = li $ do
@ -226,8 +241,8 @@ cmdInit nixpkgs = do
parseCmdAdd :: Opts.ParserInfo (NIO ())
parseCmdAdd =
Opts.info
((parseCommands <|> parseShortcuts) <**> Opts.helper) $
(description githubCmd)
((parseCommands <|> parseShortcuts) <**> Opts.helper)
$ (description githubCmd)
where
-- XXX: this should parse many shortcuts (github, git). Right now we only
-- parse GitHub because the git interface is still experimental. note to
@ -242,12 +257,13 @@ parseCmdAdd =
Opts.info (parseCmd localCmd <**> Opts.helper) (description localCmd)
parseCmdAddGitHub =
Opts.info (parseCmd githubCmd <**> Opts.helper) (description githubCmd)
parseCommands = Opts.subparser
( Opts.hidden <>
Opts.commandGroup "Experimental commands:" <>
Opts.command "git" parseCmdAddGit <>
Opts.command "github" parseCmdAddGitHub <>
Opts.command "local" parseCmdAddLocal
parseCommands =
Opts.subparser
( Opts.hidden
<> Opts.commandGroup "Experimental commands:"
<> Opts.command "git" parseCmdAddGit
<> Opts.command "github" parseCmdAddGitHub
<> Opts.command "local" parseCmdAddLocal
)
-- | only used in shortcuts (niv add foo/bar ...) because PACKAGE is NOT
@ -261,16 +277,19 @@ parseShortcutArgs cmd = collapse <$> parseNameAndShortcut <*> parsePackageSpec c
((_, spec), Just pname') -> (pname', PackageSpec spec)
((pname', spec), Nothing) -> (pname', PackageSpec spec)
parseNameAndShortcut =
(,) <$>
Opts.argument
(,)
<$> Opts.argument
(Opts.maybeReader (parseCmdShortcut cmd . T.pack))
(Opts.metavar "PACKAGE") <*>
optName
optName = Opts.optional $ PackageName <$> Opts.strOption
( Opts.long "name" <>
Opts.short 'n' <>
Opts.metavar "NAME" <>
Opts.help "Set the package name to <NAME>"
(Opts.metavar "PACKAGE")
<*> optName
optName =
Opts.optional $
PackageName
<$> Opts.strOption
( Opts.long "name"
<> Opts.short 'n'
<> Opts.metavar "NAME"
<> Opts.help "Set the package name to <NAME>"
)
-- | only used in command (niv add <cmd> ...) because PACKAGE is optional
@ -285,16 +304,21 @@ parseCmdArgs cmd = collapse <$> parseNameAndShortcut <*> parsePackageSpec cmd
(Nothing, Just pname') -> (pname', PackageSpec HMS.empty)
(Nothing, Nothing) -> (PackageName "unnamed", PackageSpec HMS.empty)
parseNameAndShortcut =
(,) <$>
Opts.optional (Opts.argument
(,)
<$> Opts.optional
( Opts.argument
(Opts.maybeReader (parseCmdShortcut cmd . T.pack))
(Opts.metavar "PACKAGE")) <*>
optName
optName = Opts.optional $ PackageName <$> Opts.strOption
( Opts.long "name" <>
Opts.short 'n' <>
Opts.metavar "NAME" <>
Opts.help "Set the package name to <NAME>"
(Opts.metavar "PACKAGE")
)
<*> optName
optName =
Opts.optional $
PackageName
<$> Opts.strOption
( Opts.long "name"
<> Opts.short 'n'
<> Opts.metavar "NAME"
<> Opts.help "Set the package name to <NAME>"
)
cmdAdd :: Update () a -> PackageName -> Attrs -> NIO ()
@ -302,12 +326,10 @@ cmdAdd updateFunc packageName attrs = do
job ("Adding package " <> T.unpack (unPackageName packageName)) $ do
fsj <- getFindSourcesJson
sources <- unSources <$> li (getSources fsj)
when (HMS.member packageName sources) $
li $ abortCannotAddPackageExists packageName
when (HMS.member packageName sources)
$ li
$ abortCannotAddPackageExists packageName
eFinalSpec <- fmap attrsToSpec <$> li (tryEvalUpdate attrs updateFunc)
case eFinalSpec of
Left e -> li (abortUpdateFailed [(packageName, e)])
Right finalSpec -> do
@ -331,11 +353,9 @@ cmdShow = \case
Just packageName -> do
fsj <- getFindSourcesJson
sources <- unSources <$> li (getSources fsj)
case HMS.lookup packageName sources of
Just pspec -> showPackage packageName pspec
Nothing -> li $ abortCannotShowNoSuchPackage packageName
Nothing -> do
fsj <- getFindSourcesJson
sources <- unSources <$> li (getSources fsj)
@ -357,16 +377,16 @@ showPackage (PackageName pname) (PackageSpec spec) = do
parseCmdUpdate :: Opts.ParserInfo (NIO ())
parseCmdUpdate =
Opts.info
((cmdUpdate <$> Opts.optional parsePackage) <**> Opts.helper) $
mconcat desc
((cmdUpdate <$> Opts.optional parsePackage) <**> Opts.helper)
$ mconcat desc
where
desc =
[ Opts.fullDesc
, Opts.progDesc "Update dependencies"
, Opts.headerDoc $ Just $ Opts.nest 2 $
"Examples:" Opts.<$$>
"" Opts.<$$>
Opts.vcat
[ Opts.fullDesc,
Opts.progDesc "Update dependencies",
Opts.headerDoc $ Just $ Opts.nest 2 $
"Examples:"
Opts.<$$> ""
Opts.<$$> Opts.vcat
[ Opts.fill 30 "niv update" Opts.<+> "# update all packages",
Opts.fill 30 "niv update nixpkgs" Opts.<+> "# update nixpkgs",
Opts.fill 30 "niv update my-package -v beta-0.2" Opts.<+> "# update my-package to version \"beta-0.2\""
@ -385,7 +405,6 @@ cmdUpdate = \case
job ("Update " <> T.unpack (unPackageName packageName)) $ do
fsj <- getFindSourcesJson
sources <- unSources <$> li (getSources fsj)
eFinalSpec <- case HMS.lookup packageName sources of
Just defaultSpec -> do
-- lookup the "type" to find a Cmd to run, defaulting to legacy
@ -394,22 +413,21 @@ cmdUpdate = \case
Just "git" -> gitCmd
Just "local" -> localCmd
_ -> githubCmd
fmap attrsToSpec <$> li (tryEvalUpdate
fmap attrsToSpec
<$> li
( tryEvalUpdate
(specToLockedAttrs cliSpec <> specToFreeAttrs defaultSpec)
(updateCmd cmd))
(updateCmd cmd)
)
Nothing -> li $ abortCannotUpdateNoSuchPackage packageName
case eFinalSpec of
Left e -> li $ abortUpdateFailed [(packageName, e)]
Right finalSpec ->
li $ setSources fsj $ Sources $
HMS.insert packageName finalSpec sources
Nothing -> job "Updating all packages" $ do
fsj <- getFindSourcesJson
sources <- unSources <$> li (getSources fsj)
esources' <- forWithKeyM sources $
\packageName defaultSpec -> do
tsay $ "Package: " <> unPackageName packageName
@ -420,21 +438,24 @@ cmdUpdate = \case
Just "git" -> gitCmd
Just "local" -> localCmd
_ -> githubCmd
finalSpec <- fmap attrsToSpec <$> li (tryEvalUpdate
finalSpec <-
fmap attrsToSpec
<$> li
( tryEvalUpdate
initialSpec
(updateCmd cmd))
(updateCmd cmd)
)
pure finalSpec
let (failed, sources') = partitionEithersHMS esources'
unless (HMS.null failed) $
li $ abortUpdateFailed (HMS.toList failed)
unless (HMS.null failed)
$ li
$ abortUpdateFailed (HMS.toList failed)
li $ setSources fsj $ Sources sources'
partitionEithersHMS
:: (Eq k, Hashable k)
=> HMS.HashMap k (Either a b) -> (HMS.HashMap k a, HMS.HashMap k b)
partitionEithersHMS ::
(Eq k, Hashable k) =>
HMS.HashMap k (Either a b) ->
(HMS.HashMap k a, HMS.HashMap k b)
partitionEithersHMS =
flip HMS.foldlWithKey' (HMS.empty, HMS.empty) $ \(ls, rs) k -> \case
Left l -> (HMS.insert k l ls, rs)
@ -447,23 +468,26 @@ partitionEithersHMS =
parseCmdModify :: Opts.ParserInfo (NIO ())
parseCmdModify =
Opts.info
((cmdModify <$> parsePackageName <*> optName <*> parsePackageSpec githubCmd) <**> Opts.helper) $
mconcat desc
((cmdModify <$> parsePackageName <*> optName <*> parsePackageSpec githubCmd) <**> Opts.helper)
$ mconcat desc
where
desc =
[ Opts.fullDesc
, Opts.progDesc "Modify dependency attributes without performing an update"
, Opts.headerDoc $ Just $
"Examples:" Opts.<$$>
"" Opts.<$$>
" niv modify nixpkgs -v beta-0.2" Opts.<$$>
" niv modify nixpkgs -a branch=nixpkgs-unstable"
[ Opts.fullDesc,
Opts.progDesc "Modify dependency attributes without performing an update",
Opts.headerDoc $ Just $
"Examples:"
Opts.<$$> ""
Opts.<$$> " niv modify nixpkgs -v beta-0.2"
Opts.<$$> " niv modify nixpkgs -a branch=nixpkgs-unstable"
]
optName = Opts.optional $ PackageName <$> Opts.strOption
( Opts.long "name" <>
Opts.short 'n' <>
Opts.metavar "NAME" <>
Opts.help "Set the package name to <NAME>"
optName =
Opts.optional $
PackageName
<$> Opts.strOption
( Opts.long "name"
<> Opts.short 'n'
<> Opts.metavar "NAME"
<> Opts.help "Set the package name to <NAME>"
)
cmdModify :: PackageName -> Maybe PackageName -> PackageSpec -> NIO ()
@ -471,15 +495,14 @@ cmdModify packageName mNewName cliSpec = do
tsay $ "Modifying package: " <> unPackageName packageName
fsj <- getFindSourcesJson
sources <- unSources <$> li (getSources fsj)
finalSpec <- case HMS.lookup packageName sources of
Just defaultSpec -> pure $ attrsToSpec (specToLockedAttrs cliSpec <> specToFreeAttrs defaultSpec)
Nothing -> li $ abortCannotModifyNoSuchPackage packageName
case mNewName of
Just newName -> do
when (HMS.member newName sources) $
li $ abortCannotAddPackageExists newName
when (HMS.member newName sources)
$ li
$ abortCannotAddPackageExists newName
li $ setSources fsj $ Sources $ HMS.insert newName finalSpec $ HMS.delete packageName sources
Nothing ->
li $ setSources fsj $ Sources $ HMS.insert packageName finalSpec sources
@ -491,21 +514,23 @@ cmdModify packageName mNewName cliSpec = do
parseCmdDrop :: Opts.ParserInfo (NIO ())
parseCmdDrop =
Opts.info
((cmdDrop <$> parsePackageName <*> parseDropAttributes) <**>
Opts.helper) $
mconcat desc
( (cmdDrop <$> parsePackageName <*> parseDropAttributes)
<**> Opts.helper
)
$ mconcat desc
where
desc =
[ Opts.fullDesc
, Opts.progDesc "Drop dependency"
, Opts.headerDoc $ Just $
"Examples:" Opts.<$$>
"" Opts.<$$>
" niv drop jq" Opts.<$$>
" niv drop my-package version"
[ Opts.fullDesc,
Opts.progDesc "Drop dependency",
Opts.headerDoc $ Just $
"Examples:"
Opts.<$$> ""
Opts.<$$> " niv drop jq"
Opts.<$$> " niv drop my-package version"
]
parseDropAttributes :: Opts.Parser [T.Text]
parseDropAttributes = many $
parseDropAttributes =
many $
Opts.argument Opts.str (Opts.metavar "ATTRIBUTE")
cmdDrop :: PackageName -> [T.Text] -> NIO ()
@ -514,10 +539,9 @@ cmdDrop packageName = \case
tsay $ "Dropping package: " <> unPackageName packageName
fsj <- getFindSourcesJson
sources <- unSources <$> li (getSources fsj)
when (not $ HMS.member packageName sources) $
li $ abortCannotDropNoSuchPackage packageName
when (not $ HMS.member packageName sources)
$ li
$ abortCannotDropNoSuchPackage packageName
li $ setSources fsj $ Sources $
HMS.delete packageName sources
attrs -> do
@ -525,14 +549,14 @@ cmdDrop packageName = \case
tsay $ "In package: " <> unPackageName packageName
fsj <- getFindSourcesJson
sources <- unSources <$> li (getSources fsj)
packageSpec <- case HMS.lookup packageName sources of
Nothing ->
li $ abortCannotAttributesDropNoSuchPackage packageName
Just (PackageSpec packageSpec) -> pure $ PackageSpec $
Just (PackageSpec packageSpec) ->
pure $ PackageSpec $
HMS.mapMaybeWithKey
(\k v -> if k `elem` attrs then Nothing else Just v) packageSpec
(\k v -> if k `elem` attrs then Nothing else Just v)
packageSpec
li $ setSources fsj $ Sources $
HMS.insert packageName packageSpec sources
@ -544,15 +568,15 @@ cmdDrop packageName = \case
-- a comment line with @niv: no_update@
shouldUpdateNixSourcesNix :: B.ByteString -> Bool
shouldUpdateNixSourcesNix content =
content /= initNixSourcesNixContent &&
not (any lineForbids (B8.lines content))
content /= initNixSourcesNixContent
&& not (any lineForbids (B8.lines content))
where
lineForbids :: B8.ByteString -> Bool
lineForbids str =
case B8.uncons (B8.dropWhile isSpace str) of
Just ('#',rest) -> case B8.stripPrefix "niv:" (B8.dropWhile isSpace rest) of
Just ('#', rest) -> case B8.stripPrefix "niv:" (B8.dropWhile isSpace rest) of
Just rest' -> case B8.stripPrefix "no_update" (B8.dropWhile isSpace rest') of
Just{} -> True
Just {} -> True
_ -> False
_ -> False
_ -> False
@ -562,53 +586,67 @@ shouldUpdateNixSourcesNix content =
-------------------------------------------------------------------------------
abortCannotAddPackageExists :: PackageName -> IO a
abortCannotAddPackageExists (PackageName n) = abort $ T.unlines
[ "Cannot add package " <> n <> "."
, "The package already exists. Use"
, " niv drop " <> n
, "and then re-add the package. Alternatively use"
, " niv update " <> n <> " --attribute foo=bar"
, "to update the package's attributes."
abortCannotAddPackageExists (PackageName n) =
abort $
T.unlines
[ "Cannot add package " <> n <> ".",
"The package already exists. Use",
" niv drop " <> n,
"and then re-add the package. Alternatively use",
" niv update " <> n <> " --attribute foo=bar",
"to update the package's attributes."
]
abortCannotUpdateNoSuchPackage :: PackageName -> IO a
abortCannotUpdateNoSuchPackage (PackageName n) = abort $ T.unlines
[ "Cannot update package " <> n <> "."
, "The package doesn't exist. Use"
, " niv add " <> n
, "to add the package."
abortCannotUpdateNoSuchPackage (PackageName n) =
abort $
T.unlines
[ "Cannot update package " <> n <> ".",
"The package doesn't exist. Use",
" niv add " <> n,
"to add the package."
]
abortCannotModifyNoSuchPackage :: PackageName -> IO a
abortCannotModifyNoSuchPackage (PackageName n) = abort $ T.unlines
[ "Cannot modify package " <> n <> "."
, "The package doesn't exist. Use"
, " niv add " <> n
, "to add the package."
abortCannotModifyNoSuchPackage (PackageName n) =
abort $
T.unlines
[ "Cannot modify package " <> n <> ".",
"The package doesn't exist. Use",
" niv add " <> n,
"to add the package."
]
abortCannotDropNoSuchPackage :: PackageName -> IO a
abortCannotDropNoSuchPackage (PackageName n) = abort $ T.unlines
[ "Cannot drop package " <> n <> "."
, "The package doesn't exist."
abortCannotDropNoSuchPackage (PackageName n) =
abort $
T.unlines
[ "Cannot drop package " <> n <> ".",
"The package doesn't exist."
]
abortCannotShowNoSuchPackage :: PackageName -> IO a
abortCannotShowNoSuchPackage (PackageName n) = abort $ T.unlines
[ "Cannot show package " <> n <> "."
, "The package doesn't exist."
abortCannotShowNoSuchPackage (PackageName n) =
abort $
T.unlines
[ "Cannot show package " <> n <> ".",
"The package doesn't exist."
]
abortCannotAttributesDropNoSuchPackage :: PackageName -> IO a
abortCannotAttributesDropNoSuchPackage (PackageName n) = abort $ T.unlines
[ "Cannot drop attributes of package " <> n <> "."
, "The package doesn't exist."
abortCannotAttributesDropNoSuchPackage (PackageName n) =
abort $
T.unlines
[ "Cannot drop attributes of package " <> n <> ".",
"The package doesn't exist."
]
abortUpdateFailed :: [ (PackageName, SomeException) ] -> IO a
abortUpdateFailed errs = abort $ T.unlines $
[ "One or more packages failed to update:" ] <>
map (\(PackageName pname, e) ->
abortUpdateFailed :: [(PackageName, SomeException)] -> IO a
abortUpdateFailed errs =
abort $ T.unlines $
["One or more packages failed to update:"]
<> map
( \(PackageName pname, e) ->
pname <> ": " <> tshow e
) errs
)
errs

View File

@ -2,17 +2,18 @@
module Niv.Cmd where
import Niv.Sources
import Niv.Update
import qualified Data.Aeson as Aeson
import qualified Data.Text as T
import Niv.Sources
import Niv.Update
import qualified Options.Applicative as Opts
-- TODO: add filter
data Cmd = Cmd
{ description :: forall a. Opts.InfoMod a
, parseCmdShortcut :: T.Text -> Maybe (PackageName, Aeson.Object)
, parsePackageSpec :: Opts.Parser PackageSpec
, updateCmd :: Update () ()
, name :: T.Text
data Cmd
= Cmd
{ description :: forall a. Opts.InfoMod a,
parseCmdShortcut :: T.Text -> Maybe (PackageName, Aeson.Object),
parsePackageSpec :: Opts.Parser PackageSpec,
updateCmd :: Update () (),
name :: T.Text
}

View File

@ -1,36 +1,37 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Arrows #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
module Niv.Git.Cmd where
import Control.Applicative
import Control.Arrow
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as B8
import qualified Data.HashMap.Strict as HMS
import Data.Maybe
import qualified Data.Text as T
import Data.Text.Extended as T
import Niv.Cmd
import Niv.Logger
import Niv.Sources
import Niv.Update
import System.Exit (ExitCode(ExitSuccess))
import System.Process (readProcessWithExitCode)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as B8
import qualified Data.HashMap.Strict as HMS
import qualified Data.Text as T
import qualified Options.Applicative as Opts
import qualified Options.Applicative.Help.Pretty as Opts
import System.Exit (ExitCode (ExitSuccess))
import System.Process (readProcessWithExitCode)
gitCmd :: Cmd
gitCmd = Cmd
{ description = describeGit
, parseCmdShortcut = parseGitShortcut
, parsePackageSpec = parseGitPackageSpec
, updateCmd = gitUpdate'
, name = "git"
gitCmd =
Cmd
{ description = describeGit,
parseCmdShortcut = parseGitShortcut,
parsePackageSpec = parseGitPackageSpec,
updateCmd = gitUpdate',
name = "git"
}
parseGitShortcut :: T.Text -> Maybe (PackageName, Aeson.Object)
@ -45,73 +46,81 @@ parseGitShortcut txt'@(T.dropWhileEnd (== '/') -> txt) =
else Nothing
where
isGitURL =
".git" `T.isSuffixOf` txt ||
"git@" `T.isPrefixOf` txt ||
"ssh://" `T.isPrefixOf` txt
".git" `T.isSuffixOf` txt
|| "git@" `T.isPrefixOf` txt
|| "ssh://" `T.isPrefixOf` txt
parseGitPackageSpec :: Opts.Parser PackageSpec
parseGitPackageSpec =
(PackageSpec . HMS.fromList) <$>
many (parseRepo <|> parseRef <|> parseRev <|> parseAttr <|> parseSAttr)
(PackageSpec . HMS.fromList)
<$> many (parseRepo <|> parseRef <|> parseRev <|> parseAttr <|> parseSAttr)
where
parseRepo =
("repo", ) . Aeson.String <$> Opts.strOption
( Opts.long "repo" <>
Opts.metavar "URL"
("repo",) . Aeson.String
<$> Opts.strOption
( Opts.long "repo"
<> Opts.metavar "URL"
)
parseRev =
("rev", ) . Aeson.String <$> Opts.strOption
( Opts.long "rev" <>
Opts.metavar "SHA"
("rev",) . Aeson.String
<$> Opts.strOption
( Opts.long "rev"
<> Opts.metavar "SHA"
)
parseRef =
("ref", ) . Aeson.String <$> Opts.strOption
( Opts.long "ref" <>
Opts.metavar "REF"
("ref",) . Aeson.String
<$> Opts.strOption
( Opts.long "ref"
<> Opts.metavar "REF"
)
parseAttr =
Opts.option (Opts.maybeReader parseKeyValJSON)
( Opts.long "attribute" <>
Opts.short 'a' <>
Opts.metavar "KEY=VAL" <>
Opts.help "Set the package spec attribute <KEY> to <VAL>, where <VAL> may be JSON."
Opts.option
(Opts.maybeReader parseKeyValJSON)
( Opts.long "attribute"
<> Opts.short 'a'
<> Opts.metavar "KEY=VAL"
<> Opts.help "Set the package spec attribute <KEY> to <VAL>, where <VAL> may be JSON."
)
parseSAttr =
Opts.option (Opts.maybeReader (parseKeyVal Aeson.toJSON))
( Opts.long "string-attribute" <>
Opts.short 's' <>
Opts.metavar "KEY=VAL" <>
Opts.help "Set the package spec attribute <KEY> to <VAL>."
Opts.option
(Opts.maybeReader (parseKeyVal Aeson.toJSON))
( Opts.long "string-attribute"
<> Opts.short 's'
<> Opts.metavar "KEY=VAL"
<> Opts.help "Set the package spec attribute <KEY> to <VAL>."
)
parseKeyValJSON = parseKeyVal $ \x ->
fromMaybe (Aeson.toJSON x) (Aeson.decodeStrict (B8.pack x))
-- Parse "key=val" into ("key", val)
parseKeyVal
:: (String -> Aeson.Value) -- ^ how to convert to JSON
-> String -> Maybe (T.Text, Aeson.Value)
parseKeyVal ::
-- | how to convert to JSON
(String -> Aeson.Value) ->
String ->
Maybe (T.Text, Aeson.Value)
parseKeyVal toJSON str = case span (/= '=') str of
(key, '=':val) -> Just (T.pack key, toJSON val)
(key, '=' : val) -> Just (T.pack key, toJSON val)
_ -> Nothing
describeGit :: Opts.InfoMod a
describeGit = mconcat
[ Opts.fullDesc
, Opts.progDesc "Add a git dependency. Experimental."
, Opts.headerDoc $ Just $
"Examples:" Opts.<$$>
"" Opts.<$$>
" niv add git git@github.com:stedolan/jq" Opts.<$$>
" niv add git ssh://git@github.com/stedolan/jq --rev deadb33f" Opts.<$$>
" niv add git https://github.com/stedolan/jq.git" Opts.<$$>
" niv add git --repo /my/custom/repo --name custom --ref foobar"
describeGit =
mconcat
[ Opts.fullDesc,
Opts.progDesc "Add a git dependency. Experimental.",
Opts.headerDoc $ Just $
"Examples:"
Opts.<$$> ""
Opts.<$$> " niv add git git@github.com:stedolan/jq"
Opts.<$$> " niv add git ssh://git@github.com/stedolan/jq --rev deadb33f"
Opts.<$$> " niv add git https://github.com/stedolan/jq.git"
Opts.<$$> " niv add git --repo /my/custom/repo --name custom --ref foobar"
]
gitUpdate
:: (T.Text -> T.Text -> IO T.Text) -- ^ latest rev
-> (T.Text -> IO (T.Text, T.Text)) -- ^ latest rev and default ref
-> Update () ()
gitUpdate ::
-- | latest rev
(T.Text -> T.Text -> IO T.Text) ->
-- | latest rev and default ref
(T.Text -> IO (T.Text, T.Text)) ->
Update () ()
gitUpdate latestRev' defaultRefAndHEAD' = proc () -> do
useOrSet "type" -< ("git" :: Box T.Text)
repository <- load "repo" -< ()
@ -132,38 +141,46 @@ gitUpdate latestRev' defaultRefAndHEAD' = proc () -> do
gitUpdate' :: Update () ()
gitUpdate' = gitUpdate latestRev defaultRefAndHEAD
latestRev
:: T.Text -- ^ the repository
-> T.Text -- ^ the ref/branch
-> IO T.Text
latestRev ::
-- | the repository
T.Text ->
-- | the ref/branch
T.Text ->
IO T.Text
latestRev repo ref = do
let gitArgs = [ "ls-remote", repo, "refs/heads/" <> ref ]
let gitArgs = ["ls-remote", repo, "refs/heads/" <> ref]
sout <- runGit gitArgs
case sout of
ls@(_:_:_) -> abortTooMuchOutput gitArgs ls
(l1:[]) -> parseRev gitArgs l1
ls@(_ : _ : _) -> abortTooMuchOutput gitArgs ls
(l1 : []) -> parseRev gitArgs l1
[] -> abortNoOutput gitArgs
where
parseRev args l = maybe (abortNoRev args l) pure $ do
checkRev $ T.takeWhile (/= '\t') l
checkRev t = if isRev t then Just t else Nothing
abortNoOutput args = abortGitFailure args
abortNoOutput args =
abortGitFailure
args
"Git didn't produce any output."
abortTooMuchOutput args ls = abortGitFailure args $ T.unlines $
[ "Git produced too much output:" ] <> map (" " <>) ls
abortTooMuchOutput args ls =
abortGitFailure args $ T.unlines $
["Git produced too much output:"] <> map (" " <>) ls
defaultRefAndHEAD
:: T.Text -- ^ the repository
-> IO (T.Text, T.Text)
defaultRefAndHEAD ::
-- | the repository
T.Text ->
IO (T.Text, T.Text)
defaultRefAndHEAD repo = do
sout <- runGit args
case sout of
(l1:l2:_) -> (,) <$> parseRef l1 <*> parseRev l2
_ -> abortGitFailure args $ T.unlines $
(l1 : l2 : _) -> (,) <$> parseRef l1 <*> parseRev l2
_ ->
abortGitFailure args $ T.unlines $
[ "Could not read reference and revision from stdout:"
] <> sout
]
<> sout
where
args = [ "ls-remote", "--symref", repo, "HEAD" ]
args = ["ls-remote", "--symref", repo, "HEAD"]
parseRef l = maybe (abortNoRef args l) pure $ do
-- ref: refs/head/master\tHEAD -> master\tHEAD
refAndSym <- T.stripPrefix "ref: refs/heads/" l
@ -175,6 +192,7 @@ defaultRefAndHEAD repo = do
abortNoRev :: [T.Text] -> T.Text -> IO a
abortNoRev args l = abortGitFailure args $ "Could not read revision from: " <> l
abortNoRef :: [T.Text] -> T.Text -> IO a
abortNoRef args l = abortGitFailure args $ "Could not read reference from: " <> l
@ -184,20 +202,26 @@ runGit args = do
(exitCode, sout, serr) <- readProcessWithExitCode "git" (T.unpack <$> args) ""
case (exitCode, lines sout) of
(ExitSuccess, ls) -> pure $ T.pack <$> ls
_ -> abortGitFailure args $ T.unlines
[ T.unwords [ "stdout:" , T.pack sout ]
, T.unwords [ "stderr:" , T.pack serr ]
_ ->
abortGitFailure args $
T.unlines
[ T.unwords ["stdout:", T.pack sout],
T.unwords ["stderr:", T.pack serr]
]
isRev :: T.Text -> Bool
isRev t =
-- commit hashes are comprised of abcdef0123456789
T.all (\c -> (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9')) t &&
T.all (\c -> (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9')) t
&&
-- commit _should_ be 40 chars long, but to be sure we pick 7
T.length t >= 7
abortGitFailure :: [T.Text] -> T.Text -> IO a
abortGitFailure args msg = abort $ bug $ T.unlines
[ "Could not read the output of 'git'."
, T.unwords ("command:":"git":args)
, msg ]
abortGitFailure args msg =
abort $ bug $
T.unlines
[ "Could not read the output of 'git'.",
T.unwords ("command:" : "git" : args),
msg
]

View File

@ -1,43 +1,54 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Arrows #-}
{-# LANGUAGE OverloadedStrings #-}
module Niv.Git.Test (tests) where
module Niv.Git.Test
( tests,
)
where
import Control.Monad
import Data.Bifunctor
import qualified Data.HashMap.Strict as HMS
import Niv.Git.Cmd
import Niv.Sources
import Niv.Update
import Test.Tasty.HUnit ((@=?))
import qualified Data.HashMap.Strict as HMS
import qualified Test.Tasty as Tasty
import Test.Tasty.HUnit ((@=?))
import qualified Test.Tasty.HUnit as Tasty
tests :: [Tasty.TestTree]
tests = [ test_repositoryParse , test_gitUpdates ]
tests = [test_repositoryParse, test_gitUpdates]
test_repositoryParse :: Tasty.TestTree
test_repositoryParse = Tasty.testGroup "repository parse"
test_repositoryParse =
Tasty.testGroup
"repository parse"
[ Tasty.testCase "goo" $
parseGitShortcut "goo" @=? Nothing
, Tasty.testCase "git@github.com:nmattia/niv" $
parseGitShortcut "git@github.com:nmattia/niv" @=? Just
(PackageName "niv", HMS.singleton "repo" "git@github.com:nmattia/niv")
, Tasty.testCase "ssh://git@github.com/stedolan/jq" $
parseGitShortcut "ssh://git@github.com/stedolan/jq" @=? Just
(PackageName "jq", HMS.singleton "repo" "ssh://git@github.com/stedolan/jq")
, Tasty.testCase "https://github.com/stedolan/jq.git" $
parseGitShortcut "https://github.com/stedolan/jq.git" @=? Just
(PackageName "jq", HMS.singleton "repo" "https://github.com/stedolan/jq.git")
, Tasty.testCase "https://github.com/stedolan/jq" $
parseGitShortcut "https://github.com/stedolan/jq" @=? Nothing
, Tasty.testCase "~/path/to/repo.git" $
parseGitShortcut "~/path/to/repo.git" @=? Just
parseGitShortcut "goo" @=? Nothing,
Tasty.testCase "git@github.com:nmattia/niv" $
parseGitShortcut "git@github.com:nmattia/niv"
@=? Just
(PackageName "niv", HMS.singleton "repo" "git@github.com:nmattia/niv"),
Tasty.testCase "ssh://git@github.com/stedolan/jq" $
parseGitShortcut "ssh://git@github.com/stedolan/jq"
@=? Just
(PackageName "jq", HMS.singleton "repo" "ssh://git@github.com/stedolan/jq"),
Tasty.testCase "https://github.com/stedolan/jq.git" $
parseGitShortcut "https://github.com/stedolan/jq.git"
@=? Just
(PackageName "jq", HMS.singleton "repo" "https://github.com/stedolan/jq.git"),
Tasty.testCase "https://github.com/stedolan/jq" $
parseGitShortcut "https://github.com/stedolan/jq" @=? Nothing,
Tasty.testCase "~/path/to/repo.git" $
parseGitShortcut "~/path/to/repo.git"
@=? Just
(PackageName "repo", HMS.singleton "repo" "~/path/to/repo.git")
]
test_gitUpdates :: Tasty.TestTree
test_gitUpdates = Tasty.testGroup "updates"
test_gitUpdates =
Tasty.testGroup
"updates"
[ Tasty.testCase "rev is updated" test_gitUpdateRev
]
@ -48,16 +59,19 @@ test_gitUpdateRev = do
let interState' = HMS.map (first (\_ -> Free)) interState
actualState <- evalUpdate interState' $ proc () ->
gitUpdate latestRev' (error "should update") -< ()
unless ((snd <$> actualState) == expectedState) $
error $ "State mismatch: " <> show actualState
unless ((snd <$> actualState) == expectedState)
$ error
$ "State mismatch: " <> show actualState
where
latestRev' _ _ = pure "some-other-rev"
defaultRefAndHEAD' _ = pure ("some-ref", "some-rev")
initialState = HMS.fromList
[ ("repo", (Free, "git@github.com:nmattia/niv")) ]
expectedState = HMS.fromList
[ ("repo", "git@github.com:nmattia/niv")
, ("ref", "some-ref")
, ("rev", "some-other-rev")
, ("type", "git")
initialState =
HMS.fromList
[("repo", (Free, "git@github.com:nmattia/niv"))]
expectedState =
HMS.fromList
[ ("repo", "git@github.com:nmattia/niv"),
("ref", "some-ref"),
("rev", "some-other-rev"),
("type", "git")
]

View File

@ -1,18 +1,18 @@
{-# LANGUAGE Arrows #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ViewPatterns #-}
module Niv.GitHub where
import Control.Arrow
import Data.Bool
import Data.Maybe
import qualified Data.Text as T
import Niv.GitHub.API
import Niv.Update
import qualified Data.Text as T
-- | The GitHub update function
-- TODO: fetchers for:
@ -20,17 +20,18 @@ import qualified Data.Text as T
-- * hackage
-- * docker
-- * ... ?
githubUpdate
:: (Bool -> T.Text -> IO T.Text)
-- ^ prefetch
-> (T.Text -> T.Text -> T.Text -> IO T.Text)
-- ^ latest revision
-> (T.Text -> T.Text -> IO GithubRepo)
-- ^ get repo
-> Update () ()
githubUpdate ::
-- | prefetch
(Bool -> T.Text -> IO T.Text) ->
-- | latest revision
(T.Text -> T.Text -> T.Text -> IO T.Text) ->
-- | get repo
(T.Text -> T.Text -> IO GithubRepo) ->
Update () ()
githubUpdate prefetch latestRev ghRepo = proc () -> do
urlTemplate <- template <<<
(useOrSet "url_template" <<< completeSpec) <+> (load "url_template") -<
urlTemplate <-
template
<<< (useOrSet "url_template" <<< completeSpec) <+> (load "url_template") -<
()
url <- update "url" -< urlTemplate
let isTar = (\u -> "tar.gz" `T.isSuffixOf` u || ".tgz" `T.isSuffixOf` u) <$> url
@ -44,15 +45,19 @@ githubUpdate prefetch latestRev ghRepo = proc () -> do
owner <- load "owner" -< ()
repo <- load "repo" -< ()
repoInfo <- run (\(a, b) -> ghRepo a b) -< (,) <$> owner <*> repo
branch <- useOrSet "branch" <<< arr (fmap $ fromMaybe "master") -<
branch <-
useOrSet "branch" <<< arr (fmap $ fromMaybe "master") -<
repoDefaultBranch <$> repoInfo
_description <- useOrSet "description" -< repoDescription <$> repoInfo
_homepage <- useOrSet "homepage" -< repoHomepage <$> repoInfo
_ <- update "rev" <<< run' (\(a,b,c) -> latestRev a b c) -<
_ <-
update "rev" <<< run' (\(a, b, c) -> latestRev a b c) -<
(,,) <$> owner <*> repo <*> branch
returnA -< pure githubURLTemplate
githubURLTemplate :: T.Text
githubURLTemplate =
(if githubSecure then "https://" else "http://") <>
githubHost <> githubPath <> "<owner>/<repo>/archive/<rev>.tar.gz"
(if githubSecure then "https://" else "http://")
<> githubHost
<> githubPath
<> "<owner>/<repo>/archive/<rev>.tar.gz"

View File

@ -1,31 +1,32 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ViewPatterns #-}
module Niv.GitHub.API where
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as BS8
import Data.Functor
import qualified Data.HashMap.Strict as HMS
import Data.Maybe
import Data.String.QQ (s)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Text.Extended
import qualified Network.HTTP.Simple as HTTP
import System.Environment (lookupEnv)
import System.Exit (exitFailure)
import System.IO.Unsafe (unsafePerformIO)
import Text.Read (readMaybe)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as BS8
import qualified Data.HashMap.Strict as HMS
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Network.HTTP.Simple as HTTP
-- Bunch of GitHub helpers
data GithubRepo = GithubRepo
{ repoDescription :: Maybe T.Text
, repoHomepage :: Maybe T.Text
, repoDefaultBranch :: Maybe T.Text
data GithubRepo
= GithubRepo
{ repoDescription :: Maybe T.Text,
repoHomepage :: Maybe T.Text,
repoDefaultBranch :: Maybe T.Text
}
githubRepo :: T.Text -> T.Text -> IO GithubRepo
@ -40,25 +41,27 @@ githubRepo owner repo = do
let lookupText k = case HMS.lookup k m of
Just (Aeson.String t) -> Just t
_ -> Nothing
pure GithubRepo
{ repoDescription = lookupText "description"
, repoHomepage = lookupText "homepage"
, repoDefaultBranch = lookupText "default_branch"
pure
GithubRepo
{ repoDescription = lookupText "description",
repoHomepage = lookupText "homepage",
repoDefaultBranch = lookupText "default_branch"
}
(200, Right v) -> do
error $ "expected object, got " <> show v
(200, Left e) -> do
error $ "github didn't return JSON: " <> show e
_ -> abortCouldNotFetchGitHubRepo (tshow (request,resp0)) (owner, repo)
_ -> abortCouldNotFetchGitHubRepo (tshow (request, resp0)) (owner, repo)
-- | TODO: Error instead of T.Text?
abortCouldNotFetchGitHubRepo :: T.Text -> (T.Text, T.Text) -> IO a
abortCouldNotFetchGitHubRepo e (T.unpack -> owner, T.unpack -> repo) = do
putStrLn $ unlines [ line1, line2, T.unpack line3 ]
putStrLn $ unlines [line1, line2, T.unpack line3]
exitFailure
where
line1 = "WARNING: Could not read from GitHub repo: " <> owner <> "/" <> repo
line2 = [s|
line2 =
[s|
I assumed that your package was a GitHub repository. An error occurred while
gathering information from the repository. Check whether your package was added
correctly:
@ -72,52 +75,55 @@ If not, try re-adding it:
Make sure the repository exists.
|]
line3 = T.unwords [ "(Error was:", e, ")" ]
line3 = T.unwords ["(Error was:", e, ")"]
defaultRequest :: [T.Text] -> IO HTTP.Request
defaultRequest (map T.encodeUtf8 -> parts) = do
let path = T.encodeUtf8 githubPath <> BS8.intercalate "/" (parts)
mtoken <- lookupEnv "GITHUB_TOKEN"
pure $
(flip (maybe id) mtoken $ \token ->
pure
$ ( flip (maybe id) mtoken $ \token ->
HTTP.addRequestHeader "authorization" ("token " <> BS8.pack token)
) $
HTTP.setRequestPath path $
HTTP.addRequestHeader "user-agent" "niv" $
HTTP.addRequestHeader "accept" "application/vnd.github.v3+json" $
HTTP.setRequestSecure githubSecure $
HTTP.setRequestHost (T.encodeUtf8 githubApiHost) $
HTTP.setRequestPort githubApiPort $
HTTP.defaultRequest
)
$ HTTP.setRequestPath path
$ HTTP.addRequestHeader "user-agent" "niv"
$ HTTP.addRequestHeader "accept" "application/vnd.github.v3+json"
$ HTTP.setRequestSecure githubSecure
$ HTTP.setRequestHost (T.encodeUtf8 githubApiHost)
$ HTTP.setRequestPort githubApiPort
$ HTTP.defaultRequest
-- | Get the latest revision for owner, repo and branch.
-- TODO: explain no error handling
githubLatestRev
:: T.Text
-- ^ owner
-> T.Text
-- ^ repo
-> T.Text
-- ^ branch
-> IO T.Text
githubLatestRev ::
-- | owner
T.Text ->
-- | repo
T.Text ->
-- | branch
T.Text ->
IO T.Text
githubLatestRev owner repo branch = do
request <- defaultRequest [ "repos", owner, repo, "commits", branch ] <&>
HTTP.addRequestHeader "accept" "application/vnd.github.v3.sha"
request <-
defaultRequest ["repos", owner, repo, "commits", branch]
<&> HTTP.addRequestHeader "accept" "application/vnd.github.v3.sha"
resp <- HTTP.httpBS request
case HTTP.getResponseStatusCode resp of
200 -> pure $ T.decodeUtf8 $ HTTP.getResponseBody resp
_ -> abortCouldNotGetRev owner repo branch resp
abortCouldNotGetRev :: T.Text -> T.Text -> T.Text -> HTTP.Response BS8.ByteString -> IO a
abortCouldNotGetRev owner repo branch resp = abort $ T.unlines [ line1, line2, line3 ]
abortCouldNotGetRev owner repo branch resp = abort $ T.unlines [line1, line2, line3]
where
line1 = T.unwords
[ "Cannot get latest revision for branch"
, "'" <> branch <> "'"
, "(" <> owner <> "/" <> repo <> ")"
line1 =
T.unwords
[ "Cannot get latest revision for branch",
"'" <> branch <> "'",
"(" <> owner <> "/" <> repo <> ")"
]
line2 = "The request failed: " <> tshow resp
line3 = [s|
line3 =
[s|
NOTE: You may want to retry with an authentication token:
GITHUB_TOKEN=... niv <cmd>

View File

@ -1,117 +1,134 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
module Niv.GitHub.Cmd (githubCmd) where
module Niv.GitHub.Cmd
( githubCmd,
)
where
import Control.Applicative
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import Data.Bifunctor
import qualified Data.ByteString.Char8 as B8
import Data.Char (isAlphaNum)
import qualified Data.HashMap.Strict as HMS
import Data.Maybe
import Data.String.QQ (s)
import qualified Data.Text as T
import Data.Text.Extended
import Niv.Cmd
import Niv.GitHub
import Niv.GitHub.API
import Niv.Sources
import Niv.Update
import System.Exit (ExitCode(ExitSuccess))
import System.Process (readProcessWithExitCode)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as B8
import qualified Data.HashMap.Strict as HMS
import qualified Data.Text as T
import qualified Options.Applicative as Opts
import qualified Options.Applicative.Help.Pretty as Opts
import System.Exit (ExitCode (ExitSuccess))
import System.Process (readProcessWithExitCode)
githubCmd :: Cmd
githubCmd = Cmd
{ description = describeGitHub
, parseCmdShortcut = parseAddShortcutGitHub
, parsePackageSpec = parseGitHubPackageSpec
, updateCmd = githubUpdate'
, name = "github"
githubCmd =
Cmd
{ description = describeGitHub,
parseCmdShortcut = parseAddShortcutGitHub,
parsePackageSpec = parseGitHubPackageSpec,
updateCmd = githubUpdate',
name = "github"
-- TODO: here filter by type == tarball or file or builtin-
}
parseGitHubPackageSpec :: Opts.Parser PackageSpec
parseGitHubPackageSpec =
(PackageSpec . HMS.fromList) <$>
many parseAttribute
(PackageSpec . HMS.fromList)
<$> many parseAttribute
where
parseAttribute :: Opts.Parser (T.Text, Aeson.Value)
parseAttribute =
Opts.option (Opts.maybeReader parseKeyValJSON)
( Opts.long "attribute" <>
Opts.short 'a' <>
Opts.metavar "KEY=VAL" <>
Opts.help "Set the package spec attribute <KEY> to <VAL>, where <VAL> may be JSON."
) <|>
Opts.option (Opts.maybeReader (parseKeyVal Aeson.toJSON))
( Opts.long "string-attribute" <>
Opts.short 's' <>
Opts.metavar "KEY=VAL" <>
Opts.help "Set the package spec attribute <KEY> to <VAL>."
) <|>
shortcutAttributes <|>
((("url_template",) . Aeson.String) <$> Opts.strOption
( Opts.long "template" <>
Opts.short 't' <>
Opts.metavar "URL" <>
Opts.help "Used during 'update' when building URL. Occurrences of <foo> are replaced with attribute 'foo'."
)) <|>
((("type",) . Aeson.String) <$> Opts.strOption
( Opts.long "type" <>
Opts.short 'T' <>
Opts.metavar "TYPE" <>
Opts.help "The type of the URL target. The value can be either 'file' or 'tarball'. If not set, the value is inferred from the suffix of the URL."
))
Opts.option
(Opts.maybeReader parseKeyValJSON)
( Opts.long "attribute"
<> Opts.short 'a'
<> Opts.metavar "KEY=VAL"
<> Opts.help "Set the package spec attribute <KEY> to <VAL>, where <VAL> may be JSON."
)
<|> Opts.option
(Opts.maybeReader (parseKeyVal Aeson.toJSON))
( Opts.long "string-attribute"
<> Opts.short 's'
<> Opts.metavar "KEY=VAL"
<> Opts.help "Set the package spec attribute <KEY> to <VAL>."
)
<|> shortcutAttributes
<|> ( (("url_template",) . Aeson.String)
<$> Opts.strOption
( Opts.long "template"
<> Opts.short 't'
<> Opts.metavar "URL"
<> Opts.help "Used during 'update' when building URL. Occurrences of <foo> are replaced with attribute 'foo'."
)
)
<|> ( (("type",) . Aeson.String)
<$> Opts.strOption
( Opts.long "type"
<> Opts.short 'T'
<> Opts.metavar "TYPE"
<> Opts.help "The type of the URL target. The value can be either 'file' or 'tarball'. If not set, the value is inferred from the suffix of the URL."
)
)
parseKeyValJSON = parseKeyVal $ \x ->
fromMaybe (Aeson.toJSON x) (Aeson.decodeStrict (B8.pack x))
-- Parse "key=val" into ("key", val)
parseKeyVal
:: (String -> Aeson.Value) -- ^ how to convert to JSON
-> String -> Maybe (T.Text, Aeson.Value)
parseKeyVal ::
-- | how to convert to JSON
(String -> Aeson.Value) ->
String ->
Maybe (T.Text, Aeson.Value)
parseKeyVal toJSON str = case span (/= '=') str of
(key, '=':val) -> Just (T.pack key, toJSON val)
(key, '=' : val) -> Just (T.pack key, toJSON val)
_ -> Nothing
-- Shortcuts for common attributes
shortcutAttributes :: Opts.Parser (T.Text, Aeson.Value)
shortcutAttributes = foldr (<|>) empty $ mkShortcutAttribute <$>
[ "branch", "owner", "repo", "version" ]
shortcutAttributes =
foldr (<|>) empty $
mkShortcutAttribute
<$> ["branch", "owner", "repo", "version"]
-- TODO: infer those shortcuts from 'Update' keys
mkShortcutAttribute :: T.Text -> Opts.Parser (T.Text, Aeson.Value)
mkShortcutAttribute = \case
attr@(T.uncons -> Just (c,_)) -> fmap (second Aeson.String) $ (attr,) <$> Opts.strOption
( Opts.long (T.unpack attr) <>
Opts.short c <>
Opts.metavar (T.unpack $ T.toUpper attr) <>
Opts.help
attr@(T.uncons -> Just (c, _)) ->
fmap (second Aeson.String) $
(attr,)
<$> Opts.strOption
( Opts.long (T.unpack attr)
<> Opts.short c
<> Opts.metavar (T.unpack $ T.toUpper attr)
<> Opts.help
( T.unpack $
"Equivalent to --attribute " <>
attr <> "=<" <> (T.toUpper attr) <> ">"
"Equivalent to --attribute "
<> attr
<> "=<"
<> (T.toUpper attr)
<> ">"
)
)
_ -> empty
describeGitHub :: Opts.InfoMod a
describeGitHub = mconcat
[ Opts.fullDesc
, Opts.progDesc "Add a GitHub dependency"
, Opts.headerDoc $ Just $
"Examples:" Opts.<$$>
"" Opts.<$$>
" niv add stedolan/jq" Opts.<$$>
" niv add NixOS/nixpkgs -n nixpkgs -b nixpkgs-unstable" Opts.<$$>
" niv add my-package -v alpha-0.1 -t http://example.com/archive/<version>.zip"
describeGitHub =
mconcat
[ Opts.fullDesc,
Opts.progDesc "Add a GitHub dependency",
Opts.headerDoc $ Just $
"Examples:"
Opts.<$$> ""
Opts.<$$> " niv add stedolan/jq"
Opts.<$$> " niv add NixOS/nixpkgs -n nixpkgs -b nixpkgs-unstable"
Opts.<$$> " niv add my-package -v alpha-0.1 -t http://example.com/archive/<version>.zip"
]
-- parse a github shortcut of the form "owner/repo"
@ -120,10 +137,13 @@ parseAddShortcutGitHub str =
-- parses a string "owner/repo" into package name (repo) and spec (owner +
-- repo)
case T.span (/= '/') str of
(owner@(T.null -> False)
, T.uncons -> Just ('/', repo@(T.null -> False))) -> Just
( PackageName repo
, HMS.fromList [ "owner" .= owner, "repo" .= repo ])
( owner@(T.null -> False),
T.uncons -> Just ('/', repo@(T.null -> False))
) ->
Just
( PackageName repo,
HMS.fromList ["owner" .= owner, "repo" .= repo]
)
-- XXX: this should be "Nothing" but for the time being we keep
-- backwards compatibility with "niv add foo" adding "foo" as a
-- package name.
@ -137,10 +157,10 @@ nixPrefetchURL :: Bool -> T.Text -> IO T.Text
nixPrefetchURL unpack turl@(T.unpack -> url) = do
(exitCode, sout, serr) <- runNixPrefetch
case (exitCode, lines sout) of
(ExitSuccess, l:_) -> pure $ T.pack l
(ExitSuccess, l : _) -> pure $ T.pack l
_ -> abortNixPrefetchExpectedOutput (T.pack <$> args) (T.pack sout) (T.pack serr)
where
args = (if unpack then ["--unpack"] else []) <> [ url, "--name", sanitizeName basename]
args = (if unpack then ["--unpack"] else []) <> [url, "--name", sanitizeName basename]
runNixPrefetch = readProcessWithExitCode "nix-prefetch-url" args ""
sanitizeName = T.unpack . T.filter isOk
basename = last $ T.splitOn "/" turl
@ -151,11 +171,14 @@ nixPrefetchURL unpack turl@(T.unpack -> url) = do
isOk = \c -> isAlphaNum c || T.any (c ==) "+-._?="
abortNixPrefetchExpectedOutput :: [T.Text] -> T.Text -> T.Text -> IO a
abortNixPrefetchExpectedOutput args sout serr = abort $ [s|
abortNixPrefetchExpectedOutput args sout serr =
abort $
[s|
Could not read the output of 'nix-prefetch-url'. This is a bug. Please create a
ticket:
https://github.com/nmattia/niv/issues/new
Thanks! I'll buy you a beer.
|] <> T.unlines ["command: ", "nix-prefetch-url" <> T.unwords args, "stdout: ", sout, "stderr: ", serr]
|]
<> T.unlines ["command: ", "nix-prefetch-url" <> T.unwords args, "stdout: ", sout, "stderr: ", serr]

View File

@ -5,115 +5,129 @@
module Niv.GitHub.Test where
import Control.Monad
import Data.IORef
import Data.Bifunctor
import qualified Data.HashMap.Strict as HMS
import Data.IORef
import Niv.GitHub
import Niv.GitHub.API
import Niv.Update
import qualified Data.HashMap.Strict as HMS
test_githubInitsProperly :: IO ()
test_githubInitsProperly = do
actualState <- evalUpdate initialState $ proc () ->
githubUpdate prefetch latestRev ghRepo -< ()
unless ((snd <$> actualState) == expectedState) $
error $ "State mismatch: " <> show actualState
unless ((snd <$> actualState) == expectedState)
$ error
$ "State mismatch: " <> show actualState
where
prefetch _ _ = pure "some-sha"
latestRev _ _ _ = pure "some-rev"
ghRepo _ _ = pure GithubRepo
{ repoDescription = Just "some-descr"
, repoHomepage = Just "some-homepage"
, repoDefaultBranch = Just "master"
ghRepo _ _ =
pure
GithubRepo
{ repoDescription = Just "some-descr",
repoHomepage = Just "some-homepage",
repoDefaultBranch = Just "master"
}
initialState = HMS.fromList
[ ("owner", (Free, "nmattia"))
, ("repo", (Free, "niv")) ]
expectedState = HMS.fromList
[ ("owner", "nmattia")
, ("repo", "niv")
, ("homepage", "some-homepage")
, ("description", "some-descr")
, ("branch", "master")
, ("url", "https://github.com/nmattia/niv/archive/some-rev.tar.gz")
, ("rev", "some-rev")
, ("sha256", "some-sha")
, ("type", "tarball")
, ("url_template", "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz")
initialState =
HMS.fromList
[ ("owner", (Free, "nmattia")),
("repo", (Free, "niv"))
]
expectedState =
HMS.fromList
[ ("owner", "nmattia"),
("repo", "niv"),
("homepage", "some-homepage"),
("description", "some-descr"),
("branch", "master"),
("url", "https://github.com/nmattia/niv/archive/some-rev.tar.gz"),
("rev", "some-rev"),
("sha256", "some-sha"),
("type", "tarball"),
("url_template", "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz")
]
test_githubUpdates :: IO ()
test_githubUpdates = do
actualState <- evalUpdate initialState $ proc () ->
githubUpdate prefetch latestRev ghRepo -< ()
unless ((snd <$> actualState) == expectedState) $
error $ "State mismatch: " <> show actualState
unless ((snd <$> actualState) == expectedState)
$ error
$ "State mismatch: " <> show actualState
where
prefetch _ _ = pure "new-sha"
latestRev _ _ _ = pure "new-rev"
ghRepo _ _ = pure GithubRepo
{ repoDescription = Just "some-descr"
, repoHomepage = Just "some-homepage"
, repoDefaultBranch = Just "master"
ghRepo _ _ =
pure
GithubRepo
{ repoDescription = Just "some-descr",
repoHomepage = Just "some-homepage",
repoDefaultBranch = Just "master"
}
initialState = HMS.fromList
[ ("owner", (Free, "nmattia"))
, ("repo", (Free, "niv"))
, ("homepage", (Free, "some-homepage"))
, ("description", (Free, "some-descr"))
, ("branch", (Free, "master"))
, ("url", (Free, "https://github.com/nmattia/niv/archive/some-rev.tar.gz"))
, ("rev", (Free, "some-rev"))
, ("sha256", (Free, "some-sha"))
, ("type", (Free, "tarball"))
, ("url_template", (Free, "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"))
initialState =
HMS.fromList
[ ("owner", (Free, "nmattia")),
("repo", (Free, "niv")),
("homepage", (Free, "some-homepage")),
("description", (Free, "some-descr")),
("branch", (Free, "master")),
("url", (Free, "https://github.com/nmattia/niv/archive/some-rev.tar.gz")),
("rev", (Free, "some-rev")),
("sha256", (Free, "some-sha")),
("type", (Free, "tarball")),
("url_template", (Free, "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"))
]
expectedState = HMS.fromList
[ ("owner", "nmattia")
, ("repo", "niv")
, ("homepage", "some-homepage")
, ("description", "some-descr")
, ("branch", "master")
, ("url", "https://github.com/nmattia/niv/archive/new-rev.tar.gz")
, ("rev", "new-rev")
, ("sha256", "new-sha")
, ("type", "tarball")
, ("url_template", "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz")
expectedState =
HMS.fromList
[ ("owner", "nmattia"),
("repo", "niv"),
("homepage", "some-homepage"),
("description", "some-descr"),
("branch", "master"),
("url", "https://github.com/nmattia/niv/archive/new-rev.tar.gz"),
("rev", "new-rev"),
("sha256", "new-sha"),
("type", "tarball"),
("url_template", "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz")
]
test_githubDoesntOverrideRev :: IO ()
test_githubDoesntOverrideRev = do
actualState <- evalUpdate initialState $ proc () ->
githubUpdate prefetch latestRev ghRepo -< ()
unless ((snd <$> actualState) == expectedState) $
error $ "State mismatch: " <> show actualState
unless ((snd <$> actualState) == expectedState)
$ error
$ "State mismatch: " <> show actualState
where
prefetch _ _ = pure "new-sha"
latestRev _ _ _ = error "shouldn't fetch rev"
ghRepo _ _ = error "shouldn't fetch repo"
initialState = HMS.fromList
[ ("owner", (Free, "nmattia"))
, ("repo", (Free, "niv"))
, ("homepage", (Free, "some-homepage"))
, ("description", (Free, "some-descr"))
, ("branch", (Free, "master"))
, ("url", (Free, "https://github.com/nmattia/niv/archive/some-rev.tar.gz"))
, ("rev", (Locked, "custom-rev"))
, ("sha256", (Free, "some-sha"))
, ("type", (Free, "tarball"))
, ("url_template", (Free, "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"))
initialState =
HMS.fromList
[ ("owner", (Free, "nmattia")),
("repo", (Free, "niv")),
("homepage", (Free, "some-homepage")),
("description", (Free, "some-descr")),
("branch", (Free, "master")),
("url", (Free, "https://github.com/nmattia/niv/archive/some-rev.tar.gz")),
("rev", (Locked, "custom-rev")),
("sha256", (Free, "some-sha")),
("type", (Free, "tarball")),
("url_template", (Free, "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"))
]
expectedState = HMS.fromList
[ ("owner", "nmattia")
, ("repo", "niv")
, ("homepage", "some-homepage")
, ("description", "some-descr")
, ("branch", "master")
, ("url", "https://github.com/nmattia/niv/archive/custom-rev.tar.gz")
, ("rev", "custom-rev")
, ("sha256", "new-sha")
, ("type", "tarball")
, ("url_template", "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz")
expectedState =
HMS.fromList
[ ("owner", "nmattia"),
("repo", "niv"),
("homepage", "some-homepage"),
("description", "some-descr"),
("branch", "master"),
("url", "https://github.com/nmattia/niv/archive/custom-rev.tar.gz"),
("rev", "custom-rev"),
("sha256", "new-sha"),
("type", "tarball"),
("url_template", "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz")
]
-- TODO: HMS diff for test output
@ -121,22 +135,25 @@ test_githubURLFallback :: IO ()
test_githubURLFallback = do
actualState <- evalUpdate initialState $ proc () ->
githubUpdate prefetch latestRev ghRepo -< ()
unless ((snd <$> actualState) == expectedState) $
error $ "State mismatch: " <> show actualState
unless ((snd <$> actualState) == expectedState)
$ error
$ "State mismatch: " <> show actualState
where
prefetch _ _ = pure "some-sha"
latestRev _ _ _ = error "shouldn't fetch rev"
ghRepo _ _ = error "shouldn't fetch repo"
initialState = HMS.fromList
[ ("url_template", (Free, "https://foo.com/<baz>.tar.gz"))
, ("baz", (Free, "tarball"))
initialState =
HMS.fromList
[ ("url_template", (Free, "https://foo.com/<baz>.tar.gz")),
("baz", (Free, "tarball"))
]
expectedState = HMS.fromList
[ ("url_template", "https://foo.com/<baz>.tar.gz")
, ("baz", "tarball")
, ("url", "https://foo.com/tarball.tar.gz")
, ("sha256", "some-sha")
, ("type", "tarball")
expectedState =
HMS.fromList
[ ("url_template", "https://foo.com/<baz>.tar.gz"),
("baz", "tarball"),
("url", "https://foo.com/tarball.tar.gz"),
("sha256", "some-sha"),
("type", "tarball")
]
test_githubUpdatesOnce :: IO ()
@ -144,17 +161,16 @@ test_githubUpdatesOnce = do
ioref <- newIORef False
tmpState <- evalUpdate initialState $ proc () ->
githubUpdate (prefetch ioref) latestRev ghRepo -< ()
unless ((snd <$> tmpState) == expectedState) $
error $ "State mismatch: " <> show tmpState
unless ((snd <$> tmpState) == expectedState)
$ error
$ "State mismatch: " <> show tmpState
-- Set everything free
let tmpState' = HMS.map (first (\_ -> Free)) tmpState
actualState <- evalUpdate tmpState' $ proc () ->
githubUpdate (prefetch ioref) latestRev ghRepo -< ()
unless ((snd <$> actualState) == expectedState) $
error $ "State mismatch: " <> show actualState
unless ((snd <$> actualState) == expectedState)
$ error
$ "State mismatch: " <> show actualState
where
prefetch ioref _ _ = do
readIORef ioref >>= \case
@ -163,32 +179,36 @@ test_githubUpdatesOnce = do
writeIORef ioref True
pure "new-sha"
latestRev _ _ _ = pure "new-rev"
ghRepo _ _ = pure GithubRepo
{ repoDescription = Just "some-descr"
, repoHomepage = Just "some-homepage"
, repoDefaultBranch = Just "master"
ghRepo _ _ =
pure
GithubRepo
{ repoDescription = Just "some-descr",
repoHomepage = Just "some-homepage",
repoDefaultBranch = Just "master"
}
initialState = HMS.fromList
[ ("owner", (Free, "nmattia"))
, ("repo", (Free, "niv"))
, ("homepage", (Free, "some-homepage"))
, ("description", (Free, "some-descr"))
, ("branch", (Free, "master"))
, ("url", (Free, "https://github.com/nmattia/niv/archive/some-rev.tar.gz"))
, ("rev", (Free, "some-rev"))
, ("sha256", (Free, "some-sha"))
, ("type", (Free, "tarball"))
, ("url_template", (Free, "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"))
initialState =
HMS.fromList
[ ("owner", (Free, "nmattia")),
("repo", (Free, "niv")),
("homepage", (Free, "some-homepage")),
("description", (Free, "some-descr")),
("branch", (Free, "master")),
("url", (Free, "https://github.com/nmattia/niv/archive/some-rev.tar.gz")),
("rev", (Free, "some-rev")),
("sha256", (Free, "some-sha")),
("type", (Free, "tarball")),
("url_template", (Free, "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"))
]
expectedState = HMS.fromList
[ ("owner", "nmattia")
, ("repo", "niv")
, ("homepage", "some-homepage")
, ("description", "some-descr")
, ("branch", "master")
, ("url", "https://github.com/nmattia/niv/archive/new-rev.tar.gz")
, ("rev", "new-rev")
, ("sha256", "new-sha")
, ("type", "tarball")
, ("url_template", "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz")
expectedState =
HMS.fromList
[ ("owner", "nmattia"),
("repo", "niv"),
("homepage", "some-homepage"),
("description", "some-descr"),
("branch", "master"),
("url", "https://github.com/nmattia/niv/archive/new-rev.tar.gz"),
("rev", "new-rev"),
("sha256", "new-sha"),
("type", "tarball"),
("url_template", "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz")
]

View File

@ -1,5 +1,5 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE Arrows #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
@ -8,32 +8,34 @@
module Niv.Local.Cmd where
import Niv.Cmd
import Control.Arrow
import Niv.Sources
import Niv.Update
import qualified Data.Aeson as Aeson
import qualified Data.HashMap.Strict as HMS
import qualified Data.Text as T
import Niv.Cmd
import Niv.Sources
import Niv.Update
import qualified Options.Applicative as Opts
import qualified Options.Applicative.Help.Pretty as Opts
localCmd :: Cmd
localCmd = Cmd
{ description = describeLocal
, parseCmdShortcut = parseLocalShortcut
, parsePackageSpec = parseLocalPackageSpec
, updateCmd = proc () -> do
localCmd =
Cmd
{ description = describeLocal,
parseCmdShortcut = parseLocalShortcut,
parsePackageSpec = parseLocalPackageSpec,
updateCmd = proc () -> do
useOrSet "type" -< ("local" :: Box T.Text)
returnA -< ()
, name = "local"
returnA -< (),
name = "local"
}
parseLocalShortcut :: T.Text -> Maybe (PackageName, Aeson.Object)
parseLocalShortcut txt =
if (T.isPrefixOf "./" txt || T.isPrefixOf "/" txt ) then do
if (T.isPrefixOf "./" txt || T.isPrefixOf "/" txt)
then do
let n = last $ T.splitOn "/" txt
Just (PackageName n, HMS.fromList [ ("path", Aeson.String txt) ])
Just (PackageName n, HMS.fromList [("path", Aeson.String txt)])
else Nothing
parseLocalPackageSpec :: Opts.Parser PackageSpec
@ -41,19 +43,20 @@ parseLocalPackageSpec = PackageSpec . HMS.fromList <$> parseParams
where
parseParams :: Opts.Parser [(T.Text, Aeson.Value)]
parseParams = maybe [] pure <$> Opts.optional parsePath
parsePath =
("path", ) . Aeson.String <$> Opts.strOption
( Opts.long "path" <>
Opts.metavar "PATH"
("path",) . Aeson.String
<$> Opts.strOption
( Opts.long "path"
<> Opts.metavar "PATH"
)
describeLocal :: Opts.InfoMod a
describeLocal = mconcat
[ Opts.fullDesc
, Opts.progDesc "Add a local dependency. Experimental."
, Opts.headerDoc $ Just $
"Examples:" Opts.<$$>
"" Opts.<$$>
" niv add local ./foo/bar"
describeLocal =
mconcat
[ Opts.fullDesc,
Opts.progDesc "Add a local dependency. Experimental.",
Opts.headerDoc $ Just $
"Examples:"
Opts.<$$> ""
Opts.<$$> " niv add local ./foo/bar"
]

View File

@ -1,31 +1,39 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Niv.Logger
( job
, bug
, tsay
, say
, green, tgreen
, red, tred
, blue, tblue
, yellow, tyellow
, bold, tbold
, faint, tfaint
) where
( job,
bug,
tsay,
say,
green,
tgreen,
red,
tred,
blue,
tblue,
yellow,
tyellow,
bold,
tbold,
faint,
tfaint,
)
where
import Control.Monad
import Data.List
import Data.Profunctor
import qualified Data.Text as T
import qualified System.Console.ANSI as ANSI
import System.Exit (exitFailure)
import System.IO.Unsafe (unsafePerformIO)
import qualified Data.Text as T
import UnliftIO
import qualified System.Console.ANSI as ANSI
type S = String -> String
type T = T.Text -> T.Text
-- XXX: this assumes as single thread
@ -51,6 +59,7 @@ jobStackSize = readIORef jobStack
jobStack :: IORef Int
jobStack = unsafePerformIO $ newIORef 0
{-# NOINLINE jobStackSize #-}
tsay :: MonadIO io => T.Text -> io ()
@ -66,53 +75,59 @@ say msg = do
green :: S
green str =
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity] <>
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Green] <>
str <> ANSI.setSGRCode [ANSI.Reset]
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity]
<> ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Green]
<> str
<> ANSI.setSGRCode [ANSI.Reset]
tgreen :: T
tgreen = t green
yellow :: S
yellow str =
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity] <>
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Yellow] <>
str <> ANSI.setSGRCode [ANSI.Reset]
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity]
<> ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Yellow]
<> str
<> ANSI.setSGRCode [ANSI.Reset]
tyellow :: T
tyellow = t yellow
blue :: S
blue str =
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity] <>
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Blue] <>
str <> ANSI.setSGRCode [ANSI.Reset]
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity]
<> ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Blue]
<> str
<> ANSI.setSGRCode [ANSI.Reset]
tblue :: T
tblue = t blue
red :: S
red str =
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Red] <>
str <> ANSI.setSGRCode [ANSI.Reset]
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Red]
<> str
<> ANSI.setSGRCode [ANSI.Reset]
tred :: T
tred = t red
bold :: S
bold str =
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity] <>
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.White] <>
str <> ANSI.setSGRCode [ANSI.Reset]
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity]
<> ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.White]
<> str
<> ANSI.setSGRCode [ANSI.Reset]
tbold :: T
tbold = t bold
faint :: String -> String
faint str =
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.FaintIntensity] <>
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.White] <>
str <> ANSI.setSGRCode [ANSI.Reset]
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.FaintIntensity]
<> ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.White]
<> str
<> ANSI.setSGRCode [ANSI.Reset]
tfaint :: T
tfaint = t faint
@ -121,9 +136,10 @@ t :: (String -> String) -> T.Text -> T.Text
t = dimap T.unpack T.pack
bug :: T.Text -> T.Text
bug txt = T.unlines
[ txt
, "This is a bug. Please create a ticket:"
, " https://github.com/nmattia/niv/issues/new"
, "Thanks! I'll buy you a beer."
bug txt =
T.unlines
[ txt,
"This is a bug. Please create a ticket:",
" https://github.com/nmattia/niv/issues/new",
"Thanks! I'll buy you a beer."
]

View File

@ -1,31 +1,31 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Niv.Sources where
import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
import Data.FileEmbed (embedFile)
import Data.Bifunctor (first)
import Data.Hashable (Hashable)
import Data.List
import Data.String.QQ (s)
import Data.Text.Extended
import Niv.Logger
import Niv.Update
import System.FilePath ((</>))
import UnliftIO
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Extended as Aeson
import Data.Bifunctor (first)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy.Char8 as BL8
import qualified Data.Digest.Pure.MD5 as MD5
import Data.FileEmbed (embedFile)
import qualified Data.HashMap.Strict as HMS
import Data.Hashable (Hashable)
import Data.List
import Data.String.QQ (s)
import qualified Data.Text as T
import Data.Text.Extended
import Niv.Logger
import Niv.Update
import qualified System.Directory as Dir
import System.FilePath ((</>))
import UnliftIO
-------------------------------------------------------------------------------
-- sources.json related
@ -33,16 +33,19 @@ import qualified System.Directory as Dir
-- | Where to find the sources.json
data FindSourcesJson
= Auto -- ^ use the default (nix/sources.json)
| AtPath FilePath -- ^ use the specified file path
= -- | use the default (nix/sources.json)
Auto
| -- | use the specified file path
AtPath FilePath
data SourcesError
= SourcesDoesntExist
| SourceIsntJSON
| SpecIsntAMap
newtype Sources = Sources
{ unSources :: HMS.HashMap PackageName PackageSpec }
newtype Sources
= Sources
{unSources :: HMS.HashMap PackageName PackageSpec}
deriving newtype (FromJSON, ToJSON)
getSourcesEither :: FindSourcesJson -> IO (Either SourcesError Sources)
@ -58,11 +61,14 @@ getSourcesEither fsj = do
where
valueToSources :: Aeson.Value -> Maybe Sources
valueToSources = \case
Aeson.Object obj -> fmap (Sources . mapKeys PackageName) $ traverse
(\case
Aeson.Object obj ->
fmap (Sources . mapKeys PackageName) $
traverse
( \case
Aeson.Object obj' -> Just (PackageSpec obj')
_ -> Nothing
) obj
)
obj
_ -> Nothing
mapKeys :: (Eq k2, Hashable k2) => (k1 -> k2) -> HMS.HashMap k1 v -> HMS.HashMap k2 v
mapKeys f = HMS.fromList . map (first f) . HMS.toList
@ -70,20 +76,22 @@ getSourcesEither fsj = do
getSources :: FindSourcesJson -> IO Sources
getSources fsj = do
warnIfOutdated
getSourcesEither fsj >>= either
(\case
getSourcesEither fsj
>>= either
( \case
SourcesDoesntExist -> (abortSourcesDoesntExist fsj)
SourceIsntJSON -> (abortSourcesIsntJSON fsj)
SpecIsntAMap -> (abortSpecIsntAMap fsj)
) pure
)
pure
setSources :: FindSourcesJson -> Sources -> IO ()
setSources fsj sources = Aeson.encodeFilePretty (pathNixSourcesJson fsj) sources
newtype PackageName = PackageName { unPackageName :: T.Text }
newtype PackageName = PackageName {unPackageName :: T.Text}
deriving newtype (Eq, Hashable, FromJSONKey, ToJSONKey, Show)
newtype PackageSpec = PackageSpec { unPackageSpec :: Aeson.Object }
newtype PackageSpec = PackageSpec {unPackageSpec :: Aeson.Object}
deriving newtype (FromJSON, ToJSON, Show, Semigroup, Monoid)
-- | Simply discards the 'Freedom'
@ -101,24 +109,26 @@ pathNixSourcesJson = \case
--
abortSourcesDoesntExist :: FindSourcesJson -> IO a
abortSourcesDoesntExist fsj = abort $ T.unlines [ line1, line2 ]
abortSourcesDoesntExist fsj = abort $ T.unlines [line1, line2]
where
line1 = "Cannot use " <> T.pack (pathNixSourcesJson fsj)
line2 = [s|
line2 =
[s|
The sources file does not exist! You may need to run 'niv init'.
|]
abortSourcesIsntJSON :: FindSourcesJson -> IO a
abortSourcesIsntJSON fsj = abort $ T.unlines [ line1, line2 ]
abortSourcesIsntJSON fsj = abort $ T.unlines [line1, line2]
where
line1 = "Cannot use " <> T.pack (pathNixSourcesJson fsj)
line2 = "The sources file should be JSON."
abortSpecIsntAMap :: FindSourcesJson -> IO a
abortSpecIsntAMap fsj = abort $ T.unlines [ line1, line2 ]
abortSpecIsntAMap fsj = abort $ T.unlines [line1, line2]
where
line1 = "Cannot use " <> T.pack (pathNixSourcesJson fsj)
line2 = [s|
line2 =
[s|
The package specifications in the sources file should be JSON maps from
attribute name to attribute value, e.g.:
{ "nixpkgs": { "foo": "bar" } }
@ -128,7 +138,6 @@ attribute name to attribute value, e.g.:
-- sources.nix related
-------------------------------------------------------------------------------
-- | All the released versions of nix/sources.nix
data SourcesNixVersion
= V1
@ -148,9 +157,9 @@ data SourcesNixVersion
| V15
| V16
| V17
-- prettify derivation name
| -- prettify derivation name
-- add 'local' type of sources
| V18
V18
deriving stock (Bounded, Enum, Eq)
-- | A user friendly version
@ -216,9 +225,11 @@ pathNixSourcesNix = "nix" </> "sources.nix"
warnIfOutdated :: IO ()
warnIfOutdated = do
tryAny (BL8.readFile pathNixSourcesNix) >>= \case
Left e -> tsay $ T.unlines -- warn with tsay
[ T.unwords [ tyellow "WARNING:", "Could not read" , T.pack pathNixSourcesNix ]
, T.unwords [ " ", "(", tshow e, ")" ]
Left e ->
tsay $
T.unlines -- warn with tsay
[ T.unwords [tyellow "WARNING:", "Could not read", T.pack pathNixSourcesNix],
T.unwords [" ", "(", tshow e, ")"]
]
Right content -> do
case md5ToSourcesVersion (T.pack $ show $ MD5.md5 content) of
@ -229,15 +240,19 @@ warnIfOutdated = do
| v == maxBound -> pure ()
-- The file is older than than latest
| otherwise -> do
tsay $ T.unlines
tsay $
T.unlines
[ T.unwords
[ tbold $ tblue "INFO:"
, "new sources.nix available:"
, sourcesVersionToText v, "->", sourcesVersionToText maxBound
]
, " Please run 'niv init' or add the following line in the " <>
T.pack pathNixSourcesNix <> " file:"
, " # niv: no_update"
[ tbold $ tblue "INFO:",
"new sources.nix available:",
sourcesVersionToText v,
"->",
sourcesVersionToText maxBound
],
" Please run 'niv init' or add the following line in the "
<> T.pack pathNixSourcesNix
<> " file:",
" # niv: no_update"
]
-- | Glue code between nix and sources.json

View File

@ -1,13 +1,13 @@
module Niv.Sources.Test where
import Niv.Sources
import Test.Tasty.HUnit ((@=?))
import qualified Data.ByteString.Lazy as BL
import qualified Data.Digest.Pure.MD5 as MD5
import qualified Data.Text as T
import Niv.Sources
import Test.Tasty.HUnit ((@=?))
-- | Ensure that the sources.nix we ship is tracked as the latest version
test_shippedSourcesNixIsLatest :: IO ()
test_shippedSourcesNixIsLatest =
latestVersionMD5 @=?
(T.pack . show . MD5.md5 . BL.fromStrict $ initNixSourcesNixContent)
latestVersionMD5
@=? (T.pack . show . MD5.md5 . BL.fromStrict $ initNixSourcesNixContent)

View File

@ -1,9 +1,9 @@
module Niv.Test (tests, test) where
import Niv.Sources.Test
import Niv.GitHub.Test
import Niv.Update.Test
import qualified Niv.Git.Test as Git
import Niv.GitHub.Test
import Niv.Sources.Test
import Niv.Update.Test
import qualified Test.Tasty as Tasty
import qualified Test.Tasty.HUnit as Tasty
@ -11,26 +11,31 @@ test :: IO ()
test = Tasty.defaultMain tests
tests :: Tasty.TestTree
tests = Tasty.testGroup "niv"
[ Tasty.testGroup "update"
[ Tasty.testCase "simply runs" simplyRuns
, Tasty.testCase "picks first" picksFirst
, Tasty.testCase "loads" loads
, Tasty.testCase "survives checks" survivesChecks
, Tasty.testCase "isn't too eager" isNotTooEager
, Tasty.testCase "dirty forces update" dirtyForcesUpdate
, Tasty.testCase "should run when no changes" shouldNotRunWhenNoChanges
, Tasty.testCase "templates expand" templatesExpand
]
, Tasty.testGroup "github"
[ Tasty.testCase "inits properly" test_githubInitsProperly
, Tasty.testCase "updates" test_githubUpdates
, Tasty.testCase "updates once" test_githubUpdatesOnce
, Tasty.testCase "doesn't override rev" test_githubDoesntOverrideRev
, Tasty.testCase "falls back to URL" test_githubURLFallback
]
, Tasty.testGroup "sources.nix"
tests =
Tasty.testGroup
"niv"
[ Tasty.testGroup
"update"
[ Tasty.testCase "simply runs" simplyRuns,
Tasty.testCase "picks first" picksFirst,
Tasty.testCase "loads" loads,
Tasty.testCase "survives checks" survivesChecks,
Tasty.testCase "isn't too eager" isNotTooEager,
Tasty.testCase "dirty forces update" dirtyForcesUpdate,
Tasty.testCase "should run when no changes" shouldNotRunWhenNoChanges,
Tasty.testCase "templates expand" templatesExpand
],
Tasty.testGroup
"github"
[ Tasty.testCase "inits properly" test_githubInitsProperly,
Tasty.testCase "updates" test_githubUpdates,
Tasty.testCase "updates once" test_githubUpdatesOnce,
Tasty.testCase "doesn't override rev" test_githubDoesntOverrideRev,
Tasty.testCase "falls back to URL" test_githubURLFallback
],
Tasty.testGroup
"sources.nix"
[ Tasty.testCase "has latest version" test_shippedSourcesNixIsLatest
]
, Tasty.testGroup "git" Git.tests
],
Tasty.testGroup "git" Git.tests
]

View File

@ -12,14 +12,14 @@ module Niv.Update where
import Control.Applicative
import Control.Arrow
import Data.Aeson (FromJSON, ToJSON, Value)
import Data.String
import Niv.Logger
import UnliftIO
import qualified Control.Category as Cat
import Data.Aeson (FromJSON, ToJSON, Value)
import qualified Data.Aeson as Aeson
import qualified Data.HashMap.Strict as HMS
import Data.String
import qualified Data.Text as T
import Niv.Logger
import UnliftIO
type Attrs = HMS.HashMap T.Text (Freedom, Value)
@ -54,7 +54,7 @@ instance Cat.Category Update where
instance Show (Update b c) where
show = \case
Id -> "Id"
Compose (Compose' f g)-> "(" <> show f <> " . " <> show g <> ")"
Compose (Compose' f g) -> "(" <> show f <> " . " <> show g <> ")"
Arr _f -> "Arr"
First a -> "First " <> show a
Zero -> "Zero"
@ -83,9 +83,10 @@ runUpdate (boxAttrs -> attrs) a = runUpdate' attrs a >>= feed
FailNoSuchKey k -> "Key could not be found: " <> k
FailZero -> bug "A dead end was reached during evaluation."
FailCheck -> "A check failed during update"
FailTemplate tpl keys -> T.unlines
[ "Could not render template " <> tpl
, "with keys: " <> T.intercalate ", " keys
FailTemplate tpl keys ->
T.unlines
[ "Could not render template " <> tpl,
"with keys: " <> T.intercalate ", " keys
]
execUpdate :: Attrs -> Update () a -> IO a
@ -104,41 +105,43 @@ data UpdateFailed
| FailZero
| FailCheck
| FailTemplate T.Text [T.Text]
deriving Show
deriving (Show)
data UpdateRes a b
= UpdateReady (UpdateReady b)
| UpdateNeedMore (a -> IO (UpdateReady b))
deriving Functor
deriving (Functor)
data UpdateReady b
= UpdateSuccess BoxedAttrs b
| UpdateFailed UpdateFailed
deriving Functor
deriving (Functor)
runBox :: Box a -> IO a
runBox = boxOp
data Box a = Box
{ boxNew :: Bool
-- ^ Whether the value is new or was retrieved (or derived) from old
data Box a
= Box
{ -- | Whether the value is new or was retrieved (or derived) from old
-- attributes
, boxOp :: IO a
boxNew :: Bool,
boxOp :: IO a
}
deriving Functor
deriving (Functor)
instance Applicative Box where
pure x = Box { boxNew = False, boxOp = pure x }
f <*> v = Box
{ boxNew = (||) (boxNew f) (boxNew v)
, boxOp = boxOp f <*> boxOp v
pure x = Box {boxNew = False, boxOp = pure x}
f <*> v =
Box
{ boxNew = (||) (boxNew f) (boxNew v),
boxOp = boxOp f <*> boxOp v
}
instance Semigroup a => Semigroup (Box a) where
(<>) = liftA2 (<>)
instance IsString (Box T.Text) where
fromString str = Box { boxNew = False, boxOp = pure $ T.pack str }
fromString str = Box {boxNew = False, boxOp = pure $ T.pack str}
type BoxedAttrs = HMS.HashMap T.Text (Freedom, Box Value)
@ -146,12 +149,16 @@ unboxAttrs :: BoxedAttrs -> IO Attrs
unboxAttrs = traverse (\(fr, v) -> (fr,) <$> runBox v)
boxAttrs :: Attrs -> BoxedAttrs
boxAttrs = fmap (\(fr, v) -> (fr,
boxAttrs =
fmap
( \(fr, v) ->
( fr,
case fr of
-- TODO: explain why hacky
Locked -> (pure v) { boxNew = True } -- XXX: somewhat hacky
Locked -> (pure v) {boxNew = True} -- XXX: somewhat hacky
Free -> pure v
))
)
)
data Freedom
= Locked
@ -167,7 +174,7 @@ runUpdate' attrs = \case
Arr f -> pure $ UpdateNeedMore $ pure . UpdateSuccess attrs . f
Zero -> pure $ UpdateReady (UpdateFailed FailZero)
Plus l r -> runUpdate' attrs l >>= \case
UpdateReady (UpdateFailed{}) -> runUpdate' attrs r
UpdateReady (UpdateFailed {}) -> runUpdate' attrs r
UpdateReady (UpdateSuccess f v) -> pure $ UpdateReady (UpdateSuccess f v)
UpdateNeedMore next -> pure $ UpdateNeedMore $ \v -> next v >>= \case
UpdateSuccess f res -> pure $ UpdateSuccess f res
@ -188,13 +195,19 @@ runUpdate' attrs = \case
UpdateFailed e -> pure $ UpdateFailed e
UpdateSuccess f res -> do
pure $ UpdateSuccess f (res, snd gtt)
Run act -> pure (UpdateNeedMore $ \gtt -> do
pure $ UpdateSuccess attrs $ Box (boxNew gtt) (act =<< runBox gtt))
Check ch -> pure (UpdateNeedMore $ \gtt -> do
Run act ->
pure
( UpdateNeedMore $ \gtt -> do
pure $ UpdateSuccess attrs $ Box (boxNew gtt) (act =<< runBox gtt)
)
Check ch ->
pure
( UpdateNeedMore $ \gtt -> do
v <- runBox gtt
if ch v
then pure $ UpdateSuccess attrs ()
else pure $ UpdateFailed FailCheck)
else pure $ UpdateFailed FailCheck
)
UseOrSet k -> pure $ case HMS.lookup k attrs of
Just (Locked, v) -> UpdateReady $ UpdateSuccess attrs v
Just (Free, v) -> UpdateReady $ UpdateSuccess attrs v
@ -211,8 +224,10 @@ runUpdate' attrs = \case
-- Here we compare the old and new values, flagging the new one as
-- "boxNew" iff they differ.
-- TODO: generalize this to all boxes
let gtt'' = if v' /= gtt' then gtt { boxNew = True, boxOp = pure gtt' }
else gtt { boxNew = False, boxOp = pure gtt' }
let gtt'' =
if v' /= gtt'
then gtt {boxNew = True, boxOp = pure gtt'}
else gtt {boxNew = False, boxOp = pure gtt'}
pure $ UpdateSuccess (HMS.insert k (Locked, gtt'') attrs) gtt''
else do
pure $ UpdateSuccess attrs v
@ -233,14 +248,16 @@ runUpdate' attrs = \case
Template -> pure $ UpdateNeedMore $ \v -> do
v' <- runBox v
case renderTemplate
(\k ->
((decodeBox $ "When rendering template " <> v') . snd) <$>
HMS.lookup k attrs) v' of
( \k ->
((decodeBox $ "When rendering template " <> v') . snd)
<$> HMS.lookup k attrs
)
v' of
Nothing -> pure $ UpdateFailed $ FailTemplate v' (HMS.keys attrs)
Just v'' -> pure $ UpdateSuccess attrs (v'' <* v) -- carries over v's newness
decodeBox :: FromJSON a => T.Text -> Box Value -> Box a
decodeBox msg v = v { boxOp = boxOp v >>= decodeValue msg }
decodeBox msg v = v {boxOp = boxOp v >>= decodeValue msg}
decodeValue :: FromJSON a => T.Text -> Value -> IO a
decodeValue msg v = case Aeson.fromJSON v of
@ -277,15 +294,15 @@ load k = Load k >>> arr (decodeBox $ "When loading key " <> k)
-- TODO: should input really be Box?
useOrSet :: JSON a => T.Text -> Update (Box a) (Box a)
useOrSet k =
arr (fmap Aeson.toJSON) >>>
UseOrSet k >>>
arr (decodeBox $ "When trying to use or set key " <> k)
arr (fmap Aeson.toJSON)
>>> UseOrSet k
>>> arr (decodeBox $ "When trying to use or set key " <> k)
update :: JSON a => T.Text -> Update (Box a) (Box a)
update k =
arr (fmap Aeson.toJSON) >>>
Update k >>>
arr (decodeBox $ "When updating key " <> k)
arr (fmap Aeson.toJSON)
>>> Update k
>>> arr (decodeBox $ "When updating key " <> k)
run :: (a -> IO b) -> Update (Box a) (Box b)
run = Run
@ -295,4 +312,4 @@ run' :: (a -> IO b) -> Update (Box a) (Box b)
run' act = Run act >>> dirty
dirty :: Update (Box a) (Box a)
dirty = arr (\v -> v { boxNew = True })
dirty = arr (\v -> v {boxNew = True})

View File

@ -1,14 +1,14 @@
{-# LANGUAGE Arrows #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Niv.Update.Test where
import Control.Arrow
import Control.Monad
import Niv.Update
import qualified Data.HashMap.Strict as HMS
import qualified Data.Text as T
import Niv.Update
simplyRuns :: IO ()
simplyRuns =
@ -19,12 +19,14 @@ simplyRuns =
picksFirst :: IO ()
picksFirst = do
v <- execUpdate HMS.empty $
let
l = proc () -> do returnA -< 2
r = proc () -> do returnA -< 3
v <-
execUpdate HMS.empty $
let l = proc () -> do
returnA -< 2
r = proc () -> do
returnA -< 3
in l <+> r
unless (v == (2::Int)) (error "bad value")
unless (v == (2 :: Int)) (error "bad value")
loads :: IO ()
loads = do
@ -58,9 +60,10 @@ survivesChecks = do
isNotTooEager :: IO ()
isNotTooEager = do
let f = constBox () >>>
run (const $ error "IO is too eager (f)") >>>
useOrSet "foo"
let f =
constBox ()
>>> run (const $ error "IO is too eager (f)")
>>> useOrSet "foo"
let f1 = proc () -> do
run (const $ error "IO is too eager (f1)") -< pure ()
useOrSet "foo" -< "foo"
@ -73,8 +76,9 @@ dirtyForcesUpdate :: IO ()
dirtyForcesUpdate = do
let f = constBox ("world" :: T.Text) >>> dirty >>> update "hello"
attrs' <- evalUpdate attrs f
unless ((snd <$> HMS.lookup "hello" attrs') == Just "world") $
error $ "bad value for hello: " <> show attrs'
unless ((snd <$> HMS.lookup "hello" attrs') == Just "world")
$ error
$ "bad value for hello: " <> show attrs'
where
attrs = HMS.singleton "hello" (Free, "foo")
@ -84,17 +88,19 @@ shouldNotRunWhenNoChanges = do
update "hello" -< ("world" :: Box T.Text)
run (\() -> error "io shouldn't be run") -< pure ()
attrs <- evalUpdate HMS.empty f
unless ((snd <$> HMS.lookup "hello" attrs) == Just "world") $
error $ "bad value for hello: " <> show attrs
unless ((snd <$> HMS.lookup "hello" attrs) == Just "world")
$ error
$ "bad value for hello: " <> show attrs
let f' = proc () -> do
run (\() -> error "io shouldn't be run") -< pure ()
update "hello" -< ("world" :: Box T.Text)
attrs' <- evalUpdate HMS.empty f'
unless ((snd <$> HMS.lookup "hello" attrs') == Just "world") $
error $ "bad value for hello: " <> show attrs'
unless ((snd <$> HMS.lookup "hello" attrs') == Just "world")
$ error
$ "bad value for hello: " <> show attrs'
v3 <- execUpdate
(HMS.fromList [("hello", (Free, "world")), ("bar", (Free, "baz"))]) $
proc () -> do
(HMS.fromList [("hello", (Free, "world")), ("bar", (Free, "baz"))])
$ proc () -> do
v1 <- update "hello" -< "world"
v2 <- run (\_ -> error "io shouldn't be run") -< (v1 :: Box T.Text)
v3 <- update "bar" -< (v2 :: Box T.Text)