Skip to content

Commit

Permalink
Implement line limit; resolves #34
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRandomLabs committed Feb 6, 2021
1 parent e4b7d16 commit 83b873a
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 114 deletions.
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
group = "com.therandomlabs.changeloggenerator"
version = "2.0.0-pre6"
version = "2.0.0-pre7"

ext {
commonGradleBranch = "master"
mainClass = "${group}.ChangelogGeneratorOptions"
mainClass = "${group}.Main"
jacocoMinimumInstructionCoverage = 0.8
jacocoExcludes = [
"com.therandomlabs.changeloggenerator.ChangelogEntry",
"com.therandomlabs.changeloggenerator.ChangelogGeneratorOptions"
"com.therandomlabs.changeloggenerator.ChangelogGeneratorOptions",
"com.therandomlabs.changeloggenerator.Main"
]
}

Expand All @@ -31,5 +33,5 @@ dependencies {
}

compileJava {
options.compilerArgs += "-Aproject=${project.group}/${project.name}"
options.compilerArgs += "-Aproject=${project.group}/${project.name}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

package com.therandomlabs.changeloggenerator;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand All @@ -32,6 +32,7 @@
import java.util.stream.Collectors;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.therandomlabs.curseapi.CurseAPI;
import com.therandomlabs.curseapi.CurseException;
import com.therandomlabs.curseapi.file.BasicCurseFile;
Expand All @@ -47,11 +48,6 @@
* A basic implementation of {@link ChangelogGenerator} that generates a plaintext changelog.
*/
public class BasicChangelogGenerator extends ChangelogGenerator {
/**
* The singleton instance of {@link BasicChangelogGenerator}.
*/
public static final BasicChangelogGenerator instance = new BasicChangelogGenerator();

/**
* A {@link Splitter} that works on line separators.
* Empty strings are omitted, and results are trimmed.
Expand All @@ -60,20 +56,29 @@ public class BasicChangelogGenerator extends ChangelogGenerator {
omitEmptyStrings().
trimResults();

private final ChangelogGeneratorOptions options;

/**
* Constructs a {@link BasicChangelogGenerator}.
*/
public BasicChangelogGenerator() {
this(new ChangelogGeneratorOptions());
}

/**
* Constructs a {@link BasicChangelogGenerator}.
*
* @param options ChangelogGenerator options.
*/
protected BasicChangelogGenerator() {
//Default constructor.
public BasicChangelogGenerator(ChangelogGeneratorOptions options) {
this.options = options;
}

/**
* {@inheritDoc}
*/
@Override
public String generate(
CurseModpack oldModpack, CurseModpack newModpack, ChangelogGeneratorOptions options
) throws CurseException {
public String generate(CurseModpack oldModpack, CurseModpack newModpack) throws CurseException {
final CurseFilesComparison<BasicCurseFile> comparison =
CurseFilesComparison.of(oldModpack.files(), newModpack.files());
final StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -105,6 +110,15 @@ public String generate(
return builder.toString();
}

/**
* Returns ChangelogGenerator options.
*
* @return a {@link ChangelogGeneratorOptions} instance.
*/
protected ChangelogGeneratorOptions getOptions() {
return options;
}

/**
* Separates two different changelog sections, e.g. "Updated" and "Downgraded".
* By default, this is done by appending {@link System#lineSeparator()} twice.
Expand Down Expand Up @@ -235,12 +249,22 @@ protected void appendChangelogEntries(
for (ChangelogEntry entry : changelogEntries.entries()) {
builder.append(System.lineSeparator()).append("\t\t").append(entry.title()).append(':');

Iterable<String> entryLines;
List<String> entryLines;

if (JsoupUtils.isEmpty(entry.entry())) {
entryLines = Collections.singleton("No changelog available.");
entryLines = Lists.newArrayList("No changelog available.");
} else {
entryLines = LINE_SEPARATOR_SPLITTER.split(JsoupUtils.getPlainText(entry.entry()));
entryLines = Lists.newArrayList(
LINE_SEPARATOR_SPLITTER.split(JsoupUtils.getPlainText(entry.entry()))
);
}

final int maxLines = getOptions().maxEntryLineCount;

if (maxLines != 0 && entryLines.size() > maxLines) {
final int extra = entryLines.size() - maxLines;
entryLines.set(maxLines, "(" + extra + " more line" + (extra == 1 ? ")" : "s)"));
entryLines = entryLines.subList(0, maxLines + 1);
}

for (String line : entryLines) {
Expand Down Expand Up @@ -268,7 +292,7 @@ protected void appendChangelogEntries(
* @param builder a {@link StringBuilder} to append to.
*/
protected void appendTail(StringBuilder builder) {
builder.append("Generated using [ChangelogGenerator ").append(VERSION).
append("](https://github.com/TheRandomLabs/ChangelogGenerator).");
builder.append("Generated using ChangelogGenerator ").append(VERSION).
append(": https://github.com/TheRandomLabs/ChangelogGenerator");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,8 @@ public ChangelogEntries getChangelogEntries(
* @return the generated changelog.
* @throws CurseException if an error occurs.
*/
public String generate(CurseModpack oldModpack, CurseModpack newModpack) throws CurseException {
return generate(oldModpack, newModpack, new ChangelogGeneratorOptions());
}

/**
* Generates a changelog that details changes between an older and newer version of a
* CurseForge modpack.
*
* @param oldModpack a {@link CurseModpack} instance that represents an older version of a
* CurseForge modpack.
* @param newModpack a {@link CurseModpack} instance that represents a newer version of a
* CurseForge modpack.
* @param options ChangelogGenerator options.
* @return the generated changelog.
* @throws CurseException if an error occurs.
*/
public abstract String generate(
CurseModpack oldModpack, CurseModpack newModpack, ChangelogGeneratorOptions options
) throws CurseException;
public abstract String generate(CurseModpack oldModpack, CurseModpack newModpack)
throws CurseException;

/**
* Registers the specified {@link ChangelogProvider} to this {@link ChangelogGenerator}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,102 +24,74 @@
package com.therandomlabs.changeloggenerator;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Callable;

import com.therandomlabs.curseapi.minecraft.modpack.CurseModpack;
import okio.BufferedSink;
import okio.Okio;
import org.checkerframework.checker.nullness.qual.Nullable;
import picocli.CommandLine;

/**
* Options for ChangelogGenerator.
* This class also contains the main method for the runnable ChangelogGenerator application.
*/
@CommandLine.Command(
mixinStandardHelpOptions = true, version = ChangelogGenerator.VERSION,
description = "Generates changelogs for CurseForge modpacks."
)
public final class ChangelogGeneratorOptions implements Callable<Integer> {
@CommandLine.Option(
names = {"-l", "--lines"},
description = "The maximum number of lines to display in a changelog entry."
)
public int maxEntryLineCount;

@CommandLine.Option(
names = {"-m", "--markdown"},
description = "Generate a Markdown changelog."
)
boolean markdown;

@CommandLine.Option(
names = {"-o", "--old-manifest"},
description = "The old modpack manifest. \"old.json\" by default."
)
@Nullable
public Path oldManifest;
Path oldManifest;

@CommandLine.Option(
names = {"-n", "--new-manifest"},
description = "The new modpack manifest. \"new.json\" by default."
)
@Nullable
public Path newManifest;
Path newManifest;

@CommandLine.Option(
names = {"-O", "--output"},
description = "The changelog output location. \"changelog.txt\" or " +
"\"changelog.md\" by default."
)
@Nullable
public Path output;

@CommandLine.Option(
names = {"-l", "--lines"},
description = "The maximum number of lines to display in a changelog entry."
)
public int maxEntryLineCount;
Path output;

@CommandLine.Option(
names = {"-m", "--markdown"},
description = "Generate a Markdown changelog."
)
public boolean markdown;
@Nullable
private final Callable<Integer> callable;

/**
* {@inheritDoc}
* Constructs a {@link ChangelogGeneratorOptions}.
*/
@Override
public Integer call() throws Exception {
if (oldManifest == null) {
oldManifest = Paths.get("old.json");
}

if (newManifest == null) {
newManifest = Paths.get("new.json");
}

if (output == null) {
output = Paths.get(markdown ? "changelog.md" : "changelog.txt");
}

if (maxEntryLineCount < 0) {
throw new IllegalArgumentException("Cannot display negative number of lines");
}

final CurseModpack oldModpack = CurseModpack.fromJSON(oldManifest);
final CurseModpack newModpack = CurseModpack.fromJSON(newManifest);
final ChangelogGenerator generator =
markdown ? MarkdownChangelogGenerator.instance : BasicChangelogGenerator.instance;
final String changelog = generator.generate(oldModpack, newModpack, this);

try (BufferedSink sink = Okio.buffer(Okio.sink(output))) {
sink.writeUtf8(changelog).writeUtf8(System.lineSeparator());
}
public ChangelogGeneratorOptions() {
this(null);
}

return 0;
ChangelogGeneratorOptions(@Nullable Callable<Integer> callable) {
this.callable = callable;
}

/**
* The main method for the runnable ChangelogGenerator application.
* This should not be called directly.
*
* @param args the command-line arguments.
* @return an exit code.
*/
public static void main(String[] args) {
System.exit(
new CommandLine(new ChangelogGeneratorOptions()).
registerConverter(Path.class, Paths::get).
execute(args)
);
@Override
public Integer call() throws Exception {
return callable == null ? Integer.valueOf(0) : callable.call();
}
}
96 changes: 96 additions & 0 deletions src/main/java/com/therandomlabs/changeloggenerator/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019-2020 TheRandomLabs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.therandomlabs.changeloggenerator;

import java.nio.file.Path;
import java.nio.file.Paths;

import com.therandomlabs.curseapi.minecraft.modpack.CurseModpack;
import okio.BufferedSink;
import okio.Okio;
import org.checkerframework.checker.nullness.qual.Nullable;
import picocli.CommandLine;

/**
* The main class for the runnable ChangelogGenerator application.
*/
public final class Main {
@Nullable
private static ChangelogGeneratorOptions options;

@Nullable
private static CommandLine commandLine;

private Main() {}

/**
* The main method for the runnable ChangelogGenerator application.
*
* @param args the command-line arguments.
*/
public static void main(String[] args) {
options = new ChangelogGeneratorOptions(Main::run);
commandLine = new CommandLine(options).registerConverter(Path.class, Paths::get);
System.exit(commandLine.execute(args));
}

private static Integer run() throws Exception {
if (options == null) {
throw new IllegalStateException("options should not be null");
}

if (commandLine == null) {
throw new IllegalStateException("commandLine should not be null");
}

if (options.oldManifest == null) {
options.oldManifest = Paths.get("old.json");
}

if (options.newManifest == null) {
options.newManifest = Paths.get("new.json");
}

if (options.output == null) {
options.output = Paths.get(options.markdown ? "changelog.md" : "changelog.txt");
}

if (options.maxEntryLineCount < 0) {
System.err.println("Cannot display negative number of lines");
return 1;
}

final CurseModpack oldModpack = CurseModpack.fromJSON(options.oldManifest);
final CurseModpack newModpack = CurseModpack.fromJSON(options.newManifest);
final ChangelogGenerator generator = options.markdown ?
new MarkdownChangelogGenerator(options) : new BasicChangelogGenerator(options);
final String changelog = generator.generate(oldModpack, newModpack);

try (BufferedSink sink = Okio.buffer(Okio.sink(options.output))) {
sink.writeUtf8(changelog).writeUtf8(System.lineSeparator());
}

return 0;
}
}
Loading

0 comments on commit 83b873a

Please sign in to comment.