From 6ea79800311d10d6bab4ccd0e80852dd6f841c54 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Sun, 10 Nov 2019 15:59:33 +0100 Subject: [PATCH 1/5] Prettify show --- src/Niv/Cli.hs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index b795141..60099d5 100644 --- a/src/Niv/Cli.hs +++ b/src/Niv/Cli.hs @@ -293,31 +293,25 @@ parseCmdShow = cmdShow :: Maybe PackageName -> IO () cmdShow = \case Just packageName -> do - tsay $ "Showing package " <> unPackageName packageName - sources <- unSources <$> getSources case HMS.lookup packageName sources of - Just (PackageSpec spec) -> do - forM_ (HMS.toList spec) $ \(attrName, attrValValue) -> do - let attrValue = case attrValValue of - Aeson.String str -> str - _ -> "" - tsay $ " " <> attrName <> ": " <> attrValue + Just pspec -> showPackage packageName pspec Nothing -> abortCannotShowNoSuchPackage packageName Nothing -> do - say $ "Showing sources file" - sources <- unSources <$> getSources + forWithKeyM_ sources $ showPackage + +showPackage :: PackageName -> PackageSpec -> IO () +showPackage (PackageName pname) (PackageSpec spec) = do + tsay $ tbold pname + forM_ (HMS.toList spec) $ \(attrName, attrValValue) -> do + let attrValue = case attrValValue of + Aeson.String str -> str + _ -> tfaint "" + tsay $ " " <> attrName <> ": " <> attrValue - forWithKeyM_ sources $ \key (PackageSpec spec) -> do - tsay $ "Showing " <> tbold (unPackageName key) - forM_ (HMS.toList spec) $ \(attrName, attrValValue) -> do - let attrValue = case attrValValue of - Aeson.String str -> str - _ -> tfaint "" - tsay $ " " <> attrName <> ": " <> attrValue ------------------------------------------------------------------------------- -- UPDATE From b70f81b6f913d995b487afd7740e6be2ad634f25 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Sun, 10 Nov 2019 15:59:59 +0100 Subject: [PATCH 2/5] Extend getSources with either --- src/Niv/Cli.hs | 56 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index 60099d5..1fe69d1 100644 --- a/src/Niv/Cli.hs +++ b/src/Niv/Cli.hs @@ -12,6 +12,7 @@ module Niv.Cli where import Control.Applicative import Control.Monad import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey, (.=)) +import Data.Bifunctor (first) import Data.Char (isSpace) import Data.FileEmbed (embedFile) import Data.Hashable (Hashable) @@ -71,24 +72,41 @@ newtype Sources = Sources { unSources :: HMS.HashMap PackageName PackageSpec } deriving newtype (FromJSON, ToJSON) -getSources :: IO Sources -getSources = do - exists <- Dir.doesFileExist pathNixSourcesJson - unless exists abortSourcesDoesntExist +data SourcesError + = SourcesDoesntExist + | SourceIsntJSON + | SpecIsntAMap - warnIfOutdated - -- TODO: if doesn't exist: run niv init - say $ "Reading sources file" - decodeFileStrict pathNixSourcesJson >>= \case - Just (Aeson.Object obj) -> - fmap (Sources . mconcat) $ - forM (HMS.toList obj) $ \(k, v) -> - case v of - Aeson.Object v' -> - pure $ HMS.singleton (PackageName k) (PackageSpec v') - _ -> abortAttributeIsntAMap - Just _ -> abortSourcesIsntAMap - Nothing -> abortSourcesIsntJSON +getSourcesEither :: IO (Either SourcesError Sources) +getSourcesEither = do + Dir.doesFileExist pathNixSourcesJson >>= \case + False -> pure $ Left SourcesDoesntExist + True -> + decodeFileStrict pathNixSourcesJson >>= \case + Just value -> case valueToSources value of + Nothing -> pure $ Left SpecIsntAMap + Just srcs -> pure $ Right srcs + Nothing -> pure $ Left SourceIsntJSON + where + valueToSources :: Aeson.Value -> Maybe Sources + valueToSources = \case + Aeson.Object obj -> fmap (Sources . mapKeys PackageName) $ traverse + (\case + Aeson.Object obj' -> Just (PackageSpec obj') + _ -> Nothing + ) 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 + +getSources :: IO Sources +getSources = + getSourcesEither >>= either + (\case + SourcesDoesntExist -> abortSourcesDoesntExist + SourceIsntJSON -> abortSourcesIsntJSON + SpecIsntAMap -> abortSpecIsntAMap + ) pure setSources :: Sources -> IO () setSources sources = encodeFile pathNixSourcesJson sources @@ -615,8 +633,8 @@ specification, e.g.: { ... } |] -abortAttributeIsntAMap :: IO a -abortAttributeIsntAMap = abort $ T.unlines [ line1, line2 ] +abortSpecIsntAMap :: IO a +abortSpecIsntAMap = abort $ T.unlines [ line1, line2 ] where line1 = "Cannot use " <> T.pack pathNixSourcesJson line2 = [s| From 3a8c2a83b8dd28fcdd13c055d51df815fb7f90d4 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Sun, 10 Nov 2019 16:00:22 +0100 Subject: [PATCH 3/5] Add comments to examples --- src/Niv/Cli.hs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index 1fe69d1..3a73108 100644 --- a/src/Niv/Cli.hs +++ b/src/Niv/Cli.hs @@ -344,12 +344,14 @@ parseCmdUpdate = desc = [ Opts.fullDesc , Opts.progDesc "Update dependencies" - , Opts.headerDoc $ Just $ + , Opts.headerDoc $ Just $ Opts.nest 2 $ "Examples:" Opts.<$$> "" Opts.<$$> - " niv update" Opts.<$$> - " niv update nixpkgs" Opts.<$$> - " niv update my-package -v beta-0.2" + 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\"" + ] ] specToFreeAttrs :: PackageSpec -> Attrs From 61990bc3a878e6157f58dce88f22daeb5fa4c6de Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Sun, 10 Nov 2019 16:00:31 +0100 Subject: [PATCH 4/5] Remove outdated comment --- src/Niv/Cli.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index 3a73108..cff7695 100644 --- a/src/Niv/Cli.hs +++ b/src/Niv/Cli.hs @@ -360,7 +360,6 @@ specToFreeAttrs = fmap (Free,) . unPackageSpec specToLockedAttrs :: PackageSpec -> Attrs specToLockedAttrs = fmap (Locked,) . unPackageSpec --- TODO: sexy logging + concurrent updates cmdUpdate :: Maybe (PackageName, PackageSpec) -> IO () cmdUpdate = \case Just (packageName, cliSpec) -> From 7820785799f6778773ac555b76abffbc30c6b53f Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Sun, 10 Nov 2019 16:03:33 +0100 Subject: [PATCH 5/5] Update README --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eda7fc7..321008c 100644 --- a/README.md +++ b/README.md @@ -251,10 +251,10 @@ Available options: ``` Examples: - - niv update - niv update nixpkgs - niv update my-package -v beta-0.2 + + niv update # update all packages + niv update nixpkgs # update nixpkgs + niv update my-package -v beta-0.2 # update my-package to version "beta-0.2" Usage: niv update [PACKAGE] ([-a|--attribute KEY=VAL] | [-b|--branch BRANCH] | [-o|--owner OWNER] | [-r|--repo REPO] | [-v|--version VERSION]