Skip to content

Commit d7f267d

Browse files
committed
Add support for path-based patches
1 parent 326694e commit d7f267d

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

src/build.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@ use std::path::PathBuf;
66
use std::vec::Vec;
77

88
#[derive(Clone)]
9-
pub(crate) struct CratePatch {
9+
pub(crate) enum CratePatch {
10+
Git(GitCratePatch),
11+
Path(PathCratePatch),
12+
}
13+
14+
#[derive(Clone)]
15+
pub(crate) struct GitCratePatch {
1016
pub(crate) name: String,
1117
pub(crate) uri: String,
1218
pub(crate) branch: String,
1319
}
1420

21+
#[derive(Clone)]
22+
pub(crate) struct PathCratePatch {
23+
pub(crate) name: String,
24+
pub(crate) path: String,
25+
}
26+
1527
/// Directory in the [`Workspace`](struct.Workspace.html) where builds can be executed.
1628
///
1729
/// The build directory contains the source code of the crate being built and the target directory
@@ -32,7 +44,7 @@ pub struct BuildBuilder<'a> {
3244
}
3345

3446
impl<'a> BuildBuilder<'a> {
35-
/// Add a patch to this build.
47+
/// Add a git-based patch to this build.
3648
/// Patches get added to the crate's Cargo.toml in the `patch.crates-io` table.
3749
/// # Example
3850
///
@@ -54,11 +66,45 @@ impl<'a> BuildBuilder<'a> {
5466
/// # Ok(())
5567
/// # }
5668
pub fn patch_with_git(mut self, name: &str, uri: &str, branch: &str) -> Self {
57-
self.patches.push(CratePatch {
69+
self.patches.push(CratePatch::Git(GitCratePatch {
5870
name: name.into(),
5971
uri: uri.into(),
6072
branch: branch.into(),
61-
});
73+
}));
74+
self
75+
}
76+
77+
/// Add a path-based patch to this build.
78+
/// Patches get added to the crate's Cargo.toml in the `patch.crates-io` table.
79+
/// # Example
80+
///
81+
/// ```no_run
82+
/// # use rustwide::{WorkspaceBuilder, Toolchain, Crate, cmd::{MountKind, SandboxBuilder}};
83+
/// # use std::{error::Error, path::{Path, PathBuf}};
84+
/// # fn main() -> Result<(), Box<dyn Error>> {
85+
/// # let workspace = WorkspaceBuilder::new("".as_ref(), "").init()?;
86+
/// # let toolchain = Toolchain::dist("");
87+
/// # let krate = Crate::local("".as_ref());
88+
/// # let manifest_dir = "/path/to/bar";
89+
/// let sandbox = SandboxBuilder::new().mount(
90+
/// Path::new(manifest_dir),
91+
/// Path::new("/patch/bar"),
92+
/// MountKind::ReadOnly,
93+
/// );
94+
/// let mut build_dir = workspace.build_dir("foo");
95+
/// build_dir.build(&toolchain, &krate, sandbox)
96+
/// .patch_with_path("bar", "/patch/bar")
97+
/// .run(|build| {
98+
/// build.cargo().args(&["test", "--all"]).run()?;
99+
/// Ok(())
100+
/// })?;
101+
/// # Ok(())
102+
/// # }
103+
pub fn patch_with_path(mut self, name: &str, path: &str) -> Self {
104+
self.patches.push(CratePatch::Path(PathCratePatch {
105+
name: name.into(),
106+
path: path.into(),
107+
}));
62108
self
63109
}
64110

src/prepare.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,24 @@ impl<'a> TomlTweaker<'a> {
312312
};
313313

314314
for patch in self.patches.iter().cloned() {
315-
let mut table = Table::new();
316-
table.insert("git".into(), Value::String(patch.uri));
317-
table.insert("branch".into(), Value::String(patch.branch));
315+
let (name, table) = match patch {
316+
CratePatch::Git(patch) => {
317+
let mut table = Table::new();
318+
table.insert("git".into(), Value::String(patch.uri));
319+
table.insert("branch".into(), Value::String(patch.branch));
320+
(patch.name, table)
321+
}
322+
CratePatch::Path(patch) => {
323+
let mut table = Table::new();
324+
table.insert("path".into(), Value::String(patch.path.clone()));
325+
(patch.name, table)
326+
}
327+
};
328+
318329
cratesio_table
319330
.as_table_mut()
320331
.unwrap()
321-
.insert(patch.name, Value::Table(table));
332+
.insert(name, Value::Table(table));
322333
}
323334
}
324335
}
@@ -361,7 +372,7 @@ pub enum PrepareError {
361372
#[cfg(test)]
362373
mod tests {
363374
use super::TomlTweaker;
364-
use crate::build::CratePatch;
375+
use crate::build::{CratePatch, GitCratePatch, PathCratePatch};
365376
use crate::crates::Crate;
366377
use toml::{self, Value};
367378

@@ -459,14 +470,21 @@ mod tests {
459470

460471
[patch.crates-io]
461472
quux = { git = "https://git.example.com/quux", branch = "dev" }
473+
baz = { path = "/path/to/baz" }
462474
};
463475

464476
let krate = Crate::local("/dev/null".as_ref());
465-
let patches = vec![CratePatch {
466-
name: "quux".into(),
467-
uri: "https://git.example.com/quux".into(),
468-
branch: "dev".into(),
469-
}];
477+
let patches = vec![
478+
CratePatch::Git(GitCratePatch {
479+
name: "quux".into(),
480+
uri: "https://git.example.com/quux".into(),
481+
branch: "dev".into(),
482+
}),
483+
CratePatch::Path(PathCratePatch {
484+
name: "baz".into(),
485+
path: "/path/to/baz".into(),
486+
}),
487+
];
470488
let mut tweaker =
471489
TomlTweaker::new_with_table(&krate, toml.as_table().unwrap().clone(), &patches);
472490
tweaker.tweak();

0 commit comments

Comments
 (0)