Skip to content

Commit

Permalink
Auto-generate licenses text with Gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
bwRavencl committed Aug 17, 2024
1 parent fe3b661 commit ab6b1a8
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2,422 deletions.
47 changes: 45 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep
import com.github.spotbugs.snom.Confidence
import com.github.spotbugs.snom.Effort
import com.github.spotbugs.snom.SpotBugsTask
import java.util.stream.Collectors
import org.ajoberstar.grgit.Grgit
import org.gradle.nativeplatform.platform.internal.Architectures
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform

plugins {
id 'application'
id 'com.benjaminsproule.license-report' version '0.16.2'
id 'com.diffplug.spotless' version '6.25.0'
id 'com.github.spotbugs' version '6.0.20'
id 'net.ltgt.errorprone' version '4.0.1'
Expand Down Expand Up @@ -279,16 +281,57 @@ spotless {
}
}

downloadLicenses {
def mit = license('MIT', 'https://opensource.org/license/mit/')

includeProjectDependencies = true
ignoreFatalParseErrors = false
licenses = ['com.github.strikerx3:jxinput:1.0.0': mit]
// @formatter:off
aliases = [(license('Apache-2.0', 'https://www.apache.org/licenses/LICENSE-2.0')): [
'Apache-2.0',
'Apache License, Version 2.0',
'The Apache License, Version 2.0',
'The Apache Software License, Version 2.0'
], (mit): [
'MIT',
'The MIT License (MIT)'
]]
// @formatter:on
dependencyConfiguration = 'runtimeClasspath'

report {
reportByLicenseType = false
json.enabled = false
xml.enabled = false
}
}

