Skip to content

Commit

Permalink
Configure adb forward and adb reverse for x run (#123)
Browse files Browse the repository at this point in the history
Just like a similar PR for `cargo-apk` we'd like to set up some port
forwarding to/from a debug phone automatically while using `x run`.
This allows configuring a mapping of ports under the `debug:` section of
`android:` in `manifest.yaml`.
  • Loading branch information
MarijnS95 authored Aug 22, 2023
1 parent 9e58a8a commit 4e2c8e9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
14 changes: 14 additions & 0 deletions xbuild/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use apk::VersionCode;
use appbundle::InfoPlist;
use msix::AppxManifest;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -330,6 +331,16 @@ pub struct GenericConfig {
runtime_libs: Vec<PathBuf>,
}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct AndroidDebugConfig {
/// Forward remote (phone) socket connection to local (host)
#[serde(default)]
pub forward: HashMap<String, String>,
/// Forward local (host) socket connection to remote (phone)
#[serde(default)]
pub reverse: HashMap<String, String>,
}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct AndroidConfig {
#[serde(flatten)]
Expand All @@ -342,6 +353,9 @@ pub struct AndroidConfig {
pub gradle: bool,
#[serde(default)]
pub wry: bool,
/// Debug configuration for `x run`
#[serde(default)]
pub debug: AndroidDebugConfig,
}

#[derive(Clone, Debug, Default, Deserialize)]
Expand Down
40 changes: 39 additions & 1 deletion xbuild/src/devices/adb.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::AndroidDebugConfig;
use crate::devices::{Backend, Device};
use crate::{Arch, Platform};
use anyhow::Result;
Expand Down Expand Up @@ -126,6 +127,36 @@ impl Adb {
Ok(())
}

fn forward_reverse(&self, device: &str, debug_config: &AndroidDebugConfig) -> Result<()> {
for (local, remote) in &debug_config.forward {
let status = self
.adb(device)
.arg("forward")
.arg(local)
.arg(remote)
.status()?;
anyhow::ensure!(
status.success(),
"adb forward exited with code {:?}",
status.code()
);
}
for (remote, local) in &debug_config.reverse {
let status = self
.adb(device)
.arg("reverse")
.arg(remote)
.arg(local)
.status()?;
anyhow::ensure!(
status.success(),
"adb reverse exited with code {:?}",
status.code()
);
}
Ok(())
}

fn set_debug_app(&self, device: &str, package: &str) -> Result<()> {
let status = self
.shell(device, None)
Expand Down Expand Up @@ -292,7 +323,13 @@ impl Adb {
Ok(())
}

pub fn run(&self, device: &str, path: &Path, debug: bool) -> Result<()> {
pub fn run(
&self,
device: &str,
path: &Path,
debug_config: &AndroidDebugConfig,
debug: bool,
) -> Result<()> {
let entry_point = Apk::entry_point(path)?;
let package = &entry_point.package;
let activity = &entry_point.activity;
Expand All @@ -303,6 +340,7 @@ impl Adb {
self.clear_debug_app(device)?;
}
self.install(device, path)?;
self.forward_reverse(device, debug_config)?;
let last_timestamp = self.logcat_last_timestamp(device)?;
self.start(device, package, activity)?;
let pid = self.pidof(device, package)?;
Expand Down
2 changes: 1 addition & 1 deletion xbuild/src/devices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Device {

pub fn run(&self, env: &BuildEnv, path: &Path) -> Result<()> {
match &self.backend {
Backend::Adb(adb) => adb.run(&self.id, path, false),
Backend::Adb(adb) => adb.run(&self.id, path, &env.config.android().debug, false),
Backend::Host(host) => host.run(path),
Backend::Imd(imd) => imd.run(env, &self.id, path),
}?;
Expand Down

0 comments on commit 4e2c8e9

Please sign in to comment.