Skip to content

Commit

Permalink
feat: use gitignore when copying the recipe files (#1193)
Browse files Browse the repository at this point in the history
Also ignores the output dir when copying the recipe contents and ignores hidden files when copying the recipe
  • Loading branch information
wolfv authored Nov 14, 2024
1 parent 31a1e44 commit f6e5409
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
9 changes: 0 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,6 @@ pub async fn get_build_output(
if output_dir.exists() {
output_dir = canonicalize(&output_dir).into_diagnostic()?;
}
if output_dir.starts_with(
recipe_path
.parent()
.expect("Could not get parent of recipe"),
) {
return Err(miette::miette!(
"The output directory cannot be in the recipe directory.\nThe current output directory is: {}\nSelect a different output directory with the --output-dir option or set the CONDA_BLD_PATH environment variable"
, output_dir.to_string_lossy()));
}

let recipe_text = fs::read_to_string(recipe_path).into_diagnostic()?;

Expand Down
34 changes: 29 additions & 5 deletions src/packaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ mod metadata;
pub use file_finder::{content_type, Files, TempFiles};
pub use metadata::{contains_prefix_binary, contains_prefix_text, create_prefix_placeholder};

use crate::{metadata::Output, package_test::write_test_files, post_process, tool_configuration};
use crate::{
metadata::Output,
package_test::write_test_files,
post_process,
recipe::parser::GlobVec,
source::{self, copy_dir},
tool_configuration,
};

#[allow(missing_docs)]
#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -58,7 +65,7 @@ pub enum PackagingError {
RelinkError(#[from] crate::post_process::relink::RelinkError),

#[error(transparent)]
SourceError(#[from] crate::source::SourceError),
SourceError(#[from] source::SourceError),

#[error("could not create python entry point: {0}")]
CannotCreateEntryPoint(String),
Expand Down Expand Up @@ -90,7 +97,7 @@ fn copy_license_files(
let licenses_folder = tmp_dir_path.join("info/licenses/");
fs::create_dir_all(&licenses_folder)?;

let copy_dir = crate::source::copy_dir::CopyDir::new(
let copy_dir = copy_dir::CopyDir::new(
&output.build_configuration.directories.recipe_dir,
&licenses_folder,
)
Expand All @@ -101,7 +108,7 @@ fn copy_license_files(
let copied_files_recipe_dir = copy_dir.copied_paths();
let any_include_matched_recipe_dir = copy_dir.any_include_glob_matched();

let copy_dir = crate::source::copy_dir::CopyDir::new(
let copy_dir = copy_dir::CopyDir::new(
&output.build_configuration.directories.work_dir,
&licenses_folder,
)
Expand Down Expand Up @@ -139,8 +146,25 @@ fn write_recipe_folder(
let recipe_folder = tmp_dir_path.join("info/recipe/");
let recipe_dir = &output.build_configuration.directories.recipe_dir;
let recipe_path = &output.build_configuration.directories.recipe_path;
let output_dir = &output.build_configuration.directories.output_dir;

let mut copy_builder = copy_dir::CopyDir::new(recipe_dir, &recipe_folder)
.use_gitignore(true)
.ignore_hidden_files(true);

// if the output dir is inside the same directory as the recipe, then we
// need to ignore the output dir when copying
if let Ok(ignore_output) = output_dir.strip_prefix(recipe_dir) {
tracing::info!(
"Ignoring output dir in recipe folder: {}",
output_dir.to_string_lossy()
);
let output_dir_glob = format!("{}/**", ignore_output.to_string_lossy());
let glob_vec = GlobVec::from_vec(vec![], Some(vec![&output_dir_glob]));
copy_builder = copy_builder.with_globvec(&glob_vec);
}

let copy_result = crate::source::copy_dir::CopyDir::new(recipe_dir, &recipe_folder).run()?;
let copy_result = copy_builder.run()?;

let mut files = Vec::from(copy_result.copied_paths());

Expand Down
1 change: 0 additions & 1 deletion src/recipe/parser/glob_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ impl GlobVec {
}

/// Only used for testing
#[cfg(test)]
pub fn from_vec(include: Vec<&str>, exclude: Option<Vec<&str>>) -> Self {
let include_vec: Vec<Glob> = include
.into_iter()
Expand Down
7 changes: 4 additions & 3 deletions src/source/copy_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ pub(crate) fn copy_file(
/// It uses the `ignore` crate to read the `.gitignore` file in the source directory and uses the globs
/// to filter the files and directories to copy.
///
/// The copy process also ignores hidden files and directories by default.
///
/// # Return
///
/// The returned `Vec<PathBuf>` contains the paths of the copied files.
Expand All @@ -130,8 +128,11 @@ impl<'a> CopyDir<'a> {
from_path,
to_path,
globvec: GlobVec::default(),
// use the gitignore file by default
use_gitignore: false,
// use the global git ignore file by default
use_git_global: false,
// include hidden files by default
hidden: false,
copy_options: CopyOptions::default(),
}
Expand All @@ -154,7 +155,7 @@ impl<'a> CopyDir<'a> {
}

#[allow(unused)]
pub fn hidden(mut self, b: bool) -> Self {
pub fn ignore_hidden_files(mut self, b: bool) -> Self {
self.hidden = b;
self
}
Expand Down

0 comments on commit f6e5409

Please sign in to comment.