Skip to content

Commit

Permalink
Use filename for lockfile generation commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewillmon committed May 9, 2023
1 parent 380d222 commit 6865a60
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lockfile_generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::process::{Command, Stdio};
use std::{fs, io};

Expand All @@ -14,6 +14,17 @@ pub mod poetry;
pub mod python_requirements;
pub mod yarn;

/// Canonicalize and split into parent and file_name
fn split_path(path: &Path) -> Option<(PathBuf, PathBuf)> {
let canonicalized = fs::canonicalize(path).ok()?;
let mut comps = canonicalized.components();
let file_name = match comps.next_back()? {
Component::Normal(f) => Path::new(f).to_path_buf(),
_ => return None,
};
Some((comps.as_path().to_path_buf(), file_name))
}

/// Lockfile generation.
pub trait Generator {
/// Lockfile file name.
Expand All @@ -35,9 +46,7 @@ pub trait Generator {
/// This will ignore all existing lockfiles and create a new lockfile based
/// on the current project configuration.
fn generate_lockfile(&self, manifest_path: &Path) -> Result<String> {
let canonicalized = fs::canonicalize(manifest_path)?;
let project_path = canonicalized
.parent()
let (project_path, file_name) = split_path(manifest_path)
.ok_or_else(|| Error::InvalidManifest(manifest_path.to_path_buf()))?;

// Move files which interfere with lockfile generation.
Expand All @@ -48,8 +57,8 @@ pub trait Generator {
.collect::<Result<Vec<_>>>()?;

// Generate lockfile at the target location.
let mut command = self.command(&canonicalized);
command.current_dir(project_path);
let mut command = self.command(&file_name);
command.current_dir(&project_path);
command.stdin(Stdio::null());
command.stdout(Stdio::null());
command.stderr(Stdio::null());
Expand Down

0 comments on commit 6865a60

Please sign in to comment.