// This file inherits from "java-conventions" and adds additional error
// checks and publishing data for modules that will be published to
// Maven Central. It should only be used for those modules that will
// be published and used by other projects.

plugins {
    id 'rhino.java-conventions'
    id 'maven-publish'
    id 'signing'
    id 'jacoco'
    id 'net.ltgt.errorprone'
}

dependencies {
    errorprone "com.google.errorprone:error_prone_core:2.28.0"
}

version = project.version

tasks.named('compileJava') {
    options.javaModuleVersion = provider { version }
}

tasks.withType(Jar).configureEach {
    manifest {
        attributes (
            "Implementation-Version": project.version,
            "Implementation-Title":  "Mozilla Rhino",
            "Implementation-Vendor": "Mozilla Foundation",
            "Implementation-URL": "http://github.com/mozilla/rhino",
        )
    }
}

tasks.withType(JavaCompile).configureEach {
    options.errorprone.disable(
        // JavaDoc stuff
        "AlmostJavadoc",
        "EmptyBlockTag",
        "EscapedEntity",
        "MissingSummary",
        "InvalidBlockTag",
        "InvalidLink",
        "InvalidParam",
        "NotJavadoc",
        "UnicodeEscape",
        // Great ideas for when minimum version is more than Java 11
        "PatternMatchingInstanceof",
        // Stuff that we just love to do but should do less of eventually
        "EmptyCatch",
        "LabelledBreakTarget",
        "JavaUtilDate",
        // Less important for now, more stylistic than bug
        "InlineMeSuggester",
        // This one either alerts for parameters that we don't use,
        // or spuriously for local variables in my opinion.
        "UnusedVariable")
    options.errorprone.enable(
        // These two do the last thing that Checkstyle used to do for us
        "RemoveUnusedImports",
        "WildcardImport",
        // Warn us proactively that we are using things that might not work on Android.
        // We will aggressively used "SuppressWarnings" for these, but these can be
        // a reminder to check with an Android user since we don't have automated
        // Android unit tests.
        "AndroidJdkLibsChecker")
    options.errorprone.excludedPaths = '.+/src/test/java/.+'
}

tasks.withType(Javadoc) {
      failOnError false
      options.addStringOption('Xdoclint:none', '-quiet')
}

jacocoTestReport {
    reports {
        xml.required = false
        csv.required = false
        html.outputLocation = layout.buildDirectory.dir('reports/jacoco')
    }
}

task javadocJar(type: Jar) {
    archiveClassifier = 'javadoc'
    from javadoc
}

task sourceJar(type: Jar) {
    from sourceSets.main.allJava
    archiveClassifier = 'sources'
    from ('LICENSE.txt') {
        into 'META-INF'
    }
    from ('NOTICE.txt') {
        into 'META-INF'
    }
    from ('NOTICE-tools.txt') {
        into 'META-INF'
    }
}

signing {
    if (project.hasProperty('SIGNINGKEY')) {
        // Check for ORG_GRADLE_PROJECT_SIGNINGKEY environment variable for use in CI system.
        // Otherwise, do not sign.
        def signingKey = getProperty('SIGNINGKEY')
        def signingPassword = getProperty('SIGNINGPASSWORD')
        useInMemoryPgpKeys(signingKey, signingPassword)
        sign publishing.publications
    }
}


publishing {
    if (project.hasProperty("mavenPassword")) {
        repositories {
            maven {
                credentials {
                    username mavenUser
                    password mavenPassword
                }
                if (project.version.endsWith('-SNAPSHOT')) {
                    url mavenSnapshotRepo
                } else {
                    url mavenReleaseRepo
                }
            }
        }
    } else if (System.getenv("GITHUB_TOKEN") != null) {
        repositories {
            maven {
                credentials {
                    username = System.getenv("GITHUB_ACTOR")
                    password = System.getenv("GITHUB_TOKEN")
                }
                url githubPackagesRepo
            }
        }
    }
}
