I said I was done, but is there any chance that th...
# gradle
x
I said I was done, but is there any chance that this is a bug in the dsl? using groovy I could just pass a string, and that could be null as long as I wasn't trying to publish
Copy code
publishing {
    repositories {
        maven {
            url = uri(System.getenv("JRS_S3_URI") ?: "" )
g
what exactly your problem is?
If null is fine for groovy, it should be fine for kotlin
but you shouldn’t pass “” to uri
x
I guess i'm just finding it weird that now I have to do the null check, where in groovy it was fine to just pass the null or a string, and it would only complain if it was missing at publish, and since publish is a ci task https://bitbucket.org/xenworks/util/src/d83084173dbe91bdcf9e658a93091162a7402cc7/build.gradle?at=master&fileviewer=file-view-default
g
maybe you need:
url = System.getenv("JRS_S3_URI")?.run { uri(this) }
just because groovy implicitly creates
uri
for you
because you definitely shouldn’t pass empty string to
uri()
constructor, because empty string is invalid uri
x
right, that's what I thought, let me try that string
g
or if you need less tricky code just:
Copy code
val s3Uri = System.getenv("JRS_S3_URI")
url = if (s3Uri == null) null else uri(s3Uri)
x
how exactly does that magic work? not the second one the first where does ?.run come from?
g
it’s Kotlin stdlib function
?. - means do not call anything after
.
if expression before
?
is null run - mens: get receiver object as context (this) and return result of
lambda
, so run converts string to uri. If there is null instead of string just return null
.run
and
.let
very similar, run gets receiver as
this
but let gets receiver as first lambda argument
x
idea is warning me on the return that argument may not be null in java but may be null in kotlin
weird
I "think" that's on the url
g
Because uri returns platform type (null or non-null) I suppose
Could you show screenshot of warning?
x
C:\Users\xeno\Desktop\2017-12-21 02_24_56-.png
hmm
g
If you don’t need publishing config if you not provide publishing environment varialbes, just use if:
Copy code
val publishConigAvailable = System.getenv('JRS_S3_URI') != null
If (publishConigAvailable) {
    publishing {
    ...
    }

}
x
could work, though I try to avoid too much logic in build scripts, this
run
seems to work well enough
g
yes. but you actually have different logic depending on evironment variables
in our case we use the same approach, add publishing configs only when publishing env variables available
x
Copy code
System.getenv("JRS_S3_URI")?.let {
    val sourcesJar by tasks.creating(Jar::class) {
        classifier = "sources"
        from(java.sourceSets["main"].allSource)
    }

    publishing {
        repositories {
            maven {
                url = uri(it)

                credentials(AwsCredentials::class.java) {
                    accessKey = System.getenv("JRS_ACCESSKEYID")
                    secretKey = System.getenv("JRS_SECRETACCESSKEY")
                }
            }
        }
        (publications) {
            "mavenJava"(MavenPublication::class) {
                from(components["java"])
                artifact(sourcesJar)
            }
        }
    }
}
going with this, presuming it's effectively the same