Added web-dev related settings

- Customized_files.Path can now be used to choose where Html Customized files are placed
  (Change this to `"<absolute path to git repo>/Plan/common/src/main/resources/assets/plan/web"`
   for easy time developing)
- Customized_files.Enable_web_dev_mode can be used to enable modifications of all files
  as well as bypass resource caching for instant updates on browser refresh.

Adding new files still needs recompiling the plugin, but this should speed up
modifications to existing parts of the website considerably.

- #2098
This commit is contained in:
Risto Lahtela 2021-09-26 14:50:14 +03:00
parent 4258e27a66
commit 5c618099af
8 changed files with 88 additions and 15 deletions

View File

@ -146,7 +146,7 @@ public class ResourceSvc implements ResourceService {
}
} catch (IOException e) {
errorLogger.warn(e, ErrorContext.builder()
.whatToDo("Report this or provide " + fileName + " in " + files.getCustomizationDirectory())
.whatToDo("Report this or provide " + fileName + " in " + resourceSettings.getCustomizationDirectory())
.related("Fetching resource", "Of: " + pluginName, fileName).build());
}
// Return original by default
@ -174,7 +174,7 @@ public class ResourceSvc implements ResourceService {
WebResource original = source.get();
byte[] bytes = original.asBytes();
OpenOption[] overwrite = {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE};
Path to = files.getCustomizationDirectory().resolve(fileName);
Path to = resourceSettings.getCustomizationDirectory().resolve(fileName);
Path dir = to.getParent();
if (!Files.isSymbolicLink(dir)) Files.createDirectories(dir);
Files.write(to, bytes, overwrite);

View File

@ -67,7 +67,7 @@ public class PlanConfig extends Config {
this.files = files;
this.extensionSettings = new ExtensionSettings(this);
this.resourceSettings = new ResourceSettings(this);
this.resourceSettings = new ResourceSettings(files, this);
this.worldAliasSettings = worldAliasSettings;
this.logger = logger;
}
@ -123,7 +123,7 @@ public class PlanConfig extends Config {
public Path getPageExportPath() {
Path exportDirectory = Paths.get(get(ExportSettings.HTML_EXPORT_PATH));
Path customizationDirectory = files.getCustomizationDirectory();
Path customizationDirectory = resourceSettings.getCustomizationDirectory();
if (exportDirectory.toAbsolutePath().equals(customizationDirectory.toAbsolutePath())) {
logger.warn("'" + ExportSettings.HTML_EXPORT_PATH.getPath() + "' can not be '/Plan/web/' directory, using '/Plan/Analysis Results' as fallback.");

View File

@ -16,23 +16,33 @@
*/
package com.djrapitops.plan.settings.config;
import com.djrapitops.plan.settings.config.paths.CustomizedFileSettings;
import com.djrapitops.plan.storage.file.PlanFiles;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
public class ResourceSettings {
private final PlanFiles files;
private final PlanConfig config;
public ResourceSettings(
PlanConfig config
PlanFiles files, PlanConfig config
) {
this.files = files;
this.config = config;
}
public boolean shouldBeCustomized(String plugin, String fileName) {
if (config.isTrue(CustomizedFileSettings.WEB_DEV_MODE)) {
return true;
}
ConfigNode fileCustomization = getCustomizationConfigNode();
fileCustomization.setComment(Collections.singletonList("The files are placed in /Plan/web/ if the setting is 'true' when accessed."));
@ -57,4 +67,10 @@ public class ResourceSettings {
return config.getNode("Customized_files").orElseGet(() -> config.addNode("Customized_files"));
}
public Path getCustomizationDirectory() {
Path exportDirectory = Paths.get(config.get(CustomizedFileSettings.PATH));
return exportDirectory.isAbsolute()
? exportDirectory
: files.getDataDirectory().resolve(exportDirectory);
}
}

View File

@ -0,0 +1,26 @@
/*
* 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.settings.config.paths;
import com.djrapitops.plan.settings.config.paths.key.BooleanSetting;
import com.djrapitops.plan.settings.config.paths.key.Setting;
import com.djrapitops.plan.settings.config.paths.key.StringSetting;
public class CustomizedFileSettings {
public static final Setting<Boolean> WEB_DEV_MODE = new BooleanSetting("Customized_files.Enable_web_dev_mode");
public static final Setting<String> PATH = new StringSetting("Customized_files.Path");
}

View File

@ -18,6 +18,9 @@ package com.djrapitops.plan.storage.file;
import com.djrapitops.plan.SubSystem;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.CustomizedFileSettings;
import dagger.Lazy;
import javax.inject.Inject;
import javax.inject.Named;
@ -42,13 +45,17 @@ public class PlanFiles implements SubSystem {
private final File dataFolder;
private final File configFile;
private final Lazy<PlanConfig> config;
@Inject
public PlanFiles(
@Named("dataFolder") File dataFolder,
JarResource.StreamFunction getResourceStream
JarResource.StreamFunction getResourceStream,
Lazy<PlanConfig> config
) {
this.dataFolder = dataFolder;
this.getResourceStream = getResourceStream;
this.config = config;
this.configFile = getFileFromPluginFolder("config.yml");
}
@ -60,8 +67,9 @@ public class PlanFiles implements SubSystem {
return dataFolder.toPath();
}
@Deprecated
public Path getCustomizationDirectory() {
return getDataDirectory().resolve("web");
return config.get().getResourceSettings().getCustomizationDirectory();
}
public File getLogsFolder() {
@ -129,16 +137,28 @@ public class PlanFiles implements SubSystem {
return new FileResource(resourceName, getFileFromPluginFolder(resourceName));
}
// TODO Customized file logic should be moved to another class so the circular dependency on config can be removed.
public Optional<Resource> getCustomizableResource(String resourceName) {
return Optional.ofNullable(ResourceCache.getOrCache(resourceName,
() -> attemptToFind(resourceName)
.map(found -> new FileResource(resourceName, found))
.orElse(null)
));
return Optional.ofNullable(findCustomized(resourceName));
}
private Resource findCustomized(String resourceName) {
if (config.get().isTrue(CustomizedFileSettings.WEB_DEV_MODE)) {
// Bypass cache in web developer mode.
return getFileResource(resourceName);
} else {
return ResourceCache.getOrCache(resourceName, () -> getFileResource(resourceName));
}
}
private FileResource getFileResource(String resourceName) {
return attemptToFind(resourceName)
.map(found -> new FileResource(resourceName, found))
.orElse(null);
}
public Optional<File> attemptToFind(String resourceName) {
Path dir = getCustomizationDirectory();
Path dir = config.get().getResourceSettings().getCustomizationDirectory();
if (dir.toFile().exists() && dir.toFile().isDirectory()) {
Path asPath = dir.resolve(resourceName);
File found = asPath.toFile();

View File

@ -216,3 +216,7 @@ Plugins:
Buycraft:
# https://docs.tebex.io/store/faq#how-can-i-find-my-secret-key
Secret: "-"
Customized_files:
Path: "web"
# Web dev mode enables all customized files and disables webserver resource cache for instant changes on browser refresh.
Enable_web_dev_mode: false

View File

@ -228,3 +228,7 @@ Plugins:
Towny:
HideTowns:
- ExampleTown
Customized_files:
Path: "web"
# Web dev mode enables all customized files and disables webserver resource cache for instant changes on browser refresh.
Enable_web_dev_mode: false

View File

@ -17,6 +17,8 @@
package com.djrapitops.plan.storage.file;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.settings.config.PlanConfig;
import dagger.Lazy;
import org.spongepowered.api.Sponge;
import javax.inject.Inject;
@ -38,9 +40,10 @@ public class SpongePlanFiles extends PlanFiles {
public SpongePlanFiles(
@Named("dataFolder") File dataFolder,
JarResource.StreamFunction getResourceStream,
PlanPlugin plugin
PlanPlugin plugin,
Lazy<PlanConfig> config
) {
super(dataFolder, getResourceStream);
super(dataFolder, getResourceStream, config);
this.plugin = plugin;
}