|
| 1 | +/* |
| 2 | + * Copyright 2012-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.cli.command.install; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.OutputStreamWriter; |
| 23 | +import java.net.URISyntaxException; |
| 24 | +import java.net.URL; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import org.codehaus.groovy.control.CompilationFailedException; |
| 30 | +import org.springframework.boot.cli.compiler.GroovyCompiler; |
| 31 | +import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration; |
| 32 | + |
| 33 | +/** |
| 34 | + * A {@code DependencyResolver} implemented using Groovy's {@code @Grab} |
| 35 | + * |
| 36 | + * @author Dave Syer |
| 37 | + * @author Andy Wilkinson |
| 38 | + * @since 1.2.0 |
| 39 | + */ |
| 40 | +class GroovyGrabDependencyResolver implements DependencyResolver { |
| 41 | + |
| 42 | + private final GroovyCompilerConfiguration configuration; |
| 43 | + |
| 44 | + public GroovyGrabDependencyResolver(GroovyCompilerConfiguration configuration) { |
| 45 | + this.configuration = configuration; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public List<File> resolve(List<String> artifactIdentifiers) |
| 50 | + throws CompilationFailedException, IOException { |
| 51 | + GroovyCompiler groovyCompiler = new GroovyCompiler(this.configuration); |
| 52 | + List<File> artifactFiles = new ArrayList<File>(); |
| 53 | + if (!artifactIdentifiers.isEmpty()) { |
| 54 | + List<URL> initialUrls = getClassPathUrls(groovyCompiler); |
| 55 | + groovyCompiler.compile(createSources(artifactIdentifiers)); |
| 56 | + List<URL> artifactUrls = getClassPathUrls(groovyCompiler); |
| 57 | + artifactUrls.removeAll(initialUrls); |
| 58 | + |
| 59 | + for (URL artifactUrl : artifactUrls) { |
| 60 | + artifactFiles.add(toFile(artifactUrl)); |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + return artifactFiles; |
| 65 | + } |
| 66 | + |
| 67 | + private List<URL> getClassPathUrls(GroovyCompiler compiler) { |
| 68 | + return new ArrayList<URL>(Arrays.asList(compiler.getLoader().getURLs())); |
| 69 | + } |
| 70 | + |
| 71 | + private String createSources(List<String> artifactIdentifiers) throws IOException { |
| 72 | + File file = File.createTempFile("SpringCLIDependency", ".groovy"); |
| 73 | + file.deleteOnExit(); |
| 74 | + OutputStreamWriter stream = new OutputStreamWriter(new FileOutputStream(file), |
| 75 | + "UTF-8"); |
| 76 | + try { |
| 77 | + for (String artifactIdentifier : artifactIdentifiers) { |
| 78 | + stream.write("@Grab('" + artifactIdentifier + "')"); |
| 79 | + } |
| 80 | + // Dummy class to force compiler to do grab |
| 81 | + stream.write("class Installer {}"); |
| 82 | + } |
| 83 | + finally { |
| 84 | + stream.close(); |
| 85 | + } |
| 86 | + // Windows paths get tricky unless you work with URI |
| 87 | + return file.getAbsoluteFile().toURI().toString(); |
| 88 | + } |
| 89 | + |
| 90 | + private File toFile(URL url) { |
| 91 | + try { |
| 92 | + return new File(url.toURI()); |
| 93 | + } |
| 94 | + catch (URISyntaxException ex) { |
| 95 | + return new File(url.getPath()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments