import BaseApplicationGenerator from '../base-application/index.js';
import writeTask from './files.js';
import cleanupTask from './cleanup.js';
export default class CucumberGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnJHipster('begcode:java:build-tool');
        }
    }
    get writing() {
        return this.asWritingTaskGroup({
            cleanupTask,
            writeTask,
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            addDependencies({ application, source }) {
                const { javaDependencies, gradleBuildSrc } = application;
                source.addJavaDependencies?.([
                    {
                        groupId: 'io.cucumber',
                        artifactId: 'cucumber-bom',
                        version: javaDependencies['cucumber-bom'],
                        type: 'pom',
                        scope: 'import',
                    },
                    { groupId: 'io.cucumber', artifactId: 'cucumber-junit-platform-engine', scope: 'test' },
                    { groupId: 'io.cucumber', artifactId: 'cucumber-java', scope: 'test' },
                    { groupId: 'io.cucumber', artifactId: 'cucumber-spring', scope: 'test' },
                    { groupId: 'org.junit.platform', artifactId: 'junit-platform-console', scope: 'test' },
                    { groupId: 'org.testng', artifactId: 'testng', scope: 'test', version: javaDependencies.testng },
                ], { gradleFile: `${gradleBuildSrc}src/main/groovy/jhipster.cucumber-conventions.gradle` });
                if (application.buildToolMaven) {
                    source.addMavenDefinition?.({
                        plugins: [{ groupId: 'org.apache.maven.plugins', artifactId: 'maven-antrun-plugin' }],
                        pluginManagement: [
                            {
                                groupId: 'org.apache.maven.plugins',
                                artifactId: 'maven-antrun-plugin',
                                additionalContent: `
<executions>
  <execution>
  <!--Work around. Surefire does not use JUnit's Test Engine discovery functionality -->
  <id>prepare cucumber feature files</id>
  <phase>integration-test</phase>
  <goals>
    <goal>run</goal>
  </goals>
  <configuration>
    <target>
        <echo message="Running JUnit Platform CLI"/>
        <java classname="org.junit.platform.console.ConsoleLauncher"
              fork="true"
              failonerror="true"
              newenvironment="true"
              maxmemory="512m"
              classpathref="maven.test.classpath">
            <jvmarg value="-Dspring.profiles.active=testprod"/>${application.reactive
                                    ? `
            <jvmarg value="-XX:+AllowRedefinitionToAddDeleteMethods"/>`
                                    : ''}
            <arg value="--include-engine"/>
            <arg value="cucumber"/>
            <arg value="--scan-classpath"/>
            <arg value="\${project.build.testOutputDirectory}"/>
        </java>
    </target>
  </configuration>
  </execution>
</executions>
`,
                            },
                        ],
                    });
                }
                if (application.buildToolGradle) {
                    source.addGradlePlugin?.({ id: 'jhipster.cucumber-conventions' });
                }
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
