Skip to content

Commit

Permalink
* gradle plugin: arguments support for launch* tasks
Browse files Browse the repository at this point in the history
These changes allows to pass arguments to application, when launching it on device/simulator similar to arguments field in Idea run/debug configuration.
Beside debug/specific cases this is also required to enable debug mode of Crashlytics: https://firebase.google.com/docs/crashlytics/test-implementation?platform=ios

Custom commands are supported since Gradle v5.
Usage as simple as: `./gradlew  launchIPhoneSimulator --info --args "'hello world'"`

String passed in `--args` is then tokenized into individual arguments,
e.g. `--args="one two"` will produce two arguments in args array ["one", "two"], if spaces to be considered as part of argument, quotation to be used.

Gradle also propagate option description to help command:
```
./gradlew help --task launchIPhoneSimulator

> Task :help
Detailed task information for launchIPhoneSimulator

Path
     :launchIPhoneSimulator

Type
     IPhoneSimulatorTask (org.robovm.gradle.tasks.IPhoneSimulatorTask)

Options
     --args     Command line arguments passed to app.

Description
     Runs your iOS app in the iPhone simulator

Group
     MobiVM

```
  • Loading branch information
dkimitsa committed May 19, 2024
1 parent 833f6d3 commit fa6cde6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.robovm.gradle.tasks;

import org.apache.tools.ant.types.Commandline;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.options.Option;
import org.robovm.compiler.AppCompiler;
import org.robovm.compiler.config.Arch;
import org.robovm.compiler.config.Config;
Expand All @@ -25,11 +27,18 @@
import org.robovm.gradle.RoboVMGradleException;

import java.io.File;
import java.util.Arrays;

/**
*
*/
public abstract class AbstractSimulatorTask extends AbstractRoboVMTask {
private String[] args;

@Option(option = "args", description = "Command line arguments passed to app.")
public void setArgs(String args) {
this.args = Commandline.translateCommandline(args);
}

protected void launch(DeviceType type) {
try {
Expand All @@ -42,6 +51,9 @@ protected void launch(DeviceType type) {
Config config = compiler.getConfig();
IOSSimulatorLaunchParameters launchParameters = (IOSSimulatorLaunchParameters) config.getTarget().createLaunchParameters();
launchParameters.setDeviceType(type);
if (args != null) {
launchParameters.setArguments(Arrays.asList(args));
}

if (extension.getStdoutFifo() != null) {
File stdoutFifo = new File(extension.getStdoutFifo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.robovm.gradle.tasks;

import org.apache.tools.ant.types.Commandline;
import org.gradle.api.tasks.options.Option;
import org.robovm.compiler.AppCompiler;
import org.robovm.compiler.config.Arch;
import org.robovm.compiler.config.Config;
Expand All @@ -23,10 +25,18 @@
import org.robovm.compiler.target.LaunchParameters;
import org.robovm.gradle.RoboVMGradleException;

import java.util.Arrays;

/**
*
*/
public class ConsoleTask extends AbstractRoboVMTask {
private String[] args;

@Option(option = "args", description = "Command line arguments passed to app.")
public void setArgs(String args) {
this.args = Commandline.translateCommandline(args);
}

@Override
public void invoke() {
Expand All @@ -39,6 +49,9 @@ public void invoke() {
AppCompiler compiler = build(OS.getDefaultOS(), arch, ConsoleTarget.TYPE);
Config config = compiler.getConfig();
LaunchParameters launchParameters = config.getTarget().createLaunchParameters();
if (args != null) {
launchParameters.setArguments(Arrays.asList(args));
}
compiler.launch(launchParameters);
} catch (Throwable t) {
throw new RoboVMGradleException("Failed to launch console application", t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.robovm.gradle.tasks;

import org.apache.tools.ant.types.Commandline;
import org.gradle.api.tasks.options.Option;
import org.robovm.compiler.AppCompiler;
import org.robovm.compiler.config.Arch;
import org.robovm.compiler.config.Config;
Expand All @@ -23,12 +25,21 @@
import org.robovm.compiler.target.ios.IOSTarget;
import org.robovm.gradle.RoboVMGradleException;

import java.util.Arrays;

/**
*
* @author Junji Takakura
*/
public class IOSDeviceTask extends AbstractRoboVMTask {

private String[] args;

@Option(option = "args", description = "Command line arguments passed to app.")
public void setArgs(String args) {
this.args = Commandline.translateCommandline(args);
}

@Override
public void invoke() {
try {
Expand All @@ -47,6 +58,9 @@ public void invoke() {
if(udid != null && !udid.isEmpty()){
launchParameters.setDeviceId(udid);
}
if (args != null) {
launchParameters.setArguments(Arrays.asList(args));
}
compiler.launch(launchParameters);
} catch (Throwable t) {
throw new RoboVMGradleException("Failed to launch IOS Device", t);
Expand Down

0 comments on commit fa6cde6

Please sign in to comment.