Skip to content

Commit 2df97ee

Browse files
committed
Add separate renderers for GitChangelog
- Refactored filename of GitChangelog.php - Renderer interface added. - Markdown renderer moved from GitChangelog to its own class. - Html interface added.
1 parent f8ec9f7 commit 2df97ee

File tree

4 files changed

+379
-139
lines changed

4 files changed

+379
-139
lines changed

src/GitChangeLog.php renamed to src/GitChangelog.php

Lines changed: 33 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -77,69 +77,19 @@
7777
*/
7878
class GitChangelog
7979
{
80-
/**
81-
* @var string Format of tag strings. {tag} is replaced by the tags found in the git log, {date} is replaced by the
82-
* corresponding tag date.
83-
*/
84-
public $formatTag = "## {tag} ({date})\n\n";
85-
/**
86-
* @var string Format of hashes. {hashes} is replaced by the concatenated commit hashes.
87-
*/
88-
public $formatHashes = "({hashes})";
89-
/**
90-
* @var string Format of subjects. {subject} is replaced by commit subjects, {hashes} is replaced by the formatted
91-
* commit hashes.
92-
*/
93-
public $formatSubject = "* {subject} {hashes}\n";
9480
/**
9581
* @var string Path to a base (changelog) file. The generated changelog can be prepend this file.
9682
*/
9783
public $baseFile;
98-
/**
99-
* @var string Format of a single commit hash. {hash} is replaced by the commit hash.
100-
*/
101-
public $formatHash = '{hash}';
10284
/**
10385
* @var string Path to local git repository. Leave null for repository at current folder.
10486
*/
10587
public $gitPath;
106-
/**
107-
* @var string Value of the oldest tag to include into the generated changelog.
108-
* @see GitChangelog::setFromTag()
109-
*/
110-
protected $fromTag;
111-
/**
112-
* @var string Value of the newest tag to include into the generated changelog.
113-
* @see GitChangelog::setToTag()
114-
*/
115-
protected $toTag = 'HEAD';
116-
/**
117-
* @var array Contains the tags which exist in the git repository.
118-
* @see GitChangelog::fetchTags();
119-
*/
120-
protected $gitTags;
12188
/**
12289
* @var string The generated changelog.
12390
* @see GitChangelog::build()
12491
*/
12592
protected $changelog;
126-
/**
127-
* @var string[] Contains the labels to filter the commit subjects. All subjects which do not start with any of
128-
* these labels will not be listed. To disable this filtering, remove all labels from this variable.
129-
*/
130-
protected $labels = [
131-
// 'Add', // Create a capability e.g. feature, test, dependency.
132-
// 'Cut', // Remove a capability e.g. feature, test, dependency.
133-
// 'Fix', // Fix an issue e.g. bug, typo, accident, misstatement.
134-
// 'Bump', // Increase the version of something e.g. dependency.
135-
// 'Make', // Change the build process, or tooling, or infra.
136-
// 'Start', // Begin doing something; e.g. create a feature flag.
137-
// 'Stop', // End doing something; e.g. remove a feature flag.
138-
// 'Refactor', // A code change that MUST be just a refactoring.
139-
// 'Reformat', // Refactor of formatting, e.g. omit whitespace.
140-
// 'Optimize', // Refactor of performance, e.g. speed up code.
141-
// 'Document', // Refactor of documentation, e.g. help files.
142-
];
14393
/**
14494
* @var array Contains the (processed) information which is fetched from the git repository.
14595
*/
@@ -161,7 +111,7 @@ class GitChangelog
161111
* @see https://git-scm.com/docs/git-for-each-ref
162112
*/
163113
protected $options = [
164-
'logHeader' => "# Changelog\n\n",
114+
'logHeader' => 'Changelog',
165115
'headSubject' => 'Upcoming changes',
166116
'nextTagDate' => 'Undetermined',
167117
'noChangesMessage' => 'No changes.',
@@ -171,6 +121,38 @@ class GitChangelog
171121
'tagOrderDesc' => true,
172122
'commitOrder' => 'ASC',
173123
];
124+
/**
125+
* @var string Value of the oldest tag to include into the generated changelog.
126+
* @see GitChangelog::setFromTag()
127+
*/
128+
private $fromTag;
129+
/**
130+
* @var string Value of the newest tag to include into the generated changelog.
131+
* @see GitChangelog::setToTag()
132+
*/
133+
private $toTag = 'HEAD';
134+
/**
135+
* @var array Contains the tags which exist in the git repository.
136+
* @see GitChangelog::fetchTags();
137+
*/
138+
private $gitTags;
139+
/**
140+
* @var string[] Contains the labels to filter the commit subjects. All subjects which do not start with any of
141+
* these labels will not be listed. To disable this filtering, remove all labels from this variable.
142+
*/
143+
private $labels = [
144+
// 'Add', // Create a capability e.g. feature, test, dependency.
145+
// 'Cut', // Remove a capability e.g. feature, test, dependency.
146+
// 'Fix', // Fix an issue e.g. bug, typo, accident, misstatement.
147+
// 'Bump', // Increase the version of something e.g. dependency.
148+
// 'Make', // Change the build process, or tooling, or infra.
149+
// 'Start', // Begin doing something; e.g. create a feature flag.
150+
// 'Stop', // End doing something; e.g. remove a feature flag.
151+
// 'Refactor', // A code change that MUST be just a refactoring.
152+
// 'Reformat', // Refactor of formatting, e.g. omit whitespace.
153+
// 'Optimize', // Refactor of performance, e.g. speed up code.
154+
// 'Document', // Refactor of documentation, e.g. help files.
155+
];
174156

175157
/**
176158
* GitChangelog constructor.
@@ -223,66 +205,6 @@ public function fetchTags($force = false): array
223205
return $this->gitTags;
224206
}
225207

226-
227-
/**
228-
* Generate the changelog.
229-
*
230-
* The generated changelog will be stored into a class property.
231-
*
232-
* @throws Exception When the defined From- or To-tag doesn't exist in the git repository.
233-
* @see GitChangelog::changelog
234-
*/
235-
public function build(): void
236-
{
237-
$logContent = $this->options['logHeader'];
238-
$commitData = $this->fetchCommitData();
239-
240-
if (!$commitData) {
241-
$logContent .= $this->options['noChangesMessage'];
242-
$this->changelog = $logContent . "\n";
243-
244-
return;
245-
}
246-
247-
if (!$this->options['tagOrderDesc']) {
248-
$commitData = array_reverse($commitData);
249-
}
250-
251-
// Build changelog.
252-
foreach ($commitData as $tag => &$data) {
253-
// Add tag header and date.
254-
$tagData = [$tag, $data['date']];
255-
if ($tag == 'HEAD') {
256-
$tagData = [$this->options['headSubject'], $this->options['nextTagDate']];
257-
}
258-
259-
$logContent .= str_replace(['{tag}', '{date}'], $tagData, $this->formatTag);
260-
261-
// No subjects present for this tag.
262-
if (!$data['subjects']) {
263-
$subject = $this->options['noChangesMessage'];
264-
$logContent .= str_replace(['{subject}', '{hashes}'], [$subject, ''], $this->formatSubject);
265-
$logContent .= "\n";
266-
continue;
267-
}
268-
269-
// Sort commit subjects.
270-
Utilities::natSort($data['subjects'], $this->options['commitOrder']);
271-
272-
// Add commit subjects.
273-
foreach ($data['subjects'] as $subjectKey => &$subject) {
274-
$logContent .= str_replace(
275-
['{subject}', '{hashes}'],
276-
[$subject, $this->formatHashes($data['hashes'][$subjectKey])],
277-
$this->formatSubject
278-
);
279-
}
280-
$logContent .= "\n";
281-
}
282-
283-
$this->changelog = trim($logContent) . "\n";
284-
}
285-
286208
/**
287209
* Fetch the commit data from the git repository.
288210
*
@@ -395,34 +317,6 @@ private function processCommitData(): void
395317
}
396318
}
397319

398-
/**
399-
* Format the hashes of a commit subject into a string.
400-
*
401-
* Each hash is formatted as defined by property formatHash.
402-
* After formatting, all hashes are concatenated to a single line, comma separated.
403-
* Finally this line is formatted as defined by property formatHashes.
404-
*
405-
* @param array $hashes Hashes to format
406-
*
407-
* @return string Formatted hash string.
408-
* @see GitChangelog::$formatHash
409-
* @see GitChangelog::$formatHashes
410-
*/
411-
protected function formatHashes(array $hashes): string
412-
{
413-
if (!$this->options['addHashes']) {
414-
return '';
415-
}
416-
417-
foreach ($hashes as &$hash) {
418-
$hash = str_replace('{hash}', $hash, $this->formatHash);
419-
}
420-
unset($hash);
421-
$hashes = implode(', ', $hashes);
422-
423-
return str_replace('{hashes}', $hashes, $this->formatHashes);
424-
}
425-
426320
/**
427321
* Save the generated changelog to a file.
428322
*

src/Renderers/Html.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/*
4+
* BSD 3-Clause License
5+
*
6+
* Copyright (c) 2020, Ferry Cools (DigiLive)
7+
* All rights reserved.
8+
*
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice, this
13+
* list of conditions and the following disclaimer.
14+
*
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* 3. Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived from
21+
* this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
*/
35+
36+
namespace DigiLive\GitChangelog\Renderers;
37+
38+
use DigiLive\GitChangelog\GitChangelog;
39+
use DigiLive\GitChangelog\Utilities;
40+
use Exception;
41+
42+
/**
43+
* Class Html
44+
*
45+
* Renderer for GitChangelog.
46+
* The rendered changelog is formatted in markdown.
47+
*
48+
* @package DigiLive\GitChangelog\Renderers
49+
*/
50+
class Html extends GitChangelog implements RendererInterface
51+
{
52+
/**
53+
* @var string Format of a single commit hash. {hash} is replaced by the commit hash.
54+
*/
55+
public $formatHash = '{hash}';
56+
57+
/**
58+
* Generate the changelog.
59+
*
60+
* The generated changelog will be stored into a class property.
61+
*
62+
* @throws Exception When the defined From- or To-tag doesn't exist in the git repository.
63+
*/
64+
public function build(): void
65+
{
66+
$logContent = "<h1>{$this->options['logHeader']}<h1>";
67+
68+
$commitData = $this->fetchCommitData();
69+
70+
if (!$commitData) {
71+
$this->changelog = "<p>$logContent{$this->options['noChangesMessage']}</p>";
72+
}
73+
74+
if (!$this->options['tagOrderDesc']) {
75+
$commitData = array_reverse($commitData);
76+
}
77+
78+
foreach ($commitData as $tag => $data) {
79+
// Add tag header and date.
80+
if ($tag == 'HEAD') {
81+
$tag = $this->options['headSubject'];
82+
$data['date'] = $this->options['nextTagDate'];
83+
}
84+
85+
$logContent .= "<h2>$tag ({$data['date']})</h2><ul>";
86+
87+
// No subjects present for this tag.
88+
if (!$data['subjects']) {
89+
$logContent .= "<li>{$this->options['noChangesMessage']}</li></ul>";
90+
continue;
91+
}
92+
93+
// Sort commit subjects.
94+
Utilities::natSort($data['subjects'], $this->options['commitOrder']);
95+
96+
// Add commit subjects.
97+
foreach ($data['subjects'] as $subjectKey => $subject) {
98+
$logContent .= "<li>$subject (" . $this->formatHashes($data['hashes'][$subjectKey]) . ")</li>";
99+
}
100+
101+
$logContent .= '</ul>';
102+
}
103+
104+
$this->changelog = $logContent;
105+
}
106+
107+
/**
108+
* Format the hashes of a commit subject into a string.
109+
*
110+
* Each hash is formatted as defined by property formatHash.
111+
* After formatting, all hashes are concatenated to a single line, comma separated.
112+
*
113+
* @param array $hashes Hashes to format
114+
*
115+
* @return string Formatted hash string.
116+
* @see GitChangelog::$formatHash
117+
*/
118+
protected function formatHashes(array $hashes): string
119+
{
120+
if (!$this->options['addHashes']) {
121+
return '';
122+
}
123+
124+
foreach ($hashes as &$hash) {
125+
$hash = str_replace('{hash}', $hash, $this->formatHash);
126+
}
127+
unset($hash);
128+
129+
return implode(', ', $hashes);
130+
}
131+
}

0 commit comments

Comments
 (0)