Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring of jbake configuration #341

Merged
merged 19 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 50 additions & 37 deletions jbake-core/src/main/java/org/jbake/app/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.io.FileUtils;
import org.jbake.app.configuration.JBakeConfiguration;
import org.jbake.app.configuration.JBakeConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -17,50 +19,61 @@
* Deals with assets (static files such as css, js or image files).
*
* @author Jonathan Bullock <a href="mailto:[email protected]">[email protected]</a>
*
*/
public class Asset {

private static final Logger LOGGER = LoggerFactory.getLogger(Asset.class);
private final List<Throwable> errors = new LinkedList<>();
private JBakeConfiguration config;

private final File source;
private final File destination;
private CompositeConfiguration config;
private final List<Throwable> errors = new LinkedList<Throwable>();
private final boolean ignoreHidden;
/**
* @param source Source file for the asset
* @param destination Destination (target) directory for asset file
* @param config Project configuration
* @deprecated Use {@link #Asset(JBakeConfiguration)} instead.
* Compatibility constructor.
* Creates an instance of Asset.
*/
@Deprecated
public Asset(File source, File destination, CompositeConfiguration config) {
this.config = new JBakeConfigurationFactory().createDefaultJbakeConfiguration(source, destination, config);
}

/**
* Creates an instance of Asset.
*
* @param source Source file for the asset
* @param destination Destination (target) directory for asset file
* @param config Project configuration
*/
public Asset(File source, File destination, CompositeConfiguration config) {
this.source = source;
this.destination = destination;
this.config = config;
this.ignoreHidden = config.getBoolean(ConfigUtil.Keys.ASSET_IGNORE_HIDDEN, false);
}
/**
* Creates an instance of Asset.
*
* @param config The project configuration. @see{{@link JBakeConfiguration}}
*/
public Asset(JBakeConfiguration config) {
this.config = config;
}

/**
* Copy all files from assets folder to destination folder
* read from configuration
*/
public void copy() {
copy(config.getAssetFolder());
}

/**
* Copy all files from supplied path.
*
* @param path The starting path
*/
public void copy(File path) {
FileFilter filter = new FileFilter() {
/**
* Copy all files from supplied path.
*
* @param path The starting path
*/
public void copy(File path) {
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File file) {
return (!ignoreHidden || !file.isHidden()) && (file.isFile() || FileUtil.directoryOnlyIfNotIgnored(file));
return (!config.getAssetIgnoreHidden() || !file.isHidden()) && (file.isFile() || FileUtil.directoryOnlyIfNotIgnored(file));
}
};
copy(path, destination, filter);
copy(path, config.getDestinationFolder(), filter);
}

private void copy(File sourceFolder, File targetFolder, final FileFilter filter) {
final File[] assets = sourceFolder.listFiles(filter);

final File[] assets = sourceFolder.listFiles(filter);
if (assets != null) {
Arrays.sort(assets);
for (File asset : assets) {
Expand All @@ -79,14 +92,14 @@ private void copy(File sourceFolder, File targetFolder, final FileFilter filter)
}
}
}
public void copyAssetsFromContent(File path){
copy(path, destination, FileUtil.getNotContentFileFilter());

public void copyAssetsFromContent(File path) {
copy(path, config.getDestinationFolder(), FileUtil.getNotContentFileFilter());
}


public List<Throwable> getErrors() {
return new ArrayList<Throwable>(errors);
}

public List<Throwable> getErrors() {
return new ArrayList<>(errors);
}

}
227 changes: 0 additions & 227 deletions jbake-core/src/main/java/org/jbake/app/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -1,227 +0,0 @@
package org.jbake.app;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
* Provides Configuration related functions.
*
* @author Jonathan Bullock <a href="mailto:[email protected]">[email protected]</a>
*/
public class ConfigUtil {
/**
* Set of config keys used by JBake
* @author ndx
*
*/
public interface Keys {
/**
* Output filename for archive file, is only used when {@link #RENDER_ARCHIVE} is true
*/
String ARCHIVE_FILE = "archive.file";
/**
* Asciidoctor attributes to be set when processing input
*/
String ASCIIDOCTOR_ATTRIBUTES = "asciidoctor.attributes";
/**
* Flag indicating if JBake properties should be made available to Asciidoctor
*/
String ASCIIDOCTOR_ATTRIBUTES_EXPORT = "asciidoctor.attributes.export";
/**
* Prefix to be used when exporting JBake properties to Asciidoctor
*/
String ASCIIDOCTOR_ATTRIBUTES_EXPORT_PREFIX = "asciidoctor.attributes.export.prefix";
/**
* Asciidoctor options to be set when processing input
*/
String ASCIIDOCTOR_OPTION = "asciidoctor.option";
/**
* Folder where assets are stored, they are copied directly in output folder and not processed
*/
String ASSET_FOLDER = "asset.folder";
/**
* Flag indicating if content matching prefix below should be given extension-less URI's
*/
String URI_NO_EXTENSION = "uri.noExtension";
/**
* URI prefix for content that should be given extension-less output URI's
*/
String URI_NO_EXTENSION_PREFIX = "uri.noExtension.prefix";
/**
* Flag indicating if hidden asset resources should be ignored
*/
String ASSET_IGNORE_HIDDEN = "asset.ignore";
/**
* Timestamp that records when JBake build was made
*/
String BUILD_TIMESTAMP = "build.timestamp";
/**
* Folder where content (that's to say files to be transformed) resides in
*/
String CONTENT_FOLDER = "content.folder";
/**
* How date is formated
*/
String DATE_FORMAT = "date.format";
/**
* Folder to store database files in
*/
String DB_PATH = "db.path";
/**
* Flag to identify if database is kept in memory (memory) or persisted to disk (local)
*/
String DB_STORE = "db.store";
/**
* Default status to use (in order to avoid putting it in all files)
*/
String DEFAULT_STATUS = "default.status";
/**
* Default type to use (in order to avoid putting it in all files)
*/
String DEFAULT_TYPE = "default.type";
/**
* Folder where rendered files are output
*/
String DESTINATION_FOLDER = "destination.folder";
/**
* Suffix used to identify draft files
*/
String DRAFT_SUFFIX = "draft.suffix";
/**
* Output filename for feed file, is only used when {@link #RENDER_FEED} is true
*/
String FEED_FILE = "feed.file";
/**
* Output filename for index, is only used when {@link #RENDER_INDEX} is true
*/
String INDEX_FILE = "index.file";
/**
* File extension to be used for all output files
*/
String OUTPUT_EXTENSION = "output.extension";
/**
* Flag indicating if archive file should be generated
*/
String RENDER_ARCHIVE = "render.archive";
/**
* Encoding used when rendering files
*/
String RENDER_ENCODING = "render.encoding";
/**
* Flag indicating if feed file should be generated
*/
String RENDER_FEED = "render.feed";
/**
* Flag indicating if index file should be generated
*/
String RENDER_INDEX = "render.index";
/**
* Flag indicating if sitemap file should be generated
*/
String RENDER_SITEMAP = "render.sitemap";
/**
* Flag indicating if tag files should be generated
*/
String RENDER_TAGS = "render.tags";
/**
* Flag indicating if tag index file should be generated
*/
String RENDER_TAGS_INDEX = "render.tagsindex";
/**
* String used to separate the header from the body.
*/
String HEADER_SEPARATOR = "header.separator";
/**
* Port used when running Jetty server
*/
String SERVER_PORT = "server.port";
/**
* Sitemap template file name. Used only when {@link #RENDER_SITEMAP} is set to true
*/
String SITEMAP_FILE = "sitemap.file";
/**
* Tags output path, used only when {@link #RENDER_TAGS} is true
*/
String TAG_PATH = "tag.path";
/**
* Should the tag value be sanitized?
*/
String TAG_SANITIZE = "tag.sanitize";
/**
* Encoding to be used for template files
*/
String TEMPLATE_ENCODING = "template.encoding";
/**
* Folder where template files are looked for
*/
String TEMPLATE_FOLDER = "template.folder";
/**
* Locale used for Thymeleaf template rendering
*/
String THYMELEAF_LOCALE = "thymeleaf.locale";
/**
* Version of JBake
*/
String VERSION = "version";
/**
* Flag indicating if there should be pagination when rendering index
*/
String PAGINATE_INDEX = "index.paginate";
/**
* How many posts per page on index
*/
String POSTS_PER_PAGE = "index.posts_per_page";
/**
* The configured base URI for the hosted content
*/
String SITE_HOST = "site.host";
}

private final static Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class);
private final static String LEGACY_CONFIG_FILE = "custom.properties";
private final static String CONFIG_FILE = "jbake.properties";
private final static String DEFAULT_CONFIG_FILE = "default.properties";
private static boolean LEGACY_CONFIG_FILE_EXISTS = false;
private static boolean LEGACY_CONFIG_FILE_WARNING_SHOWN = false;

public static CompositeConfiguration load(File source) throws ConfigurationException {
return load(source, false);
}

public static CompositeConfiguration load(File source, boolean isRunServer) throws ConfigurationException {
CompositeConfiguration config = new CompositeConfiguration();
config.setListDelimiter(',');
File customConfigFile = new File(source, LEGACY_CONFIG_FILE);
if (customConfigFile.exists()) {
LEGACY_CONFIG_FILE_EXISTS = true;
config.addConfiguration(new PropertiesConfiguration(customConfigFile));
}
customConfigFile = new File(source, CONFIG_FILE);
if (customConfigFile.exists()) {
config.addConfiguration(new PropertiesConfiguration(customConfigFile));
}
config.addConfiguration(new PropertiesConfiguration(DEFAULT_CONFIG_FILE));
config.addConfiguration(new SystemConfiguration());
if (isRunServer) {
String port = config.getString(Keys.SERVER_PORT);
config.setProperty(Keys.SITE_HOST, "http://localhost:"+port);
}
return config;
}
public static void displayLegacyConfigFileWarningIfRequired() {
if (LEGACY_CONFIG_FILE_EXISTS) {
if (!LEGACY_CONFIG_FILE_WARNING_SHOWN) {
LOGGER.warn("You have defined a part of your JBake configuration in " + LEGACY_CONFIG_FILE);
LOGGER.warn("Usage of this file is being deprecated, please rename this file to: " + CONFIG_FILE + " to remove this warning");
LEGACY_CONFIG_FILE_WARNING_SHOWN = true;
}
}
}

}
Loading