Skip to content

Commit dd017ae

Browse files
committed
Infer project name from directory name; closes #4
1 parent 2a848f4 commit dd017ae

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

.last-exported-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Last exported commit from parent repo: a1a2bdf77db95b7be1cfb417fbef33008e7dde63
1+
Last exported commit from parent repo: 00f0a851d215b77e6a116aee22625457ab03df5f

nix-bootstrap.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cabal-version: 2.0
55
-- see: https://github.com/sol/hpack
66

77
name: nix-bootstrap
8-
version: 1.5.5.1
8+
version: 1.5.5.2
99
author: gchquser
1010
maintainer: [email protected]
1111
copyright: Crown Copyright

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
name: nix-bootstrap
15-
version: 1.5.5.1
15+
version: 1.5.5.2
1616
author: gchquser
1717
maintainer: [email protected]
1818
copyright: Crown Copyright

src/Bootstrap.hs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ import System.Terminal
158158
withTerminal,
159159
)
160160

161+
-- | The name of the current directory (excluding the rest of its path)
162+
newtype CurrentDirectoryName = CurrentDirectoryName Text
163+
161164
nixBootstrap :: IO ()
162165
nixBootstrap = withTerminal $ runTerminalT do
163166
parseCommand >>= \case
@@ -196,12 +199,12 @@ nixBootstrap = withTerminal $ runTerminalT do
196199
{ rcNonInteractive = nonInteractive,
197200
rcUseFlakes = useFlakes
198201
}
199-
nixBinaryPaths <- performInitialChecks runConfig
202+
(currentDirectoryName, nixBinaryPaths) <- performInitialChecks runConfig
200203
buildPlan <-
201204
if rcFromScratch runConfig
202205
then do
203206
promptFlakesWarning runConfig
204-
projectName <- promptProjectName
207+
projectName <- promptProjectName currentDirectoryName
205208
preCommitHooksConfig <- promptPreCommitHooksConfig
206209
if useFlakes
207210
then generateIntermediateFlake nixBinaryPaths runConfig projectName
@@ -225,7 +228,7 @@ nixBootstrap = withTerminal $ runTerminalT do
225228
}
226229
Nothing -> do
227230
promptFlakesWarning runConfig
228-
projectName <- promptProjectName
231+
projectName <- promptProjectName currentDirectoryName
229232
preCommitHooksConfig <- promptPreCommitHooksConfig
230233
if useFlakes
231234
then generateIntermediateFlake nixBinaryPaths runConfig projectName
@@ -244,7 +247,7 @@ nixBootstrap = withTerminal $ runTerminalT do
244247
CommandVersion -> do
245248
putTextLn $ "nix-bootstrap version " <> toText (showVersion version)
246249

247-
performInitialChecks :: forall m. MonadBootstrap m => RunConfig -> m NixBinaryPaths
250+
performInitialChecks :: forall m. MonadBootstrap m => RunConfig -> m (CurrentDirectoryName, NixBinaryPaths)
248251
performInitialChecks rc@RunConfig {rcUseFlakes} = do
249252
currentDirectoryName <- toText . takeFileName <$> liftIO getCurrentDirectory
250253
when (currentDirectoryName == "nix-bootstrap" || currentDirectoryName == "nix-bootstrap-hs") $
@@ -263,7 +266,7 @@ performInitialChecks rc@RunConfig {rcUseFlakes} = do
263266
then checkWorkingStateIsClean rc
264267
else offerToInitialiseGitRepo
265268
when rcUseFlakes $ checkNixFlakesAreConfigured nixBinaryPaths
266-
pure nixBinaryPaths
269+
pure (CurrentDirectoryName currentDirectoryName, nixBinaryPaths)
267270
where
268271
checkWorkingStateIsClean :: RunConfig -> m ()
269272
checkWorkingStateIsClean RunConfig {rcAllowDirty} = do
@@ -345,14 +348,14 @@ promptFlakesWarning RunConfig {rcUseFlakes} = when rcUseFlakes do
345348
exitFailure
346349

347350
-- | Asks the user to enter a project name
348-
promptProjectName :: MonadBootstrap m => m ProjectName
349-
promptProjectName =
350-
promptNonemptyText "Please enter a project name: "
351+
promptProjectName :: MonadBootstrap m => CurrentDirectoryName -> m ProjectName
352+
promptProjectName cdn@(CurrentDirectoryName currentDirectoryName) =
353+
promptNonemptyText (Just currentDirectoryName) "Please enter a project name"
351354
>>= ( \case
352355
Just projectName -> pure projectName
353356
Nothing -> do
354357
putErrorLn "Invalid project name. Project names must begin with a letter and contain only alphanumerical characters, spaces, dashes (-), and underscores(_)."
355-
promptProjectName
358+
promptProjectName cdn
356359
)
357360
. mkProjectName
358361

@@ -429,7 +432,7 @@ promptProjectType nixBinaryPaths runConfig devContainerConfig = do
429432
False -> pure NoJavaBuild
430433
True ->
431434
SetUpJavaBuild . ArtefactId
432-
<$> promptNonemptyText "Enter your Maven ArtefactId (e.g. the 'demo' in 'com.example.demo'): "
435+
<$> promptNonemptyText Nothing "Enter your Maven ArtefactId (e.g. the 'demo' in 'com.example.demo'): "
433436
pure . Java $ JavaOptions installMinishift installLombok setUpJavaBuild
434437
PSTPython -> pure $ Python Python39
435438
PSTRust -> pure Rust

src/Bootstrap/Terminal.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,18 @@ handleMultipleChoiceInput state onConfirmation goAgain = \case
200200
scrubLines :: MonadScreen m => Int -> m ()
201201
scrubLines n = moveCursorUp n *> deleteLines n
202202

203-
promptNonemptyText :: MonadBootstrap m => Text -> m Text
204-
promptNonemptyText promptText = do
205-
withAttribute (foreground blue) $ putText promptText
206-
response <- T.strip <$> getFreeText (T.length promptText) initialTextInputState
203+
promptNonemptyText :: MonadBootstrap m => Maybe Text -> Text -> m Text
204+
promptNonemptyText mDefault promptText = do
205+
let fullPromptText = case mDefault of
206+
Just def -> promptText <> " (" <> def <> "):"
207+
Nothing -> promptText
208+
withAttribute (foreground blue) $ putText fullPromptText
209+
response <- T.strip <$> getFreeText (T.length fullPromptText) initialTextInputState
207210
if T.null response
208-
then reprompt "Please enter a value." $ promptNonemptyText promptText
209-
else getConfirmation "entered" response id $ promptNonemptyText promptText
211+
then case mDefault of
212+
Just def -> pure def
213+
Nothing -> reprompt "Please enter a value." $ promptNonemptyText mDefault promptText
214+
else getConfirmation "entered" response id $ promptNonemptyText mDefault promptText
210215

211216
promptYesNo :: MonadBootstrap m => Text -> m Bool
212217
promptYesNo = promptYesNoWithDefault Nothing

0 commit comments

Comments
 (0)