Skip to content

Commit

Permalink
Merge pull request #807 from amvanbaren/feature/issue-777
Browse files Browse the repository at this point in the history
Simplify license logic
  • Loading branch information
amvanbaren authored Oct 5, 2023
2 parents d7fba39 + 800a8ea commit 3e9e023
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 1,727 deletions.
53 changes: 18 additions & 35 deletions server/src/main/java/org/eclipse/openvsx/ExtensionProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
Expand All @@ -44,11 +43,8 @@ public class ExtensionProcessor implements AutoCloseable {
private static final String VSIX_MANIFEST = "extension.vsixmanifest";
private static final String PACKAGE_JSON = "extension/package.json";
private static final String[] README = { "extension/README.md", "extension/README", "extension/README.txt" };
private static final String[] LICENSE = { "extension/LICENSE.md", "extension/LICENSE", "extension/LICENSE.txt" };
private static final String[] CHANGELOG = { "extension/CHANGELOG.md", "extension/CHANGELOG", "extension/CHANGELOG.txt" };

private static final Pattern LICENSE_PATTERN = Pattern.compile("SEE( (?<license>\\S+))? LICENSE IN (?<file>\\S+)");

protected final Logger logger = LoggerFactory.getLogger(ExtensionProcessor.class);

private final TempFile extensionFile;
Expand Down Expand Up @@ -410,40 +406,19 @@ public FileResource getLicense(ExtensionVersion extVersion) {
var license = new FileResource();
license.setExtension(extVersion);
license.setType(FileResource.LICENSE);
// Parse specifications in the form "SEE MIT LICENSE IN LICENSE.txt"
if (!StringUtils.isEmpty(extVersion.getLicense())) {
var matcher = LICENSE_PATTERN.matcher(extVersion.getLicense());
if (matcher.find()) {
extVersion.setLicense(matcher.group("license"));
var fileName = matcher.group("file");
var bytes = ArchiveUtil.readEntry(zipFile, "extension/" + fileName);
if (bytes != null) {
var lastSegmentIndex = fileName.lastIndexOf('/');
var lastSegment = fileName.substring(lastSegmentIndex + 1);
license.setName(lastSegment);
license.setContent(bytes);
detectLicense(bytes, extVersion);
return license;
}
}
}

var result = readFromVsixPackage(ExtensionQueryResult.ExtensionFile.FILE_LICENSE, LICENSE);
if (result == null) {
return null;
}

license.setName(result.getSecond());
license.setContent(result.getFirst());
detectLicense(result.getFirst(), extVersion);
return license;
}
var assetPath = tryGetLicensePath();
if(StringUtils.isNotEmpty(assetPath)) {
var bytes = ArchiveUtil.readEntry(zipFile, assetPath);
var lastSegmentIndex = assetPath.lastIndexOf('/');
var lastSegment = assetPath.substring(lastSegmentIndex + 1);

private void detectLicense(byte[] content, ExtensionVersion extVersion) {
if (StringUtils.isEmpty(extVersion.getLicense())) {
var detection = new LicenseDetection();
extVersion.setLicense(detection.detectLicense(content));
license.setName(lastSegment);
license.setContent(bytes);
return license;
}

return null;
}

private Pair<byte[], String> readFromVsixPackage(String assetType, String[] alternateNames) {
Expand Down Expand Up @@ -472,6 +447,14 @@ private Pair<byte[], String> readFromAlternateNames(String[] names) {
return null;
}

private String tryGetLicensePath() {
loadVsixManifest();
var licensePath = vsixManifest.path("Metadata").path("License").asText();
return licensePath.isEmpty()
? tryGetAssetPath(ExtensionQueryResult.ExtensionFile.FILE_LICENSE)
: licensePath;
}

private String tryGetAssetPath(String type) {
loadVsixManifest();
for(var asset : vsixManifest.path("Assets").path("Asset")) {
Expand Down
194 changes: 0 additions & 194 deletions server/src/main/java/org/eclipse/openvsx/util/LicenseDetection.java

This file was deleted.

This file was deleted.

Loading

0 comments on commit 3e9e023

Please sign in to comment.