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

Merge pull request #12 from zimbatm/rename-specs

rename specs.json to sources.json
This commit is contained in:
Nicolas Mattia 2019-02-07 13:46:45 +00:00 committed by GitHub
commit 12dc085e14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 52 deletions

96
Main.hs
View File

@ -50,28 +50,28 @@ parseCommand = Opts.subparser (
Opts.command "update" parseCmdUpdate <>
Opts.command "drop" parseCmdDrop )
newtype Specs = Specs
{ unSpecs :: HMap.HashMap PackageName PackageSpec }
newtype Sources = Sources
{ unSources :: HMap.HashMap PackageName PackageSpec }
deriving newtype (FromJSON, ToJSON)
getSpecs :: IO Specs
getSpecs = do
getSources :: IO Sources
getSources = do
-- TODO: if doesn't exist: run niv init
putStrLn $ "Reading specs file"
decodeFileStrict pathNixSpecsJson >>= \case
putStrLn $ "Reading sources file"
decodeFileStrict pathNixSourcesJson >>= \case
Just (Aeson.Object obj) ->
fmap (Specs . mconcat) $
fmap (Sources . mconcat) $
forM (HMap.toList obj) $ \(k, v) ->
case v of
Aeson.Object v' ->
pure $ HMap.singleton (PackageName (T.unpack k)) (PackageSpec v')
_ -> abortAttributeIsntAMap
Just _ -> abortSpecsIsntAMap
Nothing -> abortSpecsIsntJSON
Just _ -> abortSourcesIsntAMap
Nothing -> abortSourcesIsntJSON
-- TODO: pretty
setSpecs :: Specs -> IO ()
setSpecs specs = encodeFile pathNixSpecsJson specs
setSources :: Sources -> IO ()
setSources sources = encodeFile pathNixSourcesJson sources
newtype PackageName = PackageName { unPackageName :: String }
deriving newtype (Eq, Hashable, FromJSONKey, ToJSONKey, Show)
@ -276,7 +276,7 @@ cmdInit = do
-- Writes all the default files
forM_
[ (pathNixSpecsJson, initNixSpecsJsonContent)
[ (pathNixSourcesJson, initNixSourcesJsonContent)
, (pathNixSourcesNix, initNixSourcesNixContent)
, (pathNixDefaultNix, initNixDefaultNixContent)
, (pathNixOverlayNix, initNixOverlayNixContent)
@ -343,18 +343,18 @@ cmdAdd mPackageName (PackageName str, spec) = do
pure (PackageName repo)
_ -> pure (PackageName str)
specs <- unSpecs <$> getSpecs
sources <- unSources <$> getSources
let packageName' = fromMaybe packageName mPackageName
when (HMap.member packageName' specs) $
when (HMap.member packageName' sources) $
abortCannotAddPackageExists packageName'
spec'' <- updatePackageSpec =<< completePackageSpec spec'
putStrLn $ "Writing new specs file"
setSpecs $ Specs $
HMap.insert packageName' spec'' specs
putStrLn $ "Writing new sources file"
setSources $ Sources $
HMap.insert packageName' spec'' sources
-------------------------------------------------------------------------------
-- SHOW
@ -365,11 +365,11 @@ parseCmdShow = Opts.info (pure cmdShow <**> Opts.helper) Opts.fullDesc
cmdShow :: IO ()
cmdShow = do
putStrLn $ "Showing specs file"
putStrLn $ "Showing sources file"
specs <- unSpecs <$> getSpecs
sources <- unSources <$> getSources
forWithKeyM_ specs $ \key (PackageSpec spec) -> do
forWithKeyM_ sources $ \key (PackageSpec spec) -> do
putStrLn $ "Package: " <> unPackageName key
forM_ (HMap.toList spec) $ \(attrName, attrValValue) -> do
let attrValue = case attrValValue of
@ -402,9 +402,9 @@ cmdUpdate :: Maybe (PackageName, PackageSpec) -> IO ()
cmdUpdate = \case
Just (packageName, packageSpec) -> do
putStrLn $ "Updating single package: " <> unPackageName packageName
specs <- unSpecs <$> getSpecs
sources <- unSources <$> getSources
packageSpec' <- case HMap.lookup packageName specs of
packageSpec' <- case HMap.lookup packageName sources of
Just packageSpec' -> do
-- TODO: something fishy happening here
@ -413,18 +413,18 @@ cmdUpdate = \case
Nothing -> abortCannotUpdateNoSuchPackage packageName
setSpecs $ Specs $
HMap.insert packageName packageSpec' specs
setSources $ Sources $
HMap.insert packageName packageSpec' sources
Nothing -> do
specs <- unSpecs <$> getSpecs
sources <- unSources <$> getSources
specs' <- forWithKeyM specs $
sources' <- forWithKeyM sources $
\packageName packageSpec -> do
putStrLn $ "Package: " <> unPackageName packageName
updatePackageSpec =<< completePackageSpec packageSpec
setSpecs $ Specs specs'
setSources $ Sources sources'
-------------------------------------------------------------------------------
-- DROP
@ -448,13 +448,13 @@ parseCmdDrop =
cmdDrop :: PackageName -> IO ()
cmdDrop packageName = do
putStrLn $ "Dropping package: " <> unPackageName packageName
specs <- unSpecs <$> getSpecs
sources <- unSources <$> getSources
when (not $ HMap.member packageName specs) $
when (not $ HMap.member packageName sources) $
abortCannotDropNoSuchPackage packageName
setSpecs $ Specs $
HMap.delete packageName specs
setSources $ Sources $
HMap.delete packageName sources
-------------------------------------------------------------------------------
-- Aux
@ -547,14 +547,14 @@ nixPrefetchURL url =
pathNixSourcesNix :: FilePath
pathNixSourcesNix = "nix" </> "sources.nix"
-- | Glue code between nix and specs.json
-- | Glue code between nix and sources.json
-- TODO: update this
initNixSourcesNixContent :: String
initNixSourcesNixContent = [s|
# A record, from name to path, of the third-party packages
with
{
versions = builtins.fromJSON (builtins.readFile ./specs.json);
versions = builtins.fromJSON (builtins.readFile ./sources.json);
# fetchTarball version that is compatible between all the versions of Nix
fetchTarball =
@ -629,24 +629,24 @@ pkgs.mkShell
}
|]
-- | @nix/specs.json"
pathNixSpecsJson :: FilePath
pathNixSpecsJson = "nix" </> "specs.json"
-- | @nix/sources.json"
pathNixSourcesJson :: FilePath
pathNixSourcesJson = "nix" </> "sources.json"
-- | Empty JSON map
initNixSpecsJsonContent :: String
initNixSpecsJsonContent = "{}"
initNixSourcesJsonContent :: String
initNixSourcesJsonContent = "{}"
-------------------------------------------------------------------------------
-- Abort
-------------------------------------------------------------------------------
abortSpecsIsntAMap :: IO a
abortSpecsIsntAMap = abort $ unlines [ line1, line2 ]
abortSourcesIsntAMap :: IO a
abortSourcesIsntAMap = abort $ unlines [ line1, line2 ]
where
line1 = "Cannot use " <> pathNixSpecsJson
line1 = "Cannot use " <> pathNixSourcesJson
line2 = [s|
The specs file should be a JSON map from package name to package
The sources file should be a JSON map from package name to package
specification, e.g.:
{ ... }
|]
@ -654,18 +654,18 @@ specification, e.g.:
abortAttributeIsntAMap :: IO a
abortAttributeIsntAMap = abort $ unlines [ line1, line2 ]
where
line1 = "Cannot use " <> pathNixSpecsJson
line1 = "Cannot use " <> pathNixSourcesJson
line2 = [s|
The package specifications in the specs file should be JSON maps from
The package specifications in the sources file should be JSON maps from
attribute name to attribute value, e.g.:
{ "nixpkgs": { "foo": "bar" } }
|]
abortSpecsIsntJSON :: IO a
abortSpecsIsntJSON = abort $ unlines [ line1, line2 ]
abortSourcesIsntJSON :: IO a
abortSourcesIsntJSON = abort $ unlines [ line1, line2 ]
where
line1 = "Cannot use " <> pathNixSpecsJson
line2 = "The specs file should be JSON."
line1 = "Cannot use " <> pathNixSourcesJson
line2 = "The sources file should be JSON."
abortCannotAddPackageExists :: PackageName -> IO a
abortCannotAddPackageExists (PackageName n) = abort $ unlines

View File

@ -1,9 +1,9 @@
# A record, from name to path, of the third-party packages
with
{
specs = builtins.fromJSON (builtins.readFile ./specs.json);
sources = builtins.fromJSON (builtins.readFile ./sources.json);
# fetchTarball version that is compatible between all the specs of Nix
# fetchTarball version that is compatible between all the sources of Nix
fetchTarball =
{ url, sha256 }:
if builtins.lessThan builtins.nixVersion "1.12" then
@ -19,11 +19,11 @@ with
mapAttrs (_: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in specs.json should not have an 'outPath' attribute"
"The values in sources.json should not have an 'outPath' attribute"
else
if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec
then
spec //
{ outPath = fetchTarball { inherit (spec) url sha256; } ; }
else spec
) specs
) sources