-
-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#48] Add configurations, read from the file #59
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
module Summoner | ||
( module Summoner.Ansi | ||
, module Summoner.CLI | ||
, module Summoner.Config | ||
, module Summoner.Default | ||
, module Summoner.License | ||
, module Summoner.Process | ||
, module Summoner.Project | ||
, module Summoner.ProjectData | ||
, module Summoner.Question | ||
, module Summoner.Template | ||
, module Summoner.Validation | ||
) where | ||
|
||
import Summoner.Ansi | ||
import Summoner.CLI | ||
import Summoner.Config | ||
import Summoner.Default | ||
import Summoner.License | ||
import Summoner.Process | ||
import Summoner.Project | ||
import Summoner.ProjectData | ||
import Summoner.Question | ||
import Summoner.Template | ||
import Summoner.Validation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
{-# LANGUAGE ApplicativeDo #-} | ||
{-# LANGUAGE QuasiQuotes #-} | ||
{-# LANGUAGE TupleSections #-} | ||
|
||
-- | This module contains functions and data types to parse CLI inputs. | ||
|
||
module Summoner.CLI | ||
( summon | ||
) where | ||
|
||
import Data.Foldable (fold) | ||
import Data.Foldable (fold, for_) | ||
import Data.Semigroup (Semigroup (..)) | ||
import Data.Text (Text) | ||
import NeatInterpolation (text) | ||
import Options.Applicative (Parser, ParserInfo, command, execParser, flag, fullDesc, help, helper, | ||
info, infoFooter, infoHeader, long, metavar, optional, progDesc, short, | ||
strArgument, subparser) | ||
strArgument, strOption, subparser) | ||
import Options.Applicative.Help.Chunk (stringChunk) | ||
import System.Directory (doesFileExist) | ||
import System.Exit (exitFailure) | ||
|
||
import Summoner.Ansi (boldText) | ||
import Summoner.Default (endLine) | ||
import Summoner.Project (Decision (..), Targets (..), generateProject) | ||
import Summoner.Ansi (boldText, errorMessage, infoMessage, warningMessage) | ||
import Summoner.Config (ConfigP (..), PartialConfig, defaultConfig, finalise, loadFileConfig) | ||
import Summoner.Default (defaultConfigFile, endLine) | ||
import Summoner.Project (generateProject) | ||
import Summoner.ProjectData (Decision (..)) | ||
import Summoner.Validation (Validation (..)) | ||
|
||
import qualified Data.Text as T | ||
|
||
|
@@ -31,27 +37,63 @@ summon = execParser prsr >>= runWithOptions | |
|
||
-- | Run 'hs-init' with cli options | ||
runWithOptions :: InitOpts -> IO () | ||
runWithOptions (InitOpts projectName targets) = do | ||
-- Generate the project. | ||
generateProject projectName targets | ||
runWithOptions (InitOpts projectName cliConfig maybeFile) = do | ||
(isDefault, file) <- case maybeFile of | ||
Nothing -> (True,) <$> defaultConfigFile | ||
Just x -> pure (False, x) | ||
isFile <- doesFileExist file | ||
fileConfig <- | ||
if isFile | ||
then do | ||
infoMessage $ "Configurations from " <> T.pack file <> " will be used." | ||
loadFileConfig file | ||
else if isDefault | ||
then do | ||
fp <- T.pack <$> defaultConfigFile | ||
warningMessage $ "Default config " <> fp <> " file is missing." | ||
pure mempty | ||
else do | ||
errorMessage $ "Specified configuration file " <> T.pack file <> " is not found." | ||
exitFailure | ||
-- union all possible configs | ||
let unionConfig = defaultConfig <> fileConfig <> cliConfig | ||
-- get the final config | ||
finalConfig <- case finalise unionConfig of | ||
Failure msgs -> do | ||
for_ msgs errorMessage | ||
exitFailure | ||
Success c -> pure c | ||
-- Generate the project. | ||
generateProject projectName finalConfig | ||
|
||
boldText "\nJob's done\n" | ||
|
||
-- | Initial parsed options from cli | ||
data InitOpts = InitOpts Text Targets -- ^ Includes the project name and target options. | ||
data InitOpts = InitOpts Text PartialConfig (Maybe FilePath) | ||
-- ^ Includes the project name, config from the CLI and possible file where custom congifs are. | ||
|
||
targetsP :: Decision -> Parser Targets | ||
targetsP :: Decision -> Parser PartialConfig | ||
targetsP d = do | ||
githubFlag <- githubP d | ||
travisFlag <- travisP d | ||
appVeyorFlag <- appVeyorP d | ||
privateFlag <- privateP d | ||
scriptFlag <- scriptP d | ||
isLibrary <- libraryP d | ||
isExecutable <- execP d | ||
isTest <- testP d | ||
isBenchmark <- benchmarkP d | ||
pure Targets{..} | ||
cGitHub <- githubP d | ||
cTravis <- travisP d | ||
cAppVey <- appVeyorP d | ||
cPrivate <- privateP d | ||
cScript <- scriptP d | ||
cLib <- libraryP d | ||
cExe <- execP d | ||
cTest <- testP d | ||
cBench <- benchmarkP d | ||
pure mempty | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's too pity we can't write something like this |
||
{ cGitHub = cGitHub | ||
, cTravis = cTravis | ||
, cAppVey = cAppVey | ||
, cPrivate= cPrivate | ||
, cScript = cScript | ||
, cLib = cLib | ||
, cExe = cExe | ||
, cTest = cTest | ||
, cBench = cBench | ||
} | ||
|
||
githubP :: Decision -> Parser Decision | ||
githubP d = flag Idk d | ||
|
@@ -107,25 +149,33 @@ benchmarkP d = flag Idk d | |
<> short 'b' | ||
<> help "Benchmarks" | ||
|
||
withP :: Parser Targets | ||
withP :: Parser PartialConfig | ||
withP = subparser $ mconcat | ||
[ metavar "with [OPTIONS]" | ||
, command "with" $ info (helper <*> targetsP Yes) (progDesc "Specify options to enable") | ||
] | ||
|
||
withoutP :: Parser Targets | ||
withoutP :: Parser PartialConfig | ||
withoutP = subparser $ mconcat | ||
[ metavar "without [OPTIONS]" | ||
, command "without" $ info (helper <*> targetsP Nop) (progDesc "Specify options to disable") | ||
] | ||
|
||
fileP :: Parser FilePath | ||
fileP = strOption | ||
$ long "file" | ||
<> short 'f' | ||
<> metavar "FILENAME" | ||
<> help "Path to the toml file with configurations. If not specified '~/summoner.toml' will be used if present" | ||
|
||
optsP :: Parser InitOpts | ||
optsP = do | ||
projectName <- strArgument (metavar "PROJECT_NAME") | ||
with <- optional withP | ||
without <- optional withoutP | ||
file <- optional fileP | ||
|
||
pure $ InitOpts projectName (fold $ with <> without) | ||
pure $ InitOpts projectName (fold $ with <> without) file | ||
|
||
prsr :: ParserInfo InitOpts | ||
prsr = modifyHeader | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I right that
Default
module will be removed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be