001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.cli.command.grab;
018
019import java.util.List;
020
021import joptsimple.OptionSet;
022
023import org.springframework.boot.cli.command.Command;
024import org.springframework.boot.cli.command.OptionParsingCommand;
025import org.springframework.boot.cli.command.options.CompilerOptionHandler;
026import org.springframework.boot.cli.command.options.OptionSetGroovyCompilerConfiguration;
027import org.springframework.boot.cli.command.options.SourceOptions;
028import org.springframework.boot.cli.command.status.ExitStatus;
029import org.springframework.boot.cli.compiler.GroovyCompiler;
030import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
031import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory;
032import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
033
034/**
035 * {@link Command} to grab the dependencies of one or more Groovy scripts.
036 *
037 * @author Andy Wilkinson
038 */
039public class GrabCommand extends OptionParsingCommand {
040
041        public GrabCommand() {
042                super("grab", "Download a spring groovy script's dependencies to ./repository",
043                                new GrabOptionHandler());
044        }
045
046        private static final class GrabOptionHandler extends CompilerOptionHandler {
047
048                @Override
049                protected ExitStatus run(OptionSet options) throws Exception {
050                        SourceOptions sourceOptions = new SourceOptions(options);
051                        List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
052                                        .createDefaultRepositoryConfiguration();
053                        GroovyCompilerConfiguration configuration = new OptionSetGroovyCompilerConfiguration(
054                                        options, this, repositoryConfiguration);
055                        if (System.getProperty("grape.root") == null) {
056                                System.setProperty("grape.root", ".");
057                        }
058                        GroovyCompiler groovyCompiler = new GroovyCompiler(configuration);
059                        groovyCompiler.compile(sourceOptions.getSourcesArray());
060                        return ExitStatus.OK;
061                }
062
063        }
064
065}