/*
 * Copyright 2013-2019 the original author or authors.
 *
 * Licensed 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.
 */

agProject {
    withOfflineTestConfigurations()
    configurePlugin(
            'org.asciidoctor.js.base',
            'Asciidoctor.js Base Plugin',
            'Base plugin for all Asciidoctor.js tasks & extensions. Provides the asciidoctorjs project extension',
            'org.asciidoctor.gradle.js.nodejs.core.AsciidoctorNodeJSBasePlugin',
            ['asciidoctor.js']
    )

    configurePlugin(
            'org.asciidoctor.js.convert',
            'Asciidoctor.js General Purpose Document Conversion Plugin',
            'Provides asciidoctor task and convention using the asciidoctor.js erngine',
            'org.asciidoctor.gradle.js.nodejs.AsciidoctorNodeJSPlugin',
            ['asciidoctor.js', 'html5', 'docbook']
    )
}

ext {
    nodejsCacheDir = "${offlineRepoBinariesRoot}/nodejs"

    if (OS.windows) {
        nodejsExt = 'zip'
        if (System.getProperty('os.arch').contains('64')) {
            nodejsArch = 'win-x64'
        } else {
            nodejsArch = 'win-x86'
        }
    } else if (OS.linux) {
        nodejsExt = 'tar.xz'
        if (System.getProperty('os.arch').contains('64')) {
            nodejsArch = 'linux-x64'
        } else {
            nodejsArch = 'linux-x86'
        }
    } else if (OS.macOsX) {
        nodejsExt = 'tar.gz'
        nodejsArch = 'darwin-x64'
    }
}

dependencies {
    api project(':asciidoctor-gradle-base')
    api "org.ysb33r.gradle:nodejs-gradle-plugin:${nodejsGradleVersion}"

    // defaultNodeJsVersion is set in buildSrc
    cachedBinaries.add "nodejs:node:${project.ext.defaultNodeJsVersion}:${nodejsArch}@${nodejsExt}"
}

generateModuleVersions {
    basename = 'asciidoctorjs-extension'
    propertyNames ~/^asciidoctorjs$/, ~/^asciidoctorjs\./
}

tasks.named('test', Test) {
    systemProperties ROOT_PROJECT_DIR: rootProject.projectDir.absolutePath
}

tasks.named('intTest', Test) {
    systemProperties TEST_PROJECTS_DIR: file('src/intTest/projects')
}

tasks.withType(Test).configureEach {
    // Save testing time and bandwidth, but pointing the tests to the locally cached Nodejs. distribution
    systemProperties 'org.ysb33r.gradle.nodejs.uri': file(nodejsCacheDir).absoluteFile.toURI()
}

