Skip to content

Commit

Permalink
Merge pull request #89 from GetShopTV/levels
Browse files Browse the repository at this point in the history
Enhancements
  • Loading branch information
viviag authored Apr 13, 2018
2 parents cec1e3c + 569ce6e commit fd19a98
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 60 deletions.
4 changes: 4 additions & 0 deletions .changelogged.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ changelogs:
# version in package.yaml should be updated
- path: package.yaml
version_pattern: "version:"

# Apply common options like --level when is not target changelog or not.
# false by default.
default: true

# HTTP API changelog
- changelog: API_ChangeLog.md
Expand Down
13 changes: 13 additions & 0 deletions .changelogged.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
changelogs:
- changelog: ChangeLog.md

ignore_files:
- ChangeLog.md

version_files:
- path: package.yaml
version_pattern: "version:"
- path: src/Changelogged/Pure.hs
version_pattern: "changeloggedVersion ="

default: true
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ library:
- mtl
- exceptions
- system-filepath
- string-conversions
- foldl
- vector
- either
Expand Down
2 changes: 1 addition & 1 deletion src/Changelogged/Bump/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bumpAny VersionFile{..} version = do
output versionFilePath (select $ generateVersionedFile file changed matched)

-- |Replace given lines in the file.
-- Here is used and called to write new lines wih versions.
-- Here is used and called to write new lines with versions.
generateVersionedFile
-- template file
:: [Line]
Expand Down
24 changes: 14 additions & 10 deletions src/Changelogged/Bump/Local.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ currentLocalVersion VersionFile{..} = do
Just realVer -> fromMaybe
(throw (PatternMatchFail $ "cannot get local version. Given variable " <> show versionFileVersionPattern <> " doesn't store version. Check config.\n"))
(versionMatch . lineToText $ realVer)
Nothing -> throw (PatternMatchFail $ "cannot get local version. Cannot find given variable " <> show versionFileVersionPattern <> " in file " <> encodeString versionFilePath <> ". Check config.\n")
Nothing -> throw (PatternMatchFail $ "cannot get local version. Cannot match given pattern " <> show versionFileVersionPattern <> " in file " <> encodeString versionFilePath <> ". Check config.\n")

-- |Generate new local version.
generateLocalVersion :: Level -> VersionFile -> IO Text
generateLocalVersion lev indicator = do
generateLocalVersionForFile :: Level -> VersionFile -> IO Text
generateLocalVersionForFile lev indicator = do
current <- currentLocalVersion indicator
-- This print must not be here but I think it's better than throw current vrsion to main.
printf ("Version: "%s%" -> ") current
Expand All @@ -40,17 +40,21 @@ generateLocalVersion lev indicator = do
where
new current = bump (delimited current) lev

-- |Set new local version.
generateLocalVersion :: Level -> ChangelogConfig -> IO (Maybe Text)
generateLocalVersion lev ChangelogConfig{..} = do
case changelogVersionFiles of
Nothing -> error "No file version files specified for changelog."
Just versionFiles -> do
localVersions <- mapM (generateLocalVersionForFile lev) versionFiles
return (listToMaybe localVersions) -- FIXME: don't ignore other version files

-- |Infer new local version.
generateLocalVersionByChangelog :: ChangelogConfig -> IO (Maybe Text)
generateLocalVersionByChangelog ChangelogConfig{..} = do
generateLocalVersionByChangelog logConfig@ChangelogConfig{..} = do
versionedChanges <- getChangelogEntries changelogChangelog
case versionedChanges of
Just lev -> do
case changelogVersionFiles of
Nothing -> error "No file version files specified for changelog."
Just versionFiles -> do
localVersions <- mapM (generateLocalVersion lev) versionFiles
return (listToMaybe localVersions) -- FIXME: don't ignore other version files
Just lev -> generateLocalVersion lev logConfig
Nothing -> do
warning $ "keeping current version since " <> showPath changelogChangelog <> " apparently does not contain any new entries"
return Nothing
2 changes: 1 addition & 1 deletion src/Changelogged/CheckLog/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ checkLocalChangelogF fmt writeLog GitInfo{..} ChangelogConfig{..} = do
message <- commitMessage PR commit
changelogIsUp fmt writeLog gitRemoteUrl pnum PR message changelogChangelog

