1
1
mirror of https://github.com/nmattia/niv.git synced 2024-11-07 22:36:53 +03:00

Implement basic CLI

This commit is contained in:
Nicolas Mattia 2019-01-17 23:00:48 +01:00
parent ca1d6a681a
commit f3cc2718e1
6 changed files with 156 additions and 6 deletions

73
Main.hs Normal file
View File

@ -0,0 +1,73 @@
-- TODO: qualified imports
-- TODO: format code
import Options.Applicative
import Control.Monad
import Data.Semigroup ((<>))
import System.Directory
import System.FilePath
fileFetchNix :: FilePath
fileFetchNix = "nix" </> "fetch.nix"
fileFetchNixContent :: String
fileFetchNixContent = unlines
[
]
fileVersionsJson :: FilePath
fileVersionsJson = "nix" </> "versions.json"
fileVersionsJsonContent :: String
fileVersionsJsonContent = unlines
[
]
cmdInit :: IO ()
cmdInit = do
putStrLn "Creating directory nix (if it doesn't exist)"
createDirectoryIfMissing True "nix"
putStrLn $ "Creating file " <> fileFetchNix <> " (if it doesn't exist)"
fileFetchNixExists <- doesFileExist fileFetchNix
if fileFetchNixExists
then do
putStrLn $ "Not writing " <> fileFetchNix
putStrLn "(file exists)"
else do
putStrLn $ "Writing " <> fileFetchNix
writeFile fileFetchNix fileFetchNixContent
putStrLn $ "Creating file " <> fileVersionsJson <> " (if it doesn't exist)"
fileVersionsJsonExists <- doesFileExist fileVersionsJson
if fileVersionsJsonExists
then do
putStrLn $ "Not writing " <> fileVersionsJson
putStrLn "(file exists)"
else do
putStrLn $ "Writing " <> fileVersionsJson
writeFile fileVersionsJson fileVersionsJsonContent
cmdAdd :: IO ()
cmdAdd = putStrLn "add"
cmdShow :: IO ()
cmdShow = putStrLn "show"
cmdUpdate :: IO ()
cmdUpdate = putStrLn "update"
opts :: Parser (IO ())
opts = subparser (
command "init" (info (pure cmdInit) idm) <>
command "add" (info (pure cmdAdd) idm) <>
command "show" (info (pure cmdAdd) idm) <>
command "update" (info (pure cmdAdd) idm) )
main :: IO ()
main = join $ execParser (info opts idm)

View File

@ -2,6 +2,17 @@
A tool for dealing with third-party packages in [Nix].
## Building
Inside the provided nix shell:
``` bash
$ # GHCi:
$ snack ghci -p package.yaml
$ # actual build:
$ snack build -p package.yaml
```
## Usage
### Global options
@ -39,11 +50,11 @@ in pkgs.hello
#### show
`[--branch] [--rev] [--owner] [--repo] <p1> <p2>`... if no attribute
(br, rev, own, repo) is given, all attributes are shown for `<packages>`.
Otherwise the specified attributes are shown. If no package is specified:
`<packages> = <all packages>`, otherwise `<packages>` is set to the specified
packages.
`[--branch] [--rev] [--owner] [--repo] [--attribute <attribute>] <p1> <p2>`...
if no attribute (br, rev, ...) is given, all attributes are shown for
`<packages>`. Otherwise the specified attributes are shown. If no package is
specified: `<packages> = <all packages>`, otherwise `<packages>` is set to
the specified packages.
#### update
@ -69,9 +80,10 @@ in pkgs.hello
}
```
* `--branch`: specifies `<branch>`
* `--branch`: specifies `<branch>` (default: master)
* `--username <username>`: then `let <repo> = <package>`
* `--gitlab`: use gitlab instead of GitHub
* `--attribute <attribute> <value>`: sets `<attribute>` to `<value>`
**NOTE**: should the URLs be used instead? or more simply, how do we differentiate between Gitlab/GitHub?

17
default.nix Normal file
View File

@ -0,0 +1,17 @@
with { fetch = import ./nix/fetch.nix; };
let
# The (pinned) Nixpkgs where the original packages are sourced from
pkgs = import fetch.nixpkgs {};
# The list of packages to be installed
shellPackages = [ snack-exe ];
snack-exe = (import fetch.snack).snack-exe;
snack-lib = (import fetch.snack).snack-lib;
in
if pkgs.lib.inNixShell
then pkgs.mkShell
{ buildInputs = shellPackages;
}
else snack-lib.inferHPackBuild ./package.yaml

23
nix/fetch.nix Normal file
View File

@ -0,0 +1,23 @@
# A record, from name to path, of the third-party packages
let
versions = builtins.fromJSON (builtins.readFile ./versions.json);
fetchTarball =
# fetchTarball version that is compatible between all the versions of
# Nix
{ url, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball { inherit url; }
else
fetchTarball attrs;
in
builtins.mapAttrs (_: spec:
fetchTarball {
url =
with spec;
"https://github.com/${owner}/${repo}/archive/${rev}.tar.gz";
sha256 = spec.sha256;
}
) versions

16
nix/versions.json Normal file
View File

@ -0,0 +1,16 @@
{
"nixpkgs": {
"owner": "NixOS",
"repo": "nixpkgs-channels",
"branch": "nixos-18.09",
"rev": "c2950341d038995bf46a7b72db961bb3d3e9ac12",
"sha256": "1adpvp20b3zwxchnvkag8d0m7qlnwxv3l9z1lg96kr5ffwk37prv"
},
"snack": {
"owner": "nmattia",
"repo": "snack",
"branch": "master",
"rev": "9754f4120d28ce25888a608606deddef9f490654",
"sha256": "1a2gcap2z41vk3d7cqydj880lwcpxljd49w6srb3gplciipha6zv"
}
}

9
package.yaml Normal file
View File

@ -0,0 +1,9 @@
name: niv
executable:
source-dirs: . # remove when https://github.com/nmattia/snack/pull/96 is merged
main: Main.hs
dependencies:
- directory
- filepath
- optparse-applicative