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.options; 018 019import java.util.List; 020 021import joptsimple.OptionSet; 022import joptsimple.OptionSpec; 023 024import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration; 025import org.springframework.boot.cli.compiler.GroovyCompilerScope; 026import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory; 027import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration; 028 029/** 030 * Simple adapter class to present an {@link OptionSet} as a 031 * {@link GroovyCompilerConfiguration}. 032 * 033 * @author Andy Wilkinson 034 */ 035public class OptionSetGroovyCompilerConfiguration implements GroovyCompilerConfiguration { 036 037 private final OptionSet options; 038 039 private final CompilerOptionHandler optionHandler; 040 041 private final List<RepositoryConfiguration> repositoryConfiguration; 042 043 protected OptionSetGroovyCompilerConfiguration(OptionSet optionSet, 044 CompilerOptionHandler compilerOptionHandler) { 045 this(optionSet, compilerOptionHandler, 046 RepositoryConfigurationFactory.createDefaultRepositoryConfiguration()); 047 } 048 049 public OptionSetGroovyCompilerConfiguration(OptionSet optionSet, 050 CompilerOptionHandler compilerOptionHandler, 051 List<RepositoryConfiguration> repositoryConfiguration) { 052 this.options = optionSet; 053 this.optionHandler = compilerOptionHandler; 054 this.repositoryConfiguration = repositoryConfiguration; 055 } 056 057 protected OptionSet getOptions() { 058 return this.options; 059 } 060 061 @Override 062 public GroovyCompilerScope getScope() { 063 return GroovyCompilerScope.DEFAULT; 064 } 065 066 @Override 067 public boolean isGuessImports() { 068 return !this.options.has(this.optionHandler.getNoGuessImportsOption()); 069 } 070 071 @Override 072 public boolean isGuessDependencies() { 073 return !this.options.has(this.optionHandler.getNoGuessDependenciesOption()); 074 } 075 076 @Override 077 public boolean isAutoconfigure() { 078 return this.optionHandler.getAutoconfigureOption().value(this.options); 079 } 080 081 @Override 082 public String[] getClasspath() { 083 OptionSpec<String> classpathOption = this.optionHandler.getClasspathOption(); 084 if (this.options.has(classpathOption)) { 085 return this.options.valueOf(classpathOption).split(":"); 086 } 087 return DEFAULT_CLASSPATH; 088 } 089 090 @Override 091 public List<RepositoryConfiguration> getRepositoryConfiguration() { 092 return this.repositoryConfiguration; 093 } 094 095 @Override 096 public boolean isQuiet() { 097 return false; 098 } 099 100}