jruby-gradle / jruby-gradle-plugin

A Gradle plugin for working with JRuby projects for fun and profit. Mostly profit.
http://jruby-gradle.org
Other
92 stars 41 forks source link

com.github.jruby-gradle.base 2.0.0 not working when use kotlin DSL #407

Open blling opened 4 years ago

blling commented 4 years ago

I configed like this:

plugins {
    id("com.github.jruby-gradle.base") version "2.0.0"
}

repositories{
   // Unresolved reference: ruby
    ruby.gems()
}

but i get an error Unresolved reference: ruby

ciscoo commented 3 years ago

The way this plugin configures the gems repository is a bit of a convoluted IMO and uses internals of Gradle from what I can tell. This just does not play nice with the Kotlin DSL.

import com.github.jrubygradle.api.core.RepositoryHandlerExtension

val repositoriesInternal = repositories as ExtensionAware
repositoriesInternal.extensions.configure(RepositoryHandlerExtension::class) {
    gems()
}

This likely stems from the wide support of Gradle version judging from:

repositories is decorated as ExtensionAware internally by Gradle:

You can confirm this yourself by printing the class:

println(repositories::class)
// class org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler_Decorated

The Javadoc for Instantiator says: (emphasis mine)

An implementation may accept abstract classes or interfaces and provide implementations for the missing pieces, for example providing an implementation of ExtensionAware.

So the "fix" from to get this plugin to nicely with the Kotlin DSL:

  1. Drop Groovy for Java as the implementation language for this plugin.
  2. Use as much of the public Gradle API as possible, where applicable.

I do not know enough about this plugin to say those two points are accurate. I wanted to use Asciidoctor Diagram and went down a rabbit hole trying to figure out how to configure it which led me here since our projects use Kotlin DSL.

cc @ysb33r

ysb33r commented 3 years ago

Thanks for raising this! There is a lot of older code that needs clean up in these plugins and it will only happen if people find issues :smile_cat:

ysb33r commented 3 years ago

However, you should have had no need to go down to RepositoriesInternal, because as you said it is ExtensionAware. I'll look into this when I get a chance.

ciscoo commented 3 years ago

Well, to my understanding of Gradle's codebase, repositories { } is made ExtensionAware at runtime. So the cast to ExtensionAware is needed in my snippet above because the Kotlin DSL extension for repositories { } references RepositoryHandler which has no references to ExtensionAware.

Vampire commented 3 years ago

While it would probably be nice if the plugin provides a ruby accessor, the hoops are not that high and ugly as ciscoo depicted. This should work fine:

repositories {
    this as ExtensionAware
    the<RepositoryHandlerExtension>().gems()
}

Disclaimer: this is untested as I don't use ruby, but it should work fine.

pedromalta commented 2 years ago

And using

repositories {
    this as ExtensionAware
    the<RepositoryHandlerExtension>().gem()
}

How would I add the gems? Below code

dependencies {
    gems("rubygems:aws-sdk-s3:1.42.0")
}
Vampire commented 2 years ago

Yes, that just works. gems is a configuration added by the plugin, so there is an accessor generated so that you can use it like you wrote it.

pedromalta commented 2 years ago

I get "unresolved reference"

Captura-de-tela-de-2021-10-07-13-10-41.png

Vampire commented 2 years ago

That's because you are using the legacy way to apply the plugin. Use the plugins block, only then the accessors for Kotlin DSL usage are generated.

pedromalta commented 2 years ago

If I add the jruby to the plugins block like id("com.github.jruby-gradle.base") not even the "android" part is recognized. This part I have no idea why. That's why I used it the legacy way

pedromalta commented 2 years ago

Ok, fixed, thanks, you were right, I was mixing new with legacy way :)

pedromalta commented 2 years ago

I found that using

repositories {
    this as ExtensionAware
    the<RepositoryHandlerExtension>().gem()
}

Breaks the release build.

> Failed to query the value of task ':app:collect<Flavor>ReleaseDependencies' property 'projectRepositories'.
> repo.url must not be nul

Any input?