import lfformat.LfFormatStep

plugins {
    id 'com.diffplug.spotless'
    id 'org.lflang.distribution-conventions'
    id 'idea'
}

spotless {
    format 'misc', {
        target rootProject.fileTree(rootProject.rootDir) {
            include '**/*.gradle', '**/*.md', '.gitignore', '**/*.yml', '**/*.sh', '**/*.psi'
            exclude '**/reactor-cpp/**', '**/reactor-c/**', '**/reactor-rs/**', '**/lf-python-support/**',
                    '**/src-gen/**', '**/fed-gen/**', '**/test-gen/**', '**/build/**',
                    'test/*/include', 'test/*/bin/', 'test/*/share', 'test/*/lib'
        }

        // define the steps to apply to those files
        trimTrailingWhitespace()
        indentWithSpaces(2) // or spaces. Takes an integer argument if you don't like 4
        endWithNewline()
    }

    format 'linguaFranca', {
        addStep(LfFormatStep.create())
        target 'test/*/src/**/*.lf' // you have to set the target manually
        targetExclude 'test/**/failing/**'
    }
}

// Make the LF formatting task depend on lff
spotlessLinguaFranca.dependsOn('cli:lff:installDist')
spotlessLinguaFranca.inputs.files(tasks.getByPath('cli:lff:installDist').outputs)

distributions {
    clitools {
        distributionBaseName = "lf-cli"
        if (project.hasProperty('nightly')) {
          distributionClassifier = 'nightly-' + project.nightly + '-' + platform.os + '-' + platform.arch
        } else if (!platform.isNative) {
            distributionClassifier = platform.os + '-' + platform.arch
        }
        contents {
          from tasks.getByPath('cli:lfc:installDist').outputs
          from tasks.getByPath('cli:lff:installDist').outputs
          from tasks.getByPath('cli:lfd:installDist').outputs
          duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        }
    }
}
installDist.dependsOn('installClitoolsDist')
assemble.dependsOn('installDist')


// Alias tasks for simpler access
tasks.register('runLfc', JavaExec) {
    dependsOn('cli:lfc:run')
}
tasks.register('runLff', JavaExec) {
    dependsOn('cli:lff:run')
}
tasks.register('targetTest') {
    doLast {
        if (!project.hasProperty('target')) {
            def testFiles = rootProject.fileTree("${rootProject.rootDir}/core/src/integrationTest/java/org/lflang/tests/runtime").files
            def targets = testFiles.collect { it.getName().substring(0, it.getName().length() - 9); }
            throw new GradleException("Please set the \'target\' project property using -Ptarget=<...>. You may chose any of $targets")
        }
    }
    finalizedBy('core:integrationTestCodeCoverageReport')
}
tasks.register('singleTest') {
    doLast {
        if (System.getProperty('singleTest') == null) {
            throw new GradleException('Please set the \'singleTest\' system property using -DsingleTest=<...> to specify the LF test file that you would like to run.')
        }
    }
    finalizedBy('core:integrationTest')
}

// Old deprecated tasks.
tasks.register('buildAll') {
    doLast {
        throw new GradleException('The "buildAll" task was removed. Pleas use "./gradlew build" or "./gradlew assemble instead')
    }
}
tasks.register('runSingleTestl') {
    doLast {
        throw new GradleException('The "runSingleTest" task was removed. Pleas use "./gradlew singleTest -DsingleTest=testFile.lf" instead')
    }
}
