Skip to content

Commit

Permalink
Merge pull request #4 from arnestorksen/master
Browse files Browse the repository at this point in the history
Adding support for json-based files with implementations for bower.json and package.json
  • Loading branch information
ciroque committed Apr 16, 2015
2 parents 31487c5 + 9cfec34 commit 053b764
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<artifactId>maven-plugin</artifactId>
<version>1.509.2</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>

<developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BuildDefinitionParser> getDescriptor() {
return new AbstractSemanticParserDescription() {

@Override
public String getDisplayName() {

return Messages.Parsers.BOWER_JSON_PARSER;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -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.");
}
}

}
Original file line number Diff line number Diff line change
@@ -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<BuildDefinitionParser> getDescriptor() {
return new AbstractSemanticParserDescription() {

@Override
public String getDisplayName() {

return Messages.Parsers.NPM_JSON_PARSER;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -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<String> lines = new ArrayList<String>();
lines.add("This is an invalid build file.");

writeLinesToFile(filename, lines);
}

@Override
protected void generateValidBuildFile(String filename) throws IOException {
Collection<String> lines = new ArrayList<String>();
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<String> lines = new ArrayList<String>();
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.";
}
}

0 comments on commit 053b764

Please sign in to comment.