This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-config. See https://gitlab.nuiton.org/nuiton/nuiton-config.git commit a7df6b110d3a22f89a4dc617bc45a4f4963972c1 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Oct 2 13:44:21 2016 +0200 Support toml format for configuration description --- nuiton-config-maven-plugin/pom.xml | 18 +++++ .../org/nuiton/config/plugin/io/ConfigModelIO.java | 20 ++++++ .../config/plugin/io/ConfigModelIOTomlImpl.java | 56 +++++++++++++++ .../ConfigModelIOYamlImpl.java} | 34 ++++++---- .../config/plugin/io/ReadConfigModelException.java | 13 ++++ .../plugin/io/WriteConfigModelException.java | 13 ++++ .../plugin/io/ConfigModelIOTomlImplTest.java | 79 ++++++++++++++++++++++ .../plugin/io/ConfigModelIOYamlImplTest.java | 79 ++++++++++++++++++++++ .../ConfigMoldeIOFixtures.java} | 47 ++----------- .../src/test/resources/NuitonConfigExample.toml | 51 ++++++++++++++ pom.xml | 6 ++ 11 files changed, 362 insertions(+), 54 deletions(-) diff --git a/nuiton-config-maven-plugin/pom.xml b/nuiton-config-maven-plugin/pom.xml index da8758e..83b1d6e 100644 --- a/nuiton-config-maven-plugin/pom.xml +++ b/nuiton-config-maven-plugin/pom.xml @@ -164,6 +164,11 @@ </dependency> <dependency> + <groupId>com.moandjiezana.toml</groupId> + <artifactId>toml4j</artifactId> + </dependency> + + <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <scope>runtime</scope> @@ -217,6 +222,19 @@ </configuration> </plugin> + <!-- expose new plexus components --> + <plugin> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-component-metadata</artifactId> + <executions> + <execution> + <goals> + <goal>generate-metadata</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> </build> diff --git a/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIO.java b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIO.java new file mode 100644 index 0000000..b323efc --- /dev/null +++ b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIO.java @@ -0,0 +1,20 @@ +package org.nuiton.config.plugin.io; + +import org.nuiton.config.plugin.model.ConfigModel; + +import java.nio.file.Path; + +/** + * Perform IO operations on {@link ConfigModel}. + * + * Created on 02/10/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public interface ConfigModelIO { + + ConfigModel read(Path path) throws ReadConfigModelException; + + void write(ConfigModel model, Path path) throws WriteConfigModelException; +} diff --git a/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIOTomlImpl.java b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIOTomlImpl.java new file mode 100644 index 0000000..aa4ccbc --- /dev/null +++ b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIOTomlImpl.java @@ -0,0 +1,56 @@ +package org.nuiton.config.plugin.io; + +import com.google.common.io.Files; +import com.moandjiezana.toml.Toml; +import com.moandjiezana.toml.TomlWriter; +import org.nuiton.config.plugin.model.ConfigModel; +import org.nuiton.config.plugin.model.OptionModel; + +import java.io.Reader; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; + +/** + * Implementation using {@code toml} format. + * + * Created on 02/10/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @plexus.component role="org.nuiton.config.plugin.io.ConfigModelIO" role-hint="toml" + * @since 3.0 + */ +public class ConfigModelIOTomlImpl implements ConfigModelIO { + + @Override + public ConfigModel read(Path path) throws ReadConfigModelException { + + try (Reader reader = Files.newReader(path.toFile(), StandardCharsets.UTF_8)) { + Toml toml = new Toml().read(reader); + ConfigModel configModel = toml.to(ConfigModel.class); + + // re-set the type to get real java class and not your alias + for (OptionModel optionModel : configModel.getOptions()) { + optionModel.setType(optionModel.getType()); + } + return configModel; + } catch (Exception e) { + throw new ReadConfigModelException("Can't real toml config model from file: " + path, e); + } + + } + + @Override + public void write(ConfigModel configModel, Path path) throws WriteConfigModelException { + + try (Writer writer = Files.newWriter(path.toFile(), StandardCharsets.UTF_8)) { + + new TomlWriter().write(configModel, writer); + + } catch (Exception e) { + throw new WriteConfigModelException("Can't write toml config model from file: " + path, e); + } + + } + +} diff --git a/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/model/ConfigModelUtil.java b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIOYamlImpl.java similarity index 64% rename from nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/model/ConfigModelUtil.java rename to nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIOYamlImpl.java index a25a7ec..eb32d2b 100644 --- a/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/model/ConfigModelUtil.java +++ b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ConfigModelIOYamlImpl.java @@ -1,4 +1,4 @@ -package org.nuiton.config.plugin.model; +package org.nuiton.config.plugin.io; /*- * #%L @@ -25,6 +25,9 @@ package org.nuiton.config.plugin.model; import com.esotericsoftware.yamlbeans.YamlConfig; import com.esotericsoftware.yamlbeans.YamlReader; import com.esotericsoftware.yamlbeans.YamlWriter; +import org.nuiton.config.plugin.model.ActionModel; +import org.nuiton.config.plugin.model.ConfigModel; +import org.nuiton.config.plugin.model.OptionModel; import java.io.Reader; import java.io.Writer; @@ -33,46 +36,51 @@ import java.nio.file.Files; import java.nio.file.Path; /** + * Implementation using {@code yaml} format. + * * Created on 01/10/16. * * @author Tony Chemit - chemit@codelutin.com + * @plexus.component role="org.nuiton.config.plugin.io.ConfigModelIO" role-hint="yml" * @since 3.0 */ -public class ConfigModelUtil { +public class ConfigModelIOYamlImpl implements ConfigModelIO { - public static ConfigModel read(Path file) { + @Override + public ConfigModel read(Path path) throws ReadConfigModelException { - try (Reader fileReader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { + try (Reader fileReader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { YamlReader reader = new YamlReader(fileReader, createConfig()); - return reader.read(ConfigModel.class); - + ConfigModel configModel = reader.read(ConfigModel.class); + return configModel; } catch (Exception e) { - throw new RuntimeException("Could not read config model from file: " + file, e); + throw new ReadConfigModelException("Can't real yaml config model from file: " + path, e); } + } - public static void write(ConfigModel configModel, Path file) { + @Override + public void write(ConfigModel configModel, Path path) throws WriteConfigModelException { - try (Writer fileWriter = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { + try (Writer fileWriter = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) { YamlWriter writer = new YamlWriter(fileWriter, createConfig()); writer.write(configModel); fileWriter.flush(); writer.close(); } catch (Exception e) { - throw new RuntimeException("Could not write config model to file: " + file, e); + throw new WriteConfigModelException("Can't write yaml config model from file: " + path, e); } + } - private static YamlConfig createConfig() { + private YamlConfig createConfig() { YamlConfig yamlConfig = new YamlConfig(); yamlConfig.setClassTag("option", OptionModel.class); yamlConfig.setClassTag("action", ActionModel.class); yamlConfig.writeConfig.setIndentSize(2); yamlConfig.writeConfig.setWriteRootTags(false); -// yamlConfig.setPropertyElementType(OptionModel.class, "options", OptionModel.class); -// yamlConfig.setPropertyElementType(ActionModel.class, "actions", ActionModel.class); return yamlConfig; } diff --git a/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ReadConfigModelException.java b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ReadConfigModelException.java new file mode 100644 index 0000000..6d50c19 --- /dev/null +++ b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/ReadConfigModelException.java @@ -0,0 +1,13 @@ +package org.nuiton.config.plugin.io; + +/** + * Created on 02/10/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class ReadConfigModelException extends Exception { + public ReadConfigModelException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/WriteConfigModelException.java b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/WriteConfigModelException.java new file mode 100644 index 0000000..c6d0334 --- /dev/null +++ b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/io/WriteConfigModelException.java @@ -0,0 +1,13 @@ +package org.nuiton.config.plugin.io; + +/** + * Created on 02/10/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class WriteConfigModelException extends Exception { + public WriteConfigModelException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigModelIOTomlImplTest.java b/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigModelIOTomlImplTest.java new file mode 100644 index 0000000..390f114 --- /dev/null +++ b/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigModelIOTomlImplTest.java @@ -0,0 +1,79 @@ +package org.nuiton.config.plugin.io; + +/*- + * #%L + * Nuiton Config :: Maven plugin + * %% + * Copyright (C) 2016 Code Lutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.config.plugin.model.ConfigModel; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Created on 01/10/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class ConfigModelIOTomlImplTest { + + private ConfigModelIO io; + private ConfigMoldeIOFixtures ioFixtures; + + @Before + public void setUp() throws Exception { + io = new ConfigModelIOTomlImpl(); + ioFixtures = new ConfigMoldeIOFixtures(); + } + + @Test + public void read() throws Exception { + + Path path = Paths.get(new File("").getAbsolutePath(), "src", "test", "resources", "NuitonConfigExample.toml"); + + ConfigModel configModel = io.read(path); + + ioFixtures.assertConfigModel(configModel); + + } + + @Test + public void write() throws Exception { + + Path path = Paths.get(new File("").getAbsolutePath(), "src", "test", "resources", "NuitonConfigExample.toml"); + + + ConfigModel configModel = io.read(path); + Assert.assertNotNull(configModel); + + Path path2 = Paths.get(new File("").getAbsolutePath(), "target", "surefire-workdir", "NuitonConfigExample2.toml"); + io.write(configModel, path2); + + ConfigModel configModel2 = io.read(path); + ioFixtures.assertConfigModel(configModel2); + + } + +} diff --git a/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigModelIOYamlImplTest.java b/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigModelIOYamlImplTest.java new file mode 100644 index 0000000..531ba13 --- /dev/null +++ b/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigModelIOYamlImplTest.java @@ -0,0 +1,79 @@ +package org.nuiton.config.plugin.io; + +/*- + * #%L + * Nuiton Config :: Maven plugin + * %% + * Copyright (C) 2016 Code Lutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.config.plugin.model.ConfigModel; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Created on 01/10/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class ConfigModelIOYamlImplTest { + + private ConfigModelIO io; + private ConfigMoldeIOFixtures ioFixtures; + + @Before + public void setUp() throws Exception { + io = new ConfigModelIOYamlImpl(); + ioFixtures = new ConfigMoldeIOFixtures(); + } + + @Test + public void read() throws Exception { + + Path path = Paths.get(new File("").getAbsolutePath(), "src", "test", "resources", "NuitonConfigExample.yml"); + + ConfigModel configModel = io.read(path); + + ioFixtures.assertConfigModel(configModel); + + } + + @Test + public void write() throws Exception { + + Path path = Paths.get(new File("").getAbsolutePath(), "src", "test", "resources", "NuitonConfigExample.yml"); + + + ConfigModel configModel = io.read(path); + Assert.assertNotNull(configModel); + + Path path2 = Paths.get(new File("").getAbsolutePath(), "target", "surefire-workdir", "NuitonConfigExample2.yml"); + io.write(configModel, path2); + + ConfigModel configModel2 = io.read(path); + ioFixtures.assertConfigModel(configModel2); + + } + +} diff --git a/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/parser/yml/ConfigModelUtilTest.java b/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigMoldeIOFixtures.java similarity index 66% rename from nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/parser/yml/ConfigModelUtilTest.java rename to nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigMoldeIOFixtures.java index b7bc295..6b11d7f 100644 --- a/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/parser/yml/ConfigModelUtilTest.java +++ b/nuiton-config-maven-plugin/src/test/java/org/nuiton/config/plugin/io/ConfigMoldeIOFixtures.java @@ -1,59 +1,27 @@ -package org.nuiton.config.plugin.parser.yml; - -/*- - * #%L - * Nuiton Config :: Maven plugin - * %% - * Copyright (C) 2016 Code Lutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program 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 General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ +package org.nuiton.config.plugin.io; import org.junit.Assert; -import org.junit.Test; import org.nuiton.config.plugin.model.ActionModel; import org.nuiton.config.plugin.model.ConfigModel; -import org.nuiton.config.plugin.model.ConfigModelUtil; import org.nuiton.config.plugin.model.OptionModel; -import java.io.File; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.List; /** - * Created by tchemit on 01/10/16. + * Created on 02/10/16. * * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 */ -public class ConfigModelUtilTest { - - @Test - public void read() throws Exception { +class ConfigMoldeIOFixtures { - Path path = Paths.get(new File("").getAbsolutePath(), "src", "test", "resources", "NuitonConfigExample.yml"); - - ConfigModel configModel = ConfigModelUtil.read(path); + void assertConfigModel(ConfigModel configModel) { Assert.assertNotNull(configModel); Assert.assertEquals("Exemple de configuration", configModel.getDescription()); List<OptionModel> options = configModel.getOptions(); Assert.assertNotNull(options); Assert.assertEquals(5, options.size()); - assertOption(options.get(0), "identity.firstName", "Prénom de l'utilisateur", String.class, "Joshua"); assertOption(options.get(1), "identity.lastName", "Nom de l'utilisateur", String.class, "Bloch"); assertOption(options.get(2), "identity.email", "Courriel de l'utilisateur", String.class, null); @@ -64,13 +32,10 @@ public class ConfigModelUtilTest { Assert.assertNotNull(actions); Assert.assertEquals(1, actions.size()); assertAction(actions.get(0), "help", "Pour afficher l'aide", "org.nuiton.config.example.NuitonConfigExample#help", "-h", "--help"); - } private void assertOption(OptionModel optionModel, String expectedKey, String expectedDescpription, Class<?> expectedType, String expectedDefaultValue) { Assert.assertNotNull(optionModel); - Assert.assertNotNull(optionModel); - Assert.assertNotNull(optionModel.getKey()); Assert.assertEquals(expectedKey, optionModel.getKey()); Assert.assertNotNull(optionModel.getDescription()); @@ -82,7 +47,6 @@ public class ConfigModelUtilTest { private void assertAction(ActionModel actionModel, String expectedName, String expectedDescription, String expectedAction, String... expectedAliases) { Assert.assertNotNull(actionModel); - Assert.assertNotNull(actionModel.getName()); Assert.assertEquals(expectedName, actionModel.getName()); Assert.assertNotNull(actionModel.getDescription()); @@ -91,4 +55,5 @@ public class ConfigModelUtilTest { Assert.assertEquals(expectedAction, actionModel.getAction()); Assert.assertArrayEquals(expectedAliases, actionModel.getAliases()); } + } diff --git a/nuiton-config-maven-plugin/src/test/resources/NuitonConfigExample.toml b/nuiton-config-maven-plugin/src/test/resources/NuitonConfigExample.toml new file mode 100644 index 0000000..5c69a8c --- /dev/null +++ b/nuiton-config-maven-plugin/src/test/resources/NuitonConfigExample.toml @@ -0,0 +1,51 @@ +description = "Exemple de configuration" + +[[options]] +name = "firstName" +key = "identity.firstName" +description = "Prénom de l'utilisateur" +type = "string" +defaultValue = "Joshua" +_transient = false +_final = false + +[[options]] +name = "lastName" +key = "identity.lastName" +description = "Nom de l'utilisateur" +type = "string" +defaultValue = "Bloch" +_transient = false +_final = false + +[[options]] +name = "email" +key = "identity.email" +description = "Courriel de l'utilisateur" +type = "string" +_transient = false +_final = false + +[[options]] +name = "twitter" +key = "identity.twitter" +description = "Compte Twitter de l'utilisateur" +type = "string" +defaultValue = "jbloch" +_transient = false +_final = false + +[[options]] +name = "age" +key = "identity.age" +description = "age de l'utilisateur" +type = "int" +defaultValue = "56" +_transient = false +_final = false + +[[actions]] +name = "help" +description = "Pour afficher l'aide" +action = "org.nuiton.config.example.NuitonConfigExample#help" +aliases = ["-h", "--help"] diff --git a/pom.xml b/pom.xml index 631497c..cc02e05 100644 --- a/pom.xml +++ b/pom.xml @@ -166,6 +166,12 @@ </dependency> <dependency> + <groupId>com.moandjiezana.toml</groupId> + <artifactId>toml4j</artifactId> + <version>0.7.1</version> + </dependency> + + <dependency> <groupId>com.esotericsoftware.yamlbeans</groupId> <artifactId>yamlbeans</artifactId> <!--Ne pas utiliser la version 1.09 elle est buggée--> -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.