Skip to content

Commit

Permalink
Make it a subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 19, 2024
1 parent 9509dc7 commit 9e912c8
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 45 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions crates/ruff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Arc;

use anyhow::{anyhow, bail};
use clap::builder::{TypedValueParser, ValueParserFactory};
use clap::{command, Parser};
use clap::{command, Parser, Subcommand};
use colored::Colorize;
use path_absolutize::path_dedot;
use regex::Regex;
Expand Down Expand Up @@ -132,17 +132,24 @@ pub enum Command {
Format(FormatCommand),
/// Run the language server.
Server(ServerCommand),
/// Generate a map of Python file dependencies.
ImportMap(ImportMapCommand),
/// Analyze the import graph of the given files or directories.
#[clap(subcommand)]
Graph(GraphCommand),
/// Display Ruff's version
Version {
#[arg(long, value_enum, default_value = "text")]
output_format: HelpFormat,
},
}

#[derive(Debug, Subcommand)]
pub enum GraphCommand {
/// Generate a map of Python file dependencies.
Build(GraphBuildCommand),
}

#[derive(Clone, Debug, clap::Parser)]
pub struct ImportMapCommand {
pub struct GraphBuildCommand {
/// List of files or directories to include.
#[clap(help = "List of files or directories to include [default: .]")]
pub files: Vec<PathBuf>,
Expand Down Expand Up @@ -758,14 +765,14 @@ impl FormatCommand {
}
}

impl ImportMapCommand {
impl GraphBuildCommand {
/// Partition the CLI into command-line arguments and configuration
/// overrides.
pub fn partition(
self,
global_options: GlobalConfigArgs,
) -> anyhow::Result<(ImportMapArgs, ConfigArguments)> {
let format_arguments = ImportMapArgs {
) -> anyhow::Result<(GraphArgs, ConfigArguments)> {
let format_arguments = GraphArgs {
files: self.files,
direction: self.direction,
};
Expand Down Expand Up @@ -1199,7 +1206,7 @@ impl LineColumnParseError {
}

/// CLI settings that are distinct from configuration (commands, lists of files, etc.).
pub struct ImportMapArgs {
pub struct GraphArgs {
pub files: Vec<PathBuf>,
pub direction: Direction,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::args::{ConfigArguments, ImportMapArgs};
use crate::args::{ConfigArguments, GraphArgs};
use crate::resolve::resolve;
use crate::{resolve_default_files, ExitStatus};
use anyhow::Result;
Expand All @@ -13,10 +13,7 @@ use std::path::Path;
use std::sync::Arc;

/// Generate an import map.
pub(crate) fn import_map(
args: ImportMapArgs,
config_arguments: &ConfigArguments,
) -> Result<ExitStatus> {
pub(crate) fn graph(args: GraphArgs, config_arguments: &ConfigArguments) -> Result<ExitStatus> {
// Construct the "default" settings. These are used when no `pyproject.toml`
// files are present, or files are injected from outside the hierarchy.
let pyproject_config = resolve(config_arguments, None)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) mod clean;
pub(crate) mod config;
pub(crate) mod format;
pub(crate) mod format_stdin;
pub(crate) mod import_map;
pub(crate) mod graph;
pub(crate) mod linter;
pub(crate) mod rule;
pub(crate) mod server;
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use ruff_linter::settings::types::OutputFormat;
use ruff_linter::{fs, warn_user, warn_user_once};
use ruff_workspace::Settings;

use crate::args::{Args, CheckCommand, Command, FormatCommand, ImportMapCommand};
use crate::args::{Args, CheckCommand, Command, FormatCommand, GraphBuildCommand, GraphCommand};
use crate::printer::{Flags as PrinterFlags, Printer};

pub mod args;
Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn run(
Command::Check(args) => check(args, global_options),
Command::Format(args) => format(args, global_options),
Command::Server(args) => server(args),
Command::ImportMap(args) => import_map(args, global_options),
Command::Graph(GraphCommand::Build(args)) => graph_build(args, global_options),
}
}

Expand All @@ -200,10 +200,10 @@ fn format(args: FormatCommand, global_options: GlobalConfigArgs) -> Result<ExitS
}
}

fn import_map(args: ImportMapCommand, global_options: GlobalConfigArgs) -> Result<ExitStatus> {
fn graph_build(args: GraphBuildCommand, global_options: GlobalConfigArgs) -> Result<ExitStatus> {
let (cli, config_arguments) = args.partition(global_options)?;

commands::import_map::import_map(cli, &config_arguments)
commands::graph::graph(cli, &config_arguments)
}

fn server(args: ServerCommand) -> Result<ExitStatus> {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_db/src/system/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ impl SystemPath {
/// [`self.file_name`]: SystemPath::file_name
///
/// The stem is:
/// * The entire file name if there is no embedded `.`;
///
/// * [`None`], if there is no file name;
/// * The entire file name if there is no embedded `.`;
/// * The entire file name if the file name begins with `.` and has no other `.`s within;
/// * Otherwise, the portion of the file name before the final `.`
///
Expand Down
1 change: 0 additions & 1 deletion crates/ruff_graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ ruff_python_stdlib = { workspace = true }

anyhow = { workspace = true }
clap = { workspace = true, optional = true }
itertools = { workspace = true }
salsa = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
Expand Down
23 changes: 9 additions & 14 deletions crates/ruff_graph/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,32 @@ pub struct ModuleDb {

impl Default for ModuleDb {
fn default() -> Self {
Self::new()
Self {
storage: salsa::Storage::default(),
system: OsSystem::default(),
vendored: VendoredFileSystem::default(),
files: Files::default(),
}
}
}

impl ModuleDb {
/// Initialize a [`ModuleDb`] from the given source root.
pub fn from_src_root(src_root: PathBuf) -> Result<Self> {
let db = Self::new();
let db = Self::default();
Program::from_settings(
&db,
&ProgramSettings {
target_version: PythonVersion::default(),
search_paths: SearchPathSettings::new(
SystemPathBuf::from_path_buf(src_root).map_err(|path| {
anyhow::anyhow!(format!("Invalid path: {}", path.display()))
})?,
SystemPathBuf::from_path_buf(src_root)
.map_err(|path| anyhow::anyhow!("Invalid path: {}", path.display()))?,
),
},
)?;
Ok(db)
}

pub fn new() -> Self {
Self {
storage: salsa::Storage::default(),
system: OsSystem::default(),
vendored: VendoredFileSystem::default(),
files: Files::default(),
}
}

#[must_use]
pub fn snapshot(&self) -> Self {
Self {
Expand Down
20 changes: 10 additions & 10 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,16 +515,16 @@ Ruff: An extremely fast Python linter and code formatter.
Usage: ruff [OPTIONS] <COMMAND>
Commands:
check Run Ruff on the given files or directories (default)
rule Explain a rule (or all rules)
config List or describe the available configuration options
linter List all supported upstream linters
clean Clear any caches in the current directory and any subdirectories
format Run the Ruff formatter on the given files or directories
server Run the language server
import-map Generate a map of Python file dependencies
version Display Ruff's version
help Print this message or the help of the given subcommand(s)
check Run Ruff on the given files or directories (default)
rule Explain a rule (or all rules)
config List or describe the available configuration options
linter List all supported upstream linters
clean Clear any caches in the current directory and any subdirectories
format Run the Ruff formatter on the given files or directories
server Run the language server
graph Analyze the import graph of the given files or directories
version Display Ruff's version
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
Expand Down

0 comments on commit 9e912c8

Please sign in to comment.