-- |This is actually part if '@Main@'
-- |This is actually part of '@Main@'
-- Check given changelog regarding options.
checkChangelogWrap :: Options -> GitInfo -> ChangelogConfig -> IO Bool
checkChangelogWrap Options{..} git config@ChangelogConfig{..} = do
Expand Down
12 changes: 0 additions & 12 deletions src/Changelogged/CheckLog/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ changelogIsUp fmt writeSug link item mode message changelog = do
return False
_ -> return True

-- |Ignore commits which only affect '.md' files
noMarkdown :: Text -> IO Bool
noMarkdown commit = do
statCommit <- fold (inproc "git" ["show", "--stat", commit] empty) Fold.list
chLogUpdated <- fold
(grep (has $ text ".md ")
(select statCommit)) countLines
onlyChLogUpdated <- fold
(grep (has $ text "|")
(select statCommit)) countLines
return $ chLogUpdated /= onlyChLogUpdated

-- |
warnMissing :: Text -> Mode -> Text -> IO ()
warnMissing item mode message = do
Expand Down
11 changes: 8 additions & 3 deletions src/Changelogged/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Changelogged.Config where

import Data.Aeson
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.String (fromString)
import Data.Text (Text)
Expand All @@ -15,19 +16,21 @@ data Config = Config
{ configChangelogs :: [ChangelogConfig]
, configIgnoreCommits :: Maybe [Text]
, configBranch :: Maybe Text
}
} deriving Eq

data ChangelogConfig = ChangelogConfig
{ changelogChangelog :: Turtle.FilePath
, changelogWatchFiles :: Maybe [Turtle.FilePath]
, changelogIgnoreFiles :: Maybe [Turtle.FilePath]
, changelogVersionFiles :: Maybe [VersionFile]
}
-- If changelog is marked as default it's version files will be bumped with explicit level from options.
, changelogDefault :: Bool
} deriving Eq

data VersionFile = VersionFile
{ versionFilePath :: Turtle.FilePath
, versionFileVersionPattern :: Text
}
} deriving (Show, Eq)

instance FromJSON Config where
parseJSON = withObject "Config" $ \o -> Config
Expand All @@ -41,6 +44,7 @@ instance FromJSON ChangelogConfig where
<*> (fmap (map fromString) <$> o .:? "watch_files")
<*> (fmap (map fromString) <$> o .:? "ignore_files")
<*> o .:? "version_files"
<*> (fromMaybe False <$> o .:? "default")

instance FromJSON VersionFile where
parseJSON = withObject "VersionFile" $ \o -> VersionFile
Expand All @@ -54,6 +58,7 @@ defaultConfig = Config
, changelogWatchFiles = Nothing -- watch everything
, changelogIgnoreFiles = Nothing -- ignore nothing
, changelogVersionFiles = Just [VersionFile "package.yaml" "version:"]
, changelogDefault = True
}
, configIgnoreCommits = Nothing
, configBranch = Nothing
Expand Down
56 changes: 37 additions & 19 deletions src/Changelogged/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Changelogged.Main where

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

import Control.Exception
import Data.Maybe (fromMaybe)
import Data.Text (unpack, pack)

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

Expand All @@ -15,28 +17,45 @@ import Changelogged.Bump.Common
import Changelogged.Git
import Changelogged.Options
import Changelogged.Utils
import Changelogged.Pure (showText)
import Changelogged.Types (Level)
import Changelogged.Pure (showText, showPath, changeloggedVersion)
import Changelogged.Config

