-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from arnestorksen/master
Adding support for json-based files with implementations for bower.json and package.json
- Loading branch information
Showing
6 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/BowerParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/JsonVersionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/org/jenkinsci/plugins/SemanticVersioning/parsing/NpmPackageParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/test/org/jenkinsci/plugins/SemanticVersioning/test/BowerParserTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
} | ||
} |