Skip to content

Commit

Permalink
Infer project name from directory name; closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
sd234678 committed Mar 6, 2024
1 parent 2a848f4 commit dd017ae
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .last-exported-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Last exported commit from parent repo: a1a2bdf77db95b7be1cfb417fbef33008e7dde63
Last exported commit from parent repo: 00f0a851d215b77e6a116aee22625457ab03df5f
2 changes: 1 addition & 1 deletion nix-bootstrap.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 2.0
-- see: https://github.com/sol/hpack

name: nix-bootstrap
version: 1.5.5.1
version: 1.5.5.2
author: gchquser
maintainer: [email protected]
copyright: Crown Copyright
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
name: nix-bootstrap
version: 1.5.5.1
version: 1.5.5.2
author: gchquser
maintainer: [email protected]
copyright: Crown Copyright
Expand Down
23 changes: 13 additions & 10 deletions src/Bootstrap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ import System.Terminal
withTerminal,
)

-- | The name of the current directory (excluding the rest of its path)
newtype CurrentDirectoryName = CurrentDirectoryName Text

nixBootstrap :: IO ()
nixBootstrap = withTerminal $ runTerminalT do
parseCommand >>= \case
Expand Down Expand Up @@ -196,12 +199,12 @@ nixBootstrap = withTerminal $ runTerminalT do
{ rcNonInteractive = nonInteractive,
rcUseFlakes = useFlakes
}
nixBinaryPaths <- performInitialChecks runConfig
(currentDirectoryName, nixBinaryPaths) <- performInitialChecks runConfig
buildPlan <-
if rcFromScratch runConfig
then do
promptFlakesWarning runConfig
projectName <- promptProjectName
projectName <- promptProjectName currentDirectoryName
preCommitHooksConfig <- promptPreCommitHooksConfig
if useFlakes
then generateIntermediateFlake nixBinaryPaths runConfig projectName
Expand All @@ -225,7 +228,7 @@ nixBootstrap = withTerminal $ runTerminalT do
}
Nothing -> do
promptFlakesWarning runConfig
projectName <- promptProjectName
projectName <- promptProjectName currentDirectoryName
preCommitHooksConfig <- promptPreCommitHooksConfig
if useFlakes
then generateIntermediateFlake nixBinaryPaths runConfig projectName
Expand All @@ -244,7 +247,7 @@ nixBootstrap = withTerminal $ runTerminalT do
CommandVersion -> do
putTextLn $ "nix-bootstrap version " <> toText (showVersion version)

performInitialChecks :: forall m. MonadBootstrap m => RunConfig -> m NixBinaryPaths
performInitialChecks :: forall m. MonadBootstrap m => RunConfig -> m (CurrentDirectoryName, NixBinaryPaths)
performInitialChecks rc@RunConfig {rcUseFlakes} = do
currentDirectoryName <- toText . takeFileName <$> liftIO getCurrentDirectory
when (currentDirectoryName == "nix-bootstrap" || currentDirectoryName == "nix-bootstrap-hs") $
Expand All @@ -263,7 +266,7 @@ performInitialChecks rc@RunConfig {rcUseFlakes} = do
then checkWorkingStateIsClean rc
else offerToInitialiseGitRepo
when rcUseFlakes $ checkNixFlakesAreConfigured nixBinaryPaths
pure nixBinaryPaths
pure (CurrentDirectoryName currentDirectoryName, nixBinaryPaths)
where
checkWorkingStateIsClean :: RunConfig -> m ()
checkWorkingStateIsClean RunConfig {rcAllowDirty} = do
Expand Down Expand Up @@ -345,14 +348,14 @@ promptFlakesWarning RunConfig {rcUseFlakes} = when rcUseFlakes do
exitFailure

-- | Asks the user to enter a project name
promptProjectName :: MonadBootstrap m => m ProjectName
promptProjectName =
promptNonemptyText "Please enter a project name: "
promptProjectName :: MonadBootstrap m => CurrentDirectoryName -> m ProjectName
promptProjectName cdn@(CurrentDirectoryName currentDirectoryName) =
promptNonemptyText (Just currentDirectoryName) "Please enter a project name"
>>= ( \case
Just projectName -> pure projectName
Nothing -> do
putErrorLn "Invalid project name. Project names must begin with a letter and contain only alphanumerical characters, spaces, dashes (-), and underscores(_)."
promptProjectName
promptProjectName cdn
)
. mkProjectName

Expand Down Expand Up @@ -429,7 +432,7 @@ promptProjectType nixBinaryPaths runConfig devContainerConfig = do
False -> pure NoJavaBuild
True ->
SetUpJavaBuild . ArtefactId
<$> promptNonemptyText "Enter your Maven ArtefactId (e.g. the 'demo' in 'com.example.demo'): "
<$> promptNonemptyText Nothing "Enter your Maven ArtefactId (e.g. the 'demo' in 'com.example.demo'): "
pure . Java $ JavaOptions installMinishift installLombok setUpJavaBuild
PSTPython -> pure $ Python Python39
PSTRust -> pure Rust
Expand Down
17 changes: 11 additions & 6 deletions src/Bootstrap/Terminal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,18 @@ handleMultipleChoiceInput state onConfirmation goAgain = \case
scrubLines :: MonadScreen m => Int -> m ()
scrubLines n = moveCursorUp n *> deleteLines n

promptNonemptyText :: MonadBootstrap m => Text -> m Text
promptNonemptyText promptText = do
withAttribute (foreground blue) $ putText promptText
response <- T.strip <$> getFreeText (T.length promptText) initialTextInputState
promptNonemptyText :: MonadBootstrap m => Maybe Text -> Text -> m Text
promptNonemptyText mDefault promptText = do
let fullPromptText = case mDefault of
Just def -> promptText <> " (" <> def <> "):"
Nothing -> promptText
withAttribute (foreground blue) $ putText fullPromptText
response <- T.strip <$> getFreeText (T.length fullPromptText) initialTextInputState
if T.null response
then reprompt "Please enter a value." $ promptNonemptyText promptText
else getConfirmation "entered" response id $ promptNonemptyText promptText
then case mDefault of
Just def -> pure def
Nothing -> reprompt "Please enter a value." $ promptNonemptyText mDefault promptText
else getConfirmation "entered" response id $ promptNonemptyText mDefault promptText

promptYesNo :: MonadBootstrap m => Text -> m Bool
promptYesNo = promptYesNoWithDefault Nothing
Expand Down

0 comments on commit dd017ae

Please sign in to comment.