Search in sources :

Example 1 with ComponentType

use of org.gradle.buildinit.plugins.internal.modifiers.ComponentType 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)

Aggregations

GradleException (org.gradle.api.GradleException)1 UserInputHandler (org.gradle.api.internal.tasks.userinput.UserInputHandler)1 TaskAction (org.gradle.api.tasks.TaskAction)1 BuildConverter (org.gradle.buildinit.plugins.internal.BuildConverter)1 BuildInitializer (org.gradle.buildinit.plugins.internal.BuildInitializer)1 InitSettings (org.gradle.buildinit.plugins.internal.InitSettings)1 ProjectLayoutSetupRegistry (org.gradle.buildinit.plugins.internal.ProjectLayoutSetupRegistry)1 BuildInitDsl (org.gradle.buildinit.plugins.internal.modifiers.BuildInitDsl)1 BuildInitTestFramework (org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework)1 ComponentType (org.gradle.buildinit.plugins.internal.modifiers.ComponentType)1 Language (org.gradle.buildinit.plugins.internal.modifiers.Language)1 ModularizationOption (org.gradle.buildinit.plugins.internal.modifiers.ModularizationOption)1 TreeFormatter (org.gradle.internal.logging.text.TreeFormatter)1