Skip to content

Commit

Permalink
Add --source and --set options to --init command, fix #22 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
paf31 authored Jul 12, 2017
1 parent 8b542c3 commit d14c67f
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ data PackageConfig = PackageConfig
pathToTextUnsafe :: Turtle.FilePath -> Text
pathToTextUnsafe = either (error "Path.toText failed") id . Path.toText

defaultPackage :: Version -> PackageName -> PackageConfig
defaultPackage pursVersion pkgName =
PackageConfig { name = pkgName
, depends = [ preludePackageName ]
, set = "psc-" <> pack (showVersion pursVersion)
, source = "https://github.com/purescript/package-sets.git"
}

readPackageFile :: IO PackageConfig
readPackageFile = do
exists <- testfile packageFile
Expand Down Expand Up @@ -213,24 +205,37 @@ getPureScriptVersion = do
echoT "Unable to parse output of purs --version" >> exit (ExitFailure 1)
_ -> echoT "Unexpected output from purs --version" >> exit (ExitFailure 1)

initialize :: IO ()
initialize = do
exists <- testfile "psc-package.json"
when exists $ do
echoT "psc-package.json already exists"
exit (ExitFailure 1)
echoT "Initializing new project in current directory"
pkgName <- packageNameFromPWD . pathToTextUnsafe . Path.filename <$> pwd
pursVersion <- getPureScriptVersion
echoT ("Using the default package set for PureScript compiler version " <>
fromString (showVersion pursVersion))
let pkg = defaultPackage pursVersion pkgName
writePackageFile pkg
updateImpl pkg

initialize :: Maybe (Text, Maybe Text) -> IO ()
initialize setAndSource = do
exists <- testfile "psc-package.json"
when exists $ do
echoT "psc-package.json already exists"
exit (ExitFailure 1)
echoT "Initializing new project in current directory"
pkgName <- packageNameFromPWD . pathToTextUnsafe . Path.filename <$> pwd
pkg <- case setAndSource of
Nothing -> do
pursVersion <- getPureScriptVersion
echoT ("Using the default package set for PureScript compiler version " <>
fromString (showVersion pursVersion))
echoT "(Use --source / --set to override this behavior)"
pure PackageConfig { name = pkgName
, depends = [ preludePackageName ]
, source = "https://github.com/purescript/package-sets.git"
, set = ("psc-" <> pack (showVersion pursVersion))
}
Just (set, source) ->
pure PackageConfig { name = pkgName
, depends = [ preludePackageName ]
, source = fromMaybe "https://github.com/purescript/package-sets.git" source
, set
}

writePackageFile pkg
updateImpl pkg
where
packageNameFromPWD =
either (const untitledPackageName) id . mkPackageName
packageNameFromPWD =
either (const untitledPackageName) id . mkPackageName

install :: String -> IO ()
install pkgName' = do
Expand Down Expand Up @@ -453,7 +458,9 @@ main = do
commands :: Parser (IO ())
commands = (Opts.subparser . fold)
[ Opts.command "init"
(Opts.info (pure initialize)
(Opts.info (initialize <$> optional ((,) <$> (fromString <$> set)
<*> optional (fromString <$> source))
Opts.<**> Opts.helper)
(Opts.progDesc "Initialize a new package"))
, Opts.command "uninstall"
(Opts.info (uninstall <$> pkg Opts.<**> Opts.helper)
Expand All @@ -480,7 +487,7 @@ main = do
(Opts.info (pure listSourcePaths)
(Opts.progDesc "List all (active) source paths for dependencies"))
, Opts.command "available"
(Opts.info (listPackages <$> sorted)
(Opts.info (listPackages <$> sorted Opts.<**> Opts.helper)
(Opts.progDesc "List all packages available in the package set"))
, Opts.command "updates"
(Opts.info (checkForUpdates <$> apply <*> applyMajor Opts.<**> Opts.helper)
Expand All @@ -494,6 +501,14 @@ main = do
Opts.metavar "PACKAGE"
<> Opts.help "The name of the package to install"

source = Opts.strOption $
Opts.long "source"
<> Opts.help "The Git repository for the package set"

set = Opts.strOption $
Opts.long "set"
<> Opts.help "The package set tag name"

apply = Opts.switch $
Opts.long "apply"
<> Opts.help "Apply all minor package updates"
Expand Down

0 comments on commit d14c67f

Please sign in to comment.