forked from boxuk/jslint-jenkins-plugin
/
LintRunner.java
144 lines (126 loc) · 4.59 KB
/
LintRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.boxuk.jenkins.jslint;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.remoting.Callable;
import java.io.FileReader;
import java.net.URLDecoder;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import jenkins.model.Jenkins;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Class that actually runs the linting. Because it is callable, it is safe
* to run across slave nodes.
*
* @author Gavin Davies <gavin.davies@boxuk.com>
* @license MIT License http://www.opensource.org/licenses/MIT
*/
public class LintRunner implements Callable<Properties, RuntimeException> {
/**
* Build logger used to record what's happened.
*/
private final BuildListener listener;
/**
* Collection of arguments to pass to JSLint.
*/
private final List<String> args = new ArrayList<String>();
/**
* Create a new LintRunner.
*
* @param build The build
* @param listener Build listener
* @param includePattern Apache ant pattern
* @param excludePattern Apache ant pattern
* @param logfile Where the XML is output to
* @throws IOException Can be thrown if can't read files
* @throws InterruptedException Can be thrown if build interrupted
*/
public LintRunner(
final AbstractBuild build,
final BuildListener listener,
final String includePattern,
final String excludePattern,
final String logfile,
final String arguments
) throws IOException, InterruptedException {
this.listener = listener;
final FilePath workspaceDir = build.getWorkspace();
args.add("-DxmlOutput=" + workspaceDir.toString() + "/" + logfile);
if(arguments != null && arguments.length() > 1) {
args.add(arguments);
}
FilePath[] files = workspaceDir.list(
includePattern,
excludePattern
);
for (int i = 0; i < files.length; i++) {
args.add(files[i].toString());
}
}
/**
* Call this method.
*
* @return Some properties. No bearing on anything really, it's just
* needed by the interface
*/
public Properties call() {
listener.getLogger().println("[JSLint] calling jslint with args"
+ args.toString());
// Create and enters a Context. The Context stores information
// about the execution environment of a script.
Context context = Context.enter();
try {
// Initialize the standard objects (Object, Function, etc.)
// This must be done before scripts can be executed. Returns
// a scope object that we use in later calls.
ScriptableObject scope = context.initStandardObjects();
Scriptable argsObj = context.newArray(scope, args.toArray());
scope.defineProperty(
"arguments",
argsObj,
ScriptableObject.DONTENUM
);
URL res = this.getClass().getResource("JSLintBuilder/jslint.js");
if(res == null) {
this.getClass().getResource("JSLint/jslint.js");
}
String file;
try {
file = URLDecoder.decode(res.toString(), "UTF-8");
} catch(java.io.UnsupportedEncodingException ex) {
listener.getLogger().println("[JSLint] Unable to decode using UTF-8: " + ex.toString());
file = URLDecoder.decode(res.toString());
}
// Breaks on Windows without this
// BUT!! breaks on EVERYTHING with it since circa Jenkins 1.519
// file = file.replaceAll("file:", "");
listener.getLogger().println("[JSLint] JSLint path is " + file);
try {
URL url = new URL(file);
InputStream inputStream = url.openStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
context.evaluateReader(
scope,
inputStreamReader,
"jslint.js",
0,
null
);
} catch (IOException e) {
e.printStackTrace();
}
} finally {
// Exit from the context.
Context.exit();
}
return System.getProperties();
}
}