Skip to content

Commit

Permalink
增加插件帮助文档
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce.wu committed Nov 14, 2024
1 parent 9d428dd commit e3e3b95
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
Expand All @@ -29,10 +28,10 @@ public class TemplateRenderExecution extends SynchronousNonBlockingStepExecution

private final String outputFile;

private final Map<String, Serializable> vars;
private final Map<String, Object> vars;

public TemplateRenderExecution(
@NonNull StepContext context, String templateFile, String outputFile, Map<String, Serializable> vars) {
@NonNull StepContext context, String templateFile, String outputFile, Map<String, Object> vars) {
super(context);
this.templateFile = templateFile;
this.outputFile = outputFile;
Expand All @@ -57,9 +56,9 @@ protected Map<String, String> run() throws Exception {

private static class RemoteCallable extends MasterToSlaveFileCallable<Boolean> {
private final String outputFile;
private final Map<String, Serializable> vars;
private final Map<String, Object> vars;

private RemoteCallable(String outputFile, Map<String, Serializable> vars) {
private RemoteCallable(String outputFile, Map<String, Object> vars) {
this.outputFile = outputFile;
this.vars = vars;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package io.jenkins.plugins.dumasd.tools;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.verb.POST;

@Setter
@Getter
Expand All @@ -29,7 +40,7 @@ public class TemplateRenderStep extends Step implements Serializable {

private String outputFile;

private Map<String, Serializable> vars;
private Map<String, Object> vars;

@DataBoundConstructor
public TemplateRenderStep(String templateFile, String outputFile) {
Expand All @@ -38,10 +49,15 @@ public TemplateRenderStep(String templateFile, String outputFile) {
}

@DataBoundSetter
public void setVars(Map<String, Serializable> vars) {
public void setVars(Map<String, Object> vars) {
this.vars = vars;
}

@DataBoundSetter
public void setVars(String vars) {
this.vars = JSON.parseObject(vars);
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new TemplateRenderExecution(context, templateFile, outputFile, vars);
Expand All @@ -63,5 +79,45 @@ public Set<? extends Class<?>> getRequiredContext() {
public String getFunctionName() {
return "templateRender";
}

@POST
public FormValidation doCheckTemplateFile(@QueryParameter("templateFile") String templateFile) {
if (StringUtils.isBlank(templateFile)) {
return FormValidation.error("templateFile is required");
}
return FormValidation.ok();
}

@POST
public FormValidation doCheckOutputFile(@QueryParameter("outputFile") String outputFile) {
if (StringUtils.isBlank(outputFile)) {
return FormValidation.error("outputFile is required");
}
return FormValidation.ok();
}

@POST
public FormValidation doCheckVars(@QueryParameter("vars") String vars) {
if (StringUtils.isNotBlank(vars)) {
try {
JSON.parse(vars);
} catch (JSONException e) {
return FormValidation.error("Illegal json format");
}
}
return FormValidation.ok();
}

@Override
public Step newInstance(@Nullable StaplerRequest req, @NonNull JSONObject formData) throws FormException {
String templateFile = formData.getString("templateFile");
String outputFile = formData.getString("outputFile");
Object vars = formData.getOrDefault("vars", null);
TemplateRenderStep step = new TemplateRenderStep(templateFile, outputFile);
if (Objects.nonNull(vars) && StringUtils.isNotBlank(vars.toString())) {
step.setVars(vars.toString());
}
return step;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%templateFile}" field="templateFile">
<f:textbox/>
</f:entry>
<f:entry title="${%outputFile}" field="outputFile">
<f:textbox/>
</f:entry>
<f:entry title="${%vars}" field="vars">
<f:textarea/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
templateFile=TemplateFile
outputFile=OutputFile
vars=Vars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The output file.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The template file.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The variables to use when render the template. please input <strong>json</strong> content.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
<p>
Use <a href="https://velocity.apache.org">Apache Velocity</a> template syntax to
render the template and output.
</p>
</div>

0 comments on commit e3e3b95

Please sign in to comment.