Skip to content

Commit

Permalink
Merge pull request #702 from jonbullock/change/config-extension
Browse files Browse the repository at this point in the history
Change config interface to simplify build plugins
  • Loading branch information
jonbullock authored May 24, 2021
2 parents f09fa75 + 87c5cea commit 38946aa
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ private void displayLegacyConfigFileWarningIfRequired() {
* @param source the source directory of the project
* @param propertiesFile the properties file for the project
* @return the configuration
* @throws ConfigurationException if unable to configure
* @throws JBakeException if unable to configure
*/
public JBakeConfiguration loadConfig(File source, File propertiesFile) throws ConfigurationException {
CompositeConfiguration configuration = load(source, propertiesFile);
return new DefaultJBakeConfiguration(source, configuration);
public JBakeConfiguration loadConfig(File source, File propertiesFile) throws JBakeException {
try {
CompositeConfiguration configuration = load(source, propertiesFile);
return new DefaultJBakeConfiguration(source, configuration);
} catch (ConfigurationException e) {
throw new JBakeException(SystemExit.CONFIGURATION_ERROR, e.getMessage(), e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.configuration2.CompositeConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.MapConfiguration;
import org.apache.commons.configuration2.SystemConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -678,6 +679,11 @@ public List<Property> getJbakeProperties() {
return jbakeKeys;
}

@Override
public void addConfiguration(Properties properties) {
compositeConfiguration.addConfiguration(new MapConfiguration(properties));
}

@Override
public String getAbbreviatedGitHash() {
return getAsString(GIT_HASH.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
* JBakeConfiguration gives you access to the project configuration. Typically located in a file called jbake.properties.
Expand Down Expand Up @@ -367,5 +368,7 @@ public interface JBakeConfiguration {
Map<String, Object> asHashMap();

List<Property> getJbakeProperties();

void addConfiguration(Properties properties);
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import org.apache.commons.configuration2.CompositeConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.jbake.app.JBakeException;

import java.io.File;

Expand All @@ -18,16 +18,15 @@ public JBakeConfigurationFactory() {
}

/**
* Creates a {@link DefaultJBakeConfiguration}
* Creates a {@link DefaultJBakeConfiguration} using default.properties and jbake.properties
*
* @param sourceFolder The source folder of the project
* @param destination The destination folder to render and copy files to
* @param isClearCache Whether to clear database cache or not
* @return A configuration by given parameters
* @throws ConfigurationException if loading the configuration fails
* @deprecated use {@link #createDefaultJbakeConfiguration(File, File, File, boolean)} instead
* @throws JBakeException if loading the configuration fails
*/
@Deprecated
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, boolean isClearCache) throws ConfigurationException {
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, boolean isClearCache) throws JBakeException {
return createDefaultJbakeConfiguration(sourceFolder, destination, (File) null, isClearCache);
}

Expand All @@ -38,9 +37,9 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold
* @param propertiesFile The properties file for the project
* @param isClearCache Whether to clear database cache or not
* @return A configuration by given parameters
* @throws ConfigurationException if loading the configuration fails
* @throws JBakeException if loading the configuration fails
*/
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, File propertiesFile, boolean isClearCache) throws ConfigurationException {
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, File propertiesFile, boolean isClearCache) throws JBakeException {
DefaultJBakeConfiguration configuration = (DefaultJBakeConfiguration) getConfigUtil().loadConfig(sourceFolder, propertiesFile);
configuration.setDestinationFolder(destination);
configuration.setClearCache(isClearCache);
Expand All @@ -57,8 +56,10 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold
* @param compositeConfiguration A given {@link CompositeConfiguration}
* @param isClearCache Whether to clear database cache or not
* @return A configuration by given parameters
* @deprecated use {@link #createDefaultJbakeConfiguration(File, File, File, boolean)} instead
*/
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, CompositeConfiguration compositeConfiguration, boolean isClearCache) {
@Deprecated
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, CompositeConfiguration compositeConfiguration, boolean isClearCache) throws JBakeException {
DefaultJBakeConfiguration configuration = new DefaultJBakeConfiguration(sourceFolder, compositeConfiguration);
configuration.setDestinationFolder(destination);
configuration.setClearCache(isClearCache);
Expand All @@ -74,8 +75,10 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold
* @param destination The destination folder to render and copy files to
* @param compositeConfiguration A given {@link CompositeConfiguration}
* @return A configuration by given parameters
* @deprecated use {@link #createDefaultJbakeConfiguration(File, File, File, boolean)} instead
*/
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, CompositeConfiguration compositeConfiguration) {
@Deprecated
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, File destination, CompositeConfiguration compositeConfiguration) throws JBakeException {
DefaultJBakeConfiguration configuration = new DefaultJBakeConfiguration(sourceFolder, compositeConfiguration);
configuration.setDestinationFolder(destination);
return configuration;
Expand All @@ -89,7 +92,8 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold
* @param config A {@link CompositeConfiguration}
* @return A configuration by given parameters
*/
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, CompositeConfiguration config) {
@Deprecated
public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFolder, CompositeConfiguration config) throws JBakeException {
return new DefaultJBakeConfiguration(sourceFolder,config);
}

Expand All @@ -102,11 +106,11 @@ public DefaultJBakeConfiguration createDefaultJbakeConfiguration(File sourceFold
* @param destinationFolder The destination folder to render and copy files to
* @param isClearCache Whether to clear database cache or not
* @return A configuration by given parameters
* @throws ConfigurationException if loading the configuration fails
* @throws JBakeException if loading the configuration fails
* @deprecated use {@link #createJettyJbakeConfiguration(File, File, File, boolean)} instead
*/
@Deprecated
public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder, File destinationFolder, boolean isClearCache) throws ConfigurationException {
public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder, File destinationFolder, boolean isClearCache) throws JBakeException {
return createJettyJbakeConfiguration(sourceFolder, destinationFolder, (File)null, isClearCache);
}

Expand All @@ -120,9 +124,9 @@ public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder
* @param propertiesFile The properties file for the project
* @param isClearCache Whether to clear database cache or not
* @return A configuration by given parameters
* @throws ConfigurationException if loading the configuration fails
* @throws JBakeException if loading the configuration fails
*/
public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder, File destinationFolder, File propertiesFile, boolean isClearCache) throws ConfigurationException {
public DefaultJBakeConfiguration createJettyJbakeConfiguration(File sourceFolder, File destinationFolder, File propertiesFile, boolean isClearCache) throws JBakeException {
DefaultJBakeConfiguration configuration = (DefaultJBakeConfiguration) getConfigUtil().loadConfig(sourceFolder, propertiesFile);
configuration.setDestinationFolder(destinationFolder);
configuration.setClearCache(isClearCache);
Expand Down
4 changes: 2 additions & 2 deletions jbake-core/src/main/java/org/jbake/launcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void run(String[] args) throws JBakeException {
config = getJBakeConfigurationFactory().setEncoding(res.getPropertiesEncoding()).createDefaultJbakeConfiguration(res.getSource(), res.getDestination(), res.getConfig(), res.isClearCache());
}
run(res, config);
} catch (final ConfigurationException e) {
throw new JBakeException(SystemExit.CONFIGURATION_ERROR, "Configuration error: " + e.getMessage(), e);
} catch (final JBakeException e) {
throw e;
} catch (MissingParameterException mex) {
throw new JBakeException(SystemExit.CONFIGURATION_ERROR, mex.getMessage(), mex);
} catch (final Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.File;
import java.io.FileWriter;
import java.util.Properties;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -111,4 +112,19 @@ public void shouldUseCustomEncoding() throws Exception {
assertThat(factory.getConfigUtil().getEncoding()).isEqualTo("latin1");
verify(util).loadConfig(sourceFolder, null);
}

@Test
void shouldBeAbleToAddCustomProperties() {
File sourceFolder = root;
File destinationFolder = TestUtils.newFolder(root, "output");
DefaultJBakeConfiguration config = new JBakeConfigurationFactory().createDefaultJbakeConfiguration(sourceFolder, destinationFolder, true);
Properties properties = new Properties();
properties.setProperty("custom.key", "custom value");
properties.setProperty("custom.key2", "custom value 2");

config.addConfiguration(properties);

assertThat(config.get("custom.key")).isEqualTo("custom value");
assertThat(config.get("custom.key2")).isEqualTo("custom value 2");
}
}
8 changes: 8 additions & 0 deletions jbake-core/src/test/java/org/jbake/launcher/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ void shouldThrowJBakeExceptionWithSystemExitCodeOnUnexpectedError(@TempDir Path
assertThat(e.getExit()).isEqualTo(SystemExit.ERROR.getStatus());
}

@Test
void shouldThrowAJBakeExceptionWithConfigurationErrorIfLoadThrowsAnCompositeException() {
when(factory.setEncoding(any())).thenReturn(factory);
doThrow(new JBakeException(SystemExit.CONFIGURATION_ERROR, "something went wrong")).when(factory).createDefaultJbakeConfiguration(any(File.class), any(File.class), any(File.class), anyBoolean());
JBakeException e = assertThrows(JBakeException.class, () -> main.run(new String[]{"-b"}));
assertThat(e.getExit()).isEqualTo(SystemExit.CONFIGURATION_ERROR.getStatus());
}

@Test
void shouldListCurrentSettings(@TempDir Path source) throws ConfigurationException {
File src = newFolder(source, "src/jbake");
Expand Down

0 comments on commit 38946aa

Please sign in to comment.