defaultMain :: IO ()
defaultMain = do
-- parse command line options
opts@Options{..} <- parseOptions
-- load config file (or default config)
config@Config{..} <- fromMaybe defaultConfig <$> loadConfig ".changelogged.yaml"
-- load git info
gitInfo <- loadGitInfo optFromBC configBranch
coloredPrint Blue (ppConfig config)
coloredPrint Blue (ppGitInfo gitInfo)
-- process changelogs
processChangelogs config opts gitInfo

if optVersion
then versionP changeloggedVersion
else do
-- load config file (or default config)
let configPath = fromMaybe ".changelogged.yaml" (unpack . showPath <$> optConfigPath)
config@Config{..} <- fromMaybe defaultConfig <$> loadConfig configPath
-- load git info
gitInfo <- loadGitInfo optFromBC configBranch
if config == defaultConfig
then coloredPrint Blue "Using default config.\n"
else coloredPrint Blue ("Configuration file: " <> pack configPath <> "\n")
coloredPrint Blue (ppConfig config)
coloredPrint Blue (ppGitInfo gitInfo)
-- process changelogs
processChangelogs config opts gitInfo

processChangelogs :: Config -> Options -> GitInfo -> IO ()
processChangelogs config opts gitInfo = do
mapM_ (processChangelog opts gitInfo) (configChangelogs config)
processChangelogs config opts@Options{..} gitInfo = case optTargetChangelog of
Nothing -> do
mapM_ (processChangelog opts gitInfo optChangeLevel) (filter (\entry -> changelogDefault entry == True) $ configChangelogs config)
mapM_ (processChangelog opts gitInfo Nothing) (filter (\entry -> changelogDefault entry /= True) $ configChangelogs config)
Just changelogPath -> do
case lookupChangelog changelogPath of
Just changelog -> processChangelog opts gitInfo optChangeLevel changelog
Nothing -> failure $ "Given target changelog " <> format fp changelogPath <> " is missed in config or mistyped."
where
lookupChangelog path = find (\entry -> changelogChangelog entry == path) (configChangelogs config)

processChangelog :: Options -> GitInfo -> ChangelogConfig -> IO ()
processChangelog opts@Options{..} gitInfo config@ChangelogConfig{..} = do
processChangelog :: Options -> GitInfo -> Maybe Level -> ChangelogConfig -> IO ()
processChangelog opts@Options{..} gitInfo level config@ChangelogConfig{..} = do
putStrLn ""
info $ "processing " <> format fp changelogChangelog
changelogExists <- testfile changelogChangelog
Expand All @@ -57,12 +76,12 @@ processChangelog opts@Options{..} gitInfo config@ChangelogConfig{..} = do
| not upToDate && optForce ->
warning $ format fp changelogChangelog <> " is out of date. Bumping versions anyway due to --force."
| otherwise -> (do
newVersion <- if optNoCheck
then do
newVersion <- case (optNoCheck, level) of
(_, Just lev) -> generateLocalVersion lev config
(True, Nothing) -> do
failure "cannot infer new version from changelog because of --no-check.\nUse explicit --level CHANGE_LEVEL."
return Nothing
else do
generateLocalVersionByChangelog config
(False, Nothing) -> generateLocalVersionByChangelog config

case newVersion of
Nothing -> return ()
Expand All @@ -72,4 +91,3 @@ processChangelog opts@Options{..} gitInfo config@ChangelogConfig{..} = do
headChangelog version changelogChangelog
Nothing -> warning "no files specified to bump versions in"
) `catch` (\(ex :: PatternMatchFail) -> failure (showText ex))

