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

Allow mapping of Java source files. #455

Merged
merged 1 commit into from
Sep 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,26 @@ class J2objcConfig {
return ConfigureUtil.configure(cl, translatePattern)
}

/**
* A mapping from source file names (in the project Java sourcesets) to alternate
* source files.
* Both before and after names (keys and values) are evaluated using project.file(...).
* <p/>
* Mappings can be used to have completely different implementations in your Java
* jar vs. your Objective-C library. This can be especially useful when compiling
* a third-party library and you need to provide non-trivial OCNI implementations
* in Objective-C.
*/
Map<String, String> translateSourceMapping = [:]

/**
* Adds a new source mapping.
* @see #translateSourceMapping
*/
void translateSourceMapping(String before, String after) {
translateSourceMapping.put(before, after)
}

/**
* @see #dependsOnJ2objcLib(org.gradle.api.Project)
* @deprecated Use `dependencies { j2objcLinkage project(':beforeProjectName') }` or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.gradle.api.tasks.TaskAction
class CycleFinderTask extends DefaultTask {

@InputFiles
FileTree getSrcInputFiles() {
FileCollection getSrcInputFiles() {
// Note that translatePattern does not need to be an @Input because it is
// solely an input to this method, which is already an input (via @InputFiles).
FileTree allFiles = Utils.srcSet(project, 'main', 'java')
Expand All @@ -45,7 +45,7 @@ class CycleFinderTask extends DefaultTask {
if (J2objcConfig.from(project).translatePattern != null) {
ret = allFiles.matching(J2objcConfig.from(project).translatePattern)
Copy link
Contributor

Choose a reason for hiding this comment

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

Change ret => result, I think it's more legible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, it seems like ret is a convention so shall we stick with that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep it is

}
return ret
return Utils.mapSourceFiles(project, ret, getTranslateSourceMapping())
}

// All input files that could affect translation output, except those in j2objc itself.
Expand Down Expand Up @@ -82,6 +82,9 @@ class CycleFinderTask extends DefaultTask {
@Input
List<String> getTranslateJ2objcLibs() { return J2objcConfig.from(project).translateJ2objcLibs }

@Input
Map<String, String> getTranslateSourceMapping() { return J2objcConfig.from(project).translateSourceMapping }

@Input
boolean getFilenameCollisionCheck() { return J2objcConfig.from(project).filenameCollisionCheck }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ class TranslateTask extends DefaultTask {
allFiles = allFiles.matching(J2objcConfig.from(project).translatePattern)
}
FileCollection ret = allFiles
ret = Utils.mapSourceFiles(project, ret, getTranslateSourceMapping())

if (additionalSrcFiles != null) {
ret = allFiles.plus(additionalSrcFiles)
ret = ret.plus(additionalSrcFiles)
}
return ret
}
Expand Down Expand Up @@ -99,6 +101,9 @@ class TranslateTask extends DefaultTask {
@Input
List<String> getTranslateJ2objcLibs() { return J2objcConfig.from(project).translateJ2objcLibs }

@Input
Map<String, String> getTranslateSourceMapping() { return J2objcConfig.from(project).translateSourceMapping }

@Input
boolean getFilenameCollisionCheck() { return J2objcConfig.from(project).filenameCollisionCheck }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,16 @@ class Utils {
static boolean projectMkDir(Project proj, Object path) {
return proj.mkdir(path)
}

static FileCollection mapSourceFiles(Project proj, FileCollection files,
Map<String, String> sourceMapping) {
for (String before : sourceMapping.keySet()) {
if (files.contains(proj.file(before))) {
// Replace the before file with the after file.
files = files.minus(proj.files(before)).plus(
proj.files(sourceMapping.get(before)))
Copy link
Contributor

Choose a reason for hiding this comment

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

How efficient is this for large sets of files? I'm sure it isn't efficient but is it in practice an issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'm using this for large libraries and have noticed no issue. the slowdown is proportional to the number of sourceMappings, which we expect to be very small.

Copy link
Contributor

Choose a reason for hiding this comment

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

Make sense that performance will not be an issue.

}
Copy link
Contributor

Choose a reason for hiding this comment

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

Produce an error if a sourceMapping doesn't exist? Likely that indicates a deeper error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it would mean a failure of the JDK. let's not verify basic Java functionality. (the before key definitively came from sourceMapping 4 lines above).

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds like that should be an assert then ;-)

}
return files
}
}