diff --git a/pom.xml b/pom.xml index cecec53..02c983c 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,11 @@ maven-plugin 1.509.2 + + com.jayway.jsonpath + json-path + 0.9.1 + diff --git a/src/main/java/org/jenkinsci/plugins/SemanticVersioning/Messages.java b/src/main/java/org/jenkinsci/plugins/SemanticVersioning/Messages.java index 859037a..3b605f9 100644 --- a/src/main/java/org/jenkinsci/plugins/SemanticVersioning/Messages.java +++ b/src/main/java/org/jenkinsci/plugins/SemanticVersioning/Messages.java @@ -34,6 +34,8 @@ public class Parsers { public static final String SBT_BUILD_SBT_PARSER = "SBT build.sbt parser"; public static final String MAVEN_POM_PARSER = "Maven Pom Parser"; public static final String SBT_BUILD_SCALA_PARSER = "SBT Build.scala Parser"; + public static final String BOWER_JSON_PARSER = "Bower bower.json Parser"; + public static final String NPM_JSON_PARSER = "NPM package.json Parser"; } public class ColumnDisplayStrategies { diff --git a/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/BowerParser.java b/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/BowerParser.java new file mode 100644 index 0000000..ecdf10a --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/BowerParser.java @@ -0,0 +1,49 @@ +/* + * The MIT License + * + * Copyright (c) 2014, Arne M. Størksen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jenkinsci.plugins.SemanticVersioning.parsing; + +import hudson.Extension; +import hudson.model.Descriptor; +import org.jenkinsci.plugins.SemanticVersioning.Messages; + +@Extension +public class BowerParser extends JsonVersionParser { + + public BowerParser() { + super("bower.json", "$.version"); + } + + @SuppressWarnings("unchecked") + public Descriptor getDescriptor() { + return new AbstractSemanticParserDescription() { + + @Override + public String getDisplayName() { + + return Messages.Parsers.BOWER_JSON_PARSER; + } + }; + } +} diff --git a/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/JsonVersionParser.java b/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/JsonVersionParser.java new file mode 100644 index 0000000..d89fae0 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/JsonVersionParser.java @@ -0,0 +1,69 @@ +/* + * The MIT License + * + * Copyright (c) 2014, Arne M. Størksen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jenkinsci.plugins.SemanticVersioning.parsing; + +import com.jayway.jsonpath.JsonPath; +import hudson.model.AbstractBuild; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import org.apache.commons.io.FileUtils; +import org.jenkinsci.plugins.SemanticVersioning.AppVersion; +import org.jenkinsci.plugins.SemanticVersioning.InvalidBuildFileFormatException; + +public abstract class JsonVersionParser extends AbstractBuildDefinitionParser { + + private final String filepath; + private final String jsonpathVersion; + + protected JsonVersionParser(String filepath, String jsonpathVersion) { + this.filepath = filepath; + this.jsonpathVersion = jsonpathVersion; + } + + public AppVersion extractAppVersion(AbstractBuild build) throws IOException, InvalidBuildFileFormatException { + + File file = new File(filepath); + if(file.exists()) { + + String content = FileUtils.readFileToString(file); + + if(content == null || content.isEmpty()) { + throw new InvalidBuildFileFormatException("'" + filepath + "' is not a valid file."); + } else { + + String version = JsonPath.read(content, jsonpathVersion); + + if(version == null || version.isEmpty()) { + throw new InvalidBuildFileFormatException("No version information found in " + filepath); + } + return AppVersion.parse(version); + } + } else { + throw new FileNotFoundException("'" + filepath + "' was not found."); + } + } + +} diff --git a/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/NpmPackageParser.java b/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/NpmPackageParser.java new file mode 100644 index 0000000..5a6f932 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/NpmPackageParser.java @@ -0,0 +1,49 @@ +/* + * The MIT License + * + * Copyright (c) 2014, Arne M. Størksen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jenkinsci.plugins.SemanticVersioning.parsing; + +import hudson.Extension; +import hudson.model.Descriptor; +import org.jenkinsci.plugins.SemanticVersioning.Messages; + +@Extension +public class NpmPackageParser extends JsonVersionParser { + + public NpmPackageParser() { + super("package.json", "$.version"); + } + + @SuppressWarnings("unchecked") + public Descriptor getDescriptor() { + return new AbstractSemanticParserDescription() { + + @Override + public String getDisplayName() { + + return Messages.Parsers.NPM_JSON_PARSER; + } + }; + } +} diff --git a/src/main/test/org/jenkinsci/plugins/SemanticVersioning/test/BowerParserTests.java b/src/main/test/org/jenkinsci/plugins/SemanticVersioning/test/BowerParserTests.java new file mode 100644 index 0000000..11ada2e --- /dev/null +++ b/src/main/test/org/jenkinsci/plugins/SemanticVersioning/test/BowerParserTests.java @@ -0,0 +1,76 @@ +/* + * The MIT License + * + * Copyright (c) 2014, Arne M. Størksen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jenkinsci.plugins.SemanticVersioning; + +import org.jenkinsci.plugins.SemanticVersioning.parsing.BuildDefinitionParser; +import org.jenkinsci.plugins.SemanticVersioning.parsing.BuildScalaParser; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; + +public class BowerParserTests extends ParserTests { + + @Override + protected BuildDefinitionParser getParser(String filename) { + return new BuildScalaParser(); + } + + @Override + protected void generateInvalidBuildFile(String filename) throws IOException { + Collection lines = new ArrayList(); + lines.add("This is an invalid build file."); + + writeLinesToFile(filename, lines); + } + + @Override + protected void generateValidBuildFile(String filename) throws IOException { + Collection lines = new ArrayList(); + lines.add("object ApplicationBuild extends Build {\n"); + lines.add("val neo4jVersion = \"1.9.2\"\n"); + lines.add("val appName = \"atomicsteampunk\"\n"); + lines.add("val appVersion = \"" + version + "\""); + lines.add("}\n"); + + writeLinesToFile(filename, lines); + } + + @Override + protected void generateBuildFileWithMissingVersion(String filename) throws IOException { + Collection lines = new ArrayList(); + lines.add("object ApplicationBuild extends Build {\n"); + lines.add("val neo4jVersion = \"1.9.2\"\n"); + lines.add("val appName = \"atomicsteampunk\"\n"); + lines.add("}\n"); + + writeLinesToFile(filename, lines); + } + + @Override + protected String getExpectedInvalidBuildFileFormatExceptionMessage(String filename) { + return "'" + filename + "' is not a valid build definition file."; + } +}