Skip to content

Commit

Permalink
Initial work in a new change logs management
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Belloy <[email protected]>
  • Loading branch information
pbe-axelor authored and admin committed Sep 19, 2019
1 parent 3616750 commit dfec37e
Show file tree
Hide file tree
Showing 13 changed files with 523 additions and 2 deletions.
3 changes: 2 additions & 1 deletion axelor-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ dependencies {

compile project(':axelor-common')

compile libs.guava
compile libs.guava
compile libs.opencsv
compile libs.plugin_license
compile libs.snakeyaml

tomcat libs.hotswap_agent
tomcat project(":axelor-tomcat")
Expand Down
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);
}
}
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
Loading

0 comments on commit dfec37e

Please sign in to comment.