Skip to content

Commit

Permalink
Merge pull request #272 from a1k0u/read-write-toml
Browse files Browse the repository at this point in the history
Add steps to read and write TOML file
  • Loading branch information
rsandell authored Oct 11, 2024
2 parents cab5181 + 3a461bb commit 9f3563e
Show file tree
Hide file tree
Showing 16 changed files with 1,043 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/STEPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* `writeJSON` - Write a [JSON](http://www.json.org/json-it.html) object to a file in the workspace, or to a String. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/json/WriteJSONStep/help.html))
* `readCSV` - Read [CSV](https://commons.apache.org/proper/commons-csv/) from files in the workspace or text. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/csv/ReadCSVStep/help.html))
* `writeCSV` - Write a [CSV](https://commons.apache.org/proper/commons-csv/) file from an object. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/csv/WriteCSVStep/help.html))
* `readTOML` - Read [TOML](https://toml.io) from a file in the workspace or text. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/toml/ReadTOMLStep/help.html))
* `writeTOML` - Write a [TOML](https://toml.io) file from an object. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/toml/WriteTOMLStep/help.html))

#### Maven Projects
* `readMavenPom` - Read a [Maven Project](https://maven.apache.org/pom.html) into a [Model](http://maven.apache.org/components/ref/3.3.9/maven-model/apidocs/org/apache/maven/model/Model.html) data structure. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/maven/ReadMavenPomStep/help.html))
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@
<artifactId>commons-csv</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-toml</artifactId>
<version>2.17.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Nikolas Falco
*
* 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.pipeline.utility.steps.toml;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStep;
import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Reads a TOML file from the workspace.
*
*/
public class ReadTOMLStep extends AbstractFileOrTextStep {

@DataBoundConstructor
public ReadTOMLStep() {}

@Override
public StepExecution start(StepContext context) throws Exception {
return new ReadTOMLStepExecution(this, context);
}

@Extension
public static class DescriptorImpl extends AbstractFileOrTextStepDescriptorImpl {

public DescriptorImpl() {}

@Override
public String getFunctionName() {
return "readTOML";
}

@Override
@NonNull
public String getDisplayName() {
return Messages.ReadTOMLStep_DescriptorImpl_displayName();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Nikolas Falco
*
* 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.pipeline.utility.steps.toml;

import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang.StringUtils.isNotBlank;

import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.FilePath;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContext;

/**
* Execution of {@link ReadTOMLStep}.
*
*/
public class ReadTOMLStepExecution extends AbstractFileOrTextStepExecution<Object> {
private static final long serialVersionUID = 1L;

private transient ReadTOMLStep step;

protected ReadTOMLStepExecution(@NonNull ReadTOMLStep step, @NonNull StepContext context) {
super(step, context);
this.step = step;
}

@Override
protected Object doRun() throws Exception {
String fName = step.getDescriptor().getFunctionName();
if (isNotBlank(step.getFile()) && isNotBlank(step.getText())) {
throw new IllegalArgumentException(Messages.ReadTOMLStepExecution_tooManyArguments(fName));
}

Object toml = null;
TomlMapper mapper = new TomlMapper();
if (!isBlank(step.getFile())) {
FilePath f = ws.child(step.getFile());

if (!f.exists()) {

Check warning on line 66 in src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/toml/ReadTOMLStepExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 66 is only partially covered, one branch is missing
throw new FileNotFoundException(Messages.TOMLStepExecution_fileNotFound(f.getRemote()));

Check warning on line 67 in src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/toml/ReadTOMLStepExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 67 is not covered by tests
}

if (f.isDirectory()) {

Check warning on line 70 in src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/toml/ReadTOMLStepExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 70 is only partially covered, one branch is missing
throw new IllegalArgumentException(Messages.TOMLStepExecution_fileIsDirectory(f.getRemote()));

Check warning on line 71 in src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/toml/ReadTOMLStepExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 71 is not covered by tests
}

try (InputStream is = f.read()) {
toml = mapper.readValue(IOUtils.toString(is, StandardCharsets.UTF_8), Object.class);
}
}

if (!isBlank(step.getText())) {
toml = mapper.readValue(step.getText().trim(), Object.class);
}

return toml;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Nikolas Falco
*
* 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.pipeline.utility.steps.toml;

import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang.StringUtils.isNotBlank;

import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import com.google.common.collect.ImmutableSet;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Util;
import hudson.model.TaskListener;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Set;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

/**
* Writes a {@link java.util.LinkedHashMap} object to file in the current working directory.
*
*/
public class WriteTOMLStep extends Step {

private String file;
private final Object toml;
private boolean returnText;

@DataBoundConstructor
public WriteTOMLStep(Object toml) {
this.toml = toml;
}

@Deprecated
public WriteTOMLStep(String file, Object toml) {
this.file = Util.fixNull(file);
this.toml = toml;
}

Check warning on line 61 in src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/toml/WriteTOMLStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 58-61 are not covered by tests

/**
* Returns the name of the file to write.
*
* @return the file name
*/
public String getFile() {
return file;
}

@DataBoundSetter
public void setFile(String file) {
this.file = file;
}

/**
* Return the toml object to save.
*
* @return an object
*/
public Object getTOML() {
return toml;
}

public boolean isReturnText() {
return returnText;

Check warning on line 87 in src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/toml/WriteTOMLStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 83-87 are not covered by tests
}

@DataBoundSetter
public void setReturnText(boolean returnText) {
this.returnText = returnText;
}

@Override
public StepExecution start(StepContext context) throws Exception {
if (this.toml == null) {
throw new IllegalArgumentException(Messages.WriteTOMLStepExecution_missingTOML(
this.getDescriptor().getFunctionName()));
}

if (this.returnText) {
if (isNotBlank(this.file)) {
throw new IllegalArgumentException(Messages.WriteTOMLStepExecution_bothReturnTextAndFile(
this.getDescriptor().getFunctionName()));
}
return new ReturnTextExecution(this, context);
}

if (isBlank(this.file)) {
throw new IllegalArgumentException(Messages.WriteTOMLStepExecution_missingReturnTextAndFile(
this.getDescriptor().getFunctionName()));
}

return new WriteTOMLStepExecution(this, context);
}

@Extension
public static class DescriptorImpl extends StepDescriptor {

public DescriptorImpl() {}

@Override
public Set<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(TaskListener.class);
}

@Override
public String getFunctionName() {
return "writeTOML";
}

@Override
@NonNull
public String getDisplayName() {
return Messages.WriteTOMLStep_DescriptorImpl_displayName();
}
}

void execute(Writer writer) throws java.io.IOException {
TomlMapper mapper = new TomlMapper();
mapper.writeValue(writer, toml);
}

private static class ReturnTextExecution extends SynchronousNonBlockingStepExecution<String> {
private static final long serialVersionUID = 1L;

private transient WriteTOMLStep step;

protected ReturnTextExecution(WriteTOMLStep step, @NonNull StepContext context) {
super(context);
this.step = step;
}

@Override
protected String run() throws Exception {
StringWriter w = new StringWriter();
this.step.execute(w);
return w.toString();
}
}
}
Loading

0 comments on commit 9f3563e

Please sign in to comment.