Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PT-391: [NNP] Support java only modules #256

Merged
merged 19 commits into from Jan 18, 2017

Conversation

tobiasheine
Copy link
Contributor

@tobiasheine tobiasheine commented Jan 16, 2017

PT-391

Description
This PR adds support for java-only modules to the gradle-nonnull-plugin.
Problem: The generated folders are not picked up by IDEA as source folders in java-only modules, see this ticket.

Details
We're applying the idea plugin programatically to add the generated folder as source folder.

Tests
We added a test that verifies the generated folder in our java-only module is added to the generatedSourceDirectories.

Paired with @tasomaniac

@tasomaniac tasomaniac changed the base branch from PT-380/gradle_nonnull_plugin to master January 17, 2017 13:19
Copy link
Contributor

@mr-archano mr-archano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. It would be good for @devisnik to scan this too.

classesTask.mustRunAfter task

JavaCompile compileTask =
(JavaCompile) project.tasks.getByName("compile${taskName.capitalize()}Java")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is on a separate line? it doesn't look like too long

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because copy pasta 😄

setValue(null);
}

// this should result in a warning in the IDEA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the right place for this comment? shouldn't be on line 6 instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so true :)

Copy link
Contributor

@devisnik devisnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions.

Apart from that, LGTM.

@@ -14,3 +14,4 @@ TODO

The plugin is **under early development** and to be considered in pre-alpha stage.

The implementation of the java-only module support is inspired by https://github.com/tbroyer/gradle-apt-plugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

applyAndroid(project, project.android.applicationVariants)
} else if (project.plugins.hasPlugin("com.android.library")) {
applyAndroid(project, project.android.libraryVariants)
} else if (project.plugins.hasPlugin('java')) {
applyJava(project)
} else {
throw new StopExecutionException("The 'android' plugin is required.")
Copy link
Contributor

@devisnik devisnik Jan 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message doesn't seem up to date anymore.


public class AndroidNonNullPlugin implements Plugin<Project> {

void apply(Project project) {

if (project.plugins.hasPlugin("com.android.application")) {
if (project.plugins.hasPlugin('com.android.application')) {
applyAndroid(project, project.android.applicationVariants)
} else if (project.plugins.hasPlugin("com.android.library")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have single quotes here as well?

task.outputDir = project.file("${project.buildDir}/generated/source/nonNull/${variant.dirName}")
task.variant = variant
def outputPath = "${project.buildDir}/generated/source/nonNull/${variant.dirName}"
def sourceSets = variant.sourceSets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be inlined?

def task = project.task("generate${taskName.capitalize()}NonNullAnnotations", type: GeneratePackageAnnotationsTask)
task.outputDir = project.file(outputPath)
task.sourceSets = sourceSets
task
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this here might work as well:

project.task(...) {
  outputDir = ...
  sourceSets = ...
}

It returns the created task, so no need to define any local variable.

String taskName = "main".equals(sourceSetName) ? '' : sourceSetName

def generatedSourcesDir = "${project.buildDir}/generated/source/nonNull/${sourceSet.name}"
GeneratePackageAnnotationsTask task = createTask(project, taskName, generatedSourcesDir, [sourceSet])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this specific type, or could we just have a Taskhere?


// inspired by https://github.com/tbroyer/gradle-apt-plugin/blob/master/src/main/groovy/net/ltgt/gradle/apt/AptPlugin.groovy#L171-L213
private static void configureIdeaModule(Project project) {
// so the user does not need to apply it when using the plugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'm not sure about that. If the user doesn't care about idea, I'd rather not force it. There's other IDEs out there. ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Downloading Eclipse now.
Something I haven't been done for a long long time. 😈

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried it:
image

The compilation works and it generates the files successfully in Eclipse.

They are not picked up by Eclipse as source folders. But that is something we should do manually. (Just like the gradle-apt-plugin)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to let the user apply the plugin (as gradle-apt-plugin does).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm embarrassed. The screenshot was like that because I opened the project that already has the files.

We tried with Eclipse with a brand new clone of the project. With or without idea plugin, we couldn't make it work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As side note, we also couldn't make the NonNull annotation work.

def generatedSourcesDir = new File("${project.buildDir}/generated/source/nonNull/${sourceSet.name}")

project.afterEvaluate {
project.idea.module {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inspired by https://github.com/tbroyer/gradle-apt-plugin , right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly.
It was only checking if the idea plugin is applied. We just removed that.

apply plugin: 'java'
apply plugin: com.novoda.gradle.nonnull.AndroidNonNullPlugin

sourceCompatibility = "1.7"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single quotes?

import org.junit.ClassRule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Said Tahsin Dane added 7 commits January 18, 2017 08:29
…ess the parameter via owner.

owner does not work to access method parameters above the closure
…here.

This requires the `idea` plugin to be applied manually, if the project using the plugin is developed under IntelliJ Idea.
And use constant in SourceSet instead of hardcoded value
@stefanhoth stefanhoth closed this Jan 18, 2017
@tobiasheine tobiasheine reopened this Jan 18, 2017
@mr-archano mr-archano merged commit ab02c21 into master Jan 18, 2017
@mr-archano mr-archano deleted the PT-391/support_java_only_moduls branch January 18, 2017 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants