apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'maven-publish'

group = 'host.exp.exponent'
version = '0.18.18'

// Utility method to derive boolean values from the environment or from Java properties,
// and return them as strings to be used in BuildConfig fields
def getBoolStringFromPropOrEnv(String name, Boolean defaultValue) {
  def env_value = System.getenv(name)
  def prop_value = findProperty(name)
  def value = defaultValue.toString()
  // If present, property value in gradle.properties overrides default
  if (prop_value != null) {
    value = boolish(prop_value).toString()
    println("expo-updates: Value of ${name} was overridden by property, new value = ${value}")
  }
  // If present, env value overrides default and gradle.properties
  if (env_value != null) {
    value = boolish(env_value).toString()
    println("expo-updates: Value of ${name} was overridden by environment, new value = ${value}")
  }
  return value
}

// If true, app will use bundled manifest and updates will be enabled, even for
// debug builds. (default false)
def exUpdatesNativeDebug = getBoolStringFromPropOrEnv("EX_UPDATES_NATIVE_DEBUG", false)

// If true, code will run that delays app loading until updates is initialized, to prevent ANR issues.
// (default true)
def exUpdatesAndroidDelayLoadApp = getBoolStringFromPropOrEnv("EX_UPDATES_ANDROID_DELAY_LOAD_APP", true)

buildscript {
  def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
  if (expoModulesCorePlugin.exists()) {
    apply from: expoModulesCorePlugin
    applyKotlinExpoModulesCorePlugin()
  }

  // Simple helper that allows the root project to override versions declared by this library.
  ext.safeExtGet = { prop, fallback ->
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
  }

  // Ensures backward compatibility
  ext.getKotlinVersion = {
    if (ext.has("kotlinVersion")) {
      ext.kotlinVersion()
    } else {
      ext.safeExtGet("kotlinVersion", "1.8.10")
    }
  }

  repositories {
    mavenCentral()
  }

  dependencies {
    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${getKotlinVersion()}")
  }
}

afterEvaluate {
  publishing {
    publications {
      release(MavenPublication) {
        from components.release
      }
    }
    repositories {
      maven {
        url = mavenLocal().url
      }
    }
  }
}

android {
  compileSdkVersion safeExtGet("compileSdkVersion", 33)

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
  }

  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_11.majorVersion
  }

  namespace "expo.modules.updates"
  defaultConfig {
    minSdkVersion safeExtGet("minSdkVersion", 21)
    targetSdkVersion safeExtGet("targetSdkVersion", 33)
    versionCode 31
    versionName '0.18.18'
    consumerProguardFiles("proguard-rules.pro")
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    buildConfigField("boolean", "EX_UPDATES_NATIVE_DEBUG", exUpdatesNativeDebug)
    buildConfigField("boolean", "EX_UPDATES_ANDROID_DELAY_LOAD_APP", exUpdatesAndroidDelayLoadApp)

    // uncomment below to export the database schema when making changes
    /* javaCompileOptions {
      annotationProcessorOptions {
        arguments += ["room.schemaLocation":
                      "$projectDir/src/androidTest/schemas".toString()]
      }
    } */
  }
  lintOptions {
    abortOnError false
  }
  testOptions {
    unitTests.includeAndroidResources = true
  }
  sourceSets {
    main.assets.srcDirs += files("$projectDir/src/main/certificates".toString())
    androidTest.assets.srcDirs += files("$projectDir/src/androidTest/schemas".toString())
    androidTest.assets.srcDirs += files("$projectDir/src/androidTest/certificates".toString())
  }
  publishing {
    singleVariant("release") {
      withSourcesJar()
    }
  }
}

dependencies {
  implementation project(':expo-modules-core')
  implementation project(':expo-structured-headers')
  implementation project(':expo-updates-interface')
  implementation project(':expo-manifests')
  implementation project(':expo-json-utils')
  implementation project(':expo-eas-client')
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"

  def room_version = "2.4.2"

  implementation "androidx.room:room-runtime:$room_version"
  kapt "androidx.room:room-compiler:$room_version"

  implementation("com.squareup.okhttp3:okhttp:4.9.2")
  implementation("com.squareup.okhttp3:okhttp-urlconnection:4.9.2")
  implementation("com.squareup.okhttp3:okhttp-brotli:4.9.2")
  implementation("com.squareup.okio:okio:2.9.0")
  implementation("commons-codec:commons-codec:1.10")
  implementation("commons-io:commons-io:2.6")
  implementation("commons-fileupload:commons-fileupload:1.4")
  implementation("org.apache.commons:commons-lang3:3.9")
  implementation("org.bouncycastle:bcutil-jdk15to18:1.70")

  testImplementation 'junit:junit:4.13.2'
  testImplementation 'androidx.test:core:1.4.0'
  testImplementation 'io.mockk:mockk:1.12.3'
  testImplementation "org.jetbrains.kotlin:kotlin-test-junit:${getKotlinVersion()}"

  androidTestImplementation 'androidx.test:runner:1.4.0'
  androidTestImplementation 'androidx.test:core:1.4.0'
  androidTestImplementation 'androidx.test:rules:1.4.0'
  androidTestImplementation 'io.mockk:mockk-android:1.12.3'
  androidTestImplementation "androidx.room:room-testing:$room_version"
  androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:${getKotlinVersion()}"

  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
  implementation "org.jetbrains.kotlin:kotlin-reflect:${getKotlinVersion()}"
}
repositories {
  mavenCentral()
}
