Skip to content
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

Improve restyled.yaml.5 #287

Merged
merged 9 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,27 @@ import Data.Aeson
import Restyler.App
import Restyler.CLI qualified as CLI
import Restyler.Config
import Restyler.Docs
import Restyler.GitHub.PullRequest
import Restyler.Restyle qualified as Restyle

main :: IO ()
main = CLI.main withApp $ do
paths <- toList <$> asks (.config.paths)
mJSON <- asks (.config.pullRequestJson)
main = do
getArgs >>= \case
["__render-docs-man1__"] -> renderDocsPage Restyle1
["__render-docs-man5__"] -> renderDocsPage RestyledYaml5
_ -> pure ()
Comment on lines +24 to +28
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this here is better, and idiomatic according to ronn documentation.


case mJSON of
Nothing -> Restyle.run NullPullRequest paths
Just path -> do
result <- liftIO $ eitherDecodeFileStrict @PullRequest $ toFilePath path
case result of
Left err -> do
logError $ ("pull-request-json is invalid:\n" <> pack err) :# []
exitFailure
Right pr -> Restyle.run pr paths
CLI.main withApp $ do
paths <- toList <$> asks (.config.paths)
mJSON <- asks (.config.pullRequestJson)

case mJSON of
Nothing -> Restyle.run NullPullRequest paths
Just path -> do
result <- liftIO $ eitherDecodeFileStrict @PullRequest $ toFilePath path
case result of
Left err -> do
logError $ ("pull-request-json is invalid:\n" <> pack err) :# []
exitFailure
Right pr -> Restyle.run pr paths
53 changes: 0 additions & 53 deletions config/default.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ library:
- aeson-casing
- annotated-exception
- autodocodec
- bytestring
- autodocodec-yaml
- composition-extra
- conduit
- errors
- exceptions
- extra
- file-embed
- filepath
- http-conduit
- microlens
Expand All @@ -62,6 +61,7 @@ library:
- relude
- ronn
- ronn-opt-env-conf
- safe-coloured-text
- semigroups
- text
- typed-process
Expand Down
4 changes: 2 additions & 2 deletions restyler.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ library
, aeson-casing
, annotated-exception
, autodocodec
, autodocodec-yaml
, base
, bytestring
, composition-extra
, conduit
, errors
, exceptions
, extra
, file-embed
, filepath
, http-conduit
, microlens
Expand All @@ -100,6 +99,7 @@ library
, relude
, ronn
, ronn-opt-env-conf
, safe-coloured-text
, semigroups
, text
, typed-process
Expand Down
58 changes: 23 additions & 35 deletions src/Restyler/Config.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}

