forked from axelor/axelor-open-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work in a new change logs management
Signed-off-by: Pierre Belloy <[email protected]>
- Loading branch information
1 parent
3616750
commit dfec37e
Showing
13 changed files
with
523 additions
and
2 deletions.
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
139 changes: 139 additions & 0 deletions
139
axelor-gradle/src/main/java/com/axelor/gradle/tasks/GenerateChangeLog.java
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 |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Axelor Business Solutions | ||
* | ||
* Copyright (C) 2005-2019 Axelor (<http://axelor.com>). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.axelor.gradle.tasks; | ||
|
||
import com.axelor.common.ObjectUtils; | ||
import com.axelor.common.VersionUtils; | ||
import com.axelor.tools.changelog.ChangelogEntry; | ||
import com.axelor.tools.changelog.ChangelogEntryParser; | ||
import com.axelor.tools.changelog.Release; | ||
import com.axelor.tools.changelog.ReleaseGenerator; | ||
import com.axelor.tools.changelog.ReleaseProcessor; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.file.FileTree; | ||
import org.gradle.api.internal.tasks.options.Option; | ||
import org.gradle.api.tasks.InputFiles; | ||
import org.gradle.api.tasks.SkipWhenEmpty; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class GenerateChangeLog extends DefaultTask { | ||
|
||
private final Logger LOG = LoggerFactory.getLogger(this.getClass()); | ||
|
||
private static final String CHANGELOG_FILENAME = "CHANGELOG.md"; | ||
|
||
private String version = VersionUtils.getVersion().version.replace("-SNAPSHOT", ""); | ||
|
||
private boolean preview; | ||
|
||
@Option(option = "preview", description = "Preview mode.") | ||
public void setPreview(boolean preview) { | ||
this.preview = preview; | ||
} | ||
|
||
@InputFiles @SkipWhenEmpty private FileTree files; | ||
|
||
public FileTree getFiles() { | ||
return files; | ||
} | ||
|
||
public void setFiles(FileTree files) { | ||
this.files = files; | ||
} | ||
|
||
@TaskAction | ||
public void generate() throws IOException { | ||
List<ChangelogEntry> entries = getChangelogEntries(); | ||
if(ObjectUtils.isEmpty(entries)) { | ||
getLogger().info("No change log entries to process. Skipping."); | ||
return; | ||
} | ||
|
||
String newChangelog = generate(entries); | ||
|
||
if(preview) { | ||
System.out.println("Generated change log : "); | ||
System.out.println("--------------------"); | ||
System.out.println(newChangelog); | ||
System.out.println("--------------------"); | ||
return; | ||
} | ||
|
||
write(newChangelog); | ||
clean(); | ||
} | ||
|
||
private List<ChangelogEntry> getChangelogEntries() throws IOException { | ||
ChangelogEntryParser parser = new ChangelogEntryParser(); | ||
List<ChangelogEntry> entries = new ArrayList<>(); | ||
for (File file : getFiles()) { | ||
entries.add(parser.parse(file)); | ||
} | ||
return entries; | ||
} | ||
|
||
private String generate(List<ChangelogEntry> entries) { | ||
ReleaseProcessor processor = new ReleaseProcessor(); | ||
Release release = processor.process(entries, version); | ||
|
||
ReleaseGenerator generator = new ReleaseGenerator(); | ||
return generator.generate(release); | ||
} | ||
|
||
private void write(String newChangelog) throws IOException { | ||
File mFile = new File(CHANGELOG_FILENAME); | ||
|
||
StringBuilder contentBuilder = new StringBuilder(); | ||
try (BufferedReader br = new BufferedReader(new FileReader(mFile))) { | ||
|
||
String sCurrentLine; | ||
while ((sCurrentLine = br.readLine()) != null) { | ||
contentBuilder.append(sCurrentLine).append(System.lineSeparator()); | ||
} | ||
} | ||
|
||
mFile.delete(); | ||
|
||
try (FileOutputStream fos = new FileOutputStream(mFile)) { | ||
fos.write((newChangelog + contentBuilder.toString()).getBytes()); | ||
fos.flush(); | ||
} | ||
} | ||
|
||
private void clean() { | ||
for (File file : getFiles()) { | ||
try { | ||
Files.delete(file.toPath()); | ||
} catch (IOException ex) { | ||
throw new GradleException("Could not delete file: " + file, ex); | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
axelor-gradle/src/main/java/com/axelor/tools/changelog/ChangelogEntry.java
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Axelor Business Solutions | ||
* | ||
* Copyright (C) 2005-2019 Axelor (<http://axelor.com>). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.axelor.tools.changelog; | ||
|
||
public class ChangelogEntry { | ||
|
||
private String title; | ||
private EntryType type; | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public EntryType getType() { | ||
return type; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setType(EntryType type) { | ||
this.type = type; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
axelor-gradle/src/main/java/com/axelor/tools/changelog/ChangelogEntryParser.java
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Axelor Business Solutions | ||
* | ||
* Copyright (C) 2005-2019 Axelor (<http://axelor.com>). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.axelor.tools.changelog; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
public class ChangelogEntryParser { | ||
|
||
public ChangelogEntry parse(File file) throws IOException { | ||
return createEntry(loadYaml(file)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Map<String, Object> loadYaml(File file) throws IOException { | ||
Yaml yaml = new Yaml(); | ||
try (InputStream ios = new FileInputStream(file)) { | ||
return (Map<String, Object>) yaml.load(ios); | ||
} | ||
} | ||
|
||
private ChangelogEntry createEntry(Map<String, Object> entries) { | ||
ChangelogEntry changelogEntry = new ChangelogEntry(); | ||
for (Map.Entry<String, Object> item : entries.entrySet()) { | ||
String value = item.getValue().toString(); | ||
if ("title".equalsIgnoreCase(item.getKey())) { | ||
changelogEntry.setTitle(value); | ||
} else if ("type".equalsIgnoreCase(item.getKey())) { | ||
changelogEntry.setType(EntryType.valueOf(value.toUpperCase())); | ||
} | ||
} | ||
return changelogEntry; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
axelor-gradle/src/main/java/com/axelor/tools/changelog/EntryType.java
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Axelor Business Solutions | ||
* | ||
* Copyright (C) 2005-2019 Axelor (<http://axelor.com>). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.axelor.tools.changelog; | ||
|
||
public enum EntryType { | ||
ADDED("Added"), | ||
CHANGED("Changed"), | ||
DEPRECATED("Deprecated"), | ||
REMOVED("Removed"), | ||
FIXED("Fixed"), | ||
SECURITY("Security"); | ||
|
||
private final String value; | ||
|
||
private EntryType(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
axelor-gradle/src/main/java/com/axelor/tools/changelog/Release.java
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Axelor Business Solutions | ||
* | ||
* Copyright (C) 2005-2019 Axelor (<http://axelor.com>). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.axelor.tools.changelog; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Release { | ||
|
||
private String version; | ||
private String date; | ||
private Map<EntryType, List<ChangelogEntry>> entries; | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public void setVersion(String version) { | ||
this.version = version; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
|
||
public void setDate(String date) { | ||
this.date = date; | ||
} | ||
|
||
public Map<EntryType, List<ChangelogEntry>> getEntries() { | ||
return entries; | ||
} | ||
|
||
public void setEntries(Map<EntryType, List<ChangelogEntry>> entries) { | ||
this.entries = entries; | ||
} | ||
} |
Oops, something went wrong.