feat(task): support object notation, remove support for JSDocs (#26886)

This commit changes three aspects of `deno task`:
1. Tasks can now be written using object notation like so:
```jsonc
{
  "tasks": {
     "foo": "deno run foo.js",
     "bar": {
        "command": "deno run bar.js"
     }
}
```
2. Support for comments for tasks is now removed. Comments above tasks
will
no longer be printed when running `deno task`.
3. Tasks written using object notation can have "description" field that
replaces
support for comments above tasks:
```jsonc
{
  "tasks": {
     "bar": {
        "description": "This is a bar task"
        "command": "deno run bar.js"
     }
}
```
```
$ deno task
Available tasks:
- bar
    // This is a bar task
    deno run bar.js
```

Pulled most of the changes from
https://github.com/denoland/deno/pull/26467 to
support "dependencies" in tasks. Additionally some cleanup was performed
to make code easier to read.

---------

Co-authored-by: David Sherret <dsherret@gmail.com>
This commit is contained in:
Bartek Iwańczuk 2024-11-16 11:18:17 +00:00 committed by GitHub
parent 48b94c0995
commit 84e1238648
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 279 additions and 249 deletions

4
Cargo.lock generated
View File

@ -1421,9 +1421,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_config" name = "deno_config"
version = "0.38.2" version = "0.39.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "966825073480a6ac7e01977a3879d13edc8d6ea2d65ea164b37156a5fb206e9a" checksum = "0a91aa99751ebe305a7edad12a3ad751f3b3b9f5ecddbfe4a0459e3cdc8493b6"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"deno_package_json", "deno_package_json",

View File

@ -49,7 +49,7 @@ deno_ast = { version = "=0.43.3", features = ["transpiling"] }
deno_core = { version = "0.319.0" } deno_core = { version = "0.319.0" }
deno_bench_util = { version = "0.171.0", path = "./bench_util" } deno_bench_util = { version = "0.171.0", path = "./bench_util" }
deno_config = { version = "=0.38.2", features = ["workspace", "sync"] } deno_config = { version = "=0.39.1", features = ["workspace", "sync"] }
deno_lockfile = "=0.23.1" deno_lockfile = "=0.23.1"
deno_media_type = { version = "0.2.0", features = ["module_specifier"] } deno_media_type = { version = "0.2.0", features = ["module_specifier"] }
deno_npm = "=0.25.4" deno_npm = "=0.25.4"

View File

@ -868,12 +868,8 @@ impl CliOptions {
} else { } else {
&[] &[]
}; };
let config_parse_options = deno_config::deno_json::ConfigParseOptions { let config_parse_options =
include_task_comments: matches!( deno_config::deno_json::ConfigParseOptions::default();
flags.subcommand,
DenoSubcommand::Task(..)
),
};
let discover_pkg_json = flags.config_flag != ConfigFlag::Disabled let discover_pkg_json = flags.config_flag != ConfigFlag::Disabled
&& !flags.no_npm && !flags.no_npm
&& !has_flag_env_var("DENO_NO_PACKAGE_JSON"); && !has_flag_env_var("DENO_NO_PACKAGE_JSON");

View File

