Skip to content

Commit

Permalink
Remove legacy code for older versions of Keycloak JS
Browse files Browse the repository at this point in the history
Closes #500

Signed-off-by: Jon Koops <[email protected]>
  • Loading branch information
jonkoops committed Oct 28, 2024
1 parent 459db09 commit 6203e04
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 205 deletions.
7 changes: 1 addition & 6 deletions pages/app/index.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

<@tmpl.page current="test-app" title="Test application" noindex=true nocsp=true>

<#if version.majorVersion < 26>
<script src="keycloak.js" type="text/javascript"></script>
<script src="app-legacy.js" type="text/javascript"></script>
<#else>
<script src="app.js" type="module"></script>
</#if>
<script src="app.js" type="module"></script>

<div class="jumbotron jumbotron-fluid bg-light kc-bg-triangles py-5 kc-app">
<div class="container">
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/keycloak/webbuilder/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Context {
private Guides guides;
private GuidesMetadata guidesMetadata;
private News news;
private ImportMap importMap;

private FreeMarker freeMarker;
private AsciiDoctor asciiDoctor;
Expand Down Expand Up @@ -66,6 +67,7 @@ public void init() throws Exception {
guidesMetadata = new YamlParser().read(new File(getWebSrcDir(),"/guides.yaml"), GuidesMetadata.class);
guides = new Guides(guidesMetadata, tmpDir, getWebSrcDir(), asciiDoctor);
news = new News(newsDir, blogs, config);
importMap = new ImportMap(tmpDir.toPath());

freeMarker.init(this);
asciiDoctor.init(this);
Expand Down Expand Up @@ -123,6 +125,10 @@ public Links getLinks() {
return links;
}

public ImportMap getImportMap() {
return importMap;
}

public File getWebSrcDir() {
return webSrcDir;
}
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/org/keycloak/webbuilder/ImportMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.keycloak.webbuilder;

import org.keycloak.webbuilder.utils.JsonParser;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class ImportMap {
private final String FILE_NAME = "import-map.json";

private Path targetDir = null;
private String value;

public ImportMap(Path targetDir) {
this.targetDir = targetDir;
}

public String getValue() throws Exception {
// Return cached value if present.
if (value != null) {
return value;
}

// Read import map from disk and cache.
Path source = targetDir.resolve(FILE_NAME);
value = Files.readString(source);
return value;
}

public void writeValue(Map<String, String> imports) throws Exception {
// Create target directory if needed.
Files.createDirectories(targetDir);

// Build import map from installed packages.
Map<String, Map<String, String>> importMap = new HashMap<>();
importMap.put("imports", imports);

// Write import map to disk.
Path target = targetDir.resolve(FILE_NAME);
JsonParser.write(target.toFile(), importMap);
}
}
5 changes: 0 additions & 5 deletions src/main/java/org/keycloak/webbuilder/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ public String getVersionShorter() {
return split[0] + "." + split[1];
}

public int getMajorVersion() {
String[] split = version.split("\\.");
return Integer.parseInt(split[0]);
}

public Date getDate() {
return date;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/keycloak/webbuilder/WebBuilder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.keycloak.webbuilder;

import org.keycloak.webbuilder.builders.AbstractBuilder;
import org.keycloak.webbuilder.builders.AppBuilder;
import org.keycloak.webbuilder.builders.VendorBuilder;
import org.keycloak.webbuilder.builders.BlogBuilder;
import org.keycloak.webbuilder.builders.ChangelogBuilder;
import org.keycloak.webbuilder.builders.DocumentationArchiveBuilder;
Expand All @@ -22,6 +22,7 @@ public class WebBuilder {

private AbstractBuilder[] builders = new AbstractBuilder[] {
// new Cleaner(),
new VendorBuilder(),
new ChangelogBuilder(),
new ResourcesBuilder(),
new ReleaseNotesBuilder(),
Expand All @@ -31,8 +32,7 @@ public class WebBuilder {
new PageBuilder(),
new DocumentationArchiveBuilder(),
new DownloadsArchiveBuilder(),
new RssFeedBuilder(),
new AppBuilder()
new RssFeedBuilder()
};

public static void main(String[] args) throws Exception {
Expand Down
51 changes: 0 additions & 51 deletions src/main/java/org/keycloak/webbuilder/builders/AppBuilder.java

This file was deleted.

80 changes: 80 additions & 0 deletions src/main/java/org/keycloak/webbuilder/builders/VendorBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.keycloak.webbuilder.builders;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.keycloak.webbuilder.npm.Package;
import org.keycloak.webbuilder.npm.Registry;
import org.keycloak.webbuilder.npm.Version;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class VendorBuilder extends AbstractBuilder {
private final Set<String> ALLOWED_EXTENSIONS = Set.of("js");

private final Map<String, String> imports = new HashMap<>();

@Override
protected void build() throws Exception {
// Install packages.
installPackage("keycloak-js", "latest");
// Write import map to disk.
context.getImportMap().writeValue(imports);
}

private void installPackage(String name, String version) throws Exception {
// Resolve installation path and create directories.
Path installationPath = context.getTargetDir().toPath().resolve("vendor").resolve(name);
Files.createDirectories(installationPath);

// Get package contents as tarball stream.
Package packageInfo = Registry.getPackage(name);
Version latestVersion = packageInfo.getVersionByTag(version);
ArchiveInputStream<TarArchiveEntry> tarball = latestVersion.getDist().getTarballStream();

// Copy package contents to installation path.
ArchiveEntry entry;
while ((entry = tarball.getNextEntry()) != null) {
// Skip any files not part of the package contents.
String packagePrefix = "package";
if (!entry.getName().startsWith(packagePrefix)) {
continue;
}

// Resolve path without 'package' prefix.
Path entryPath = Path.of(packagePrefix).relativize(Path.of(entry.getName()));

// Skip file if it's extension is not permitted.
String extension = getFileExtension(entryPath.getFileName().toString());
if(!ALLOWED_EXTENSIONS.contains(extension)) {
continue;
}

// Resolve target path and copy file.
Path targetPath = installationPath.resolve(entryPath);
Files.createDirectories(targetPath.getParent());
Files.copy(tarball, targetPath);
}

// Add package to the imports so it can be written to the import map later.
Path importPath = Path.of("/vendor", name, latestVersion.resolveEntryPoint()).normalize();
imports.put(name, importPath.toString());
}

private String getFileExtension(String filename) {
int dotIndex = filename.lastIndexOf(".");
if (dotIndex >= 0) {
return filename.substring(dotIndex + 1);
}
return "";
}

@Override
protected String getTitle() {
return "Vendor";
}
}
44 changes: 0 additions & 44 deletions src/main/java/org/keycloak/webbuilder/npm/SemanticVersion.java

This file was deleted.

16 changes: 1 addition & 15 deletions src/main/java/org/keycloak/webbuilder/npm/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,17 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public class Version {
@JsonProperty("version")
private String version;

@JsonProperty("main")
private String main;

@JsonProperty("exports")
private Map<String, Export> exports;

@JsonProperty("dist")
private Dist dist;

public SemanticVersion getSemanticVersion() {
return SemanticVersion.fromString(version);
}

public Dist getDist() {
return dist;
}

public String resolveEntryPoint(boolean useLegacy) {
if (useLegacy) {
return main;
}

public String resolveEntryPoint() {
Export defaultExport = exports.get(".");
return defaultExport != null ? defaultExport.getDefaultPath() : null;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/keycloak/webbuilder/utils/FreeMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void writeFile(Map<String, Object> attr, String template, File targetDir,
downloadTemplate.process(attributes, out);
}

public void init(Context context) {
public void init(Context context) throws Exception {
globalAttributes = new HashMap<>();

String root = context.config().isPublish() ? context.config().getUrls().getHome() : context.getTargetDir().toURI().toString();
Expand Down Expand Up @@ -97,6 +97,8 @@ public void init(Context context) {

globalAttributes.put("links", context.getLinks());

globalAttributes.put("importMap", context.getImportMap());

isPublish = context.config().isPublish();
targetDir = context.getTargetDir();
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/keycloak/webbuilder/utils/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public static <T> T read(URL url, Class<T> t) {
}
}

public static void write(File f, Object v) {
try {
mapper.writeValue(f, v);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
Loading

0 comments on commit 6203e04

Please sign in to comment.