Search in sources :

Example 1 with InitSettings

use of org.gradle.buildinit.plugins.internal.InitSettings in project gradle by gradle.

the class InitBuild method setupProjectLayout.

@TaskAction
public void setupProjectLayout() {
    UserInputHandler inputHandler = getServices().get(UserInputHandler.class);
    ProjectLayoutSetupRegistry projectLayoutRegistry = getProjectLayoutRegistry();
    BuildInitializer initDescriptor = null;
    if (isNullOrEmpty(type)) {
        BuildConverter converter = projectLayoutRegistry.getBuildConverter();
        if (converter.canApplyToCurrentDirectory(projectDir)) {
            if (inputHandler.askYesNoQuestion("Found a " + converter.getSourceBuildDescription() + " build. Generate a Gradle build from this?", true)) {
                initDescriptor = converter;
            }
        }
        if (initDescriptor == null) {
            ComponentType componentType = inputHandler.selectOption("Select type of project to generate", projectLayoutRegistry.getComponentTypes(), projectLayoutRegistry.getDefault().getComponentType());
            List<Language> languages = projectLayoutRegistry.getLanguagesFor(componentType);
            if (languages.size() == 1) {
                initDescriptor = projectLayoutRegistry.get(componentType, languages.get(0));
            } else {
                if (!languages.contains(Language.JAVA)) {
                    // Not yet implemented
                    throw new UnsupportedOperationException();
                }
                Language language = inputHandler.selectOption("Select implementation language", languages, Language.JAVA);
                initDescriptor = projectLayoutRegistry.get(componentType, language);
            }
        }
    } else {
        initDescriptor = projectLayoutRegistry.get(type);
    }
    ModularizationOption modularizationOption;
    if (splitProject.isPresent()) {
        modularizationOption = splitProject.get() ? ModularizationOption.WITH_LIBRARY_PROJECTS : ModularizationOption.SINGLE_PROJECT;
    } else if (initDescriptor.getModularizationOptions().size() == 1) {
        modularizationOption = initDescriptor.getModularizationOptions().iterator().next();
    } else if (!isNullOrEmpty(type)) {
        modularizationOption = ModularizationOption.SINGLE_PROJECT;
    } else {
        modularizationOption = inputHandler.selectOption("Split functionality across multiple subprojects?", initDescriptor.getModularizationOptions(), ModularizationOption.SINGLE_PROJECT);
    }
    BuildInitDsl dsl;
    if (isNullOrEmpty(this.dsl)) {
        dsl = initDescriptor.getDefaultDsl();
        if (initDescriptor.getDsls().size() > 1) {
            dsl = inputHandler.selectOption("Select build script DSL", initDescriptor.getDsls(), dsl);
        }
    } else {
        dsl = BuildInitDsl.fromName(getDsl());
        if (!initDescriptor.getDsls().contains(dsl)) {
            throw new GradleException("The requested DSL '" + getDsl() + "' is not supported for '" + initDescriptor.getId() + "' build type");
        }
    }
    boolean useIncubatingAPIs;
    if (this.useIncubatingAPIs.isPresent()) {
        useIncubatingAPIs = this.useIncubatingAPIs.get();
    } else {
        useIncubatingAPIs = inputHandler.askYesNoQuestion("Generate build using new APIs and behavior (some features may change in the next minor release)?", false);
    }
    BuildInitTestFramework testFramework = null;
    if (modularizationOption == ModularizationOption.WITH_LIBRARY_PROJECTS) {
        // currently we only support JUnit5 tests for this combination
        testFramework = BuildInitTestFramework.JUNIT_JUPITER;
    } else if (isNullOrEmpty(this.testFramework)) {
        testFramework = initDescriptor.getDefaultTestFramework();
        if (initDescriptor.getTestFrameworks().size() > 1) {
            testFramework = inputHandler.selectOption("Select test framework", initDescriptor.getTestFrameworks(), testFramework);
        }
    } else {
        for (BuildInitTestFramework candidate : initDescriptor.getTestFrameworks()) {
            if (this.testFramework.equals(candidate.getId())) {
                testFramework = candidate;
                break;
            }
        }
        if (testFramework == null) {
            TreeFormatter formatter = new TreeFormatter();
            formatter.node("The requested test framework '" + getTestFramework() + "' is not supported for '" + initDescriptor.getId() + "' build type. Supported frameworks");
            formatter.startChildren();
            for (BuildInitTestFramework framework : initDescriptor.getTestFrameworks()) {
                formatter.node("'" + framework.getId() + "'");
            }
            formatter.endChildren();
            throw new GradleException(formatter.toString());
        }
    }
    String projectName = this.projectName;
    if (initDescriptor.supportsProjectName()) {
        if (isNullOrEmpty(projectName)) {
            projectName = inputHandler.askQuestion("Project name", getProjectName());
        }
    } else if (!isNullOrEmpty(projectName)) {
        throw new GradleException("Project name is not supported for '" + initDescriptor.getId() + "' build type.");
    }
    String packageName = this.packageName;
    if (initDescriptor.supportsPackage()) {
        if (isNullOrEmpty(packageName)) {
            packageName = inputHandler.askQuestion("Source package", toPackageName(projectName));
        }
    } else if (!isNullOrEmpty(packageName)) {
        throw new GradleException("Package name is not supported for '" + initDescriptor.getId() + "' build type.");
    }
    if (!isNullOrEmpty(packageName)) {
        if (!SourceVersion.isName(packageName)) {
            throw new GradleException("Package name: '" + packageName + "' is not valid - it may contain invalid characters or reserved words.");
        }
    }
    List<String> subprojectNames = initDescriptor.getComponentType().getDefaultProjectNames();
    InitSettings settings = new InitSettings(projectName, useIncubatingAPIs, subprojectNames, modularizationOption, dsl, packageName, testFramework, insecureProtocol.get(), projectDir);
    initDescriptor.generate(settings);
    initDescriptor.getFurtherReading(settings).ifPresent(link -> getLogger().lifecycle("Get more help with your project: {}", link));
}
Also used : ProjectLayoutSetupRegistry(org.gradle.buildinit.plugins.internal.ProjectLayoutSetupRegistry) BuildConverter(org.gradle.buildinit.plugins.internal.BuildConverter) BuildInitDsl(org.gradle.buildinit.plugins.internal.modifiers.BuildInitDsl) ComponentType(org.gradle.buildinit.plugins.internal.modifiers.ComponentType) BuildInitializer(org.gradle.buildinit.plugins.internal.BuildInitializer) ModularizationOption(org.gradle.buildinit.plugins.internal.modifiers.ModularizationOption) UserInputHandler(org.gradle.api.internal.tasks.userinput.UserInputHandler) Language(org.gradle.buildinit.plugins.internal.modifiers.Language) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) GradleException(org.gradle.api.GradleException) InitSettings(org.gradle.buildinit.plugins.internal.InitSettings) BuildInitTestFramework(org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework) TaskAction(org.gradle.api.tasks.TaskAction)

