diff --git a/src/Options.hs b/src/Options.hs index da7f031..af74f91 100644 --- a/src/Options.hs +++ b/src/Options.hs @@ -1,4 +1,4 @@ -module Options (Package(..), Options(..), def, Packages, options) where +module Options (Package(..), Options(..), OutputForm(..), def, Packages(Packages), options) where import Prelude hiding (takeWhile, writeFile) import Constants @@ -23,14 +23,27 @@ import System.Exit (exitWith) import System.IO (stderr) import Text.StringTemplate (newSTMP, render, setAttribute, StringTemplate) +data OutputForm = Traditional + | Flake + deriving (Eq, Show) + +newtype Packages = Packages [ Package ] deriving Show + + type Package = Text -type Packages = [ Package ] + +instance Eq Packages where + Packages a == Packages b = sort a == sort b + +packageList = toPackageList . packages + where toPackageList (Packages p) = p + data Options = Options { packages :: Packages , command :: Maybe Text - , generateFlake :: Bool + , outputForm :: !OutputForm , prioritiseLocalPinnedSystem :: Bool -} +} deriving (Eq, Show) data OptionsParser = OptionsParser [Text] -- remainingOptions (Either Text (Options -> Options)) -- result @@ -47,7 +60,7 @@ options progName args = let (OptionsParser newRemaining newRes) = f hd tl in worker $ OptionsParser newRemaining ((.) <$> newRes <*> res) - screenForNoPackages (Right opts) | null (packages opts) = Left noPackagesError + screenForNoPackages (Right opts) | null (packageList opts) = Left noPackagesError screenForNoPackages anyThingElse = anyThingElse initialArgumentsToParse = shellArgFilter args initialModifier = Right $ if hasShellArg args then setFlakeGeneration else id @@ -66,7 +79,7 @@ options progName args = baseOption :: Text -> [Text] -> OptionsParser baseOption "-h" = returnError $ helpText progName baseOption "--help" = returnError $ helpText progName - baseOption "--version" = returnError $ "Shellify " <> (pack $ showVersion version) + baseOption "--version" = returnError $ "Shellify " <> pack ( showVersion version) baseOption "--command" = handleCommandSwitch baseOption "--run" = handleCommandSwitch baseOption "--with-flake" = transformOptionsWith setFlakeGeneration @@ -81,13 +94,15 @@ options progName args = = transformOptionsWith (setCommand hd) tl handleCommandSwitch [] = returnError "Argument missing to switch" [] - appendPackages ps opts = opts{packages=ps ++ packages opts} + appendPackages ps opts = opts{ + packages = Packages (ps ++ packageList opts) + } setCommand cmd opts = opts{command=Just cmd} - setFlakeGeneration opts = opts{generateFlake=True} + setFlakeGeneration opts = opts{outputForm=Flake} setPrioritiseLocalPinnedSystem opts = opts {prioritiseLocalPinnedSystem=True} returnError errorText remaining = OptionsParser remaining $ Left errorText -consumePackageArgs :: [Text] -> (Packages, [Text]) +consumePackageArgs :: [Text] -> ([Package], [Text]) consumePackageArgs = worker [] where worker pkgs [] = (pkgs, []) worker pkgs options@(hd:_) | isSwitch hd @@ -102,10 +117,4 @@ hasShellArg (hd:tl) | isSwitch hd = hasShellArg tl isSwitch = isPrefixOf "-" instance Default Options where - def = Options [] Nothing False False - -instance Eq Options where - a == b = isEqual command - && isEqual (sort . packages) - && isEqual generateFlake - where isEqual f = f a == f b + def = Options (Packages []) Nothing Traditional False diff --git a/src/TemplateGeneration.hs b/src/TemplateGeneration.hs index 3d42b5f..1574120 100644 --- a/src/TemplateGeneration.hs +++ b/src/TemplateGeneration.hs @@ -17,7 +17,7 @@ import Text.ParserCombinators.Parsec (Parser, char, endBy, eof, many1, noneOf, p import Text.StringTemplate (newSTMP, render, setAttribute) generateFlakeText :: Text -> Options -> Maybe Text -generateFlakeText db Options{packages=packages, generateFlake=shouldGenerateFlake, prioritiseLocalPinnedSystem=prioritiseLocalPinnedSystem} = +generateFlakeText db Options{packages=Packages packages, outputForm=outputForm, prioritiseLocalPinnedSystem=prioritiseLocalPinnedSystem} = bool Nothing (Just $ render @@ -26,7 +26,7 @@ generateFlakeText db Options{packages=packages, generateFlake=shouldGenerateFlak $ setAttribute "pkgs_decls" pkgsDecls $ setAttribute "shell_args" shellArgs $ newSTMP flakeTemplate) - shouldGenerateFlake + (outputForm == Flake) where repos = getPackageRepoWrapper packages repoVars = getPackageRepoVarName <$> repos repoInputs = repoInput <$> repos @@ -42,7 +42,7 @@ generateFlakeText db Options{packages=packages, generateFlake=shouldGenerateFlak shellArgs = (\(a,b) -> a <> "=" <> b <> ";") <$> zip repoVars pkgsVars generateShellDotNixText :: Options -> Text -generateShellDotNixText Options{packages=packages, command=command} = +generateShellDotNixText Options{packages=Packages packages, command=command} = render $ setAttribute "build_inputs" pkgs $ setAttribute "parameters" parameters diff --git a/test/Spec.hs b/test/Spec.hs index ca6c4bf..45d048a 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -45,17 +45,17 @@ main = hspec $ do it "allows a command to be specified with a package" $ theOptions "-p python --command cowsay" `shouldBe` - Right def{packages=["python"], command=Just "cowsay"} + Right def{packages=Packages ["python"], command=Just "cowsay"} it "allows a command to be specified before a package" $ theOptions "--run cowsay -p python" `shouldBe` - Right def{packages=["python"], command=Just "cowsay"} + Right def{packages=Packages ["python"], command=Just "cowsay"} it "allows a command to be specified before and after a package" $ theOptions "-p cowsay --command cowsay -p python" `shouldBe` - Right def{packages=[ "cowsay", "python" ], command=Just "cowsay"} + Right def{packages=Packages [ "cowsay", "python" ], command=Just "cowsay"} it "fails if command has no argument" $ do shellifyWithArgs "--command -p python" @@ -96,7 +96,7 @@ main = hspec $ do it "supports new shell commands" $ theOptions "shell nixpkgs#python nixpkgs#cowsay" `shouldBe` - Right def{packages=[ "nixpkgs#python", "nixpkgs#cowsay" ], generateFlake=True} + Right def{packages=Packages [ "nixpkgs#python", "nixpkgs#cowsay" ], outputForm=Flake} describe "When dealing with multiple source repositories it should produce the correct output files for" $ do diff --git a/test/TestHelpers.hs b/test/TestHelpers.hs index 2b928c8..f75dbcf 100644 --- a/test/TestHelpers.hs +++ b/test/TestHelpers.hs @@ -129,12 +129,10 @@ shouldResultInPackages :: Text -> [Text] -> Expectation shouldResultInPackages parameters packages = theOptions parameters `shouldBe` - Right def{packages=packages} + Right def{packages=Packages packages} theOptions = options "nix-shellify" . words -instance Show Options - readNixTemplate :: FilePath -> IO Text readNixTemplate fileName = stripTrailingNewline <$> readFile ("test/outputs/" <> fileName)