diff --git a/ChangeLog.md b/ChangeLog.md index dcf0851641..cbf14d67bb 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -12,6 +12,9 @@ Behavior changes: Other enhancements: +* Add flag `--no-init` to Stack's `new` command to skip the initialisation of + the newly-created project for use with Stack. + Bug fixes: ## v2.13.1 - 2023-09-29 diff --git a/doc/new_command.md b/doc/new_command.md index c7a01ffa52..7bf87d7981 100644 --- a/doc/new_command.md +++ b/doc/new_command.md @@ -3,21 +3,27 @@ # The `stack new` command ~~~text -stack new PACKAGE_NAME [--bare] [TEMPLATE_NAME] [-p|--param KEY:VALUE] [DIR(S)] - [--omit-packages] [--force] [--ignore-subdirs] +stack new PACKAGE_NAME [--bare] [--[no-]init] [TEMPLATE_NAME] + [-p|--param KEY:VALUE] [DIR(S)] [--omit-packages] [--force] + [--ignore-subdirs] ~~~ -`stack new` creates a new Stack project for a package using a project template. +`stack new` creates a new project using a project template. -The project is created in a new directory named after the package, unless the -`--bare` flag is passed, in which case the project is created in the current -directory. +By default: + +* the project is created in a new directory named after the package. Pass the + `--bare` flag to create the project in the current directory; and + +* the project is initialised for use with Stack. Pass the `--no-init` flag to + skip such initialisation. The `--param :` option specifies a key-value pair to populate a key in a template. The option can be specified multiple times. The arguments specifying directories and the `--ignore-subdirs`, `--force` and `--omit-packages` flags are as for the [`stack init` command](init_command.md). +These arguments are ignored if the `--no-init` flag is passed. ## Project templates @@ -45,49 +51,60 @@ the default project template name is `new-template`. ## Examples Create a project for package `my-project` in new directory `my-project` with the -default project template file: +default project template file and initialise it for use with Stack: ~~~text stack new my-project ~~~ Create a project for package `my-package` in the current directory with the -default project template file: +default project template file and initialise it for use with Stack: ~~~text stack new my-package --bare ~~~ -Create a project with the `rio` project template at the default repository: +Create a project with the `rio` project template at the default repository and +initialise it for use with Stack: ~~~text stack new my-project rio ~~~ Create a project with the `mysql` project template provided by the -`yesodweb/stack-templates` repository on GitHub: +`yesodweb/stack-templates` repository on GitHub and initialise it for use with +Stack: ~~~text stack new my-project yesodweb/mysql ~~~ Create a project with the `my-template` project template provided by the -`username/stack-templates` repository on Bitbucket: +`username/stack-templates` repository on Bitbucket and initialise it for use +with Stack: ~~~text stack new my-project bitbucket:username/my-template ~~~ Create a project with the `my-template.hsfiles` project template file at -`https://example.com`: +`https://example.com` and initialise it for use with Stack: ~~~text stack new my-project https://example.com/my-template ~~~ Create a project with the local project template file -`/my-template.hsfiles`: +`/my-template.hsfiles` and initialise it for use with Stack: ~~~text stack new my-project /my-template ~~~ + +Create a project with the `simple` project template file at the default +repository (which does not use Hpack and a `package.yaml` file) and do not +initialise it for use with Stack (`stack init` could be used subsequently): + +~~~text +stack new my-project --no-init simple +~~~ diff --git a/src/Stack/New.hs b/src/Stack/New.hs index 2488ef4b87..1fbf42fc2d 100644 --- a/src/Stack/New.hs +++ b/src/Stack/New.hs @@ -215,13 +215,15 @@ instance Exception NewPrettyException -- than those applicable also to the @stack init@ command). data NewOpts = NewOpts { newOptsProjectName :: PackageName - -- ^ Name of the project to create. + -- ^ Name of the project to create. , newOptsCreateBare :: Bool - -- ^ Whether to create the project without a directory. + -- ^ Whether to create the project without a directory. + , newOptsInit :: Bool + -- ^ Whether to initialise the project for use with Stack. , newOptsTemplate :: Maybe TemplateName - -- ^ Name of the template to use. + -- ^ Name of the template to use. , newOptsNonceParams :: Map Text Text - -- ^ Nonce parameters specified just for this invocation. + -- ^ Nonce parameters specified just for this invocation. } -- | Function underlying the @stack new@ command. Create a project directory @@ -231,7 +233,7 @@ newCmd (newOpts, initOpts) = withGlobalProject $ withConfig YesReexec $ do dir <- new newOpts (forceOverwrite initOpts) exists <- doesFileExist $ dir stackDotYaml - when (forceOverwrite initOpts || not exists) $ do + when (newOptsInit newOpts && (forceOverwrite initOpts || not exists)) $ do go <- view globalOptsL initProject dir initOpts (globalResolver go) diff --git a/src/Stack/Options/NewParser.hs b/src/Stack/Options/NewParser.hs index 3c9c0f589d..bc0925e9c2 100644 --- a/src/Stack/Options/NewParser.hs +++ b/src/Stack/Options/NewParser.hs @@ -6,7 +6,8 @@ module Stack.Options.NewParser import qualified Data.Map.Strict as M import Options.Applicative - ( Parser, help, long, metavar, short, switch ) + ( Parser, help, idm, long, metavar, short, switch ) +import Options.Applicative.Builder.Extra ( boolFlags ) import Stack.Init ( InitOpts ) import Stack.New ( NewOpts (..) ) import Stack.Options.InitParser ( initOptsParser ) @@ -28,6 +29,10 @@ newOptsParser = (,) <$> newOpts <*> initOptsParser ( long "bare" <> help "Do not create a subdirectory for the project." ) + <*> boolFlags True + "init" + "the initialisation of the project for use with Stack." + idm <*> optional (templateNameArgument ( metavar "TEMPLATE_NAME" <> help "Name of a template - can take the form\