Example 2 with InitSettings

use of org.gradle.buildinit.plugins.internal.InitSettings in project gradle by gradle.

the class PomProjectInitDescriptor method generate.

@Override
public void generate(InitSettings initSettings) {
    IncubationLogger.incubatingFeatureUsed("Maven to Gradle conversion");
    try {
        Settings settings = settingsProvider.buildSettings();
        executor.classLoaderIsolation(config -> config.getClasspath().from(mavenClasspath)).submit(Maven2GradleWorkAction.class, params -> {
            params.getWorkingDir().set(initSettings.getTarget());
            params.getDsl().set(initSettings.getDsl());
            params.getUseIncubatingAPIs().set(initSettings.isUseIncubatingAPIs());
            params.getMavenSettings().set(settings);
            params.getInsecureProtocolOption().set(initSettings.getInsecureProtocolOption());
        });
    } catch (SettingsBuildingException exception) {
        throw new MavenConversionException(String.format("Could not convert Maven POM %s to a Gradle build.", initSettings.getTarget().file("pom.xml").getAsFile()), exception);
    }
}
Also used : MavenSettingsProvider(org.gradle.api.internal.artifacts.mvnsettings.MavenSettingsProvider) Arrays(java.util.Arrays) BuildInitTestFramework(org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework) InitSettings(org.gradle.buildinit.plugins.internal.InitSettings) BuildConverter(org.gradle.buildinit.plugins.internal.BuildConverter) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) Settings(org.apache.maven.settings.Settings) FileCollection(org.gradle.api.file.FileCollection) TreeSet(java.util.TreeSet) Configuration(org.gradle.api.artifacts.Configuration) DependencyHandler(org.gradle.api.artifacts.dsl.DependencyHandler) IncubationLogger(org.gradle.util.internal.IncubationLogger) ProjectInternal(org.gradle.api.internal.project.ProjectInternal) ComponentType(org.gradle.buildinit.plugins.internal.modifiers.ComponentType) Directory(org.gradle.api.file.Directory) Maven2GradleWorkAction(org.gradleinternal.buildinit.plugins.internal.maven.Maven2GradleWorkAction) Usage(org.gradle.api.attributes.Usage) MavenConversionException(org.gradleinternal.buildinit.plugins.internal.maven.MavenConversionException) Set(java.util.Set) DocumentationRegistry(org.gradle.api.internal.DocumentationRegistry) Language(org.gradle.buildinit.plugins.internal.modifiers.Language) ModularizationOption(org.gradle.buildinit.plugins.internal.modifiers.ModularizationOption) WorkerExecutor(org.gradle.workers.WorkerExecutor) ObjectFactory(org.gradle.api.model.ObjectFactory) Optional(java.util.Optional) Collections(java.util.Collections) BuildInitDsl(org.gradle.buildinit.plugins.internal.modifiers.BuildInitDsl) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) MavenConversionException(org.gradleinternal.buildinit.plugins.internal.maven.MavenConversionException) InitSettings(org.gradle.buildinit.plugins.internal.InitSettings) Settings(org.apache.maven.settings.Settings)

Aggregations

BuildConverter (org.gradle.buildinit.plugins.internal.BuildConverter)2 InitSettings (org.gradle.buildinit.plugins.internal.InitSettings)2 BuildInitDsl (org.gradle.buildinit.plugins.internal.modifiers.BuildInitDsl)2 BuildInitTestFramework (org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework)2 ComponentType (org.gradle.buildinit.plugins.internal.modifiers.ComponentType)2 Language (org.gradle.buildinit.plugins.internal.modifiers.Language)2 ModularizationOption (org.gradle.buildinit.plugins.internal.modifiers.ModularizationOption)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Settings (org.apache.maven.settings.Settings)1 SettingsBuildingException (org.apache.maven.settings.building.SettingsBuildingException)1 GradleException (org.gradle.api.GradleException)1 Configuration (org.gradle.api.artifacts.Configuration)1 DependencyHandler (org.gradle.api.artifacts.dsl.DependencyHandler)1 Usage (org.gradle.api.attributes.Usage)1 Directory (org.gradle.api.file.Directory)1 FileCollection (org.gradle.api.file.FileCollection)1