From e68c682a6a839d4dda0bca72f7b0afcf602dd3f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Mar 2018 09:38:59 +0300 Subject: [PATCH] Cleanups --- src/bin/cargo.rs | 42 ++++++++++++++++++++++++++++++++++-- src/bin/cli.rs | 56 ++++++------------------------------------------ 2 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 83c0f78cf..5765125a5 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -13,6 +13,7 @@ extern crate clap; use std::env; use std::fs; use std::path::{Path, PathBuf}; +use std::collections::BTreeSet; use cargo::core::shell::Shell; use cargo::util::{self, CliResult, lev_distance, Config, CargoResult}; @@ -37,7 +38,7 @@ fn main() { let result = { init_git_transports(&mut config); let _token = cargo::util::job::setup(); - cli::do_main(&mut config) + cli::main(&mut config) }; match result { @@ -73,8 +74,45 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult BTreeSet<(String, Option)> { + let prefix = "cargo-"; + let suffix = env::consts::EXE_SUFFIX; + let mut commands = BTreeSet::new(); + for dir in search_directories(config) { + let entries = match fs::read_dir(dir) { + Ok(entries) => entries, + _ => continue, + }; + for entry in entries.filter_map(|e| e.ok()) { + let path = entry.path(); + let filename = match path.file_name().and_then(|s| s.to_str()) { + Some(filename) => filename, + _ => continue, + }; + if !filename.starts_with(prefix) || !filename.ends_with(suffix) { + continue; + } + if is_executable(entry.path()) { + let end = filename.len() - suffix.len(); + commands.insert( + (filename[prefix.len()..end].to_string(), + Some(path.display().to_string())) + ); + } + } + } + + for cmd in commands::builtin() { + commands.insert((cmd.get_name().to_string(), None)); + } + + commands +} + + fn find_closest(config: &Config, cmd: &str) -> Option { - let cmds = cli::list_commands(config); + let cmds = list_commands(config); // Only consider candidates with a lev_distance of 3 or less so we don't // suggest out-of-the-blue options. let mut filtered = cmds.iter() diff --git a/src/bin/cli.rs b/src/bin/cli.rs index f0125b0f8..659790f1c 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -1,11 +1,9 @@ extern crate clap; -#[cfg(never)] -extern crate cargo; -use std::io::{self, Read, BufRead}; use std::cmp::min; -use std::fs::File; use std::collections::HashMap; +use std::fs::File; +use std::io::{self, Read, BufRead}; use std::process; use clap::{AppSettings, Arg, ArgMatches}; @@ -13,20 +11,15 @@ use toml; use cargo::{self, Config, CargoError, CliResult, CliError}; use cargo::core::{Source, SourceId, GitReference, Package}; -use cargo::util::{ToUrl, CargoResultExt}; use cargo::ops::{self, CompileMode, OutputMetadataOptions}; use cargo::sources::{GitSource, RegistrySource}; +use cargo::util::{ToUrl, CargoResultExt}; -use std::collections::BTreeSet; -use std::env; -use std::fs; - -use search_directories; -use is_executable; +use super::list_commands; +use super::commands; use command_prelude::*; -use commands; -pub fn do_main(config: &mut Config) -> CliResult { +pub fn main(config: &mut Config) -> CliResult { let args = cli().get_matches_safe()?; let is_verbose = args.occurrences_of("verbose") > 0; if args.is_present("version") { @@ -642,40 +635,3 @@ See 'cargo help ' for more information on a specific command." ; app } - - -/// List all runnable commands -pub fn list_commands(config: &Config) -> BTreeSet<(String, Option)> { - let prefix = "cargo-"; - let suffix = env::consts::EXE_SUFFIX; - let mut commands = BTreeSet::new(); - for dir in search_directories(config) { - let entries = match fs::read_dir(dir) { - Ok(entries) => entries, - _ => continue, - }; - for entry in entries.filter_map(|e| e.ok()) { - let path = entry.path(); - let filename = match path.file_name().and_then(|s| s.to_str()) { - Some(filename) => filename, - _ => continue, - }; - if !filename.starts_with(prefix) || !filename.ends_with(suffix) { - continue; - } - if is_executable(entry.path()) { - let end = filename.len() - suffix.len(); - commands.insert( - (filename[prefix.len()..end].to_string(), - Some(path.display().to_string())) - ); - } - } - } - - for cmd in commands::builtin() { - commands.insert((cmd.get_name().to_string(), None)); - } - - commands -} -- 2.30.2