Skip to content

Commit

Permalink
feat: add --features argument to cargo shuttle run (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed May 13, 2023
1 parent 7009284 commit 4d8d93b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 11 deletions.
3 changes: 3 additions & 0 deletions cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ pub struct RunArgs {
/// Use 0.0.0.0 instead of localhost (for usage with local external devices)
#[arg(long)]
pub external: bool,
/// Set the list of features to activate
#[arg(long)]
pub features: Vec<String>,
/// Use release mode for building the project.
#[arg(long, short = 'r')]
pub release: bool,
Expand Down
8 changes: 7 additions & 1 deletion cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,13 @@ impl Shuttle {
);

// Compile all the alpha or shuttle-next services in the workspace.
build_workspace(working_directory, run_args.release, tx).await
build_workspace(
working_directory,
run_args.features.clone(),
run_args.release,
tx,
)
.await
}

async fn setup_local_provisioner(
Expand Down
1 change: 1 addition & 0 deletions cargo-shuttle/tests/integration/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async fn cargo_shuttle_run(working_directory: &str, external: bool) -> String {
let run_args = RunArgs {
port,
external,
features: vec![],
release: false,
};

Expand Down
2 changes: 1 addition & 1 deletion deployer/src/deployment/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ async fn build_deployment(
project_path: &Path,
tx: crossbeam_channel::Sender<Message>,
) -> Result<BuiltService> {
let runtimes = build_workspace(project_path, true, tx)
let runtimes = build_workspace(project_path, vec![], true, tx)
.await
.map_err(|e| Error::Build(e.into()))?;

Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/integration/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn spawn_runtime(project_path: String, service_name: &str) -> Result<T
let runtime_address = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), runtime_port);

let (tx, _) = crossbeam_channel::unbounded();
let runtimes = build_workspace(Path::new(&project_path), false, tx).await?;
let runtimes = build_workspace(Path::new(&project_path), vec![], false, tx).await?;

let secrets: HashMap<String, String> = Default::default();

Expand Down
23 changes: 20 additions & 3 deletions service/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
use std::rc::Rc;

use anyhow::{anyhow, bail, Context};
use cargo::core::compiler::{CompileKind, CompileMode, CompileTarget, MessageFormat};
use cargo::core::{Package, Shell, Verbosity, Workspace};
use cargo::core::{FeatureValue, Package, Shell, Verbosity, Workspace};
use cargo::ops::{self, clean, compile, CleanOptions, CompileOptions};
use cargo::util::homedir;
use cargo::util::interning::InternedString;
Expand Down Expand Up @@ -79,6 +80,7 @@ fn extract_shuttle_toml_name(path: PathBuf) -> anyhow::Result<String> {
/// Given a project directory path, builds the crate
pub async fn build_workspace(
project_path: &Path,
features: Vec<String>,
release_mode: bool,
tx: Sender<Message>,
) -> anyhow::Result<Vec<BuiltService>> {
Expand Down Expand Up @@ -124,7 +126,13 @@ pub async fn build_workspace(
let mut runtimes = Vec::new();

if !alpha_packages.is_empty() {
let opts = get_compile_options(&config, alpha_packages, release_mode, false)?;
let opts = get_compile_options(
&config,
alpha_packages,
features.clone(),
release_mode,
false,
)?;
let compilation = compile(&ws, &opts)?;

let mut alpha_binaries = compilation
Expand All @@ -145,7 +153,7 @@ pub async fn build_workspace(
}

if !next_packages.is_empty() {
let opts = get_compile_options(&config, next_packages, release_mode, true)?;
let opts = get_compile_options(&config, next_packages, features, release_mode, true)?;
let compilation = compile(&ws, &opts)?;

let mut next_libraries = compilation
Expand Down Expand Up @@ -233,6 +241,7 @@ pub fn get_config(writer: PipeWriter) -> anyhow::Result<Config> {
fn get_compile_options(
config: &Config,
packages: Vec<String>,
features: Vec<String>,
release_mode: bool,
wasm: bool,
) -> anyhow::Result<CompileOptions> {
Expand Down Expand Up @@ -262,6 +271,14 @@ fn get_compile_options(
}];

opts.spec = ops::Packages::Packages(packages);
for feature in features
.iter()
.map(|v| FeatureValue::new(InternedString::new(v)))
{
if let Some(cli_features) = Rc::get_mut(&mut opts.cli_features.features) {
cli_features.insert(feature);
}
}

Ok(opts)
}
Expand Down
10 changes: 5 additions & 5 deletions service/tests/integration/build_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use shuttle_service::builder::{build_workspace, BuiltService};
async fn not_shuttle() {
let (tx, _) = crossbeam_channel::unbounded();
let project_path = format!("{}/tests/resources/not-shuttle", env!("CARGO_MANIFEST_DIR"));
build_workspace(Path::new(&project_path), false, tx)
build_workspace(Path::new(&project_path), vec![], false, tx)
.await
.unwrap();
}
Expand All @@ -17,7 +17,7 @@ async fn not_shuttle() {
async fn not_bin() {
let (tx, _) = crossbeam_channel::unbounded();
let project_path = format!("{}/tests/resources/not-bin", env!("CARGO_MANIFEST_DIR"));
match build_workspace(Path::new(&project_path), false, tx).await {
match build_workspace(Path::new(&project_path), vec![], false, tx).await {
Ok(_) => {}
Err(e) => panic!("{}", e.to_string()),
}
Expand All @@ -29,7 +29,7 @@ async fn is_bin() {
let project_path = format!("{}/tests/resources/is-bin", env!("CARGO_MANIFEST_DIR"));

assert_eq!(
build_workspace(Path::new(&project_path), false, tx)
build_workspace(Path::new(&project_path), vec![], false, tx)
.await
.unwrap(),
vec![BuiltService::new(
Expand All @@ -50,7 +50,7 @@ async fn not_found() {
"{}/tests/resources/non-existing",
env!("CARGO_MANIFEST_DIR")
);
build_workspace(Path::new(&project_path), false, tx)
build_workspace(Path::new(&project_path), vec![], false, tx)
.await
.unwrap();
}
Expand All @@ -62,7 +62,7 @@ async fn workspace() {
let project_path = format!("{}/tests/resources/workspace", env!("CARGO_MANIFEST_DIR"));

assert_eq!(
build_workspace(Path::new(&project_path), false, tx)
build_workspace(Path::new(&project_path), vec![], false, tx)
.await
.unwrap(),
vec![
Expand Down

0 comments on commit 4d8d93b

Please sign in to comment.