Improve locale json version endpoint

- Checks the last modification from git at compile time
  instead of relying on unrelated file modification dates.
- Added information for gradle to cache determineAssetModifications task results
This commit is contained in:
Aurora Lahtela 2022-08-28 10:21:46 +03:00
parent de0a5eb613
commit 4ff06e22e5
5 changed files with 43 additions and 19 deletions
Plan/common
build.gradle
src/main/java/com/djrapitops/plan

View File

@ -114,10 +114,14 @@ task copyYarnBuildResults {
} }
} }
task determineWebAssetModifications { task determineAssetModifications {
inputs.files(fileTree(dir: 'src/main/resources/assets/plan/web'))
inputs.files(fileTree(dir: 'src/main/resources/assets/plan/locale'))
outputs.file("build/resources/main/assets/plan/AssetVersion.yml")
doLast { doLast {
mkdir "build/resources/main/assets/plan" mkdir "build/resources/main/assets/plan"
def versionFile = file("build/resources/main/assets/plan/WebAssetVersion.yml") def versionFile = file("build/resources/main/assets/plan/AssetVersion.yml")
versionFile.text = "" // Clear previous build versionFile.text = "" // Clear previous build
ConfigurableFileTree tree = fileTree(dir: 'src/main/resources/assets/plan/web') ConfigurableFileTree tree = fileTree(dir: 'src/main/resources/assets/plan/web')
tree.forEach { File f -> tree.forEach { File f ->
@ -134,6 +138,21 @@ task determineWebAssetModifications {
"%s: %s\n", relativePath.toString().replace('.', ','), modified "%s: %s\n", relativePath.toString().replace('.', ','), modified
) )
} }
tree = fileTree(dir: 'src/main/resources/assets/plan/locale')
tree.forEach { File f ->
def gitModified = new ByteArrayOutputStream()
exec {
commandLine 'git', 'log', '-1', '--pretty=%ct', f.toString()
standardOutput = gitModified
}
def gitModifiedAsString = gitModified.toString().strip()
// git returns UNIX time in seconds, but most things in Java use UNIX time in milliseconds
def modified = gitModifiedAsString.isEmpty() ? System.currentTimeMillis() : Long.parseLong(gitModifiedAsString) * 1000
def relativePath = tree.getDir().toPath().relativize(f.toPath()) // File path relative to the tree
versionFile.text += String.format( // writing YAML as raw text probably isn't the best idea
"%s: %s\n", relativePath.toString().replace('.', ','), modified
)
}
} }
} }
@ -161,7 +180,7 @@ artifacts {
processResources { processResources {
dependsOn copyYarnBuildResults dependsOn copyYarnBuildResults
dependsOn determineWebAssetModifications dependsOn determineAssetModifications
dependsOn generateResourceForMySQLDriver dependsOn generateResourceForMySQLDriver
dependsOn generateResourceForSQLiteDriver dependsOn generateResourceForSQLiteDriver
dependsOn updateVersion dependsOn updateVersion

View File

@ -27,25 +27,25 @@ import java.io.IOException;
import java.util.Optional; import java.util.Optional;
@Singleton @Singleton
public class WebAssetVersions { public class AssetVersions {
private final PlanFiles files; private final PlanFiles files;
private Config webAssetConfig; private Config webAssetConfig;
@Inject @Inject
public WebAssetVersions( public AssetVersions(
PlanFiles files PlanFiles files
) { ) {
this.files = files; this.files = files;
} }
public void prepare() throws IOException { public void prepare() throws IOException {
try (ConfigReader reader = new ConfigReader(files.getResourceFromJar("WebAssetVersion.yml").asInputStream())) { try (ConfigReader reader = new ConfigReader(files.getResourceFromJar("AssetVersion.yml").asInputStream())) {
webAssetConfig = reader.read(); webAssetConfig = reader.read();
} }
} }
public Optional<Long> getWebAssetVersion(String resource) { public Optional<Long> getAssetVersion(String resource) {
if (webAssetConfig == null) return Optional.empty(); if (webAssetConfig == null) return Optional.empty();
return webAssetConfig.getNode(resource.replace('.', ',')).map(ConfigNode::getLong); return webAssetConfig.getNode(resource.replace('.', ',')).map(ConfigNode::getLong);

View File

@ -42,7 +42,7 @@ public class WebAssetVersionCheckTask extends TaskSystem.Task {
private final PlanConfig config; private final PlanConfig config;
private final PlanFiles files; private final PlanFiles files;
private final PluginLogger logger; private final PluginLogger logger;
private final WebAssetVersions webAssetVersions; private final AssetVersions assetVersions;
private final Formatters formatters; private final Formatters formatters;
@Inject @Inject
@ -50,13 +50,13 @@ public class WebAssetVersionCheckTask extends TaskSystem.Task {
PlanConfig config, PlanConfig config,
PlanFiles files, PlanFiles files,
PluginLogger logger, PluginLogger logger,
WebAssetVersions webAssetVersions, AssetVersions assetVersions,
Formatters formatters Formatters formatters
) { ) {
this.config = config; this.config = config;
this.files = files; this.files = files;
this.logger = logger; this.logger = logger;
this.webAssetVersions = webAssetVersions; this.assetVersions = assetVersions;
this.formatters = formatters; this.formatters = formatters;
} }
@ -78,7 +78,7 @@ public class WebAssetVersionCheckTask extends TaskSystem.Task {
Optional<ConfigNode> planCustomizationNode = getPlanCustomizationNode(); Optional<ConfigNode> planCustomizationNode = getPlanCustomizationNode();
if (planCustomizationNode.isPresent()) { if (planCustomizationNode.isPresent()) {
try { try {
webAssetVersions.prepare(); assetVersions.prepare();
} catch (IOException e) { } catch (IOException e) {
logger.warn(String.format("Could not read web asset versions, %s", e.toString())); logger.warn(String.format("Could not read web asset versions, %s", e.toString()));
logger.warn("Web asset version check will be skipped!"); logger.warn("Web asset version check will be skipped!");
@ -111,7 +111,7 @@ public class WebAssetVersionCheckTask extends TaskSystem.Task {
private Optional<AssetInfo> findOutdatedResource(String resource) { private Optional<AssetInfo> findOutdatedResource(String resource) {
Optional<File> resourceFile = files.attemptToFind(resource); Optional<File> resourceFile = files.attemptToFind(resource);
Optional<Long> webAssetVersion = webAssetVersions.getWebAssetVersion(resource); Optional<Long> webAssetVersion = assetVersions.getAssetVersion(resource);
if (resourceFile.isPresent() && webAssetVersion.isPresent() && webAssetVersion.get() > resourceFile.get().lastModified()) { if (resourceFile.isPresent() && webAssetVersion.isPresent() && webAssetVersion.get() > resourceFile.get().lastModified()) {
return Optional.of(new AssetInfo( return Optional.of(new AssetInfo(
resource, resource,

View File

@ -119,12 +119,13 @@ public class LocaleJSONResolver implements NoAuthResolver {
Map<String, Object> languages = new TreeMap<>(); Map<String, Object> languages = new TreeMap<>();
Map<String, Object> languageVersions = new TreeMap<>(); Map<String, Object> languageVersions = new TreeMap<>();
long localeVersion = localeSystem.getLocaleVersion(); long maxLocaleVersion = localeSystem.getMaxLocaleVersion();
Optional<Long> customLocaleVersion = localeSystem.getCustomLocaleVersion(); Optional<Long> customLocaleVersion = localeSystem.getCustomLocaleVersion();
for (LangCode lang : LangCode.values()) { for (LangCode lang : LangCode.values()) {
if (lang == LangCode.CUSTOM && locale.getLangCode() != LangCode.CUSTOM) continue; if (lang == LangCode.CUSTOM && locale.getLangCode() != LangCode.CUSTOM) continue;
languages.put(lang.toString(), lang.getName()); languages.put(lang.toString(), lang.getName());
long localeVersion = localeSystem.getLocaleVersion(lang).orElse(maxLocaleVersion);
languageVersions.put(lang.toString(), localeVersion); languageVersions.put(lang.toString(), localeVersion);
} }
customLocaleVersion.ifPresent(version -> languageVersions.put(LangCode.CUSTOM.toString(), version)); customLocaleVersion.ifPresent(version -> languageVersions.put(LangCode.CUSTOM.toString(), version));

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.settings.locale; package com.djrapitops.plan.settings.locale;
import com.djrapitops.plan.SubSystem; import com.djrapitops.plan.SubSystem;
import com.djrapitops.plan.delivery.web.WebAssetVersions; import com.djrapitops.plan.delivery.web.AssetVersions;
import com.djrapitops.plan.delivery.webserver.auth.FailReason; import com.djrapitops.plan.delivery.webserver.auth.FailReason;
import com.djrapitops.plan.settings.config.PlanConfig; import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.PluginSettings; import com.djrapitops.plan.settings.config.paths.PluginSettings;
@ -48,7 +48,7 @@ public class LocaleSystem implements SubSystem {
private final PlanFiles files; private final PlanFiles files;
private final PlanConfig config; private final PlanConfig config;
private final WebAssetVersions webAssetVersions; private final AssetVersions assetVersions;
private final PluginLogger logger; private final PluginLogger logger;
private final ErrorLogger errorLogger; private final ErrorLogger errorLogger;
@ -58,13 +58,13 @@ public class LocaleSystem implements SubSystem {
public LocaleSystem( public LocaleSystem(
PlanFiles files, PlanFiles files,
PlanConfig config, PlanConfig config,
WebAssetVersions webAssetVersions, AssetVersions assetVersions,
PluginLogger logger, PluginLogger logger,
ErrorLogger errorLogger ErrorLogger errorLogger
) { ) {
this.files = files; this.files = files;
this.config = config; this.config = config;
this.webAssetVersions = webAssetVersions; this.assetVersions = assetVersions;
this.logger = logger; this.logger = logger;
this.errorLogger = errorLogger; this.errorLogger = errorLogger;
this.locale = new Locale(); this.locale = new Locale();
@ -221,8 +221,12 @@ public class LocaleSystem implements SubSystem {
return locale; return locale;
} }
public long getLocaleVersion() { public long getMaxLocaleVersion() {
return webAssetVersions.getLatestWebAssetVersion().orElse(0L); return assetVersions.getLatestWebAssetVersion().orElse(0L);
}
public Optional<Long> getLocaleVersion(LangCode langCode) {
return assetVersions.getAssetVersion(langCode.getFileName());
} }
public Optional<Long> getCustomLocaleVersion() { public Optional<Long> getCustomLocaleVersion() {