Skip to content

Commit

Permalink
xcodeTarget to also automatically match Tests and WatchKit App / Exte…
Browse files Browse the repository at this point in the history
…nsion

- Matches all Xcode targets that start with xcodeTarget
- Standardize xcodeTarget examples and test to use ‘IOS-APP’
- Unit tests for the above
  • Loading branch information
brunobowden committed Aug 17, 2015
1 parent 2c6b8e2 commit dc488b1
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.gradle.util.ConfigureUtil
*
* j2objcConfig {
* xcodeProjectDir '../ios'
* xcodeTarget 'IosApp'
* xcodeTarget 'IOS-APP'
* finalConfigure()
* }
*
Expand Down Expand Up @@ -432,9 +432,7 @@ class J2objcConfig {
* @param extraObjcSrcDirs add directories for Objective-C source to be compiled
*/
void extraObjcSrcDirs(String... extraObjcSrcDirs) {
for (String arg in extraObjcSrcDirs) {
this.extraObjcSrcDirs += arg
}
appendArgs(this.extraObjcSrcDirs, 'extraObjcSrcDirs', extraObjcSrcDirs)
}
/**
* Additional arguments to pass to the native compiler.
Expand All @@ -447,9 +445,7 @@ class J2objcConfig {
* @param extraObjcCompilerArgs add arguments to pass to the native compiler.
*/
void extraObjcCompilerArgs(String... extraObjcCompilerArgs) {
for (String arg in extraObjcCompilerArgs) {
this.extraObjcCompilerArgs += arg
}
appendArgs(this.extraObjcCompilerArgs, 'extraObjcCompilerArgs', extraObjcCompilerArgs)
}
/**
* Additional arguments to pass to the native linker.
Expand All @@ -462,9 +458,7 @@ class J2objcConfig {
* @param extraLinkerArgs add arguments to pass to the native linker.
*/
void extraLinkerArgs(String... extraLinkerArgs) {
for (String arg in extraLinkerArgs) {
this.extraLinkerArgs += arg
}
appendArgs(this.extraLinkerArgs, 'extraLinkerArgs', extraLinkerArgs)
}

/**
Expand Down Expand Up @@ -495,6 +489,11 @@ class J2objcConfig {
String xcodeProjectDir = null
/**
* Xcode app target the generated library should be linked to.
*
* This will automatically add linkage for any target that starts with the
* same name. This should include any Test and Watch targets. For the example of
* xcodeTarget == 'IOS-APP', it will add targets for 'IOS-APPTests',
* 'IOS-APP WatchKit App' & 'IOS-APP WatchKit Extension' (if they exist).
*/
String xcodeTarget = null

Expand Down Expand Up @@ -565,8 +564,24 @@ class J2objcConfig {
// }
@VisibleForTesting
static void appendArgs(List<String> listArgs, String nameArgs, String... args) {
verifyNoSpaceArgs(nameArgs, args)
listArgs.addAll(Arrays.asList(args))
}

// As above but for String[] instead of List<String>
@VisibleForTesting
static void appendArgs(String[] arrayArgs, String nameArgs, String... args) {
verifyNoSpaceArgs(nameArgs, args)
for (String arg in args) {
arrayArgs += arg
}
}

// Verify that no argument contains a space
@VisibleForTesting
static void verifyNoSpaceArgs(String nameArgs, String... args) {
if (args == null) {
throw new InvalidUserDataException("args == null!")
throw new InvalidUserDataException("$nameArgs == null!")
}
for (String arg in args) {
if (arg.contains(' ')) {
Expand All @@ -576,6 +591,5 @@ class J2objcConfig {
"$nameArgs $rewrittenArgs")
}
}
listArgs.addAll(Arrays.asList(args))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

import java.util.regex.Matcher

/**
* Updates the Xcode project with j2objc generated files and resources.
* <p/>
Expand Down Expand Up @@ -184,11 +186,11 @@ class XcodeTask extends DefaultTask {
"Xcode settings need to be configured in this project's build.gradle.\n" +
"The directory should point to the location containing your Xcode project,\n" +
"including the .xccodeproj and .xcworkspace files. The target is the name,\n" +
"of the iOS app within Xcode (not the tests or watch extension targets).\n" +
"of the iOS app within Xcode (not the tests or watch app target).\n" +
"\n" +
"j2objcConfig {\n" +
" xcodeProjectDir '../ios'\n" +
" xcodeTarget 'IOS-APP-TARGET'\n" +
" xcodeTarget 'IOS-APP'\n" +
"}\n" +
"\n" +
"Also see the guidelines for the folder structure:\n" +
Expand Down Expand Up @@ -250,6 +252,27 @@ class XcodeTask extends DefaultTask {
"end\n"
}

/**
* Extracts all target names that start with xcodeTarget.
*
* For xcodeTarget == 'IOS-APP', likely extracted names are:
* IOS-APP
* IOS-APPTests
* IOS-APP WatchKit App
* IOS-APP WatchKit Extension
*/
@VisibleForTesting
static List<String> extractXcodeTargets(String xcodeTarget, List<String> podFileLines) {
List<String> xcodeTargets = new ArrayList<>()
for (line in podFileLines) {
Matcher matcher = (line =~ /^target '($xcodeTarget[^']*)' do$/)
if (matcher.find()) {
xcodeTargets.add(matcher.group(1))
}
}
return xcodeTargets
}

/**
* Modify in place the existing podFile.
*/
Expand All @@ -260,12 +283,23 @@ class XcodeTask extends DefaultTask {

List<String> oldPodFileLines = podFile.readLines()
List<String> newPodFileLines = new ArrayList<String>(oldPodFileLines)
newPodFileLines = updatePodFileLines(
newPodFileLines, xcodeTarget,
podNameDebug, ['Debug'], podPath)
newPodFileLines = updatePodFileLines(
newPodFileLines, xcodeTarget,
podNameRelease, ['Release'], podPath)
List<String> xcodeTargets = extractXcodeTargets(xcodeTarget, oldPodFileLines)

if (! xcodeTargets.contains(xcodeTarget)) {
throw new InvalidUserDataException(
"Can't find iOS App Target '$xcodeTarget' in Podfile: ${podFile.absolutePath}")
}

// Iterate over all the xcodeTargets for Debug and Release
for (xcodeTargetName in xcodeTargets) {
print "xcodeTargetName: $xcodeTargetName"
newPodFileLines = updatePodFileLines(
newPodFileLines, xcodeTargetName,
podNameDebug, ['Debug'], podPath)
newPodFileLines = updatePodFileLines(
newPodFileLines, xcodeTargetName,
podNameRelease, ['Release'], podPath)
}

// Write file only if it's changed
if (!oldPodFileLines.equals(newPodFileLines)) {
Expand Down
Loading

0 comments on commit dc488b1

Please sign in to comment.