if (versions == null || versions.publish_version == null) {
  throw new IllegalStateException("Unable to reference publish_version!")
} else if (module_group == null || module_name == null) {
  throw new IllegalStateException("Must provide module_group and module_name!")
}

apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: "org.jetbrains.dokka"

task androidSourcesJar(type: Jar) {
  archiveClassifier.set('sources')
  if (project.plugins.findPlugin("com.android.library")) {
    from android.sourceSets.main.java.srcDirs
    from android.sourceSets.main.kotlin.srcDirs
  } else {
    from sourceSets.main.java.srcDirs
    from sourceSets.main.kotlin.srcDirs
  }
}

tasks.withType(dokkaHtmlPartial.getClass()).configureEach {
  pluginsMapConfiguration.set(
    ["org.jetbrains.dokka.base.DokkaBase": """{ "separateInheritedMembers": true}"""]
  )
}

task javadocJar(type: Jar, dependsOn: dokkaJavadoc) {
  archiveClassifier.set('javadoc')
  from dokkaJavadoc.outputDirectory
}

artifacts {
  archives androidSourcesJar
  archives javadocJar
}

group = module_group
version = versions.publish_version

afterEvaluate {
  publishing {
    publications {
      release(MavenPublication) {
        groupId module_group
        artifactId module_name
        version versions.publish_version

        if (project.plugins.findPlugin("com.android.library")) {
          from components.release
        } else {
          from components.java
        }

        artifact androidSourcesJar
        artifact javadocJar

        pom {
          name = module_name
          description = '🙏 Android Runtime Permissions made easy and compact, for Kotlin and AndroidX. With coroutines support!'
          url = 'https://github.com/afollestad/assent'
          licenses {
            license {
              name = 'Apache 2.0 License'
              url = 'https://github.com/afollestad/assent/blob/main/LICENSE.md'
            }
          }
          developers {
            developer {
              id = 'afollestad'
              name = 'Aidan Follestad'
              email = 'dont-email-me@af.codes'
            }
          }
          scm {
            connection = 'scm:git:github.com/afollestad/assent.git'
            developerConnection = 'scm:git:ssh://github.com/afollestad/assent.git'
            url = 'https://github.com/afollestad/assent/tree/main'
          }
        }
      }
    }
  }
}

signing {
  useInMemoryPgpKeys(
      rootProject.ext["signing.keyId"],
      rootProject.ext["signing.key"],
      rootProject.ext["signing.password"],
  )
  sign publishing.publications
}

afterEvaluate {
  publishToSonatype.dependsOn assembleRelease
}