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

Respect dagger.formatGeneratedSource in AndroidProcessor #3532

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion java/dagger/android/processor/AndroidProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@
public final class AndroidProcessor extends BasicAnnotationProcessor {
private static final String FLAG_EXPERIMENTAL_USE_STRING_KEYS =
"dagger.android.experimentalUseStringKeys";
private static final String FLAG_FORMAT_GENERATED_SOURCES =
"dagger.formatGeneratedSource";
Copy link
Member

Choose a reason for hiding this comment

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

I just noticed that this is reading a Dagger flag from Dagger Android. While not the worst thing, it is something I think we want to try to avoid as we try to keep them as separate as possible. I'm thinking of instead just removing the formatting entirely, especially since in Hilt we don't try to format our generated sources at all. Would this satisfy your use case? I assume you want this flag just to turn it off entirely?

Copy link
Author

Choose a reason for hiding this comment

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

That works for me!


@Override
protected Iterable<? extends Step> steps() {
Filer filer = new FormattingFiler(processingEnv.getFiler());
Filer filer;
if (formatGeneratedSources()) {
filer = new FormattingFiler(processingEnv.getFiler());
} else {
filer = processingEnv.getFiler();
}
Messager messager = processingEnv.getMessager();
Elements elements = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
Expand Down Expand Up @@ -92,6 +99,27 @@ private boolean useStringKeys() {
}
}

private boolean formatGeneratedSources() {
if (!processingEnv.getOptions().containsKey(FLAG_FORMAT_GENERATED_SOURCES)) {
return false;
}
String flagValue = processingEnv.getOptions().get(FLAG_FORMAT_GENERATED_SOURCES);
if (Ascii.equalsIgnoreCase(flagValue, "enabled")) {
return true;
} else if (flagValue == null || Ascii.equalsIgnoreCase(flagValue, "disabled")) {
return false;
} else {
processingEnv
.getMessager()
.printMessage(
ERROR,
String.format(
"Unknown flag value: %s. %s must be set to either 'enabled' or 'disabled'.",
flagValue, FLAG_FORMAT_GENERATED_SOURCES));
return false;
}
}

@Override
public Set<String> getSupportedOptions() {
return ImmutableSet.of(FLAG_EXPERIMENTAL_USE_STRING_KEYS);
Expand Down