-- | Handling of @.restyled.yaml@ content and behavior driven there-by
--
Expand All @@ -12,6 +11,7 @@
-- Portability : POSIX
module Restyler.Config
( Config (..)
, configPaths
, configParser
, parseConfig

Expand All @@ -21,8 +21,6 @@ module Restyler.Config

import Restyler.Prelude

import Data.ByteString qualified as BS
import Data.FileEmbed (embedFile)
import OptEnvConf
import Paths_restyler qualified as Pkg
import Restyler.Config.CommitTemplate as X
Expand All @@ -42,73 +40,60 @@ import Restyler.Config.NoPull as X
import Restyler.Config.RemoteFile as X
import Restyler.Config.Restrictions as X
import Restyler.Config.Restyler as X
import Restyler.Docs
import System.IO (hClose)
import UnliftIO.Temporary (withSystemTempFile)

data Config = Config
{ logSettings :: LogSettingsOption
, enabled :: Bool
{ enabled :: Bool
, dryRun :: Bool
, failOnDifferences :: Bool
, exclude :: [Glob FilePath]
, commitTemplate :: CommitTemplate
, remoteFiles :: [RemoteFile]
, ignores :: Ignores
, restylersVersion :: String
, restylersManifest :: Maybe (Path Abs File)
, restylerOverrides :: [RestylerOverride]
, ignores :: Ignores
, remoteFiles :: [RemoteFile]
, hostDirectory :: Path Abs Dir
, imageCleanup :: Bool
, noPull :: Bool
, restrictions :: Restrictions
, commitTemplate :: CommitTemplate
, noCommit :: Bool
, noClean :: Bool
, logSettings :: LogSettingsOption
, pullRequestJson :: Maybe (Path Abs File)
, paths :: NonEmpty FilePath
}

parseConfig :: IO Config
parseConfig = do
getArgs >>= \case
["__render-docs-man1__"] -> renderDocsPage Restyle1 $ configParser []
["__render-docs-man5__"] -> renderDocsPage (RestyledYaml5 defaultConfigContent) $ configParser []
_ -> pure ()

withSystemTempFile "restyler-default-config.yaml" $ \defaults h -> do
BS.hPutStr h defaultConfigContent >> hClose h
runParser Pkg.version "Restyle local files"
$ configParser
[ ".github/restyled.yml"
, ".github/restyled.yaml"
, ".restyled.yml"
, ".restyled.yaml"
, defaults
]
parseConfig = runParser Pkg.version "Restyle local files" $ configParser configPaths

defaultConfigContent :: ByteString
defaultConfigContent = $(embedFile "config/default.yaml")
configPaths :: [FilePath]
configPaths =
[ ".github/restyled.yml"
, ".github/restyled.yaml"
, ".restyled.yml"
, ".restyled.yaml"
]

configParser :: [FilePath] -> Parser Config
configParser sources =
withCombinedYamlConfigs (traverse hiddenPath sources) $ do
logSettings <- subConfig_ "logging" logSettingsOptionParser
withFirstYamlConfig (traverse hiddenPath sources) $ do
enabled <- enabledParser
dryRun <- dryRunParser
failOnDifferences <- failOnDifferencesParser
exclude <- excludeParser
commitTemplate <- commitTemplateParser
remoteFiles <- remoteFilesParser
ignores <- ignoresParser
restylersVersion <- restylersVersionParser
restylersManifest <- optional manifestParser
restylerOverrides <- restylerOverridesParser
exclude <- excludeParser
ignores <- ignoresParser
remoteFiles <- remoteFilesParser
hostDirectory <- subConfig_ "docker" hostDirectoryParser
imageCleanup <- subConfig_ "docker" imageCleanupParser
noPull <- subConfig_ "docker" noPullParser
restrictions <- subConfig_ "docker" $ subAll "restyler" restrictionsParser
commitTemplate <- commitTemplateParser
noCommit <- subConfig_ "git" noCommitParser
noClean <- subConfig_ "git" noCleanParser
logSettings <- subConfig_ "logging" logSettingsOptionParser
pullRequestJson <-
optional
$ filePathSetting
Expand All @@ -127,5 +112,8 @@ configParser sources =
]
pure Config {..}

-- | Use 'filePathSetting' to handle creating the @'Path' 'Abs' 'File'@ we need
--
-- And mark it 'hidden' so it doesn't appear in docs.
hiddenPath :: FilePath -> Parser (Path Abs File)
hiddenPath x = filePathSetting [value x, hidden]
1 change: 1 addition & 0 deletions src/Restyler/Config/CommitTemplate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ commitTemplateParser =
setting
[ help "Template for restyling commit messages"
, conf "commit_template"
, value "Restyled by ${restyler.name}\n"
]

newtype CommitTemplateInputs = CommitTemplateInputs
Expand Down
13 changes: 7 additions & 6 deletions src/Restyler/Config/DryRun.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class HasDryRun env where

dryRunParser :: Parser Bool
dryRunParser =
yesNoSwitch
[ help "Do everything except pull and run restylers"
, long "dry-run"
, env "DRY_RUN"
, conf "dry_run"
]
withDefault False
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using withDefault is needed for yesNoSwitch. Defaults added with value don't show up in docs.

$ yesNoSwitch
[ help "Do everything except pull and run restylers"
, long "dry-run"
, env "DRY_RUN"
, conf "dry_run"
]
1 change: 1 addition & 0 deletions src/Restyler/Config/Enabled.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ enabledParser =
setting
[ help "Do anything at all"
, conf "enabled"
, value True
]
18 changes: 17 additions & 1 deletion src/Restyler/Config/Exclude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@ excludeParser :: Parser [Glob FilePath]
excludeParser =
(<>)
<$> setting
[ help "Exclude paths matching the given globs (instead of defaults)"
[ help
$ unpack
$ unlines
[ "Exclude paths matching the given globs (instead of defaults)"
, "By default, we ignore directories that are often checked-in but"
, "rarely represent project code. Some globs are slightly complicated"
, "match paths within directories of names appearing at any depth."
]
, example "exclude: []"
, option
, name "exclude"
, reader $ commaSeparatedList str
, metavar "GLOB[,GLOB]"
, value
[ "**/*.patch"
, "**/.git/**/*"
, "**/node_modules/**/*"
, "**/vendor/**/*"
, ".github/workflows/**/*"
]
]
<*> setting
[ help "Exclude paths matching the given globs (in addition to defaults)"
Expand All @@ -37,4 +52,5 @@ excludeParser =
, reader $ commaSeparatedList str
, metavar "GLOB[,GLOB]"
, conf "also_exclude"
, value []
]
13 changes: 7 additions & 6 deletions src/Restyler/Config/FailOnDifferences.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class HasFailOnDifferences env where

failOnDifferencesParser :: Parser Bool
failOnDifferencesParser =
yesNoSwitch
[ help "Exit non-zero if differences were found"
, long "fail-on-differences"
, env "FAIL_ON_DIFFERENCES"
, conf "fail_on_differences"
]
withDefault False
$ yesNoSwitch
[ help "Exit non-zero if differences were found"
, long "fail-on-differences"
, env "FAIL_ON_DIFFERENCES"
, conf "fail_on_differences"
]
1 change: 1 addition & 0 deletions src/Restyler/Config/HostDirectory.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ hostDirectoryParser =
, long "host-directory"
, env "HOST_DIRECTORY"
, conf "host_directory"
, value "."
]
3 changes: 3 additions & 0 deletions src/Restyler/Config/Ignore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,23 @@ newIgnoresParser =
, name "authors"
, reader $ commaSeparatedList str
, metavar "GLOB[,GLOB]"
, value ["*[bot]"]
]
<*> setting
[ help "Ignore branches that match globs"
, option
, name "branches"
, reader $ commaSeparatedList str
, metavar "GLOB[,GLOB]"
, value ["renovate/*"]
]
<*> setting
[ help "Ignore labels that match globs"
, option
, name "labels"
, reader $ commaSeparatedList str
, metavar "GLOB[,GLOB]"
, value ["restyled-ignore"]
]

data OldIgnores = OldIgnores
Expand Down
13 changes: 7 additions & 6 deletions src/Restyler/Config/ImageCleanup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class HasImageCleanup env where

imageCleanupParser :: Parser Bool
imageCleanupParser =
yesNoSwitch
[ help "Remove images after running them"
, long "image-cleanup"
, env "IMAGE_CLEANUP"
, conf "image_cleanup"
]
withDefault False
$ yesNoSwitch
[ help "Remove images after running them"
, long "image-cleanup"
, env "IMAGE_CLEANUP"
, conf "image_cleanup"
]
Loading
Loading