feat(dotnet): Configure when module is shown (#2327)

This makes it possible to configure when the dotnet module is shown
based on the contents of a directory. This should make it possible to be
a lot more granular when configuring the module.
This commit is contained in:
Thomas O'Donnell 2021-02-20 15:38:03 +01:00 committed by GitHub
parent 5e93160456
commit d52edaa661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 54 deletions

View File

@ -762,8 +762,8 @@ The `dotnet` module shows the relevant version of the .NET Core SDK for the curr
the SDK has been pinned in the current directory, the pinned version is shown. Otherwise the module
shows the latest installed version of the SDK.
This module will only be shown in your prompt when one or more of the following files are present in the
current directory:
By default this module will only be shown in your prompt when one or more of
the following files are present in the current directory:
- `global.json`
- `project.json`
@ -788,13 +788,16 @@ when there is a csproj file in the current directory.
### Options
| Option | Default | Description |
| ----------- | ----------------------------------------- | -------------------------------------------------------- |
| `format` | `"[$symbol($version )(🎯 $tfm )]($style)"` | The format for the module. |
| `symbol` | `"•NET "` | The symbol used before displaying the version of dotnet. |
| `heuristic` | `true` | Use faster version detection to keep starship snappy. |
| `style` | `"bold blue"` | The style for the module. |
| `disabled` | `false` | Disables the `dotnet` module. |
| Option | Default | Description |
| ------------------- | -------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
| `format` | `"[$symbol($version )(🎯 $tfm )]($style)"` | The format for the module. |
| `symbol` | `"•NET "` | The symbol used before displaying the version of dotnet. |
| `heuristic` | `true` | Use faster version detection to keep starship snappy. |
| `detect_extensions` | `["sln", "csproj", "fsproj", "xproj"]` | Which extensions should trigger this module. |
| `detect_files` | `[ "global.json", "project.json", "Directory.Build.props", "Directory.Build.targets", "Packages.props"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this modules. |
| `style` | `"bold blue"` | The style for the module. |
| `disabled` | `false` | Disables the `dotnet` module. |
### Variables

View File

@ -1,24 +1,36 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct DotnetConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub heuristic: bool,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for DotnetConfig<'a> {
fn new() -> Self {
DotnetConfig {
format: "[$symbol($version )(🎯 $tfm )]($style)",
symbol: "•NET ",
style: "blue bold",
heuristic: true,
disabled: false,
}
}
}
use crate::config::{ModuleConfig, RootModuleConfig};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct DotnetConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub heuristic: bool,
pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}
impl<'a> RootModuleConfig<'a> for DotnetConfig<'a> {
fn new() -> Self {
DotnetConfig {
format: "[$symbol($version )(🎯 $tfm )]($style)",
symbol: "•NET ",
style: "blue bold",
heuristic: true,
disabled: false,
detect_extensions: vec!["sln", "csproj", "fsproj", "xproj"],
detect_files: vec![
"global.json",
"project.json",
"Directory.Build.props",
"Directory.Build.targets",
"Packages.props",
],
detect_folders: vec![],
}
}
}

View File

@ -15,28 +15,20 @@ type JValue = serde_json::Value;
const GLOBAL_JSON_FILE: &str = "global.json";
const PROJECT_JSON_FILE: &str = "project.json";
const DIRECTORY_BUILD_PROPS_FILE: &str = "Directory.Build.props";
const DIRECTORY_BUILD_TARGETS_FILE: &str = "Directory.Build.targets";
const PACKAGES_PROPS_FILE: &str = "Packages.props";
/// A module which shows the latest (or pinned) version of the dotnet SDK
///
/// Will display if any of the following files are present in
/// the current directory:
/// global.json, project.json, *.sln, *.csproj, *.fsproj, *.xproj
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("dotnet");
let config = DotnetConfig::try_load(module.config);
// First check if this is a DotNet Project before doing the O(n)
// check for the version using the JSON files
let is_dotnet_project = context
.try_begin_scan()?
.set_files(&[
GLOBAL_JSON_FILE,
PROJECT_JSON_FILE,
DIRECTORY_BUILD_PROPS_FILE,
DIRECTORY_BUILD_TARGETS_FILE,
PACKAGES_PROPS_FILE,
])
.set_extensions(&["sln", "csproj", "fsproj", "xproj"])
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();
if !is_dotnet_project {
@ -45,9 +37,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let dotnet_files = get_local_dotnet_files(context).ok()?;
let mut module = context.new_module("dotnet");
let config = DotnetConfig::try_load(module.config);
// Internally, this module uses its own mechanism for version detection.
// Typically it is twice as fast as running `dotnet --version`.
let enable_heuristic = config.heuristic;
@ -204,9 +193,7 @@ fn try_find_nearby_global_json(current_dir: &Path, repo_root: Option<&Path>) ->
.iter()
// repo_root may be the same as the current directory. We don't need to scan it again.
.filter(|&&d| d != current_dir)
.filter_map(|d| check_directory_for_global_json(d))
// This will lazily evaluate the first directory with a global.json
.next()
.find_map(|d| check_directory_for_global_json(d))
}
fn check_directory_for_global_json(path: &Path) -> Option<Version> {