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:
parent
5e93160456
commit
d52edaa661
|
@ -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
|
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.
|
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
|
By default this module will only be shown in your prompt when one or more of
|
||||||
current directory:
|
the following files are present in the current directory:
|
||||||
|
|
||||||
- `global.json`
|
- `global.json`
|
||||||
- `project.json`
|
- `project.json`
|
||||||
|
@ -788,13 +788,16 @@ when there is a csproj file in the current directory.
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
| Option | Default | Description |
|
| Option | Default | Description |
|
||||||
| ----------- | ----------------------------------------- | -------------------------------------------------------- |
|
| ------------------- | -------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
|
||||||
| `format` | `"[$symbol($version )(🎯 $tfm )]($style)"` | The format for the module. |
|
| `format` | `"[$symbol($version )(🎯 $tfm )]($style)"` | The format for the module. |
|
||||||
| `symbol` | `"•NET "` | The symbol used before displaying the version of dotnet. |
|
| `symbol` | `"•NET "` | The symbol used before displaying the version of dotnet. |
|
||||||
| `heuristic` | `true` | Use faster version detection to keep starship snappy. |
|
| `heuristic` | `true` | Use faster version detection to keep starship snappy. |
|
||||||
| `style` | `"bold blue"` | The style for the module. |
|
| `detect_extensions` | `["sln", "csproj", "fsproj", "xproj"]` | Which extensions should trigger this module. |
|
||||||
| `disabled` | `false` | Disables the `dotnet` 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
|
### Variables
|
||||||
|
|
||||||
|
|
|
@ -1,24 +1,36 @@
|
||||||
use crate::config::{ModuleConfig, RootModuleConfig};
|
use crate::config::{ModuleConfig, RootModuleConfig};
|
||||||
|
|
||||||
use starship_module_config_derive::ModuleConfig;
|
use starship_module_config_derive::ModuleConfig;
|
||||||
|
|
||||||
#[derive(Clone, ModuleConfig)]
|
#[derive(Clone, ModuleConfig)]
|
||||||
pub struct DotnetConfig<'a> {
|
pub struct DotnetConfig<'a> {
|
||||||
pub format: &'a str,
|
pub format: &'a str,
|
||||||
pub symbol: &'a str,
|
pub symbol: &'a str,
|
||||||
pub style: &'a str,
|
pub style: &'a str,
|
||||||
pub heuristic: bool,
|
pub heuristic: bool,
|
||||||
pub disabled: bool,
|
pub disabled: bool,
|
||||||
}
|
pub detect_extensions: Vec<&'a str>,
|
||||||
|
pub detect_files: Vec<&'a str>,
|
||||||
impl<'a> RootModuleConfig<'a> for DotnetConfig<'a> {
|
pub detect_folders: Vec<&'a str>,
|
||||||
fn new() -> Self {
|
}
|
||||||
DotnetConfig {
|
|
||||||
format: "[$symbol($version )(🎯 $tfm )]($style)",
|
impl<'a> RootModuleConfig<'a> for DotnetConfig<'a> {
|
||||||
symbol: "•NET ",
|
fn new() -> Self {
|
||||||
style: "blue bold",
|
DotnetConfig {
|
||||||
heuristic: true,
|
format: "[$symbol($version )(🎯 $tfm )]($style)",
|
||||||
disabled: false,
|
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![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -15,28 +15,20 @@ type JValue = serde_json::Value;
|
||||||
|
|
||||||
const GLOBAL_JSON_FILE: &str = "global.json";
|
const GLOBAL_JSON_FILE: &str = "global.json";
|
||||||
const PROJECT_JSON_FILE: &str = "project.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
|
/// 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>> {
|
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)
|
// First check if this is a DotNet Project before doing the O(n)
|
||||||
// check for the version using the JSON files
|
// check for the version using the JSON files
|
||||||
let is_dotnet_project = context
|
let is_dotnet_project = context
|
||||||
.try_begin_scan()?
|
.try_begin_scan()?
|
||||||
.set_files(&[
|
.set_files(&config.detect_files)
|
||||||
GLOBAL_JSON_FILE,
|
.set_extensions(&config.detect_extensions)
|
||||||
PROJECT_JSON_FILE,
|
.set_folders(&config.detect_folders)
|
||||||
DIRECTORY_BUILD_PROPS_FILE,
|
|
||||||
DIRECTORY_BUILD_TARGETS_FILE,
|
|
||||||
PACKAGES_PROPS_FILE,
|
|
||||||
])
|
|
||||||
.set_extensions(&["sln", "csproj", "fsproj", "xproj"])
|
|
||||||
.is_match();
|
.is_match();
|
||||||
|
|
||||||
if !is_dotnet_project {
|
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 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.
|
// Internally, this module uses its own mechanism for version detection.
|
||||||
// Typically it is twice as fast as running `dotnet --version`.
|
// Typically it is twice as fast as running `dotnet --version`.
|
||||||
let enable_heuristic = config.heuristic;
|
let enable_heuristic = config.heuristic;
|
||||||
|
@ -204,9 +193,7 @@ fn try_find_nearby_global_json(current_dir: &Path, repo_root: Option<&Path>) ->
|
||||||
.iter()
|
.iter()
|
||||||
// repo_root may be the same as the current directory. We don't need to scan it again.
|
// repo_root may be the same as the current directory. We don't need to scan it again.
|
||||||
.filter(|&&d| d != current_dir)
|
.filter(|&&d| d != current_dir)
|
||||||
.filter_map(|d| check_directory_for_global_json(d))
|
.find_map(|d| check_directory_for_global_json(d))
|
||||||
// This will lazily evaluate the first directory with a global.json
|
|
||||||
.next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_directory_for_global_json(path: &Path) -> Option<Version> {
|
fn check_directory_for_global_json(path: &Path) -> Option<Version> {
|
||||||
|
|
Loading…
Reference in New Issue