Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-73714] Fix filtering of icons in jar file #283

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions src/main/java/io/jenkins/plugins/fontawesome/FontAwesomeIcons.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,7 @@
return getIconsFromClasspath(filter);
}

private static Map<String, String> getIconsFromClasspath(@CheckForNull final FontAwesomeStyle styleFilter) {
try {
return createIconStream(getIconFolder(), styleFilter)
.filter(Objects::nonNull)
.filter(icon -> icon.getFileName() != null)
.filter(FontAwesomeIcons::isSvgImage)
.filter(icon -> icon.getParent() != null)
.filter(icon -> icon.getParent().getFileName() != null)
.sorted()
.map(FontAwesomeIcons::createFileName)
.collect(Collectors.toMap(icon -> icon, FontAwesomeIcons::getIconClassName));
}
catch (IOException exception) {
throw new IllegalStateException("Unable to find icons: Resource unavailable.", exception);
}
}

private static Stream<Path> createIconStream(final Path iconFolder, @CheckForNull final FontAwesomeStyle filter) throws IOException {
if (filter == null) {
return Files.walk(iconFolder, 2);
}
return Files.walk(iconFolder.resolve(filter.name().toLowerCase(Locale.ENGLISH)), 1);
}

private static Path getIconFolder() {
private static Map<String, String> getIconsFromClasspath(@CheckForNull final FontAwesomeStyle filter) {
try {
Enumeration<URL> urls = FontAwesomeIcons.class.getClassLoader().getResources(IMAGES_SYMBOLS_PATH);

Expand All @@ -104,10 +80,10 @@

if (StringUtils.equals(uri.getScheme(), "jar")) {
try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
return fileSystem.getPath(IMAGES_SYMBOLS_PATH);
return filterIcons(fileSystem.getPath(IMAGES_SYMBOLS_PATH), filter);

Check warning on line 83 in src/main/java/io/jenkins/plugins/fontawesome/FontAwesomeIcons.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 83 is not covered by tests

Check warning on line 83 in src/main/java/io/jenkins/plugins/fontawesome/FontAwesomeIcons.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/jenkins/plugins/fontawesome/FontAwesomeIcons.java#L83

Added line #L83 was not covered by tests
}
}
return Paths.get(uri);
return filterIcons(Paths.get(uri), filter);
}
}
}
Expand All @@ -117,6 +93,25 @@
throw new IllegalStateException("Unable to find icons: Resource unavailable.");
}

private static Map<String, String> filterIcons(final Path iconFolder, @CheckForNull final FontAwesomeStyle filter) throws IOException {
return createIconStream(iconFolder, filter)
.filter(Objects::nonNull)
.filter(icon -> icon.getFileName() != null)

Check warning on line 99 in src/main/java/io/jenkins/plugins/fontawesome/FontAwesomeIcons.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 99 is only partially covered, one branch is missing
.filter(FontAwesomeIcons::isSvgImage)
.filter(icon -> icon.getParent() != null)

Check warning on line 101 in src/main/java/io/jenkins/plugins/fontawesome/FontAwesomeIcons.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 101 is only partially covered, one branch is missing
.filter(icon -> icon.getParent().getFileName() != null)

Check warning on line 102 in src/main/java/io/jenkins/plugins/fontawesome/FontAwesomeIcons.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 102 is only partially covered, one branch is missing
.sorted()
.map(FontAwesomeIcons::createFileName)
.collect(Collectors.toMap(icon -> icon, FontAwesomeIcons::getIconClassName));
}

private static Stream<Path> createIconStream(final Path iconFolder, @CheckForNull final FontAwesomeStyle filter) throws IOException {
if (filter == null) {
return Files.walk(iconFolder, 2);
}
return Files.walk(iconFolder.resolve(filter.name().toLowerCase(Locale.ENGLISH)), 1);
}

private static String createFileName(final Path icon) {
return icon.getParent().getFileName() + "/"
+ StringUtils.removeEnd(icon.getFileName().toString(), SVG_FILE_ENDING);
Expand Down
Loading