mirror of
https://github.com/astral-sh/uv.git
synced 2025-07-07 21:35:00 +00:00
Add output file option to compile (#93)
`pip-compile` has the same option. I need this esp. since piping doesn't work as we write to stdout.
This commit is contained in:
parent
6a7954cdd0
commit
530edb6e39
3 changed files with 23 additions and 8 deletions
|
@ -1,4 +1,6 @@
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{stdout, BufWriter};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
@ -18,6 +20,7 @@ use crate::printer::Printer;
|
||||||
/// Resolve a set of requirements into a set of pinned versions.
|
/// Resolve a set of requirements into a set of pinned versions.
|
||||||
pub(crate) async fn compile(
|
pub(crate) async fn compile(
|
||||||
src: &Path,
|
src: &Path,
|
||||||
|
output_file: Option<&Path>,
|
||||||
cache: Option<&Path>,
|
cache: Option<&Path>,
|
||||||
mut printer: Printer,
|
mut printer: Printer,
|
||||||
) -> Result<ExitStatus> {
|
) -> Result<ExitStatus> {
|
||||||
|
@ -80,12 +83,11 @@ pub(crate) async fn compile(
|
||||||
.dimmed()
|
.dimmed()
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
for (name, package) in resolution.iter() {
|
if let Some(output_file) = output_file {
|
||||||
#[allow(clippy::print_stdout)]
|
resolution.write_requirement_format(&mut BufWriter::new(File::create(output_file)?))?;
|
||||||
{
|
} else {
|
||||||
println!("{}=={}", name, package.version());
|
resolution.write_requirement_format(&mut stdout().lock())?;
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
Ok(ExitStatus::Success)
|
Ok(ExitStatus::Success)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
|
|
||||||
|
use crate::commands::ExitStatus;
|
||||||
use clap::{Args, Parser, Subcommand};
|
use clap::{Args, Parser, Subcommand};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
|
|
||||||
use crate::commands::ExitStatus;
|
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod logging;
|
mod logging;
|
||||||
mod printer;
|
mod printer;
|
||||||
|
@ -49,6 +48,9 @@ enum Commands {
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
struct CompileArgs {
|
struct CompileArgs {
|
||||||
|
/// Output `requirements.txt` file
|
||||||
|
#[clap(short, long)]
|
||||||
|
output_file: Option<PathBuf>,
|
||||||
/// Path to the `requirements.txt` file to compile.
|
/// Path to the `requirements.txt` file to compile.
|
||||||
src: PathBuf,
|
src: PathBuf,
|
||||||
}
|
}
|
||||||
|
@ -104,6 +106,7 @@ async fn main() -> ExitCode {
|
||||||
Commands::Compile(args) => {
|
Commands::Compile(args) => {
|
||||||
commands::compile(
|
commands::compile(
|
||||||
&args.src,
|
&args.src,
|
||||||
|
args.output_file.as_deref(),
|
||||||
dirs.as_ref()
|
dirs.as_ref()
|
||||||
.map(ProjectDirs::cache_dir)
|
.map(ProjectDirs::cache_dir)
|
||||||
.filter(|_| !cli.no_cache),
|
.filter(|_| !cli.no_cache),
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
use pep440_rs::Version;
|
use pep440_rs::Version;
|
||||||
use puffin_client::File;
|
use puffin_client::File;
|
||||||
|
@ -33,6 +35,14 @@ impl Resolution {
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.0.is_empty()
|
self.0.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Write the resolution in the `{name}=={version}` format of requirements.txt that pip uses.
|
||||||
|
pub fn write_requirement_format(&self, writer: &mut impl Write) -> io::Result<()> {
|
||||||
|
for (name, package) in self.iter() {
|
||||||
|
writeln!(writer, "{}=={}", name, package.version())?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Resolution {
|
impl std::fmt::Display for Resolution {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue