-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathOptimizeMojo.java
More file actions
187 lines (163 loc) · 5.17 KB
/
OptimizeMojo.java
File metadata and controls
187 lines (163 loc) · 5.17 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.github.mcheely.maven.requirejs;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.filtering.MavenFileFilter;
import org.apache.maven.shared.filtering.MavenFilteringException;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;
/**
* Mojo for running r.js optimization.
*
* @goal optimize
* @phase process-classes
*
*/
public class OptimizeMojo extends AbstractMojo {
/**
* @component role="org.apache.maven.shared.filtering.MavenFileFilter"
* role-hint="default"
* @required
*/
private MavenFileFilter mavenFileFilter;
/**
* @parameter default-value="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* @parameter default-value="${project.build.directory}"
* @required
* @readonly
*/
private File buildDirectory;
/**
* @parameter expression="${session}"
* @required
* @readonly
*/
protected MavenSession session;
/**
* Path to optimizer script.
*
* @parameter
*/
private File optimizerFile;
/**
* Path to optimizer json config.
*
* @parameter
* @required
*/
private File configFile;
/**
* Whether or not the config file should
* be maven filtered for token replacement.
*
* @parameter default-value=false
*/
private boolean filterConfig;
/**
* Skip optimization when this parameter is true.
*
* @parameter expression="${requirejs.optimize.skip}" default-value=false
*/
private boolean skip;
/**
* Defines which javascript engine to use. Possible values: rhino or nodejs.
*
* @parameter expression="${requirejs.optimize.runner}" default-value=nodejs
*/
private String runner = "nodejs";
/**
* Defines the location of the NodeJS executable to use.
*
* @parameter
*/
private String nodeExecutable;
/**
* Optimize files.
*
* @throws MojoExecutionException if there is a problem optimizing files.
*/
public void execute() throws MojoExecutionException {
if (skip) {
getLog().info("Optimization is skipped.");
return;
}
Runner runner;
String nodeCommand = getNodeCommand();
if (nodeCommand != null) {
getLog().info("Running with Node @ " + nodeCommand);
runner = new NodeJsRunner(nodeCommand);
} else {
getLog().info("Node not detected. Falling back to rhino");
runner = new RhinoRunner();
}
try {
Optimizer builder = new Optimizer();
ErrorReporter reporter = new MojoErrorReporter(getLog(), true);
if (optimizerFile != null) {
builder.optimize(createBuildProfile(), optimizerFile, reporter, runner);
} else {
builder.optimize(createBuildProfile(), reporter, runner);
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to read r.js", e);
} catch (EvaluatorException e) {
throw new MojoExecutionException("Failed to execute r.js", e);
} catch (OptimizationException e) {
throw new MojoExecutionException("r.js exited with an error.");
}
}
/**
* Returns the node command if node is available and it is the runner which should be used.
*
* @return the command or <code>null</code>
*/
private String getNodeCommand() {
if ("nodejs".equalsIgnoreCase(runner)) {
return getNodeJsPath();
}
return null;
}
@SuppressWarnings("rawtypes")
public Map getPluginContext() {
return super.getPluginContext();
}
@SuppressWarnings("rawtypes")
private File createBuildProfile() throws MojoExecutionException {
if (filterConfig) {
File filteredConfig;
try {
File profileDir = new File(buildDirectory, "requirejs-config/");
profileDir.mkdirs();
filteredConfig = new File(profileDir, "filtered-build.js");
if (!filteredConfig.exists()) {
filteredConfig.createNewFile();
}
mavenFileFilter.copyFile(configFile, filteredConfig, true, project, new ArrayList(), true, "UTF8", session);
} catch (IOException e) {
throw new MojoExecutionException("Error creating filtered build file.", e);
} catch (MavenFilteringException e) {
throw new MojoExecutionException("Error filtering config file.", e);
}
return filteredConfig;
} else {
return configFile;
}
}
private String getNodeJsPath() {
if (nodeExecutable != null) {
return nodeExecutable;
} else {
return NodeJsRunner.detectNodeCommand();
}
}
}