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

Add --working-directory option to support local dependencies #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ description = "Cargo subcommand to build rust projects remotely"
keywords = ["remote", "build"]
categories = ["command-line-utilities", "development-tools::build-utils", "development-tools::cargo-plugins"]
maintenance = { status = "experimental" }
version = "0.2.0"
authors = ["Sebastian Geisler <sgeisler@wh2.tu-dresden.de>"]
version = "0.2.2"
authors = ["Sebastian Geisler <sgeisler@wh2.tu-dresden.de>", "Zannis Kalampoukis <zannis.kal@gmail.com>"]
license = "MIT"
repository = "https://github.com/sgeisler/cargo-remote"
edition = "2018"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ OPTIONS:
-d, --rustup-default <rustup_default> Rustup default (stable|beta|nightly) [default: stable]
-p, --remote-ssh-port <ssh_port> The ssh port to communicate with the build server
-t, --remote-temp-dir <temp_dir> The directory where cargo builds the project
-w, --working-directory <local_dir> The local directory to use as project root. Useful when there are local dependencies outside the workspace directory.

ARGS:
<command> cargo command that will be executed remotely
Expand Down
63 changes: 51 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::hash_map::DefaultHasher;
use std::fs::canonicalize;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use std::process::{exit, Command, Stdio};
Expand Down Expand Up @@ -91,6 +92,13 @@ enum Opts {
#[structopt(help = "cargo command that will be executed remotely")]
command: String,

#[structopt(
short = "w",
long = "working-directory",
help = "The working directory to copy files from. Default is your workspace root."
)]
working_directory: Option<String>,

#[structopt(
help = "cargo options and flags that will be applied remotely",
name = "remote options"
Expand All @@ -111,17 +119,33 @@ fn main() {
manifest_path,
hidden,
command,
working_directory,
options,
} = Opts::from_args();

let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
metadata_cmd.manifest_path(manifest_path).no_deps();

let project_metadata = metadata_cmd.exec().unwrap();
let project_dir = project_metadata.workspace_root;
info!("Project dir: {:?}", project_dir);

let conf = match config::Config::new(&project_dir) {
let project_root = match working_directory {
Some(path) => canonicalize(PathBuf::from(path))
.expect("The provided working directory does not exist or has an error."),
None => project_metadata.workspace_root.clone(),
};
info!(
"Workspace root: {:?}",
project_metadata.workspace_root.clone()
);
info!("Project root: {:?}", project_root);

let diff_from_project_root = project_metadata
.workspace_root
.strip_prefix(project_root.clone())
.expect("Working directory should be an ancestor of the workspace root")
.to_owned();

let conf = match config::Config::new(&project_root) {
Ok(conf) => conf,
Err(error) => {
error!("{}", error);
Expand All @@ -141,8 +165,14 @@ fn main() {

// generate a unique build path by using the hashed project dir as folder on the remote machine
let mut hasher = DefaultHasher::new();
project_dir.hash(&mut hasher);
let build_path = format!("{}/{}/", remote.temp_dir, hasher.finish());
project_root.hash(&mut hasher);
let build_path = format!("{}/{}", remote.temp_dir, hasher.finish());

let mut build_workspace = PathBuf::from(build_path.clone());
build_workspace.push(diff_from_project_root.clone());

let mut project_workspace = project_root.clone();
project_workspace.push(diff_from_project_root);

info!("Transferring sources to build server.");
// transfer project to build server
Expand All @@ -164,7 +194,7 @@ fn main() {
rsync_to
.arg("--rsync-path")
.arg("mkdir -p remote-builds && rsync")
.arg(format!("{}/", project_dir.to_string_lossy()))
.arg(format!("{}/", project_root.to_string_lossy()))
.arg(format!("{}:{}", build_server, build_path))
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
Expand All @@ -181,7 +211,7 @@ fn main() {
"source {}; rustup default {}; cd {}; {} cargo {} {}",
remote.env,
rustup_default,
build_path,
build_workspace.to_string_lossy(),
build_env,
command,
options.join(" ")
Expand Down Expand Up @@ -213,12 +243,14 @@ fn main() {
.arg(format!("ssh -p {}", remote.ssh_port))
.arg(PROGRESS_FLAG)
.arg(format!(
"{}:{}target/{}",
build_server, build_path, file_name
"{}:{}/target/{}",
build_server,
build_workspace.to_string_lossy(),
file_name
))
.arg(format!(
"{}/target/{}",
project_dir.to_string_lossy(),
project_workspace.to_string_lossy(),
file_name
))
.stdout(Stdio::inherit())
Expand All @@ -243,8 +275,15 @@ fn main() {
.arg("-e")
.arg(format!("ssh -p {}", remote.ssh_port))
.arg(PROGRESS_FLAG)
.arg(format!("{}:{}Cargo.lock", build_server, build_path))
.arg(format!("{}/Cargo.lock", project_dir.to_string_lossy()))
.arg(format!(
"{}:{}/Cargo.lock",
build_server,
build_workspace.to_string_lossy()
))
.arg(format!(
"{}/Cargo.lock",
project_workspace.to_string_lossy()
))
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
Expand Down