@ -3632,9 +3632,8 @@ impl Inner {
deno_json_cache: None, deno_json_cache: None,
pkg_json_cache: None, pkg_json_cache: None,
workspace_cache: None, workspace_cache: None,
config_parse_options: deno_config::deno_json::ConfigParseOptions { config_parse_options:
include_task_comments: false, deno_config::deno_json::ConfigParseOptions::default(),
},
additional_config_file_names: &[], additional_config_file_names: &[],
discover_pkg_json: !has_flag_env_var("DENO_NO_PACKAGE_JSON"), discover_pkg_json: !has_flag_env_var("DENO_NO_PACKAGE_JSON"),
maybe_vendor_override: if force_global_cache { maybe_vendor_override: if force_global_cache {

View File

@ -431,8 +431,27 @@
"type": "object", "type": "object",
"patternProperties": { "patternProperties": {
"^[A-Za-z][A-Za-z0-9_\\-:]*$": { "^[A-Za-z][A-Za-z0-9_\\-:]*$": {
"type": "string", "oneOf": [
"description": "Command to execute for this task name." {
"type": "string",
"description": "Command to execute for this task name."
},
{
"type": "object",
"description": "A definition of a task to execute",
"properties": {
"description": {
"type": "string",
"description": "Description of a task that will be shown when running `deno task` without a task name"
},
"command": {
"type": "string",
"required": true,
"description": "The task to execute"
}
}
}
]
} }
}, },
"additionalProperties": false "additionalProperties": false

View File

@ -1,6 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
use std::path::Path; use std::path::Path;
@ -8,7 +7,7 @@ use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use deno_config::deno_json::Task; use deno_config::workspace::TaskDefinition;
use deno_config::workspace::TaskOrScript; use deno_config::workspace::TaskOrScript;
use deno_config::workspace::WorkspaceDirectory; use deno_config::workspace::WorkspaceDirectory;
use deno_config::workspace::WorkspaceTasksConfig; use deno_config::workspace::WorkspaceTasksConfig;
@ -16,8 +15,11 @@ use deno_core::anyhow::anyhow;
use deno_core::anyhow::bail; use deno_core::anyhow::bail;
use deno_core::anyhow::Context; use deno_core::anyhow::Context;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::url::Url;
use deno_path_util::normalize_path; use deno_path_util::normalize_path;
use deno_runtime::deno_node::NodeResolver;
use deno_task_shell::ShellCommand; use deno_task_shell::ShellCommand;
use indexmap::IndexMap;
use crate::args::CliOptions; use crate::args::CliOptions;
use crate::args::Flags; use crate::args::Flags;
@ -48,155 +50,188 @@ pub async fn execute_script(
v == "1" v == "1"
}) })
.unwrap_or(false); .unwrap_or(false);
let tasks_config = start_dir.to_tasks_config()?; let mut tasks_config = start_dir.to_tasks_config()?;
let tasks_config = if force_use_pkg_json { if force_use_pkg_json {
tasks_config.with_only_pkg_json() tasks_config = tasks_config.with_only_pkg_json()
} else { }
tasks_config
};
let task_name = match &task_flags.task { let Some(task_name) = &task_flags.task else {
Some(task) => task, print_available_tasks(
None => { &mut std::io::stdout(),
print_available_tasks( &cli_options.start_dir,
&mut std::io::stdout(), &tasks_config,
&cli_options.start_dir, )?;
&tasks_config, return Ok(0);
)?;
return Ok(0);
}
}; };
let npm_resolver = factory.npm_resolver().await?; let npm_resolver = factory.npm_resolver().await?;
let node_resolver = factory.node_resolver().await?; let node_resolver = factory.node_resolver().await?;
let env_vars = task_runner::real_env_vars(); let env_vars = task_runner::real_env_vars();
match tasks_config.task(task_name) { let task_runner = TaskRunner {
Some((dir_url, task_or_script)) => match task_or_script { tasks_config,
TaskOrScript::Task(_tasks, script) => { task_flags: &task_flags,
let cwd = match task_flags.cwd { npm_resolver: npm_resolver.as_ref(),
Some(path) => canonicalize_path(&PathBuf::from(path)) node_resolver: node_resolver.as_ref(),
.context("failed canonicalizing --cwd")?, env_vars,
None => normalize_path(dir_url.to_file_path().unwrap()), cli_options,
}; };
task_runner.run_task(task_name).await
}
let custom_commands = task_runner::resolve_custom_commands( struct RunSingleOptions<'a> {
npm_resolver.as_ref(), task_name: &'a str,
node_resolver, script: &'a str,
)?; cwd: &'a Path,
run_task(RunTaskOptions { custom_commands: HashMap<String, Rc<dyn ShellCommand>>,
task_name, }
script,
cwd: &cwd,
env_vars,
custom_commands,
npm_resolver: npm_resolver.as_ref(),
cli_options,
})
.await
}
TaskOrScript::Script(scripts, _script) => {
// ensure the npm packages are installed if using a managed resolver
if let Some(npm_resolver) = npm_resolver.as_managed() {
npm_resolver.ensure_top_level_package_json_install().await?;
}
let cwd = match task_flags.cwd { struct TaskRunner<'a> {
Some(path) => canonicalize_path(&PathBuf::from(path))?, tasks_config: WorkspaceTasksConfig,
None => normalize_path(dir_url.to_file_path().unwrap()), task_flags: &'a TaskFlags,
}; npm_resolver: &'a dyn CliNpmResolver,
node_resolver: &'a NodeResolver,
env_vars: HashMap<String, String>,
cli_options: &'a CliOptions,
}
// At this point we already checked if the task name exists in package.json. impl<'a> TaskRunner<'a> {
// We can therefore check for "pre" and "post" scripts too, since we're only async fn run_task(
// dealing with package.json here and not deno.json &self,
let task_names = vec![ task_name: &String,
format!("pre{}", task_name), ) -> Result<i32, deno_core::anyhow::Error> {
task_name.clone(), let Some((dir_url, task_or_script)) = self.tasks_config.task(task_name)
format!("post{}", task_name), else {
]; if self.task_flags.is_run {
let custom_commands = task_runner::resolve_custom_commands(
npm_resolver.as_ref(),
node_resolver,
)?;
for task_name in &task_names {
if let Some(script) = scripts.get(task_name) {
let exit_code = run_task(RunTaskOptions {
task_name,
script,
cwd: &cwd,
env_vars: env_vars.clone(),
custom_commands: custom_commands.clone(),
npm_resolver: npm_resolver.as_ref(),
cli_options,
})
.await?;
if exit_code > 0 {
return Ok(exit_code);
}
}
}
Ok(0)
}
},
None => {
if task_flags.is_run {
return Err(anyhow!("Task not found: {}", task_name)); return Err(anyhow!("Task not found: {}", task_name));
} }
log::error!("Task not found: {}", task_name); log::error!("Task not found: {}", task_name);
if log::log_enabled!(log::Level::Error) { if log::log_enabled!(log::Level::Error) {
print_available_tasks( print_available_tasks(
&mut std::io::stderr(), &mut std::io::stderr(),
&cli_options.start_dir, &self.cli_options.start_dir,
&tasks_config, &self.tasks_config,
)?; )?;
} }
Ok(1) return Ok(1);
};
match task_or_script {
TaskOrScript::Task(_tasks, definition) => {
self.run_deno_task(dir_url, task_name, definition).await
}
TaskOrScript::Script(scripts, _script) => {
self.run_npm_script(dir_url, task_name, scripts).await
}
} }
} }
}
struct RunTaskOptions<'a> { async fn run_deno_task(
task_name: &'a str, &self,
script: &'a str, dir_url: &Url,
cwd: &'a Path, task_name: &String,
env_vars: HashMap<String, String>, definition: &TaskDefinition,
custom_commands: HashMap<String, Rc<dyn ShellCommand>>, ) -> Result<i32, deno_core::anyhow::Error> {
npm_resolver: &'a dyn CliNpmResolver, let cwd = match &self.task_flags.cwd {
cli_options: &'a CliOptions, Some(path) => canonicalize_path(&PathBuf::from(path))
} .context("failed canonicalizing --cwd")?,
None => normalize_path(dir_url.to_file_path().unwrap()),
};
async fn run_task(opts: RunTaskOptions<'_>) -> Result<i32, AnyError> { let custom_commands = task_runner::resolve_custom_commands(
let RunTaskOptions { self.npm_resolver,
task_name, self.node_resolver,
script, )?;
cwd, self
env_vars, .run_single(RunSingleOptions {
custom_commands, task_name,
npm_resolver, script: &definition.command,
cli_options, cwd: &cwd,
} = opts; custom_commands,
})
.await
}
output_task( async fn run_npm_script(
opts.task_name, &self,
&task_runner::get_script_with_args(script, cli_options.argv()), dir_url: &Url,
); task_name: &String,
scripts: &IndexMap<String, String>,
) -> Result<i32, deno_core::anyhow::Error> {
// ensure the npm packages are installed if using a managed resolver
if let Some(npm_resolver) = self.npm_resolver.as_managed() {
npm_resolver.ensure_top_level_package_json_install().await?;
}
Ok( let cwd = match &self.task_flags.cwd {
task_runner::run_task(task_runner::RunTaskOptions { Some(path) => canonicalize_path(&PathBuf::from(path))?,
None => normalize_path(dir_url.to_file_path().unwrap()),
};
// At this point we already checked if the task name exists in package.json.
// We can therefore check for "pre" and "post" scripts too, since we're only
// dealing with package.json here and not deno.json
let task_names = vec![
format!("pre{}", task_name),
task_name.clone(),
format!("post{}", task_name),
];
let custom_commands = task_runner::resolve_custom_commands(
self.npm_resolver,
self.node_resolver,
)?;
for task_name in &task_names {
if let Some(script) = scripts.get(task_name) {
let exit_code = self
.run_single(RunSingleOptions {
task_name,
script,
cwd: &cwd,
custom_commands: custom_commands.clone(),
})
.await?;
if exit_code > 0 {
return Ok(exit_code);
}
}
}
Ok(0)
}
async fn run_single(
&self,
opts: RunSingleOptions<'_>,
) -> Result<i32, AnyError> {
let RunSingleOptions {
task_name, task_name,
script, script,
cwd, cwd,
env_vars,
custom_commands, custom_commands,
init_cwd: opts.cli_options.initial_cwd(), } = opts;
argv: cli_options.argv(),
root_node_modules_dir: npm_resolver.root_node_modules_path(), output_task(
stdio: None, opts.task_name,
}) &task_runner::get_script_with_args(script, self.cli_options.argv()),
.await? );
.exit_code,
) Ok(
task_runner::run_task(task_runner::RunTaskOptions {
task_name,
script,
cwd,
env_vars: self.env_vars.clone(),
custom_commands,
init_cwd: self.cli_options.initial_cwd(),
argv: self.cli_options.argv(),
root_node_modules_dir: self.npm_resolver.root_node_modules_path(),
stdio: None,
})
.await?
.exit_code,
)
}
} }
fn output_task(task_name: &str, script: &str) { fn output_task(task_name: &str, script: &str) {
@ -222,80 +257,89 @@ fn print_available_tasks(
" {}", " {}",
colors::red("No tasks found in configuration file") colors::red("No tasks found in configuration file")
)?; )?;
} else { return Ok(());
let mut seen_task_names = }
HashSet::with_capacity(tasks_config.tasks_count());
for maybe_config in [&tasks_config.member, &tasks_config.root] { struct AvailableTaskDescription {
let Some(config) = maybe_config else { is_root: bool,
continue; is_deno: bool,
}; name: String,
for (is_root, is_deno, (key, task)) in config task: TaskDefinition,
.deno_json }
.as_ref() let mut seen_task_names = HashSet::with_capacity(tasks_config.tasks_count());
.map(|config| { let mut task_descriptions = Vec::with_capacity(tasks_config.tasks_count());
let is_root = !is_cwd_root_dir
&& config.folder_url for maybe_config in [&tasks_config.member, &tasks_config.root] {
== *workspace_dir.workspace.root_dir().as_ref(); let Some(config) = maybe_config else {
config continue;
.tasks };
.iter()
.map(move |(k, t)| (is_root, true, (k, Cow::Borrowed(t)))) if let Some(config) = config.deno_json.as_ref() {
}) let is_root = !is_cwd_root_dir
.into_iter() && config.folder_url == *workspace_dir.workspace.root_dir().as_ref();
.flatten()
.chain( for (name, definition) in &config.tasks {
config if !seen_task_names.insert(name) {
.package_json
.as_ref()
.map(|config| {
let is_root = !is_cwd_root_dir
&& config.folder_url
== *workspace_dir.workspace.root_dir().as_ref();
config.tasks.iter().map(move |(k, v)| {
(is_root, false, (k, Cow::Owned(Task::Definition(v.clone()))))
})
})
.into_iter()
.flatten(),
)
{
if !seen_task_names.insert(key) {
continue; // already seen continue; // already seen
} }
writeln!( task_descriptions.push(AvailableTaskDescription {
writer, is_root,
"- {}{}", is_deno: true,
colors::cyan(key), name: name.to_string(),
if is_root { task: definition.clone(),
if is_deno { });
format!(" {}", colors::italic_gray("(workspace)")) }
} else { }
format!(" {}", colors::italic_gray("(workspace package.json)"))
} if let Some(config) = config.package_json.as_ref() {
} else if is_deno { let is_root = !is_cwd_root_dir
"".to_string() && config.folder_url == *workspace_dir.workspace.root_dir().as_ref();
} else { for (name, script) in &config.tasks {
format!(" {}", colors::italic_gray("(package.json)")) if !seen_task_names.insert(name) {
} continue; // already seen
)?;
let definition = match task.as_ref() {
Task::Definition(definition) => definition,
Task::Commented { definition, .. } => definition,
};
if let Task::Commented { comments, .. } = task.as_ref() {
let slash_slash = colors::italic_gray("//");
for comment in comments {
writeln!(
writer,
" {slash_slash} {}",
colors::italic_gray(comment)
)?;
}
} }
writeln!(writer, " {definition}")?;
task_descriptions.push(AvailableTaskDescription {
is_root,
is_deno: false,
name: name.to_string(),
task: deno_config::deno_json::TaskDefinition {
command: script.to_string(),
dependencies: vec![],
description: None,
},
});
} }
} }
} }
for desc in task_descriptions {
writeln!(
writer,
"- {}{}",
colors::cyan(desc.name),
if desc.is_root {
if desc.is_deno {
format!(" {}", colors::italic_gray("(workspace)"))
} else {
format!(" {}", colors::italic_gray("(workspace package.json)"))
}
} else if desc.is_deno {
"".to_string()
} else {
format!(" {}", colors::italic_gray("(package.json)"))
}
)?;
if let Some(description) = &desc.task.description {
let slash_slash = colors::italic_gray("//");
writeln!(
writer,
" {slash_slash} {}",
colors::italic_gray(description)
)?;
}
writeln!(writer, " {}", desc.task.command)?;
}
Ok(()) Ok(())
} }

