Skip to content

Commit

Permalink
fix: handle single array item in command.associations (multiple not y…
Browse files Browse the repository at this point in the history
…et implemented) (#17)
  • Loading branch information
dsherret authored Nov 20, 2022
1 parent 0626ba8 commit a6c71de
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 122 deletions.
216 changes: 134 additions & 82 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ overflow-checks = false
panic = "abort"

[dependencies]
anyhow = "1.0.51"
dprint-core = { version = "0.56.0", features = ["process"] }
globset = "0.4.8"
handlebars = "4.1.6"
serde = { version = "1.0.118", features = ["derive"] }
splitty = "0.1.0"
tokio = { version = "1.15.0", features = ["full", "time"] }
anyhow = "1.0.66"
dprint-core = { version = "0.59.0", features = ["process"] }
globset = "0.4.9"
handlebars = "4.3.5"
serde = { version = "1.0.147", features = ["derive"] }
splitty = "1.0.1"
tokio = { version = "1.22.0", features = ["full", "time"] }

[dev-dependencies]
dprint-development = "0.8.0"
dprint-development = "0.9.2"
pretty_assertions = "1.1.0"
27 changes: 25 additions & 2 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://plugins.dprint.dev/dprint/dprint-plugin-exec/0.0.0/schema.json",
"type": "object",
"required": ["associations"],
"properties": {
"locked": {
"description": "Whether the configuration is not allowed to be overriden or extended.",
Expand Down Expand Up @@ -47,6 +48,19 @@
"description": "Uses the system standard (ex. crlf on Windows)."
}]
},
"associations": {
"description": "Glob pattern that associates this plugin with certain file paths (ex. \"**/*.{rs,java,py}\").",
"anyOf": [{
"description": "Glob pattern that associates this plugin with certain file paths (ex. \"**/*.{rs,java,py}\").",
"type": "string"
}, {
"description": "Glob patterns that associates this plugin with certain file paths.",
"type": "array",
"items": {
"type": "string"
}
}]
},
"cacheKey": {
"description": "Optional value used to bust dprint's incremental cache (ex. provide \"1\"). This is useful if you want to force formatting to occur because the underlying command has changed.",
"type": "string"
Expand All @@ -59,8 +73,17 @@
},
"patternProperties": {
"^.*\\.associations$": {
"type": "string",
"description": "The pattern to use to match a file path. Ex. \"rustfmt.associations\": \"**/*.rs\""
"description": "Glob pattern that associates certain file paths with this command (ex. \"rustfmt.associations\": \"**/*.rs\").",
"anyOf": [{
"description": "Glob pattern that associates certain file paths with this command (ex. \"rustfmt.associations\": \"**/*.rs\").",
"type": "string"
}, {
"description": "Glob patterns that associates certain file paths with this command.",
"type": "array",
"items": {
"type": "string"
}
}]
},
"^.*\\.cwd$": {
"type": "string",
Expand Down
7 changes: 4 additions & 3 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"incremental": true,
"indentWidth": 2,
"lineWidth": 100,
"rustfmt": {
"imports_granularity": "Item"
"exec": {
"associations": "**/*.rs",
"rustfmt": "rustfmt --edition 2021"
},
"includes": [
"**/*.{json,md,html,scss,css,toml,rs,xml}"
Expand All @@ -17,6 +18,6 @@
"https://plugins.dprint.dev/json-0.14.1.wasm",
"https://plugins.dprint.dev/markdown-0.12.2.wasm",
"https://plugins.dprint.dev/toml-0.5.4.wasm",
"https://plugins.dprint.dev/rustfmt-0.6.0.exe-plugin@8b65ed724170bd227e92a2f01d867e452ef7f26e78dc691999ffa37a276df27c"
"https://plugins.dprint.dev/exec-0.3.1.json@9351b67ec7a6b58a69201c2834cba38cb3d191080aefc6422fb1320f03c8fc4d"
]
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.58.1"
channel = "1.65.0"
components = ["clippy", "rustfmt"]
6 changes: 3 additions & 3 deletions scripts/create_plugin_file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as path from "https://deno.land/std@0.146.0/path/mod.ts";
import { extractCargoVersion, processPlugin } from "https://raw.githubusercontent.com/dprint/automation/0.4.0/mod.ts";
import * as path from "https://deno.land/std@0.153.0/path/mod.ts";
import { extractCargoVersionOrThrow, processPlugin } from "https://raw.githubusercontent.com/dprint/automation/0.5.3/mod.ts";

const currentDirPath = path.dirname(path.fromFileUrl(import.meta.url));
const cargoFilePath = path.join(currentDirPath, "../", "Cargo.toml");

await processPlugin.createDprintOrgProcessPlugin({
pluginName: "dprint-plugin-exec",
version: await extractCargoVersion(cargoFilePath),
version: await extractCargoVersionOrThrow(cargoFilePath),
platforms: [
"darwin-aarch64",
"darwin-x86_64",
Expand Down
133 changes: 112 additions & 21 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use dprint_core::configuration::get_nullable_value;
use dprint_core::configuration::get_unknown_property_diagnostics;
use dprint_core::configuration::get_value;
use dprint_core::configuration::ConfigKeyMap;
use dprint_core::configuration::ConfigKeyValue;
use dprint_core::configuration::ConfigurationDiagnostic;
use dprint_core::configuration::GlobalConfiguration;
use dprint_core::configuration::NewLineKind;
Expand Down Expand Up @@ -145,9 +146,39 @@ impl Configuration {
args: command,
associations: {
let associations_key = format!("{}.associations", command_key);
let value: Option<String> =
get_nullable_value(&mut config, &associations_key, &mut diagnostics);
match value {
let maybe_value = config.remove(&associations_key).and_then(|value| match value {
ConfigKeyValue::String(value) => Some(value),
ConfigKeyValue::Array(mut value) => match value.len() {
0 => None,
1 => match value.remove(0) {
ConfigKeyValue::String(value) => Some(value),
_ => {
diagnostics.push(ConfigurationDiagnostic {
property_name: associations_key.clone(),
message: "Expected string value in array.".to_string(),
});
None
}
},
_ => {
diagnostics.push(ConfigurationDiagnostic {
property_name: associations_key.clone(),
message: "Unfortunately multiple globs haven't been implemented yet. Please provide a single glob or consider contributing this feature."
.to_string(),
});
None
}
},
_ => {
diagnostics.push(ConfigurationDiagnostic {
property_name: associations_key.clone(),
message: "Expected string or array value.".to_string(),
});
None
}
});

match maybe_value {
Some(value) => {
let mut builder = globset::GlobBuilder::new(&value);
builder.case_insensitive(cfg!(windows));
Expand All @@ -163,11 +194,7 @@ impl Configuration {
}
}
None => {
if resolved_config
.commands
.iter()
.any(|b| b.associations.is_none())
{
if resolved_config.commands.iter().any(|b| b.associations.is_none()) {
diagnostics.push(ConfigurationDiagnostic {
property_name: associations_key.to_string(),
message: format!(
Expand All @@ -184,17 +211,8 @@ impl Configuration {
}
}
},
cwd: get_cwd(get_nullable_value(
&mut config,
&format!("{}.cwd", command_key),
&mut diagnostics,
)),
stdin: get_value(
&mut config,
&format!("{}.stdin", command_key),
true,
&mut diagnostics,
),
cwd: get_cwd(get_nullable_value(&mut config, &format!("{}.cwd", command_key), &mut diagnostics)),
stdin: get_value(&mut config, &format!("{}.stdin", command_key), true, &mut diagnostics),
});
}

Expand Down Expand Up @@ -298,20 +316,93 @@ mod tests {
"A \"command2.associations\" configuration key must be provided because another formatting ",
"command is specified without an associations key. (Example: `\"command2.associations\": \"**/*.rs\"` ",
"would format .rs files with this command)"
).to_string(),
)
.to_string(),
},
ConfigurationDiagnostic {
property_name: "command3.associations".to_string(),
message: concat!(
"A \"command3.associations\" configuration key must be provided because another formatting ",
"command is specified without an associations key. (Example: `\"command3.associations\": \"**/*.rs\"` ",
"would format .rs files with this command)"
).to_string(),
)
.to_string(),
},
],
)
}

#[test]
fn handle_associations_value() {
let unresolved_config = ConfigKeyMap::from([
(
"command.associations".to_string(),
ConfigKeyValue::Array(vec![ConfigKeyValue::from_str("**/*.rs")]),
),
("command".to_string(), ConfigKeyValue::from_str("command")),
]);
let mut config = Configuration::resolve(unresolved_config, &Default::default()).config;
assert!(config.commands.remove(0).associations.is_some());

let unresolved_config = ConfigKeyMap::from([
(
"command.associations".to_string(),
ConfigKeyValue::Array(vec![]),
),
("command".to_string(), ConfigKeyValue::from_str("command")),
]);
let mut config = Configuration::resolve(unresolved_config, &Default::default()).config;
assert!(config.commands.remove(0).associations.is_none());

let unresolved_config = ConfigKeyMap::from([
(
"command.associations".to_string(),
ConfigKeyValue::Array(vec![
ConfigKeyValue::from_str("**/*.rs"),
ConfigKeyValue::from_str("**/*.json"),
]),
),
("command".to_string(), ConfigKeyValue::from_str("command")),
]);
run_diagnostics_test(
unresolved_config,
vec![ConfigurationDiagnostic {
property_name: "command.associations".to_string(),
message: "Unfortunately multiple globs haven't been implemented yet. Please provide a single glob or consider contributing this feature.".to_string(),
}],
);

let unresolved_config = ConfigKeyMap::from([
(
"command.associations".to_string(),
ConfigKeyValue::Array(vec![ConfigKeyValue::from_bool(true)]),
),
("command".to_string(), ConfigKeyValue::from_str("command")),
]);
run_diagnostics_test(
unresolved_config,
vec![ConfigurationDiagnostic {
property_name: "command.associations".to_string(),
message: "Expected string value in array.".to_string(),
}],
);

let unresolved_config = ConfigKeyMap::from([
(
"command.associations".to_string(),
ConfigKeyValue::from_bool(true),
),
("command".to_string(), ConfigKeyValue::from_str("command")),
]);
run_diagnostics_test(
unresolved_config,
vec![ConfigurationDiagnostic {
property_name: "command.associations".to_string(),
message: "Expected string or array value.".to_string(),
}],
);
}

fn run_diagnostics_test(
config: ConfigKeyMap,
expected_diagnostics: Vec<ConfigurationDiagnostic>,
Expand Down
4 changes: 2 additions & 2 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ async fn handle_child_exit_status(
if exit_status.success() {
return Ok(ok_text);
}
return Err(anyhow!(
Err(anyhow!(
"Child process exited with code {}: {}",
exit_status.code().unwrap(),
err_rx
.await
.expect("Could not propagate error message from child process")
));
))
}

fn timeout_err(config: &Configuration) -> Error {
Expand Down

0 comments on commit a6c71de

Please sign in to comment.