tasks.register('generateMetadata') {
dependsOn 'downloadLicenses'
description = "Generates the \'$metadataFile\' source file"
doLast {
def dependencyLicenseHtml = layout.buildDirectory.file('reports/license/dependency-license.html').get().getAsFile().text.replaceFirst('<style>[\\s\\S]*</style>', '').replaceAll('>\\s*<', '><')
def projectLicenseHtml = layout.projectDirectory.file('LICENSE').getAsFile().readLines().collect({ it.trim().codePoints().mapToObj({ it > 127 || '"\'<>&'.indexOf(it) != -1 ? "&#$it;" : new String(Character.toChars(it)) }).collect(Collectors.joining()) }).join '<br>'

def licensesHtmlStringBuilder = new StringBuilder(dependencyLicenseHtml)
def bodyTag = '<body>'
licensesHtmlStringBuilder.insert(licensesHtmlStringBuilder.indexOf(bodyTag) + bodyTag.length(), "<center><h1>$application.applicationName License:</h1><p>$projectLicenseHtml</p><br><h1>Third-Party Licenses:</h1>")
licensesHtmlStringBuilder.insert(licensesHtmlStringBuilder.lastIndexOf('</body>'), '</center>')

new File(metadataFile).write("""\
package de.bwravencl.controllerbuddy.metadata;\n
public class Metadata {
public static final String APPLICATION_NAME = "$application.applicationName";
public static final String APPLICATION_NAME = "$application.applicationName";
public static final String VERSION = "$project.version";
public static final String LICENSES_HTML;
public static final String VERSION = "$project.version";
static {
LICENSES_HTML = "${licensesHtmlStringBuilder.toString()}";
}
}
""".stripIndent())
}
Expand Down
89 changes: 58 additions & 31 deletions src/main/java/de/bwravencl/controllerbuddy/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Dialog;
import java.awt.Dimension;
Expand Down Expand Up @@ -153,6 +154,7 @@
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
Expand Down Expand Up @@ -180,6 +182,7 @@
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.DefaultFormatter;
Expand Down Expand Up @@ -311,7 +314,6 @@ public final class Main {
private static final String[] ICON_RESOURCE_PATHS = { "/icon_16.png", "/icon_32.png", "/icon_64.png",
"/icon_128.png" };
private static final String TRAY_ICON_HINT_IMAGE_RESOURCE_PATH = "/tray_icon_hint.png";
private static final String LICENSES_FILENAME = "licenses.txt";
private static final String CONTROLLER_SVG_FILENAME = "controller.svg";
private static final String GAME_CONTROLLER_DATABASE_FILENAME = "gamecontrollerdb.txt";
private static final String VJOY_GUID = "0300000034120000adbe000000000000";
Expand Down Expand Up @@ -585,9 +587,7 @@ public void windowOpened(final WindowEvent e) {
final var helpMenu = new JMenu(strings.getString("HELP_MENU"));
menuBar.add(helpMenu);
helpMenu.add(new ShowLicensesAction());
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
helpMenu.add(new ShowWebsiteAction());
}
helpMenu.add(new ShowWebsiteAction());
helpMenu.add(new ShowAboutDialogAction());

frame.getContentPane().add(tabbedPane);
Expand Down Expand Up @@ -620,8 +620,13 @@ public void windowOpened(final WindowEvent e) {
indicatorsScrollPane = new JScrollPane();
overlayPanel.add(indicatorsScrollPane, BorderLayout.CENTER);

final var controllerSvgInputStream = ClassLoader.getSystemResourceAsStream(CONTROLLER_SVG_FILENAME);
if (controllerSvgInputStream == null) {
throw new RuntimeException("Resource not found " + CONTROLLER_SVG_FILENAME);
}

try (final var bufferedReader = new BufferedReader(
new InputStreamReader(getResourceAsStream(CONTROLLER_SVG_FILENAME), StandardCharsets.UTF_8))) {
new InputStreamReader(controllerSvgInputStream, StandardCharsets.UTF_8))) {
final var svgDocumentFactory = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
templateSvgDocument = (SVGDocument) svgDocumentFactory.createDocument(null, bufferedReader);
} catch (final IOException e) {
Expand Down Expand Up @@ -1173,15 +1178,6 @@ public static List<ControllerInfo> getPresentControllers() {
return presentControllers;
}

private static InputStream getResourceAsStream(final String resourcePath) {
final var resourceInputStream = ClassLoader.getSystemResourceAsStream(resourcePath);
if (resourceInputStream == null) {
throw new RuntimeException("Resource not found " + resourcePath);
}

return resourceInputStream;
}

private static URL getResourceLocation(final String resourcePath) {
final var resourceLocation = Main.class.getResource(resourcePath);
if (resourceLocation == null) {
Expand Down Expand Up @@ -1331,6 +1327,20 @@ public static void main(final String[] args) {
printCommandLineMessage(stringWriter.toString());
}

private static void openBrowser(final Component parentComponent, final URI uri) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
try {
Desktop.getDesktop().browse(uri);
} catch (final IOException e) {
throw new RuntimeException(e);
}
} else {
JOptionPane.showMessageDialog(parentComponent,
MessageFormat.format(strings.getString("PLEASE_VISIT_DIALOG_TEXT"), uri),
strings.getString("INFORMATION_DIALOG_TITLE"), JOptionPane.INFORMATION_MESSAGE);
}
}

private static void printCommandLineMessage(final String message) {
System.out.println(message);

Expand Down Expand Up @@ -3625,9 +3635,9 @@ private ShowWebsiteAction() {
@Override
public void actionPerformed(final ActionEvent e) {
try {
Desktop.getDesktop().browse(new URI(WEBSITE_URL));
} catch (final IOException | URISyntaxException e1) {
log.log(Level.SEVERE, e1.getMessage(), e1);
openBrowser(main.frame, new URI(WEBSITE_URL));
} catch (final URISyntaxException e1) {
throw new RuntimeException(e1);
}
}
}
Expand Down Expand Up @@ -4284,20 +4294,37 @@ private ShowLicensesAction() {
}

@Override
public void actionPerformed(final ActionEvent e) {
try (final var bufferedReader = new BufferedReader(
new InputStreamReader(getResourceAsStream(LICENSES_FILENAME), StandardCharsets.UTF_8))) {
final var text = bufferedReader.lines().collect(Collectors.joining("\n"));
final var textArea = new JTextArea(text);
textArea.setLineWrap(true);
textArea.setEditable(false);
final var scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(600, 400));
GuiUtils.showMessageDialog(main, frame, scrollPane, (String) getValue(NAME),
JOptionPane.DEFAULT_OPTION);
} catch (final IOException e1) {
throw new RuntimeException(e1);
}
public void actionPerformed(final ActionEvent actionEvent) {
final var editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditable(false);
editorPane.setCaretColor(editorPane.getBackground());

final var scrollPane = new JScrollPane(editorPane);
scrollPane.setPreferredSize(new Dimension(850, 400));

editorPane.addHyperlinkListener(hyperlinkEvent -> {
if (hyperlinkEvent.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
return;
}

final var description = hyperlinkEvent.getDescription();
if (description != null && description.startsWith("#")) {
editorPane.scrollToReference(description.substring(1));
return;
}

try {
openBrowser(scrollPane, hyperlinkEvent.getURL().toURI());
} catch (final URISyntaxException e) {
throw new RuntimeException(e);
}
});

editorPane.setText(Metadata.LICENSES_HTML);
editorPane.setCaretPosition(0);

GuiUtils.showMessageDialog(main, frame, scrollPane, (String) getValue(NAME), JOptionPane.DEFAULT_OPTION);
}
}

Expand Down
Loading

0 comments on commit ab6b1a8

Please sign in to comment.