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

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 {
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
ConfigurableFileTree tree = fileTree(dir: 'src/main/resources/assets/plan/web')
tree.forEach { File f ->
@ -134,6 +138,21 @@ task determineWebAssetModifications {
"%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 {
dependsOn copyYarnBuildResults
dependsOn determineWebAssetModifications
dependsOn determineAssetModifications
dependsOn generateResourceForMySQLDriver
dependsOn generateResourceForSQLiteDriver
dependsOn updateVersion

View File

@ -27,25 +27,25 @@ import java.io.IOException;
import java.util.Optional;
@Singleton
public class WebAssetVersions {
public class AssetVersions {
private final PlanFiles files;
private Config webAssetConfig;
@Inject
public WebAssetVersions(
public AssetVersions(
PlanFiles files
) {
this.files = files;
}
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();
}
}
public Optional<Long> getWebAssetVersion(String resource) {
public Optional<Long> getAssetVersion(String resource) {
if (webAssetConfig == null) return Optional.empty();
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 PlanFiles files;
private final PluginLogger logger;
private final WebAssetVersions webAssetVersions;
private final AssetVersions assetVersions;
private final Formatters formatters;
@Inject
@ -50,13 +50,13 @@ public class WebAssetVersionCheckTask extends TaskSystem.Task {
PlanConfig config,
PlanFiles files,
PluginLogger logger,
WebAssetVersions webAssetVersions,
AssetVersions assetVersions,
Formatters formatters
) {
this.config = config;
this.files = files;
this.logger = logger;
this.webAssetVersions = webAssetVersions;
this.assetVersions = assetVersions;
this.formatters = formatters;
}
@ -78,7 +78,7 @@ public class WebAssetVersionCheckTask extends TaskSystem.Task {
Optional<ConfigNode> planCustomizationNode = getPlanCustomizationNode();
if (planCustomizationNode.isPresent()) {
try {
webAssetVersions.prepare();
assetVersions.prepare();
} catch (IOException e) {
logger.warn(String.format("Could not read web asset versions, %s", e.toString()));
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) {
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()) {
return Optional.of(new AssetInfo(
resource,

View File

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

View File

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