Skip to content

Commit

Permalink
Add niv add local for local sources
Browse files Browse the repository at this point in the history
  • Loading branch information
nmattia committed Mar 31, 2020
1 parent 5b5508f commit f73bf8d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ Available options:
Experimental commands:
git Add a git dependency. Experimental.
github Add a GitHub dependency
local Add a local dependency. Experimental.
```

#### Update
Expand Down
1 change: 1 addition & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ with rec
"^src/Niv$"
"^src/Niv/Git$"
"^src/Niv/GitHub$"
"^src/Niv/Local$"
"^src/Niv/Sources$"
"^src/Niv/Update$"
"^src.*.hs$"
Expand Down
3 changes: 3 additions & 0 deletions nix/sources.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ let
fetch_git = spec:
builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };

fetch_local = spec: spec.path;

fetch_builtin-tarball = name: throw
''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=tarball -a builtin=true'';
Expand Down Expand Up @@ -65,6 +67,7 @@ let
else if spec.type == "file" then fetch_file pkgs spec
else if spec.type == "tarball" then fetch_tarball pkgs name spec
else if spec.type == "git" then fetch_git spec
else if spec.type == "local" then fetch_local spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
else if spec.type == "builtin-url" then fetch_builtin-url name
else
Expand Down
8 changes: 7 additions & 1 deletion src/Niv/Cli.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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.Logger
import Niv.Sources
Expand Down Expand Up @@ -190,13 +191,16 @@ parseCmdAdd =
parseCmd cmd = uncurry (cmdAdd (updateCmd cmd)) <$> (parseCmdArgs cmd)
parseCmdAddGit =
Opts.info (parseCmd gitCmd <**> Opts.helper) (description gitCmd)
parseCmdAddLocal =
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 "github" parseCmdAddGitHub <>
Opts.command "local" parseCmdAddLocal
)

-- | only used in shortcuts (niv add foo/bar ...) because PACKAGE is NOT
Expand Down Expand Up @@ -341,6 +345,7 @@ cmdUpdate = \case
-- github
let cmd = case HMS.lookup "type" (unPackageSpec defaultSpec) of
Just "git" -> gitCmd
Just "local" -> localCmd
_ -> githubCmd
fmap attrsToSpec <$> li (tryEvalUpdate
(specToLockedAttrs cliSpec <> specToFreeAttrs defaultSpec)
Expand All @@ -366,6 +371,7 @@ cmdUpdate = \case
-- github
let cmd = case HMS.lookup "type" (unPackageSpec defaultSpec) of
Just "git" -> gitCmd
Just "local" -> localCmd
_ -> githubCmd
finalSpec <- fmap attrsToSpec <$> li (tryEvalUpdate
initialSpec
Expand Down
59 changes: 59 additions & 0 deletions src/Niv/Local/Cmd.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE Arrows #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}

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 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
useOrSet "type" -< ("local" :: Box T.Text)
returnA -< ()
, name = "local"
}

parseLocalShortcut :: T.Text -> Maybe (PackageName, Aeson.Object)
parseLocalShortcut txt =
if (T.isPrefixOf "./" txt || T.isPrefixOf "/" txt ) then do
let n = last $ T.splitOn "/" txt
Just (PackageName n, HMS.fromList [ ("path", Aeson.String txt) ])
else Nothing

parseLocalPackageSpec :: Opts.Parser PackageSpec
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"
)

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"
]
4 changes: 3 additions & 1 deletion src/Niv/Sources.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ data SourcesNixVersion
| V15
| V16
| V17
-- prettify derivation name
-- add 'local' type of sources
| V18
deriving stock (Bounded, Enum, Eq)

Expand Down Expand Up @@ -201,7 +203,7 @@ sourcesVersionToMD5 = \case
V15 -> "dc11af910773ec9b4e505e0f49ebcfd2"
V16 -> "2d93c52cab8e960e767a79af05ca572a"
V17 -> "149b8907f7b08dc1c28164dfa55c7fad"
V18 -> "5fec8217a6e712c817f9de5289d85fbe"
V18 -> "bc5e6aefcaa6f9e0b2155ca4f44e5a33"

-- | The MD5 sum of ./nix/sources.nix
sourcesNixMD5 :: IO T.Text
Expand Down

0 comments on commit f73bf8d

Please sign in to comment.