29 changes: 28 additions & 1 deletion src/Changelogged/Options.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{-# LANGUAGE FlexibleContexts #-}
module Changelogged.Options where

import Data.Char (toLower)
import Data.List (intercalate)
import Data.Monoid ((<>))
import Data.String.Conversions (cs)

import Options.Applicative
import qualified Turtle

import Filesystem.Path.CurrentOS (valid, fromText)

import Changelogged.Types

-- |
Expand Down Expand Up @@ -62,6 +66,13 @@ readLevel = eitherReader (r . map toLower)
"Unknown level of changes: " <> show lvl <> ".\n"
<> "Should be " <> availableLevelsStr <> ".\n"

readFilePath :: ReadM Turtle.FilePath
readFilePath = eitherReader r
where
r filePathString = if valid $ fromText $ cs filePathString
then Right (fromText $ cs filePathString)
else Left ("Invalid file path " <> filePathString <> ".\n")

parser :: Parser Options
parser = Options
<$> warningFormat
Expand All @@ -73,6 +84,9 @@ parser = Options
"Look for missing changelog entries from the start of the project."
<*> hiddenSwitch "force" "Bump versions ignoring possibly outdated changelogs."
<*> hiddenSwitch "no-check" "Do not check if changelogs have any missing entries."
<*> optional targetChangelog
<*> optional configPath
<*> hiddenSwitch "version" "Print version."
where
longSwitch name description = switch $
long name
Expand All @@ -87,7 +101,7 @@ parser = Options
long "level"
<> metavar "CHANGE_LEVEL"
<> help (unlines
[ "Level of changes (to override one inferred from changelogs)."
[ "Level of changes (to override one inferred from changelog)."
, "CHANGE_LEVEL can be " <> availableLevelsStr <> "."
])
<> hidden
Expand All @@ -97,6 +111,13 @@ parser = Options
<> 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"
<> help ("Path to config file.")

welcome :: Turtle.Description
welcome = Turtle.Description "changelogged - Changelog Manager for Git Projects"
Expand All @@ -117,6 +138,12 @@ data Options = Options
, optForce :: Bool
-- | Do not check if changelogs have any missing entries.
, optNoCheck :: Bool
-- | Check exactly one target changelog.
, optTargetChangelog :: Maybe Turtle.FilePath
-- | Use specified config file.
, optConfigPath :: Maybe Turtle.FilePath
-- | Print version
, optVersion :: Bool
}

-- | Parse command line options.
Expand Down
7 changes: 0 additions & 7 deletions src/Changelogged/Pattern.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ versionMatch str = maxByLen $ match (has versionExactRegex) str
hashRegex :: Pattern Text
hashRegex = prefix $ between (within 0 chars) spaces1 (plus (digit <|> lower))

-- >>> match hashGrepExclude "ee17741 Merge pull request #38 from GetShopTV/redesign-strategies"
-- []
-- >>> match hashGrepExclude "ee17741 Reference pull request #38 from GetShopTV/redesign-strategies"
-- [()]
hashGrepExclude :: Pattern ()
hashGrepExclude = invert (hashRegex <> spaces <> text "Merge")

-- >>> hashMatch "f4875f4 Update changelog"
-- Just "f4875f4"
-- >>> hashMatch "Update changelog"
Expand Down
9 changes: 3 additions & 6 deletions src/Changelogged/Pure.hs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
module Changelogged.Pure where

import Prelude hiding (FilePath)
import qualified Data.HashMap.Strict as HM
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as Text

import Filesystem.Path.CurrentOS (encodeString, FilePath)

import Changelogged.Types

changeloggedVersion :: Text
changeloggedVersion = "0.2.0"

-- | Maximum in list ordered by length.
maxByLen :: [Text] -> Maybe Text
maxByLen [] = Nothing
maxByLen hs = Just $ foldl1 (\left right -> if Text.length left > Text.length right then left else right) hs

--
defaultedEmpty :: Maybe (HM.HashMap k v) -> HM.HashMap k v
defaultedEmpty = fromMaybe HM.empty

-- |'@fromJust@' function with custom error message.
fromJustCustom :: String -> Maybe a -> a
fromJustCustom msg Nothing = error msg
Expand Down
4 changes: 4 additions & 0 deletions src/Changelogged/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ failure msg = coloredPrint Red $
info :: Text -> IO ()
info msg = coloredPrint Cyan $
"INFO: " <> msg <> "\n"

versionP :: Text -> IO ()
versionP ver = coloredPrint Green $
"VERSION: " <> ver <> "\n"

0 comments on commit fd19a98

Please sign in to comment.