View File

@ -0,0 +1,4 @@
{
"args": "task",
"output": "main.out"
}

View File

@ -0,0 +1,8 @@
{
"tasks": {
"echo_emoji": {
"description": "This is some task",
"command": "echo 1"
}
}
}

View File

@ -0,0 +1,4 @@
Available tasks:
- echo_emoji
// This is some task
echo 1

View File

@ -1,5 +0,0 @@
{
"args": "task doesntexist",
"output": "task.out",
"exitCode": 1
}

View File

@ -1,7 +0,0 @@
{
"tasks": {
// some docs
// on what this does
"lint": "deno lint"
}
}

View File

@ -1,6 +0,0 @@
Task not found: doesntexist
Available tasks:
- lint
// some docs
// on what this does
deno lint

View File

@ -1,5 +0,0 @@
{
"args": "task",
"output": "task.out",
"exitCode": 0
}

View File

@ -1,12 +0,0 @@
{
"tasks": {
// this task has documentation
//
// in the form of comments
"lint": "deno lint",
/*
* block comments are fine too
*/
"fmt": "deno fmt"
}
}

View File

@ -1,9 +0,0 @@
Available tasks:
- lint
// this task has documentation
//
// in the form of comments
deno lint
- fmt
// block comments are fine too
deno fmt