Skip to content

Commit a7d03de

Browse files
authored
refactor codebase (#32)
1 parent 2d5fe13 commit a7d03de

File tree

5 files changed

+47
-56
lines changed

5 files changed

+47
-56
lines changed

src/Options.hs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
{-# LANGUAGE TemplateHaskell #-}
2-
module Options (Options(..), OutputForm(..), Package(..), Packages(Packages), parseCommandLine) where
2+
module Options (Options(..), OutputForm(..), Package(), Packages(Packages), parseCommandLine, setPackages) where
33

4-
import Constants
5-
import FlakeTemplate
6-
import ShellifyTemplate
4+
import Constants (hlDesc, noPackagesError)
75

86
import Control.Lens.Combinators (makeLenses, makePrisms)
97
import Data.Default (Default(def))
10-
import Data.List (isPrefixOf, sort)
11-
import Data.Maybe (fromMaybe, isJust)
12-
import Data.Text (pack, Text(), unpack)
8+
import Data.List (isPrefixOf)
9+
import Data.Maybe (fromMaybe)
10+
import Data.Set (Set(), fromList)
11+
import Data.Text (Text())
1312
import Data.Version (showVersion)
1413
import Options.Applicative ((<**>), Parser, ParserResult(Success, Failure, CompletionInvoked), argument, command, defaultPrefs, execParserPure, fullDesc, header, help, helper, hidden, info, long, metavar, option, optional, progDesc, short, simpleVersioner, some, str, strOption, switch)
1514
import Paths_shellify (version)
16-
import System.Environment (getArgs)
1715

1816
data OutputForm = Traditional
1917
| Flake
2018
deriving (Eq, Show)
2119

22-
newtype Packages = Packages [ Package ] deriving Show
20+
newtype Packages = Packages (Set Package)
21+
deriving (Eq, Monoid, Semigroup, Show)
2322

2423
type Package = Text
2524

26-
instance Eq Packages where
27-
Packages a == Packages b = sort a == sort b
28-
2925
makePrisms ''Packages
26+
setPackages = Packages . fromList
3027

3128
opts = info (commandParser <**> simpleVersioner (showVersion version)
3229
<**> helper) $
@@ -83,8 +80,8 @@ data CommandLineOptions = CommandLineOptions {
8380
makeLenses ''Options
8481

8582
instance Default Options where
86-
def = Options{
87-
_packages = Packages [],
83+
def = Options {
84+
_packages = mempty,
8885
_command = Nothing,
8986
_outputForm = Traditional,
9087
_prioritiseLocalPinnedSystem = False
@@ -99,21 +96,20 @@ parseCommandLine =
9996
. execParserPure defaultPrefs opts . fixupRequest
10097
where parseCommandLineOptions :: CommandLineOptions -> Either Text Options
10198
parseCommandLineOptions originalParsedOptions =
102-
let transformedOptions =
103-
(Options <$> Packages . ((++) <$> fromMaybe [] . __packages
104-
<*> shellArgs . __shellPackages)
105-
<*> __command
106-
<*> \case
107-
f | __withFlake f -> Flake
108-
| (hasShellArg . __shellPackages) f -> Flake
109-
_ | otherwise -> Traditional
110-
<*> __prioritiseLocalPinnedSystem) originalParsedOptions
111-
112-
in if _packages transformedOptions == Packages [] then
113-
Left noPackagesError
114-
else
115-
Right transformedOptions
116-
where hasShellArg (Just ("shell":_)) = True
99+
if _packages transformedOptions == mempty then
100+
Left noPackagesError
101+
else
102+
Right transformedOptions
103+
where transformedOptions =
104+
(Options <$> setPackages . ((++) <$> fromMaybe mempty . __packages
105+
<*> shellArgs . __shellPackages)
106+
<*> __command
107+
<*> \case
108+
f | __withFlake f -> Flake
109+
| (hasShellArg . __shellPackages) f -> Flake
110+
_ | otherwise -> Traditional
111+
<*> __prioritiseLocalPinnedSystem) originalParsedOptions
112+
hasShellArg (Just ("shell":_)) = True
117113
hasShellArg _ = False
118114
shellArgs (Just ("shell": rst)) = rst
119115
shellArgs _ = []

src/Shellify.hs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
module Shellify (printErrorAndReturnFailure, runShellify, calculateExpectedFiles) where
22

33
import Prelude hiding (readFile, writeFile)
4-
import Constants
5-
import FlakeTemplate
6-
import Options
7-
import ShellifyTemplate
8-
import TemplateGeneration
4+
import Options (Options())
5+
import TemplateGeneration ( generateFlakeText, generateShellDotNixText, getRegistryDB)
96

10-
import Control.Monad (when, (>=>))
7+
import Control.Monad (guard, when)
118
import Data.Bool (bool)
129
import Data.Maybe (isNothing)
1310
import Data.Text (pack, Text(), unpack)
@@ -31,10 +28,9 @@ createAFile (name, content) = do extCde <- createFile (unpack name) content
3128

3229
where createFile :: FilePath -> Text -> IO ExitCode
3330
createFile fileName expectedContents = do
34-
fileContents <- doesPathExist fileName
35-
>>= bool
36-
(return Nothing)
37-
(Just <$> readFile fileName)
31+
fileContents <- traverse readFile . bool Nothing
32+
(Just fileName)
33+
=<< doesPathExist fileName
3834
printError $ actionDescription (pack fileName) expectedContents fileContents
3935
when (isNothing fileContents)
4036
$ writeFile fileName expectedContents

src/TemplateGeneration.hs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
module TemplateGeneration (generateShellDotNixText, generateFlakeText, getRegistryDB) where
22

3+
import Prelude hiding (map)
34
import Constants
45
import FlakeTemplate
56
import Options
67
import ShellifyTemplate
78

89
import Data.Bifunctor (bimap)
910
import Data.Bool (bool)
10-
import Data.List (find, sort, sortBy, sortOn)
11+
import Data.List (find, sortBy, sortOn)
1112
import Data.Maybe (fromMaybe)
12-
import Data.Set (fromList, toList)
13+
import Data.Set (Set(), insert, map, toList)
1314
import Data.Text (Text(), isInfixOf, isPrefixOf, pack, splitOn, unpack)
1415
import Development.Shake.Command (cmd, Exit(Exit), Stderr(Stderr), Stdout(Stdout))
1516
import System.Exit (ExitCode (ExitSuccess))
@@ -50,12 +51,12 @@ generateShellDotNixText Options{_packages=Packages packages, _command=command} =
5051
(setAttribute "shell_hook")
5152
command
5253
$ newSTMP shellifyTemplate
53-
where pkgs = generateBuildInput <$> sort packages
54+
where pkgs = map generateBuildInput packages
5455
parameters = generateParametersWrapper packages
5556
generateBuildInput input = (toImportVar . getPackageRepo) input <> "." <> getPackageName input
5657

57-
getPackageRepoWrapper :: [Package] -> [Text]
58-
getPackageRepoWrapper = uniq . ("nixpkgs" :) . fmap getPackageRepo . sort
58+
getPackageRepoWrapper :: Set Package -> [Text]
59+
getPackageRepoWrapper = toList . insert "nixpkgs" . map getPackageRepo
5960

6061
getPackageRepo input | "#" `isInfixOf` input
6162
= head $ splitOn "#" input
@@ -75,18 +76,16 @@ toImportVar var | var == "nixpkgs"
7576
getPackageRepoVarName "nixpkgs" = "pkgs"
7677
getPackageRepoVarName a = a
7778

78-
generateParametersWrapper :: [Package] -> [Text]
79-
generateParametersWrapper = uniq . ("pkgs ? import <nixpkgs> {}" :) . fmap generateParameters . sort
79+
generateParametersWrapper :: Set Package -> Set Text
80+
generateParametersWrapper = insert "pkgs ? import <nixpkgs> {}"
81+
. map generateParameters
8082

8183
generateParameters :: Package -> Text
8284
generateParameters package | "#" `isInfixOf` package
8385
&& not ("nixpkgs#" `isPrefixOf` package)
8486
= getPackageRepo package
8587
generateParameters _ = "pkgs ? import <nixpkgs> {}"
8688

87-
uniq :: Ord a => [a] -> [a]
88-
uniq = toList . fromList
89-
9089
getRegistryDB :: IO (Either Text Text)
9190
getRegistryDB =
9291
do (Stdout out, Stderr err, Exit ex) <- cmd

test/Spec.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ main = hspec $ do
3232
it "allows a command to be specified with a package" $
3333
theOptions "-p python --command cowsay"
3434
`shouldBe`
35-
Right def{_packages=Packages ["python"], _command=Just "cowsay"}
35+
Right def{_packages=setPackages ["python"], _command=Just "cowsay"}
3636

3737
it "allows a command to be specified before a package" $
3838
theOptions "--run cowsay -p python"
3939
`shouldBe`
40-
Right def{_packages=Packages ["python"], _command=Just "cowsay"}
40+
Right def{_packages=setPackages ["python"], _command=Just "cowsay"}
4141

4242
it "allows a command to be specified before and after a package" $
4343
theOptions "-p cowsay --command cowsay -p python"
4444
`shouldBe`
45-
Right def{_packages=Packages [ "cowsay", "python" ], _command=Just "cowsay"}
45+
Right def{_packages=setPackages [ "cowsay", "python" ], _command=Just "cowsay"}
4646

4747
it "fails if command has no argument" $
4848
shellifyWithArgs "--command"
@@ -81,12 +81,12 @@ main = hspec $ do
8181
it "supports new shell commands" $
8282
theOptions "shell nixpkgs#python nixpkgs#cowsay"
8383
`shouldBe`
84-
Right def{_packages=Packages [ "nixpkgs#python", "nixpkgs#cowsay" ], _outputForm=Flake}
84+
Right def{_packages=setPackages [ "nixpkgs#python", "nixpkgs#cowsay" ], _outputForm=Flake}
8585

8686
it "supports the --with-flake option" $
8787
theOptions "--with-flake -p python -p cowsay"
8888
`shouldBe`
89-
Right def{_packages=Packages [ "python", "cowsay" ], _outputForm=Flake}
89+
Right def{_packages=setPackages [ "python", "cowsay" ], _outputForm=Flake}
9090

9191
describe "When dealing with multiple source repositories it should produce the correct output files for" $ do
9292

test/TestHelpers.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Options.Applicative.Help.Pretty (prettyString)
1212
import Options.Applicative.Help.Types (ParserHelp(helpError))
1313
import Options.Applicative.Extra (ParserFailure(execFailure))
1414

15-
import Options (Options(..), Packages(Packages), parseCommandLine)
15+
import Options (Options(..), Packages(Packages), parseCommandLine, setPackages)
1616
import Shellify ( calculateExpectedFiles )
1717

1818
shouldReturnSubstring :: Either Text b -> [Char] -> Expectation
@@ -155,7 +155,7 @@ shouldResultInPackages :: Text -> [Text] -> Expectation
155155
shouldResultInPackages parameters packages =
156156
theOptions parameters
157157
`shouldBe`
158-
Right def{_packages=Packages packages}
158+
Right def{_packages=setPackages packages}
159159

160160
readNixTemplate :: FilePath -> IO Text
161161
readNixTemplate = readFile . ("test/outputs/" <>)

0 commit comments

Comments
 (0)