/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

apply plugin: 'polygene-tool-npm'

description = "Apache Polygene™ Project Generator."

/**
 * Modifies package.json in-place to set `polygene_version`.
 * Loaded by the generator, see `app/index.js`
 */
def setsCurrentVersionToPackageJson = task( 'setsCurrentVersionToPackageJson' ) {
  outputs.upToDateWhen { false }
  doLast {
    def packageJson = file( 'package.json' )
    packageJson.text = packageJson.text.replaceFirst( /\"polygene_version\": \".*\"/,
                                                      "\"polygene_version\": \"${ project.version }\"" )
  }
}

/**
 * Copy current Gradle Wrapper into generator templates.
 *
 * This is because we don't store the Gradle Wrapper in this project in order to
 * - keep it in synch with the Gradle Wrapper used to build the whole Polygene SDK
 * - don't include the Gradle Wrapper in the source distribution
 */
def prepareTemplateGradleWrapper = task( 'prepareTemplateGradleWrapper', type: Copy ) {
  from( rootProject.rootDir ) {
    include( 'gradlew' )
    rename { 'gradlew.tmpl' }
  }
  from( rootProject.rootDir ) {
    include( 'gradlew.bat' )
    rename { 'gradlew-bat.tmpl' }
  }
  from( rootProject.file( 'gradle/wrapper/' ) ) {
    rename { "${ it }_" }
  }
  into( 'app/templates/buildtool/wrapper' )
}

/**
 * Generate a Gradle init script that registers a maven repository with the currently built artifacts.
 * Used by the generator tests.
 */
def generateStageMavenRepositoryInitScript = task( 'generateStageMavenRepositoryInitScript' ) {
  def initScript = file( "$buildDir/stagedMavenRepoInitScript.gradle" )
  def repoDir = file( "${ rootProject.rootDir }/distributions/build/stage/maven-binaries" )
  dependsOn ':distributions:stageBinariesMavenRepository'
  inputs.property 'polygene_version', project.version
  outputs.file initScript
  doLast {
    initScript.parentFile.mkdirs()
    initScript.text = """
      allprojects {
        repositories {
          maven { url = "file://${ repoDir.absolutePath }" }
        }
      }
    """.stripIndent()
  }
}

/**
 * Runs the generator tests.
 */
def npmTest = task( 'npmTest', type: NpmTask ) {
  dependsOn 'npmInstall', setsCurrentVersionToPackageJson, generateStageMavenRepositoryInitScript, prepareTemplateGradleWrapper
  description = 'Runs Polygene generator tests'
  args = [ 'run', 'test' ]
  inputs.file file( 'package.json' )
  inputs.dir file( 'app' )
  inputs.dir file( 'test' )
  outputs.dir file( "${ buildDir }/reports/tests/npmTest" )
  doFirst {
    // Cleanup generated projects to always run from a fresh state
    file( "$buildDir/build/npm-test" ).deleteDir()
  }
}
check.dependsOn npmTest

/**
 * Runs ALL the generator tests.
 */
task( 'npmTestAll', type: NpmTask ) {
  dependsOn 'npmInstall', setsCurrentVersionToPackageJson, generateStageMavenRepositoryInitScript
  description = 'Runs ALL possible permutations of the Polygene generator tests'
  args = [ 'run', 'test_all' ]
  inputs.file file( 'package.json' )
  inputs.dir file( 'app' )
  inputs.dir file( 'test' )
  outputs.dir file( "${ buildDir }/reports/tests/npmTestAll" )
  doFirst {
    // Cleanup generated projects to always run from a fresh state
    file( "$buildDir/build/npm-test" ).deleteDir()
  }
}

// TODO This disable generator checks that fails on CI - TODO REMOVE ME
// See https://builds.apache.org/view/P/view/Polygene/job/Polygene(JavaEdition)-develop-java8-check/341/console
if( System.getenv( 'BUILD_ID' ) != null )
{
  npmTest.enabled = false
}
