Skip to content

Commit

Permalink
Make bump-versions and update-changelog proper commands
Browse files Browse the repository at this point in the history
Close #80
Remove old --bump-versions and update-changelog. They are absolutely alternative now.
Change prettyPossibleValues inners - now it hyphenates values.
Split toSnakeCase.
Update messages for user.
  • Loading branch information
viviag committed Aug 7, 2018
1 parent f7ef415 commit bede39c
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 41 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ and also list any recent changes that are not included in you changelog.
You can then prepend missing changelog entries automatically with

```
changelogged --update-changelog
changelogged update-changelog
```

Now you can see new entries in your changelog, make edits and group changes.
Even if you see simple messages on a screen, detailed (with links, see demo) are written to changelog.
When you're done you can automatically bump project's version with

```
changelogged --bump-versions
changelogged bump-versions
```

That's it! Now you have a proper changelog with no forgotten changes.
Expand Down Expand Up @@ -65,20 +65,22 @@ changelogged --help
```
changelogged - Changelog Manager for Git Projects
Usage: changelogged [--format FORMAT] [--update-changelog] [--bump-versions]
Usage: changelogged [ACTION] [--format FORMAT] [--dry-run] [TARGET_CHANGELOG]
[--config changelogged.yaml config file location]
Available options:
-h,--help Show this help text
ACTION If present could be update-changelog or
bump-versions.
--format FORMAT Format for missing changelog entry warnings. FORMAT
can be 'simple' or 'suggest'. (default: simple)
--update-changelog Add missing entries to changelogs (works with
--format=suggest).
--bump-versions Bump versions in version files.
--level CHANGE_LEVEL Level of changes (to override one inferred from
changelog). CHANGE_LEVEL can be 'app', 'major',
'minor', 'fix' or 'doc'.
--from-bc Look for missing changelog entries from the start of
the project.
--force Bump versions ignoring possibly outdated changelogs.
Usable with bump-versions only
--no-check Do not check if changelogs have any missing entries.
--no-colors Print all messages in standard terminal color.
--dry-run Do not change files while running.
Expand Down Expand Up @@ -140,7 +142,7 @@ changelogged --format suggest
Try to bump with no entries in changelog:

```
changelogged --format suggest --bump-versions
changelogged --format suggest bump-versions
```

![image3](images/failed_bump.png)
Expand All @@ -156,7 +158,7 @@ changelogged --format suggest --bump-versions --force
### Write suggested entries to changelog

```
changelogged --format suggest --update-changelog
changelogged --format suggest update-changelog
```

![image5](images/suggest.png)
Expand All @@ -172,7 +174,7 @@ It requires some manual editing after.
### Bump version and infering level of change from changelog

```
changelogged --format suggest --bump-versions
changelogged --format suggest bump-versions
```

![image7](images/bump.png)
Expand Down
6 changes: 3 additions & 3 deletions src/Changelogged/Changelog/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ checkChangelog gitInfo@GitInfo{..} config@ChangelogConfig{..} = do
return $ and flags

if upToDate
then coloredPrint Green (showPath changelogChangelog <> " is up to date.\n")
then coloredPrint Green (showPath changelogChangelog <> " is up to date.\n" <> "You can run bump-versions to bump versions.\n")
else do
warning $ showPath changelogChangelog <> " is out of date." <>
if optUpdateChangelog
if optAction == Just UpdateChangelogs
then ""
else "\nUse --update-changelog to add missing changelog entries automatically."
else "\nUse update-changelog to add missing changelog entries automatically."

return upToDate

Expand Down
2 changes: 1 addition & 1 deletion src/Changelogged/Changelog/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ changelogIsUp link item mode message changelog = do
case optFormat of
WarnSimple -> warnMissing item mode message
WarnSuggest -> suggestMissing link item mode message
when optUpdateChangelog $ addMissing link item mode message changelog
when (optAction == Just UpdateChangelogs) $ addMissing link item mode message changelog
return False
_ -> return True

Expand Down
9 changes: 8 additions & 1 deletion src/Changelogged/EntryPoint.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Changelogged.EntryPoint where

import System.Console.ANSI (Color(..))

import Turtle hiding (find)
import Prelude hiding (FilePath)
import Data.List (find)

import Changelogged.Versions.Bump
import Changelogged.Changelog.Check
import Changelogged.Options
import Changelogged.Types
import Changelogged.Pure (showPath)
import Changelogged.Utils
import Changelogged.Config
import Changelogged.Git
Expand Down Expand Up @@ -38,4 +42,7 @@ processChangelog gitInfo config@ChangelogConfig{..} = do
touch changelogChangelog

upToDate <- checkChangelog gitInfo config
bumpVersions upToDate config
case optAction of
Just BumpVersions -> bumpVersions upToDate config
Just UpdateChangelogs -> coloredPrint Green (showPath changelogChangelog <> " is updated.\n" <> "You can edit it manually or run bump-versions.\n")
Nothing -> return ()
50 changes: 38 additions & 12 deletions src/Changelogged/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import qualified Turtle
import Filesystem.Path.CurrentOS (valid, fromText)

import Changelogged.Types
import Changelogged.Pure (hyphenate)

newtype Appl a = Appl { runAppl :: ReaderT Options IO a }
deriving newtype (Functor, Applicative, Monad, MonadReader Options, MonadIO, MonadBase IO, MonadThrow, MonadCatch)
Expand Down Expand Up @@ -64,6 +65,17 @@ readWarningFormat = eitherReader (r . map toLower)
availableLevels :: [Level]
availableLevels = [minBound..maxBound]

-- |
-- >>> availableActions
-- [UpdateChangelogs,BumpVersions]
availableActions :: [Action]
availableActions = [minBound..maxBound]

-- >>> availableActionsStr
-- "'update-changelogs' or 'bump-versions'"
availableActionsStr :: String
availableActionsStr = prettyPossibleValues availableActions

-- |
-- >>> availableLevelsStr
-- "'app', 'major', 'minor', 'fix' or 'doc'"
Expand All @@ -76,7 +88,7 @@ prettyPossibleValues xs = case reverse xs of
[y] -> prettyValue y
(y:ys) -> intercalate ", " (map prettyValue (reverse ys)) <> " or " <> prettyValue y
where
prettyValue v = "'" <> map toLower (show v) <> "'"
prettyValue v = "'" <> hyphenate (show v) <> "'"

readLevel :: ReadM Level
readLevel = eitherReader (r . map toLower)
Expand All @@ -90,6 +102,17 @@ readLevel = eitherReader (r . map toLower)
"Unknown level of changes: " <> show lvl <> ".\n"
<> "Should be " <> availableLevelsStr <> ".\n"

readAction :: ReadM Action
readAction = eitherReader (r . map toLower)
where
r "update-changelog" = Right UpdateChangelogs
r "update-changelogs" = Right UpdateChangelogs
r "bump-versions" = Right BumpVersions
r "bump-version" = Right BumpVersions
r cmd = Left $
"Unknown command: " <> show cmd <> ".\n"
<> "Should be " <> availableActionsStr <> ".\n"

readFilePath :: ReadM Turtle.FilePath
readFilePath = eitherReader r
where
Expand All @@ -99,14 +122,12 @@ readFilePath = eitherReader r

parser :: Parser Options
parser = Options
<$> warningFormat
<*> longSwitch "update-changelog"
"Add missing entries to changelogs (works with --format=suggest)."
<*> longSwitch "bump-versions" "Bump versions in version files."
<$> optional changeloggedAction
<*> warningFormat
<*> optional changesLevel
<*> hiddenSwitch "from-bc"
"Look for missing changelog entries from the start of the project."
<*> hiddenSwitch "force" "Bump versions ignoring possibly outdated changelogs."
<*> hiddenSwitch "force" "Bump versions ignoring possibly outdated changelogs. Usable with bump-versions only"
<*> hiddenSwitch "no-check" "Do not check if changelogs have any missing entries."
<*> hiddenSwitch "no-colors" "Print all messages in standard terminal color."
<*> longSwitch "dry-run" "Do not change files while running."
Expand All @@ -124,6 +145,10 @@ parser = Options
<> help description
<> hidden

changeloggedAction = argument readAction $
metavar "ACTION"
<> help ("If present could be update-changelog or bump-versions.")

changesLevel = option readLevel $
long "level"
<> metavar "CHANGE_LEVEL"
Expand All @@ -132,15 +157,18 @@ parser = Options
, "CHANGE_LEVEL can be " <> availableLevelsStr <> "."
])
<> hidden

warningFormat = option readWarningFormat $
long "format"
<> metavar "FORMAT"
<> help ("Format for missing changelog entry warnings. FORMAT can be " <> availableWarningFormatsStr <> ".")
<> value WarnSimple
<> showDefault

targetChangelog = argument readFilePath $
metavar "TARGET_CHANGELOG"
<> help ("Path to target changelog.")

configPath = option readFilePath $
long "config"
<> metavar "changelogged.yaml config file location"
Expand All @@ -151,12 +179,10 @@ welcome = Turtle.Description "changelogged - Changelog Manager for Git Projects"

-- | Command line options for @changelogged@.
data Options = Options
{ -- | Format for missing changelog entry warnings.
optFormat :: WarningFormat
-- | Add missing changelog entries to changelog files.
, optUpdateChangelog :: Bool
-- | Bump versions in version files.
, optBumpVersions :: Bool
{ -- | Command to execute.
optAction :: Maybe Action
-- | Format for missing changelog entry warnings.
, optFormat :: WarningFormat
-- | Level of changes (to override one inferred from changelogs).
, optChangeLevel :: Maybe Level
-- | Look for missing changelog entries from the start of the project.
Expand Down
38 changes: 24 additions & 14 deletions src/Changelogged/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ showPath = Text.pack . encodeString
showText :: Show a => a -> Text
showText = Text.pack . show

splitCamelWords :: String -> [String]
splitCamelWords = reverse . splitWordsReversed . reverse

splitWordsReversed :: String -> [String]
splitWordsReversed [] = []
splitWordsReversed rs
| null ls = reverse us : splitWordsReversed urs
| otherwise = case lrs of
[] -> [reverse ls]
(c:cs) -> (c : reverse ls) : splitWordsReversed cs
where
(ls, lrs) = span (not . isBorder) rs
(us, urs) = span isBorder rs
isBorder c = isUpper c || isDigit c

-- | Convert @CamelCase@ to @snake_case@.
--
-- >>> toSnakeCase "VersionFile"
Expand All @@ -60,20 +75,15 @@ showText = Text.pack . show
-- "config"
toSnakeCase :: String -> String
toSnakeCase = map toLower . intercalate "_" . splitCamelWords
where
splitCamelWords = reverse . splitWordsReversed . reverse

splitWordsReversed :: String -> [String]
splitWordsReversed [] = []
splitWordsReversed rs
| null ls = reverse us : splitWordsReversed urs
| otherwise = case lrs of
[] -> [reverse ls]
(c:cs) -> (c : reverse ls) : splitWordsReversed cs
where
(ls, lrs) = span (not . isBorder) rs
(us, urs) = span isBorder rs
isBorder c = isUpper c || isDigit c

-- |
--
-- >>> hyphenate "VersionFile"
-- "version-file"
-- >>> hyphenate "Config"
-- "config"
hyphenate :: String -> String
hyphenate = map toLower . intercalate "-" . splitCamelWords

jsonDerivingModifier :: String -> Options
jsonDerivingModifier prefix = defaultOptions {
Expand Down
4 changes: 4 additions & 0 deletions src/Changelogged/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ instance FromJSON Path.FilePath where
data Level = App | Major | Minor | Fix | Doc
deriving (Generic, Show, Enum, Bounded, ToJSON)

-- |Available altenative actions
data Action = UpdateChangelogs | BumpVersions
deriving (Generic, Eq, Show, Enum, Bounded, ToJSON)

-- |Type of entry in git history.
data Mode = PR | Commit

Expand Down
2 changes: 1 addition & 1 deletion src/Changelogged/Versions/Bump.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ generateLocalVersionByChangelog logConfig@ChangelogConfig{..} = do
bumpVersions :: Bool -> ChangelogConfig -> Appl ()
bumpVersions upToDate config@ChangelogConfig{..} = do
Options{..} <- ask
when optBumpVersions $ if
if
| not upToDate && not optForce ->
failure $ "cannot bump versions because " <> format fp changelogChangelog <> " is out of date.\nUse --no-check to skip changelog checks.\nUse --force to force bump version."
| not upToDate && optForce ->
Expand Down

0 comments on commit bede39c

Please sign in to comment.