-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathGemsInstaller.java
More file actions
243 lines (210 loc) · 10.1 KB
/
GemsInstaller.java
File metadata and controls
243 lines (210 loc) · 10.1 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
*
*/
package de.saumya.mojo.ruby.gems;
import de.saumya.mojo.ruby.script.JRubyVersion;
import de.saumya.mojo.ruby.script.Script;
import de.saumya.mojo.ruby.script.ScriptException;
import de.saumya.mojo.ruby.script.ScriptFactory;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
public class GemsInstaller {
private static final String OPENSSL_VERSION = "0.8.2";
private static final String OPENSSL = "jruby-openssl";
private static final FileFilter FILTER = new FileFilter() {
public boolean accept(File f) {
return f.getName().endsWith(".gemspec");
}
};
public final GemsConfig config;
public final ScriptFactory factory;
public final GemManager manager;
public GemsInstaller(final GemsConfig config, final ScriptFactory factory,
final GemManager manager) {
this.config = config;
this.factory = factory;
this.manager = manager;
}
public void installPom(final MavenProject pom) throws IOException,
ScriptException, GemException {
installGems(pom, null);
}
public void installPom(final MavenProject pom,
final ArtifactRepository localRepository) throws IOException,
ScriptException, GemException {
installPom(pom, localRepository, null);
}
public void installPom(final MavenProject pom,
final ArtifactRepository localRepository, String scope) throws IOException,
ScriptException, GemException {
installGems(pom, localRepository, scope);
}
public MavenProject installOpenSSLGem(final Object repositorySystemSession,
final ArtifactRepository localRepository, List<ArtifactRepository> remotes) throws GemException,
IOException, ScriptException {
return installGem(OPENSSL, OPENSSL_VERSION, repositorySystemSession, localRepository, remotes);
}
public MavenProject installGem(final String name, final String version,
final Object repositorySystemSession,
final ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories) throws GemException,
IOException, ScriptException {
final Artifact artifact;
if (version == null) {
artifact = this.manager.createGemArtifactWithLatestVersion(name,
localRepository,
remoteRepositories);
}
else {
artifact = this.manager.createGemArtifact(name, version);
}
final MavenProject pom = this.manager.buildPom(artifact,
repositorySystemSession,
localRepository,
remoteRepositories);
installPom(pom);
return pom;
}
public void installGems(final MavenProject pom,
final ArtifactRepository localRepository)
throws IOException, ScriptException, GemException
{
installGems(pom, localRepository, null);
}
public void installGems(final MavenProject pom,
final ArtifactRepository localRepository, String scope )
throws IOException, ScriptException, GemException {
installGems(pom, (Collection<Artifact>)null, localRepository, scope);
}
public void installGems(final MavenProject pom, PluginDescriptor plugin,
final ArtifactRepository localRepository) throws IOException,
ScriptException, GemException {
installGems(pom, plugin.getArtifacts(), localRepository, (String) null);
}
public void installGems(final MavenProject pom, final Collection<Artifact> artifacts,
final ArtifactRepository localRepository, String scope) throws IOException,
ScriptException, GemException {
installGems(pom, artifacts, localRepository, pom.getRemoteArtifactRepositories(), scope);
}
public void installGems(final MavenProject pom, final Collection<Artifact> artifacts,
final ArtifactRepository localRepository, List<ArtifactRepository> remoteRepos)
throws IOException, ScriptException, GemException {
installGems( pom, artifacts, localRepository, remoteRepos, null );
}
public void installGems(final MavenProject pom, final Collection<Artifact> artifacts,
final ArtifactRepository localRepository, List<ArtifactRepository> remoteRepos,
String scope ) throws IOException,
ScriptException, GemException {
// start without script object.
// script object will be created when first un-installed gem is found
Script script = null;
if (pom != null) {
boolean hasAlreadyOpenSSL = false;
for (final Artifact artifact : pom.getArtifacts()) {
// assume pom.getBasedir() != null indicates the project pom
if ( ( "compile".equals(artifact.getScope()) ||
"runtime".equals(artifact.getScope()) ||
pom.getBasedir() != null ) &&
( scope == null || scope.equals(artifact.getScope()) ) ) {
if (!artifact.getFile().exists()) {
this.manager.resolve(artifact,
localRepository,
remoteRepos);
}
script = maybeAddArtifact(script, artifact);
hasAlreadyOpenSSL = hasAlreadyOpenSSL
|| artifact.getArtifactId().equals(OPENSSL);
}
}
if (artifacts != null) {
for (final Artifact artifact : artifacts) {
if (!artifact.getFile().exists()) {
this.manager.resolve(artifact,
localRepository,
remoteRepos);
}
script = maybeAddArtifact(script, artifact);
hasAlreadyOpenSSL = hasAlreadyOpenSSL
|| artifact.getArtifactId().equals(OPENSSL);
}
}
if ( pom.getArtifact().getFile() != null
// to filter out target/classes
&& pom.getArtifact().getFile().isFile()
// have only gem files
&& pom.getArtifact().getFile().getName().endsWith(".gem") ) {
script = maybeAddArtifact(script, pom.getArtifact());
}
}
if (script != null) {
script.addArg("--bindir", this.config.getBinDirectory());
if(this.config.getBinDirectory() != null && !this.config.getBinDirectory().exists()){
this.config.getBinDirectory().mkdirs();
}
script.execute();
if (this.config.getGemHome() != null){
// workaround for unpatched: https://github.com/rubygems/rubygems/commit/21cccd55b823848c5e941093a615b0fdd6cd8bc7
for(File spec : new File(this.config.getGemHome(), "specifications").listFiles(FILTER)){
String content = FileUtils.fileRead(spec);
FileUtils.fileWrite(spec.getAbsolutePath(), content.replaceFirst(" 00:00:00.000000000Z", ""));
}
}
this.factory.newScript( "require 'jruby/commands'; JRuby::Commands.generate_dir_info '" +
this.config.getGemHome() +
"' if JRuby::Commands.respond_to? :generate_dir_info" ).execute();
}
}
private boolean exists(Artifact artifact) {
// check if the specifications exists
String basename = artifact.getArtifactId() + "-"
+ artifact.getVersion().replaceFirst("-SNAPSHOT$", "");
String universalJavaBasename = basename + "-universal-java.gemspec";
String javaBasename = basename + "-java.gemspec";
String rubyBasename = basename + ".gemspec";
for (File dir : this.config.getGemsDirectory()) {
File specs = new File( dir.getParentFile(), "specifications" );
if (new File(specs, rubyBasename).exists()
|| new File(specs, javaBasename).exists()
|| new File(specs, universalJavaBasename).exists()) {
return true;
}
}
return false;
}
private Script maybeAddArtifact(Script script, final Artifact artifact)
throws IOException, GemException {
if (artifact.getType().contains("gem")) {
if (!exists(artifact)) {
if (script == null) {
script = this.factory.newScriptFromJRubyJar("gem")
.addArg("install")
.addArg("--ignore-dependencies")
.addArg(booleanArg(this.config.isUserInstall(), "user-install"))
.addArg(booleanArg(this.config.isVerbose(), "verbose"));
final JRubyVersion version = this.factory.getVersion();
if (version == null || version.isVersionLowerThan(9, 2, 10)) {
script.addArg(booleanArg(this.config.isAddRdoc(), "rdoc"))
.addArg(booleanArg(this.config.isAddRI(), "ri"));
} else {
script.addArg(booleanArg(this.config.isAddRdoc(), "document"));
}
}
if (artifact.getFile() != null)
{
script.addArg(artifact.getFile());
}
}
}
return script;
}
private String booleanArg(final boolean flag, final String name) {
return "--" + (flag ? "" : "no-") + name;
}
}