buildscript {
    repositories { jcenter() }
    dependencies { classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4' }
}

apply plugin: 'com.github.johnrengelman.shadow'

description = "gRPC: Netty Shaded"

sourceSets { testShadow {} }

dependencies {
    compile project(':grpc-netty')
    runtime libraries.netty_tcnative
    testShadowCompile files(shadowJar),
            configurations.shadow,
            project(':grpc-testing-proto'),
            project(':grpc-testing'),
            libraries.truth
    shadow project(':grpc-core')
}

artifacts { // We want uploadArchives to handle the shadowJar; we don't care about
    // uploadShadow
    archives shadowJar }

jar {
    // Must use a different classifier to avoid conflicting with shadowJar
    classifier = 'original'
}
configurations.archives.artifacts.removeAll { it.classifier == "original" }

shadowJar {
    classifier = null
    dependencies {
        include(project(':grpc-netty'))
        include(dependency('io.netty:'))
    }
    relocate 'io.grpc.netty', 'io.grpc.netty.shaded.io.grpc.netty'
    relocate 'io.netty', 'io.grpc.netty.shaded.io.netty'
    // We have to be careful with these replacements as they must not match any
    // string in NativeLibraryLoader, else they cause corruption. Note that
    // this includes concatenation of string literals and constants.
    relocate 'META-INF/native/libnetty', 'META-INF/native/libio_grpc_netty_shaded_netty'
    relocate 'META-INF/native/netty', 'META-INF/native/io_grpc_netty_shaded_netty'
    mergeServiceFiles()
}

// This is a hack to have shadow plugin modify the uploadArchives POM's
// dependencies. If we delete the uploadShadow task, then the plugin will no
// longer modify the POM. This probably can break horribly with parallel build,
// but that's broken anyway with install/uploadArchives
uploadShadow.repositories.addAll(uploadArchives.repositories)
// And then we use a further hack to share that POM with install
install.repositories.mavenInstaller.pom = uploadArchives.repositories.mavenDeployer.pom

task testShadow(type: Test) {
    testClassesDirs = sourceSets.testShadow.output.classesDirs
    classpath = sourceSets.testShadow.runtimeClasspath
}
compileTestShadowJava.options.compilerArgs = compileTestJava.options.compilerArgs
compileTestShadowJava.options.encoding = compileTestJava.options.encoding

test.dependsOn testShadow