@@ -59,13 +59,13 @@ import Distribution.Types.LibraryName
59
59
import Language.Haskell.Extension ( Language (.. ) )
60
60
61
61
import Distribution.Client.Init.Defaults
62
- ( defaultCabalVersion , myLibModule )
62
+ ( defaultApplicationDir , defaultCabalVersion , myLibModule , defaultSourceDir )
63
63
import Distribution.Client.Init.FileCreators
64
64
( writeLicense , writeChangeLog , createDirectories , createLibHs , createMainHs
65
65
, createTestSuiteIfEligible , writeCabalFile )
66
66
import Distribution.Client.Init.Prompt
67
67
( prompt , promptYesNo , promptStr , promptList , maybePrompt
68
- , promptListOptional , promptListOptional' )
68
+ , promptListOptional )
69
69
import Distribution.Client.Init.Utils
70
70
( eligibleForTestSuite , message )
71
71
import Distribution.Client.Init.Types
@@ -482,56 +482,108 @@ getGenComments flags = do
482
482
-- | Ask for the application root directory.
483
483
getAppDir :: InitFlags -> IO InitFlags
484
484
getAppDir flags = do
485
- appDirs <- return (applicationDirs flags)
486
- -- No application dir if this is a 'Library'.
487
- ?>> if (packageType flags) == Flag Library then return (Just [] ) else return Nothing
488
- ?>> fmap (: [] ) `fmap` guessAppDir flags
489
- ?>> fmap (>>= fmap ((: [] ) . either id id )) (maybePrompt
490
- flags
491
- (promptListOptional'
492
- (" Application " ++ mainFile ++ " directory" )
493
- [" src-exe" , " app" ] id ))
494
-
485
+ appDirs <-
486
+ return (applicationDirs flags)
487
+ ?>> noAppDirIfLibraryOnly
488
+ ?>> guessAppDir flags
489
+ ?>> promptUserForApplicationDir
490
+ ?>> setDefault
495
491
return $ flags { applicationDirs = appDirs }
496
492
497
493
where
494
+ -- If the packageType==Library, then there is no application dir.
495
+ noAppDirIfLibraryOnly :: IO (Maybe [String ])
496
+ noAppDirIfLibraryOnly =
497
+ if (packageType flags) == Flag Library
498
+ then return (Just [] )
499
+ else return Nothing
500
+
501
+ -- Set the default application directory.
502
+ setDefault :: IO (Maybe [String ])
503
+ setDefault = pure (Just [defaultApplicationDir])
504
+
505
+ -- Prompt the user for the application directory (defaulting to "app").
506
+ -- Returns 'Nothing' if in non-interactive mode, otherwise will always
507
+ -- return a 'Just' value ('Just []' if no separate application directory).
508
+ promptUserForApplicationDir :: IO (Maybe [String ])
509
+ promptUserForApplicationDir = fmap (either (: [] ) id ) <$> maybePrompt
510
+ flags
511
+ (promptList
512
+ (" Application " ++ mainFile ++ " directory" )
513
+ [[defaultApplicationDir], [" src-exe" ], [] ]
514
+ (Just [defaultApplicationDir])
515
+ showOption True )
516
+
517
+ showOption :: [String ] -> String
518
+ showOption [] = " (none)"
519
+ showOption (x: _) = x
520
+
521
+ -- The name
522
+ mainFile :: String
498
523
mainFile = case mainIs flags of
499
524
Flag mainPath -> " (" ++ mainPath ++ " ) "
500
525
_ -> " "
501
526
502
527
-- | Try to guess app directory. Could try harder; for the
503
528
-- moment just looks to see whether there is a directory called 'app'.
504
- guessAppDir :: InitFlags -> IO (Maybe String )
529
+ guessAppDir :: InitFlags -> IO (Maybe [ String ] )
505
530
guessAppDir flags = do
506
531
dir <- maybe getCurrentDirectory return . flagToMaybe $ packageDir flags
507
532
appIsDir <- doesDirectoryExist (dir </> " app" )
508
533
return $ if appIsDir
509
- then Just " app"
534
+ then Just [ " app" ]
510
535
else Nothing
511
536
512
537
-- | Ask for the source (library) root directory.
513
538
getSrcDir :: InitFlags -> IO InitFlags
514
539
getSrcDir flags = do
515
- srcDirs <- return (sourceDirs flags)
516
- -- source dir if this is an 'Executable'.
517
- ?>> if (packageType flags) == Flag Executable then return (Just [] ) else return Nothing
518
- ?>> fmap (: [] ) `fmap` guessSourceDir flags
519
- ?>> fmap (>>= fmap ((: [] ) . either id id )) (maybePrompt
520
- flags
521
- (promptListOptional' " Library source directory"
522
- [" src" , " lib" , " src-lib" ] id ))
540
+ srcDirs <-
541
+ return (sourceDirs flags)
542
+ ?>> noSourceDirIfExecutableOnly
543
+ ?>> guessSourceDir flags
544
+ ?>> promptUserForSourceDir
545
+ ?>> setDefault
523
546
524
547
return $ flags { sourceDirs = srcDirs }
525
548
549
+ where
550
+ -- If the packageType==Executable, then there is no source dir.
551
+ noSourceDirIfExecutableOnly :: IO (Maybe [String ])
552
+ noSourceDirIfExecutableOnly =
553
+ if (packageType flags) == Flag Executable
554
+ then return (Just [] )
555
+ else return Nothing
556
+
557
+ -- Set the default source directory.
558
+ setDefault :: IO (Maybe [String ])
559
+ setDefault = pure (Just [defaultSourceDir])
560
+
561
+ -- Prompt the user for the source directory (defaulting to "app").
562
+ -- Returns 'Nothing' if in non-interactive mode, otherwise will always
563
+ -- return a 'Just' value ('Just []' if no separate application directory).
564
+ promptUserForSourceDir :: IO (Maybe [String ])
565
+ promptUserForSourceDir = fmap (either (: [] ) id ) <$> maybePrompt
566
+ flags
567
+ (promptList
568
+ (" Library source directory" )
569
+ [[defaultSourceDir], [" lib" ], [" src-lib" ], [] ]
570
+ (Just [defaultSourceDir])
571
+ showOption True )
572
+
573
+ showOption :: [String ] -> String
574
+ showOption [] = " (none)"
575
+ showOption (x: _) = x
576
+
577
+
526
578
-- | Try to guess source directory. Could try harder; for the
527
579
-- moment just looks to see whether there is a directory called 'src'.
528
- guessSourceDir :: InitFlags -> IO (Maybe String )
580
+ guessSourceDir :: InitFlags -> IO (Maybe [ String ] )
529
581
guessSourceDir flags = do
530
582
dir <-
531
583
maybe getCurrentDirectory return . flagToMaybe $ packageDir flags
532
584
srcIsDir <- doesDirectoryExist (dir </> " src" )
533
585
return $ if srcIsDir
534
- then Just " src"
586
+ then Just [ " src" ]
535
587
else Nothing
536
588
537
589
-- | Check whether a potential source file is located in one of the
0 commit comments