Notify about old versions of html customized files (#2014)

Affects issues:
- Close #1995
This commit is contained in:
Emilia Dreamer 2021-07-23 17:17:35 +03:00 committed by GitHub
parent 5b6effb77b
commit a90f003462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 236 additions and 1 deletions

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.modules.bukkit;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.web.ResourceWriteTask;
import com.djrapitops.plan.delivery.web.WebAssetVersionCheckTask;
import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage;
import com.djrapitops.plan.extension.ExtensionServerDataUpdater;
import com.djrapitops.plan.gathering.ShutdownDataPreservation;
@ -88,4 +89,8 @@ public interface BukkitTaskModule {
@Binds
@IntoSet
TaskSystem.Task bindResourceWriteTask(ResourceWriteTask resourceWriteTask);
@Binds
@IntoSet
TaskSystem.Task bindWebAssetVersionCheckTask(WebAssetVersionCheckTask webAssetVersionCheckTask);
}

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.modules.bungee;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.web.ResourceWriteTask;
import com.djrapitops.plan.delivery.web.WebAssetVersionCheckTask;
import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage;
import com.djrapitops.plan.extension.ExtensionServerDataUpdater;
import com.djrapitops.plan.gathering.timed.BungeePingCounter;
@ -77,4 +78,8 @@ public interface BungeeTaskModule {
@Binds
@IntoSet
TaskSystem.Task bindResourceWriteTask(ResourceWriteTask resourceWriteTask);
@Binds
@IntoSet
TaskSystem.Task bindWebAssetVersionCheckTask(WebAssetVersionCheckTask webAssetVersionCheckTask);
}

View File

@ -28,6 +28,27 @@ task updateVersion(type: Copy) {
into 'build/sources/resources/'
filter(ReplaceTokens, tokens: [version: '' + project.ext.fullVersion])
}
task determineWebAssetModifications {
def versionFile = file("build/resources/main/assets/plan/WebAssetVersion.yml")
versionFile.parentFile.mkdirs()
versionFile.text = "" // Clear previous build
ConfigurableFileTree tree = fileTree(dir: 'src/main/resources/assets/plan/web')
tree.forEach { File f ->
def gitModified = new ByteArrayOutputStream()
exec {
commandLine 'git', 'log', '-1', '--pretty=%ct', f.toString()
standardOutput = gitModified
}
// git returns UNIX time in seconds, but most things in Java use UNIX time in milliseconds
def modified = Long.parseLong(gitModified.toString().strip()) * 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
)
}
}
processResources {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
dependsOn updateVersion
@ -35,6 +56,7 @@ processResources {
}
shadowJar {
dependsOn determineWebAssetModifications
dependsOn processResources
// Exclude these files

View File

@ -0,0 +1,133 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.web;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.formatting.Formatters;
import com.djrapitops.plan.settings.config.ConfigNode;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.storage.file.PlanFiles;
import net.playeranalytics.plugin.scheduling.RunnableFactory;
import net.playeranalytics.plugin.server.PluginLogger;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
* Task in charge of checking html customized files on enable to see if they are outdated.
*/
@Singleton
public class WebAssetVersionCheckTask extends TaskSystem.Task {
private final PlanConfig config;
private final PlanFiles files;
private final PluginLogger logger;
private final WebAssetVersions webAssetVersions;
private final Formatters formatters;
@Inject
public WebAssetVersionCheckTask(
PlanConfig config,
PlanFiles files,
PluginLogger logger,
WebAssetVersions webAssetVersions,
Formatters formatters
) {
this.config = config;
this.files = files;
this.logger = logger;
this.webAssetVersions = webAssetVersions;
this.formatters = formatters;
}
@Override
public void register(RunnableFactory runnableFactory) {
runnableFactory.create(this).runTaskLaterAsynchronously(3, TimeUnit.SECONDS);
}
@Override
public void run() {
Optional<ConfigNode> planCustomizationNode = getPlanCustomizationNode();
if (planCustomizationNode.isPresent()) {
try {
webAssetVersions.prepare();
} catch (IOException e) {
logger.warn("Could not read web asset versions, ", e);
logger.warn("Web asset version check will be skipped!");
return;
}
List<AssetInfo> outdated = new ArrayList<>();
for (ConfigNode child : planCustomizationNode.get().getChildren()) {
if (child.getBoolean()) {
String resource = child.getKey(false).replace(',', '.');
findOutdatedResource(resource).ifPresent(outdated::add);
}
}
if (!outdated.isEmpty()) {
logger.warn("You have customized files which are out of date due to recent updates!");
logger.warn("Plan may not work properly until these files are updated to include the new modifications.");
}
for (AssetInfo asset : outdated) {
logger.warn(String.format("- %s was modified %s, but the plugin contains a version from %s",
asset.filename,
formatters.secondLong().apply(asset.modifiedAt),
formatters.secondLong().apply(asset.expectedModifiedAt)
));
}
}
}
private Optional<AssetInfo> findOutdatedResource(String resource) {
Optional<File> resourceFile = files.attemptToFind(resource);
Optional<Long> webAssetVersion = webAssetVersions.getWebAssetVersion(resource);
if (resourceFile.isPresent() && webAssetVersion.isPresent()) {
if (webAssetVersion.get() > resourceFile.get().lastModified()) {
return Optional.of(new AssetInfo(
resource,
resourceFile.get().lastModified(),
webAssetVersion.get()
));
}
}
return Optional.empty();
}
private Optional<ConfigNode> getPlanCustomizationNode() {
return config.getResourceSettings().getCustomizationConfigNode().getNode("Plan");
}
private static class AssetInfo {
public String filename;
public long modifiedAt;
public long expectedModifiedAt;
public AssetInfo(String filename, long modifiedAt, long expectedModifiedAt) {
this.filename = filename;
this.modifiedAt = modifiedAt;
this.expectedModifiedAt = expectedModifiedAt;
}
}
}

View File

@ -0,0 +1,55 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.web;
import com.djrapitops.plan.settings.config.Config;
import com.djrapitops.plan.settings.config.ConfigNode;
import com.djrapitops.plan.settings.config.ConfigReader;
import com.djrapitops.plan.storage.file.PlanFiles;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.util.Optional;
@Singleton
public class WebAssetVersions {
private final PlanFiles files;
private Config webAssetConfig;
private boolean prepared;
@Inject
public WebAssetVersions(
PlanFiles files
) {
this.files = files;
}
public void prepare() throws IOException {
try (ConfigReader reader = new ConfigReader(files.getResourceFromJar("WebAssetVersion.yml").asInputStream())) {
webAssetConfig = reader.read();
}
}
public Optional<Long> getWebAssetVersion(String resource) {
if (webAssetConfig == null) return Optional.empty();
return webAssetConfig.getNode(resource.replace('.', ',')).map(ConfigNode::getLong);
}
}

View File

@ -137,7 +137,7 @@ public class PlanFiles implements SubSystem {
));
}
private Optional<File> attemptToFind(String resourceName) {
public Optional<File> attemptToFind(String resourceName) {
Path dir = getCustomizationDirectory();
if (dir.toFile().exists() && dir.toFile().isDirectory()) {
Path asPath = dir.resolve(resourceName);

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.modules.nukkit;
import cn.nukkit.level.Level;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.web.ResourceWriteTask;
import com.djrapitops.plan.delivery.web.WebAssetVersionCheckTask;
import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage;
import com.djrapitops.plan.extension.ExtensionServerDataUpdater;
import com.djrapitops.plan.gathering.ShutdownDataPreservation;
@ -88,4 +89,8 @@ public interface NukkitTaskModule {
@Binds
@IntoSet
TaskSystem.Task bindResourceWriteTask(ResourceWriteTask resourceWriteTask);
@Binds
@IntoSet
TaskSystem.Task bindWebAssetVersionCheckTask(WebAssetVersionCheckTask webAssetVersionCheckTask);
}

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.modules.sponge;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.web.ResourceWriteTask;
import com.djrapitops.plan.delivery.web.WebAssetVersionCheckTask;
import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage;
import com.djrapitops.plan.extension.ExtensionServerDataUpdater;
import com.djrapitops.plan.gathering.ShutdownDataPreservation;
@ -88,4 +89,8 @@ public interface SpongeTaskModule {
@Binds
@IntoSet
TaskSystem.Task bindResourceWriteTask(ResourceWriteTask resourceWriteTask);
@Binds
@IntoSet
TaskSystem.Task bindWebAssetVersionCheckTask(WebAssetVersionCheckTask webAssetVersionCheckTask);
}

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.modules.velocity;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.delivery.web.ResourceWriteTask;
import com.djrapitops.plan.delivery.web.WebAssetVersionCheckTask;
import com.djrapitops.plan.delivery.webserver.cache.JSONFileStorage;
import com.djrapitops.plan.extension.ExtensionServerDataUpdater;
import com.djrapitops.plan.gathering.timed.ProxyTPSCounter;
@ -77,4 +78,8 @@ public interface VelocityTaskModule {
@Binds
@IntoSet
TaskSystem.Task bindResourceWriteTask(ResourceWriteTask resourceWriteTask);
@Binds
@IntoSet
TaskSystem.Task bindWebAssetVersionCheckTask(WebAssetVersionCheckTask webAssetVersionCheckTask);
}