Skip to content

Commit

Permalink
Add non-interactive version of juvix init
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcadman committed Nov 6, 2023
1 parent 511e99f commit de3f9ef
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
44 changes: 31 additions & 13 deletions app/Commands/Init.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Commands.Init where

import Commands.Init.Options
import Data.Text qualified as Text
import Data.Text.IO.Utf8 qualified as Utf8
import Data.Versions
Expand All @@ -21,16 +22,32 @@ parse p t = mapLeft ppErr (P.runParser p "<stdin>" t)
ppErr :: P.ParseErrorBundle Text Void -> Text
ppErr = pack . errorBundlePretty

init :: forall r. (Members '[Embed IO] r) => Sem r ()
init = do
init :: forall r. (Members '[Embed IO] r) => InitOptions -> Sem r ()
init opts = do
checkNotInProject
say "✨ Your next Juvix adventure is about to begin! ✨"
say "I will help you set it up"
pkg <- getPackage
say ("creating " <> pack (toFilePath packageFilePath))
embed (Utf8.writeFile @IO (toFilePath packageFilePath) (renderPackageVersion PackageVersion1 pkg))
pkg <-
if
| isInteractive -> do
say "✨ Your next Juvix adventure is about to begin! ✨"
say "I will help you set it up"
getPackage
| otherwise -> do
cwd <- getCurrentDir
projectName <- getDefaultProjectName
let emptyPkg = emptyPackage DefaultBuildDir (cwd <//> packageFilePath)
return $ case projectName of
Nothing -> emptyPkg
Just n -> emptyPkg {_packageName = n}
when isInteractive (say ("creating " <> pack (toFilePath packageFilePath)))
writePackage pkg
checkPackage
say "you are all set"
when isInteractive (say "you are all set")
where
writePackage :: Package -> Sem r ()
writePackage pkg = embed (Utf8.writeFile @IO (toFilePath packageFilePath) (renderPackageVersion PackageVersion1 pkg))

isInteractive :: Bool
isInteractive = not (opts ^. initOptionsNonInteractive)

checkNotInProject :: forall r. (Members '[Embed IO] r) => Sem r ()
checkNotInProject =
Expand Down Expand Up @@ -68,9 +85,14 @@ getPackage = do
_packageLockfile = Nothing
}

getDefaultProjectName :: (Member (Embed IO) r) => Sem r (Maybe Text)
getDefaultProjectName = runFail $ do
dir <- map toLower . dropTrailingPathSeparator . toFilePath . dirname <$> getCurrentDir
Fail.fromRight (parse projectNameParser (pack dir))

getProjName :: forall r. (Members '[Embed IO] r) => Sem r Text
getProjName = do
d <- getDefault
d <- getDefaultProjectName
let defMsg :: Text
defMsg = case d of
Nothing -> mempty
Expand All @@ -82,10 +104,6 @@ getProjName = do
)
readName d
where
getDefault :: Sem r (Maybe Text)
getDefault = runFail $ do
dir <- map toLower . dropTrailingPathSeparator . toFilePath . dirname <$> getCurrentDir
Fail.fromRight (parse projectNameParser (pack dir))
readName :: Maybe Text -> Sem r Text
readName def = go
where
Expand Down
19 changes: 19 additions & 0 deletions app/Commands/Init/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Commands.Init.Options where

import CommonOptions

newtype InitOptions = InitOptions
{_initOptionsNonInteractive :: Bool}
deriving stock (Data)

makeLenses ''InitOptions

parseInitOptions :: Parser InitOptions
parseInitOptions = do
_initOptionsNonInteractive <-
switch
( long "non-interactive"
<> short 'n'
<> help "Run non-interactively. Generates a default Package.juvix"
)
pure InitOptions {..}
2 changes: 1 addition & 1 deletion app/TopCommand.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ runTopCommand = \case
DisplayNumericVersion -> embed runDisplayNumericVersion
DisplayHelp -> embed showHelpText
Doctor opts -> runLogIO (Doctor.runCommand opts)
Init -> runLogIO Init.init
Init opts -> runLogIO (Init.init opts)
Dev opts -> Dev.runCommand opts
Typecheck opts -> Typecheck.runCommand opts
Compile opts -> Compile.runCommand opts
Expand Down
5 changes: 3 additions & 2 deletions app/TopCommand/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Commands.Doctor.Options
import Commands.Eval.Options
import Commands.Format.Options
import Commands.Html.Options
import Commands.Init.Options
import Commands.Repl.Options
import Commands.Typecheck.Options
import CommonOptions hiding (Doc)
Expand All @@ -26,7 +27,7 @@ data TopCommand
| Html HtmlOptions
| Dev Dev.DevCommand
| Doctor DoctorOptions
| Init
| Init InitOptions
| JuvixRepl ReplOptions
| JuvixFormat FormatOptions
| Dependencies Dependencies.DependenciesCommand
Expand Down Expand Up @@ -108,7 +109,7 @@ parseUtility =
command
"init"
( info
(pure Init)
(Init <$> parseInitOptions)
(progDesc "Interactively initialize a Juvix project in the current directory")
)
commandDoctor :: Mod CommandFields TopCommand
Expand Down
15 changes: 15 additions & 0 deletions tests/smoke/Commands/init.smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ tests:
stdout:
contains: type checks
exit-status: 0
- name: init-non-interactive-name
command:
shell:
- bash
script: |
temp=$(mktemp -d)
trap 'rm -rf -- "$temp"' EXIT
mkdir "$temp/packagename"
cd "$temp/packagename"
juvix init -n
juvix repl Package.juvix
stdout:
contains: '"packagename"'
stdin: Package.name package
exit-status: 0
- name: init-name
command:
shell:
Expand Down

0 comments on commit de3f9ef

Please sign in to comment.