plugins {
    id 'java'
    id 'com.google.cloud.tools.jib'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.google.guava:guava:23.6-jre'
}

project.ext.value = 'original'
project.ext.jibCreationTime = '1970-01-23T00:23:42Z'
project.ext.jibFilesModificationTime = '1970-01-23T01:23:42Z'

project.afterEvaluate {
    project.ext.value = 'updated'
    project.ext.getCustomPermissions = { -> return ['/updated': '755'] }
    project.ext.jibCreationTime = '2022-07-19T10:23:42Z'
    project.ext.jibFilesModificationTime = '2022-07-19T11:23:42Z'
}

jib {
    to {
        image = project.provider { project.ext.value + '-image' }
        tags = project.provider { [project.ext.value + '-tag', 'tag2'] }
    }
    container {
        labels = project.provider {
            [
                    firstkey : project.ext.value + '-first-label',
                    secondKey: project.ext.value + '-second-label'
            ]
        }
        entrypoint = project.provider { [project.ext.value] }
        creationTime = project.provider { project.ext.jibCreationTime }
        filesModificationTime = project.provider { project.ext.jibFilesModificationTime }
        mainClass = project.provider { project.ext.value }
        jvmFlags = project.provider { [project.ext.value] }
    }
    extraDirectories {
        paths = project.provider { ['src/main/' + project.ext.value + '-custom-extra-dir'] }
        permissions = project.provider { -> project.ext.getCustomPermissions() }
    }
}

tasks.register('showlabels') {
    Map<String, String> prop = project.extensions.getByName('jib')['container']['labels'].get()
    println('labels contain values ' + prop)
}

tasks.register('showentrypoint') {
    List<String> prop = project.extensions.getByName('jib')['container'].getEntrypoint()
    println('entrypoint contains ' + prop.join(","))
}

tasks.register('check-extra-directories') {
    List<Object> paths = project.extensions.getByName('jib')['extraDirectories']['paths'].collect{ path -> path['from']}
    Map<String, String> permissions = project.extensions.getByName('jib')['extraDirectories']['permissions'].get()
    println('extraDirectories paths: ' + paths)
    println('extraDirectories permissions: ' + permissions)
}

tasks.register('showtimes') {
    String prop = project.extensions.jib.container.creationTime.get()
    println('creationTime=' + prop)
    prop = project.extensions.jib.container.filesModificationTime.get()
    println('filesModificationTime=' + prop)
}

tasks.register('showMainClass') {
    String prop = project.extensions.jib.container.mainClass
    println('mainClass value ' + prop)
}

tasks.register('showJvmFlags') {
    List<String> prop = project.extensions.jib.container.jvmFlags
    println('jvmFlags value ' + prop)
}
