Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/Options.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
6 changes: 3 additions & 3 deletions src/TemplateGeneration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions test/TestHelpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down