Skip to content
Permalink
Browse files
JENKINS-33358 use 'parse()' to archive a smaller footprint
  • Loading branch information
orctom committed Mar 13, 2016
1 parent fac269a commit d94800cfb31723ed484b4ba98339deb2544be206

Large diffs are not rendered by default.

@@ -2,51 +2,51 @@

import hudson.PluginWrapper;
import hudson.model.BuildBadgeAction;
import hudson.model.Hudson;
import jenkins.model.Jenkins;

import java.io.File;

/**
* plugin action class
* Created by CH on 6/29/2014.
*/
public class GlobalPostScriptAction implements BuildBadgeAction {

private String iconPath;
private String text;
private String iconPath;
private String text;

private GlobalPostScriptAction(String iconPath, String text) {
this.iconPath = iconPath;
this.text = text;
}

public String getIconFileName() {
return iconPath;
}
private GlobalPostScriptAction(String iconPath, String text) {
this.iconPath = iconPath;
this.text = text;
}

public String getDisplayName() {
return text;
}
public String getIconFileName() {
return iconPath;
}

public String getUrlName() {
return null;
}
public String getDisplayName() {
return text;
}

public static GlobalPostScriptAction createBadge(String icon, String text) {
return new GlobalPostScriptAction(getIconPath(icon), text);
}
public String getUrlName() {
return null;
}

public static GlobalPostScriptAction addShortText(String text) {
return new GlobalPostScriptAction(null, text);
}
public static GlobalPostScriptAction createBadge(String icon, String text) {
return new GlobalPostScriptAction(getIconPath(icon), text);
}

private static String getIconPath(String icon) {
if (null == icon) {
return null;
}
public static GlobalPostScriptAction addShortText(String text) {
return new GlobalPostScriptAction(null, text);
}

PluginWrapper wrapper = Hudson.getInstance().getPluginManager().getPlugin(GlobalPostScriptPlugin.class);
boolean pluginIconExists = (wrapper != null) && new File(wrapper.baseResourceURL.getPath() + "/img/" + icon).exists();
return pluginIconExists ? "/plugin/global-post-script/img/" + icon : Jenkins.RESOURCE_PATH + "/images/16x16/" + icon;
private static String getIconPath(String icon) {
if (null == icon) {
return null;
}

PluginWrapper wrapper = Jenkins.getInstance().getPluginManager().getPlugin(GlobalPostScriptPlugin.class);
boolean pluginIconExists = (wrapper != null) && new File(wrapper.baseResourceURL.getPath() + "/img/" + icon).exists();
return pluginIconExists ? "/plugin/global-post-script/img/" + icon : Jenkins.RESOURCE_PATH + "/images/16x16/" + icon;
}
}
@@ -3,9 +3,10 @@
import hudson.Plugin;

/**
* plugin class
* Created by hao on 7/1/2014.
*/
public class GlobalPostScriptPlugin extends Plugin {

public static final String PLUGIN_NAME = "Global Post Script";
public static final String PLUGIN_NAME = "Global Post Script";
}
@@ -0,0 +1,34 @@
package com.orctom.jenkins.plugin.globalpostscript;

import com.orctom.jenkins.plugin.globalpostscript.model.ScriptContent;
import hudson.Util;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
* load script content with cache
* Created by hao on 3/9/16.
*/
public class ScriptContentLoader {

private static String scriptFileName;
private static long scriptLastModified;

private static String scriptContent;

public static ScriptContent getScriptContent(File script, Map<String, String> variables) throws IOException {
long modified = script.lastModified();
boolean isChanged = false;
if (!script.getName().equals(scriptFileName) || scriptLastModified < modified) {
scriptFileName = script.getName();
scriptLastModified = modified;

String content = Util.loadFile(script);
scriptContent = Util.replaceMacro(content, variables);
isChanged = true;
}
return new ScriptContent(scriptContent, isChanged);
}
}
@@ -1,110 +1,43 @@
package com.orctom.jenkins.plugin.globalpostscript;

import groovy.lang.GroovyShell;
import groovy.lang.MissingPropertyException;
import hudson.Util;
import com.orctom.jenkins.plugin.globalpostscript.runner.ScriptRunners;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import org.codehaus.plexus.util.FileUtils;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;
import java.io.*;
import java.io.File;
import java.util.Map;

/**
* script executor
* Created by hao on 6/25/2014.
*/
public class ScriptExecutor {

private Map<String, String> variables;
private TaskListener listener;
private GlobalPostScript.BadgeManager manager;

public ScriptExecutor(Map<String, String> variables, TaskListener listener, GlobalPostScript.BadgeManager manager) {
this.variables = variables;
this.listener = listener;
this.manager = manager;
}

public void execute(File script) {
println("[INFO]");
println("[INFO] -----------------------------------------------------");
println("[INFO] " + GlobalPostScriptPlugin.PLUGIN_NAME);
println("[INFO] -----------------------------------------------------");
String ext = FileUtils.getExtension(script.getAbsolutePath());
if ("groovy".equalsIgnoreCase(ext) || "gvy".equalsIgnoreCase(ext) || "gs".equalsIgnoreCase(ext) || "gsh".equalsIgnoreCase(ext)) {
executeGroovy(script);
} else if ("sh".equalsIgnoreCase(ext)) {
executeScript("sh", script);
} else if ("bat".equalsIgnoreCase(ext)) {
executeScript("bat", script);
} else {
println("[ERROR] Script type not supported: " + ext + " | " + script.getName());
}
println("[INFO] -----------------------------------------------------");
}

public void executeGroovy(File script) {
try {
String scriptContent = getScriptContent(script);
GroovyShell shell = new GroovyShell(getParentClassloader());
for (Map.Entry<String, String> entry : variables.entrySet()) {
shell.setVariable(entry.getKey(), entry.getValue());
}
shell.setVariable("out", listener.getLogger());
shell.setVariable("manager", manager);
shell.evaluate(scriptContent);
} catch (MissingPropertyException e) {
println("[ERROR] Failed to execute: " + script.getName() + ", " + e.getMessage());
} catch (Throwable e) {
e.printStackTrace();
println("[ERROR] Failed to execute: " + script.getName() + ", " + e.getMessage());
}
}

public void executeScript(String executable, File script) {
File temp = null;
try {
String extension = "." + FileUtils.getExtension(script.getPath());
temp = FileUtils.createTempFile("global-post-script-", extension, null);
FileUtils.fileWrite(temp, getScriptContent(script));
Process p = Runtime.getRuntime().exec(new String[]{executable, temp.getAbsolutePath()});
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
println(builder.toString());
} catch (Throwable e) {
e.printStackTrace();
println("[ERROR] Failed to execute: " + script.getName() + ", " + e.getMessage());
} finally {
if (null != temp) {
temp.delete();
}
}
}

private void println(String message) {
listener.getLogger().println(message);
}

private String getScriptContent(File script) throws IOException {
String scriptContent = Util.loadFile(script);
return Util.replaceMacro(scriptContent, variables);
}

private ClassLoader getParentClassloader() {
if (null != Jenkins.getInstance()) {
return Jenkins.getInstance().getPluginManager().uberClassLoader;
} else {
return this.getClass().getClassLoader();
}
}

private TaskListener listener;
private GlobalPostScript.BadgeManager manager;

public ScriptExecutor(TaskListener listener, GlobalPostScript.BadgeManager manager) {
this.listener = listener;
this.manager = manager;
}

public void execute(File scriptFile, Map<String, String> variables) {
println("[INFO]");
println("[INFO] -----------------------------------------------------");
println("[INFO] " + GlobalPostScriptPlugin.PLUGIN_NAME);
println("[INFO] -----------------------------------------------------");
String ext = FileUtils.getExtension(scriptFile.getAbsolutePath());
if ("groovy".equalsIgnoreCase(ext) || "gvy".equalsIgnoreCase(ext) || "gs".equalsIgnoreCase(ext) || "gsh".equalsIgnoreCase(ext)) {
ScriptRunners.GROOVY.run(scriptFile, variables, manager, listener);
} else if ("sh".equalsIgnoreCase(ext) || "bat".equalsIgnoreCase(ext)) {
ScriptRunners.SHELL.run(scriptFile, variables, manager, listener);
} else {
println("[ERROR] Script type not supported: " + ext + " | " + scriptFile.getName());
}
println("[INFO] -----------------------------------------------------");
}

private void println(String message) {
listener.getLogger().println(message);
}
}

This file was deleted.

@@ -0,0 +1,32 @@
package com.orctom.jenkins.plugin.globalpostscript.model;

/**
* script content model
* Created by hao on 3/13/16.
*/
public class ScriptContent {

private String content;
private boolean isChanged;

public ScriptContent(String content, boolean isChanged) {
this.content = content;
this.isChanged = isChanged;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public boolean isChanged() {
return isChanged;
}

public void setChanged(boolean changed) {
this.isChanged = changed;
}
}

0 comments on commit d94800c

Please sign in to comment.