1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat: download progress bar (#15814)

This commit is contained in:
Bartek Iwańczuk 2022-09-09 21:57:39 +02:00 committed by GitHub
parent 3bce2af0eb
commit f92bd986de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 244 additions and 25 deletions

48
Cargo.lock generated
View file

@ -526,6 +526,20 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "console"
version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89eab4d20ce20cea182308bca13088fecea9c05f6776cf287205d41a0ed3c847"
dependencies = [
"encode_unicode",
"libc",
"once_cell",
"terminal_size",
"unicode-width",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "const-oid" name = "const-oid"
version = "0.9.0" version = "0.9.0"
@ -825,6 +839,7 @@ dependencies = [
"http", "http",
"import_map", "import_map",
"indexmap", "indexmap",
"indicatif",
"jsonc-parser", "jsonc-parser",
"libc", "libc",
"log 0.4.17", "log 0.4.17",
@ -1535,6 +1550,12 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.31" version = "0.8.31"
@ -2289,6 +2310,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "indicatif"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcc42b206e70d86ec03285b123e65a5458c92027d1fb2ae3555878b8113b3ddf"
dependencies = [
"console",
"number_prefix",
"unicode-width",
]
[[package]] [[package]]
name = "inotify" name = "inotify"
version = "0.9.6" version = "0.9.6"
@ -2932,6 +2964,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "number_prefix"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]] [[package]]
name = "objc" name = "objc"
version = "0.2.7" version = "0.2.7"
@ -4635,6 +4673,16 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "terminal_size"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "test_ffi" name = "test_ffi"
version = "0.1.0" version = "0.1.0"

View file

@ -75,6 +75,7 @@ flate2 = "=1.0.24"
http = "=0.2.6" http = "=0.2.6"
import_map = "=0.12.1" import_map = "=0.12.1"
indexmap = "1.8.1" indexmap = "1.8.1"
indicatif = "=0.17.0"
jsonc-parser = { version = "=0.21.0", features = ["serde"] } jsonc-parser = { version = "=0.21.0", features = ["serde"] }
libc = "=0.2.126" libc = "=0.2.126"
log = { version = "=0.4.17", features = ["serde"] } log = { version = "=0.4.17", features = ["serde"] }

View file

@ -7,6 +7,7 @@ use crate::http_util::fetch_once;
use crate::http_util::CacheSemantics; use crate::http_util::CacheSemantics;
use crate::http_util::FetchOnceArgs; use crate::http_util::FetchOnceArgs;
use crate::http_util::FetchOnceResult; use crate::http_util::FetchOnceResult;
use crate::progress_bar::ProgressBar;
use crate::text_encoding; use crate::text_encoding;
use crate::version::get_user_agent; use crate::version::get_user_agent;
@ -318,6 +319,7 @@ pub struct FileFetcher {
http_client: reqwest::Client, http_client: reqwest::Client,
blob_store: BlobStore, blob_store: BlobStore,
download_log_level: log::Level, download_log_level: log::Level,
progress_bar: Option<ProgressBar>,
} }
impl FileFetcher { impl FileFetcher {
@ -328,6 +330,7 @@ impl FileFetcher {
root_cert_store: Option<RootCertStore>, root_cert_store: Option<RootCertStore>,
blob_store: BlobStore, blob_store: BlobStore,
unsafely_ignore_certificate_errors: Option<Vec<String>>, unsafely_ignore_certificate_errors: Option<Vec<String>>,
progress_bar: Option<ProgressBar>,
) -> Result<Self, AnyError> { ) -> Result<Self, AnyError> {
Ok(Self { Ok(Self {
auth_tokens: AuthTokens::new(env::var("DENO_AUTH_TOKENS").ok()), auth_tokens: AuthTokens::new(env::var("DENO_AUTH_TOKENS").ok()),
@ -345,6 +348,7 @@ impl FileFetcher {
)?, )?,
blob_store, blob_store,
download_log_level: log::Level::Info, download_log_level: log::Level::Info,
progress_bar,
}) })
} }
@ -584,12 +588,17 @@ impl FileFetcher {
.boxed(); .boxed();
} }
let mut _maybe_guard = None;
if let Some(pb) = self.progress_bar.as_ref() {
_maybe_guard = Some(pb.update(specifier.as_str()));
} else {
log::log!( log::log!(
self.download_log_level, self.download_log_level,
"{} {}", "{} {}",
colors::green("Download"), colors::green("Download"),
specifier specifier
); );
}
let maybe_etag = match self.http_cache.get(specifier) { let maybe_etag = match self.http_cache.get(specifier) {
Ok((_, headers, _)) => headers.get("etag").cloned(), Ok((_, headers, _)) => headers.get("etag").cloned(),
@ -770,6 +779,7 @@ mod tests {
None, None,
blob_store.clone(), blob_store.clone(),
None, None,
None,
) )
.unwrap(); .unwrap();
(file_fetcher, temp_dir, blob_store) (file_fetcher, temp_dir, blob_store)
@ -1208,6 +1218,7 @@ mod tests {
None, None,
BlobStore::default(), BlobStore::default(),
None, None,
None,
) )
.unwrap(); .unwrap();
let result = file_fetcher let result = file_fetcher
@ -1234,6 +1245,7 @@ mod tests {
None, None,
BlobStore::default(), BlobStore::default(),
None, None,
None,
) )
.unwrap(); .unwrap();
let specifier = let specifier =
@ -1261,6 +1273,7 @@ mod tests {
None, None,
BlobStore::default(), BlobStore::default(),
None, None,
None,
) )
.unwrap(); .unwrap();
let result = file_fetcher_02 let result = file_fetcher_02
@ -1404,6 +1417,7 @@ mod tests {
None, None,
BlobStore::default(), BlobStore::default(),
None, None,
None,
) )
.unwrap(); .unwrap();
let specifier = let specifier =
@ -1433,6 +1447,7 @@ mod tests {
None, None,
BlobStore::default(), BlobStore::default(),
None, None,
None,
) )
.unwrap(); .unwrap();
let result = file_fetcher_02 let result = file_fetcher_02
@ -1533,6 +1548,7 @@ mod tests {
None, None,
BlobStore::default(), BlobStore::default(),
None, None,
None,
) )
.unwrap(); .unwrap();
let specifier = resolve_url("http://localhost:4545/002_hello.ts").unwrap(); let specifier = resolve_url("http://localhost:4545/002_hello.ts").unwrap();
@ -1558,6 +1574,7 @@ mod tests {
None, None,
BlobStore::default(), BlobStore::default(),
None, None,
None,
) )
.unwrap(); .unwrap();
let file_fetcher_02 = FileFetcher::new( let file_fetcher_02 = FileFetcher::new(
@ -1567,6 +1584,7 @@ mod tests {
None, None,
BlobStore::default(), BlobStore::default(),
None, None,
None,
) )
.unwrap(); .unwrap();
let specifier = resolve_url("http://localhost:4545/002_hello.ts").unwrap(); let specifier = resolve_url("http://localhost:4545/002_hello.ts").unwrap();

View file

@ -455,6 +455,7 @@ impl ModuleRegistry {
root_cert_store, root_cert_store,
BlobStore::default(), BlobStore::default(),
options.unsafely_ignore_certificate_errors, options.unsafely_ignore_certificate_errors,
None,
)?; )?;
file_fetcher.set_download_log_level(super::logging::lsp_log_level()); file_fetcher.set_download_log_level(super::logging::lsp_log_level());

View file

@ -26,6 +26,7 @@ mod node;
mod npm; mod npm;
mod ops; mod ops;
mod proc_state; mod proc_state;
mod progress_bar;
mod resolver; mod resolver;
mod standalone; mod standalone;
mod text_encoding; mod text_encoding;

View file

@ -11,12 +11,12 @@ use deno_core::anyhow::Context;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::url::Url; use deno_core::url::Url;
use deno_runtime::colors;
use deno_runtime::deno_fetch::reqwest; use deno_runtime::deno_fetch::reqwest;
use crate::deno_dir::DenoDir; use crate::deno_dir::DenoDir;
use crate::file_fetcher::CacheSetting; use crate::file_fetcher::CacheSetting;
use crate::fs_util; use crate::fs_util;
use crate::progress_bar::ProgressBar;
use super::semver::NpmVersion; use super::semver::NpmVersion;
use super::tarball::verify_and_extract_tarball; use super::tarball::verify_and_extract_tarball;
@ -173,13 +173,19 @@ impl ReadonlyNpmCache {
pub struct NpmCache { pub struct NpmCache {
readonly: ReadonlyNpmCache, readonly: ReadonlyNpmCache,
cache_setting: CacheSetting, cache_setting: CacheSetting,
progress_bar: ProgressBar,
} }
impl NpmCache { impl NpmCache {
pub fn from_deno_dir(dir: &DenoDir, cache_setting: CacheSetting) -> Self { pub fn from_deno_dir(
dir: &DenoDir,
cache_setting: CacheSetting,
progress_bar: ProgressBar,
) -> Self {
Self { Self {
readonly: ReadonlyNpmCache::from_deno_dir(dir), readonly: ReadonlyNpmCache::from_deno_dir(dir),
cache_setting, cache_setting,
progress_bar,
} }
} }
@ -211,13 +217,7 @@ impl NpmCache {
); );
} }
log::log!( let _guard = self.progress_bar.update(&dist.tarball);
log::Level::Info,
"{} {}",
colors::green("Download"),
dist.tarball,
);
let response = reqwest::get(&dist.tarball).await?; let response = reqwest::get(&dist.tarball).await?;
if response.status() == 404 { if response.status() == 404 {

View file

@ -31,6 +31,7 @@ use resolution::NpmResolution;
use crate::deno_dir::DenoDir; use crate::deno_dir::DenoDir;
use crate::file_fetcher::CacheSetting; use crate::file_fetcher::CacheSetting;
use crate::progress_bar::ProgressBar;
use self::cache::ReadonlyNpmCache; use self::cache::ReadonlyNpmCache;
use self::resolution::NpmResolutionSnapshot; use self::resolution::NpmResolutionSnapshot;
@ -87,13 +88,15 @@ impl GlobalNpmPackageResolver {
cache_setting: CacheSetting, cache_setting: CacheSetting,
unstable: bool, unstable: bool,
no_npm: bool, no_npm: bool,
progress_bar: ProgressBar,
) -> Self { ) -> Self {
Self::from_cache( Self::from_cache(
NpmCache::from_deno_dir(dir, cache_setting.clone()), NpmCache::from_deno_dir(dir, cache_setting.clone(), progress_bar.clone()),
reload, reload,
cache_setting, cache_setting,
unstable, unstable,
no_npm, no_npm,
progress_bar,
) )
} }
@ -103,8 +106,10 @@ impl GlobalNpmPackageResolver {
cache_setting: CacheSetting, cache_setting: CacheSetting,
unstable: bool, unstable: bool,
no_npm: bool, no_npm: bool,
progress_bar: ProgressBar,
) -> Self { ) -> Self {
let api = NpmRegistryApi::new(cache.clone(), reload, cache_setting); let api =
NpmRegistryApi::new(cache.clone(), reload, cache_setting, progress_bar);
let registry_url = api.base_url().to_owned(); let registry_url = api.base_url().to_owned();
let resolution = Arc::new(NpmResolution::new(api)); let resolution = Arc::new(NpmResolution::new(api));

View file

@ -21,6 +21,7 @@ use serde::Serialize;
use crate::file_fetcher::CacheSetting; use crate::file_fetcher::CacheSetting;
use crate::fs_util; use crate::fs_util;
use crate::http_cache::CACHE_PERM; use crate::http_cache::CACHE_PERM;
use crate::progress_bar::ProgressBar;
use super::cache::NpmCache; use super::cache::NpmCache;
use super::semver::NpmVersionReq; use super::semver::NpmVersionReq;
@ -106,6 +107,7 @@ pub struct NpmRegistryApi {
mem_cache: Arc<Mutex<HashMap<String, Option<NpmPackageInfo>>>>, mem_cache: Arc<Mutex<HashMap<String, Option<NpmPackageInfo>>>>,
reload: bool, reload: bool,
cache_setting: CacheSetting, cache_setting: CacheSetting,
progress_bar: ProgressBar,
} }
impl NpmRegistryApi { impl NpmRegistryApi {
@ -132,8 +134,15 @@ impl NpmRegistryApi {
cache: NpmCache, cache: NpmCache,
reload: bool, reload: bool,
cache_setting: CacheSetting, cache_setting: CacheSetting,
progress_bar: ProgressBar,
) -> Self { ) -> Self {
Self::from_base(Self::default_url(), cache, reload, cache_setting) Self::from_base(
Self::default_url(),
cache,
reload,
cache_setting,
progress_bar,
)
} }
pub fn from_base( pub fn from_base(
@ -141,6 +150,7 @@ impl NpmRegistryApi {
cache: NpmCache, cache: NpmCache,
reload: bool, reload: bool,
cache_setting: CacheSetting, cache_setting: CacheSetting,
progress_bar: ProgressBar,
) -> Self { ) -> Self {
Self { Self {
base_url, base_url,
@ -148,6 +158,7 @@ impl NpmRegistryApi {
mem_cache: Default::default(), mem_cache: Default::default(),
reload, reload,
cache_setting, cache_setting,
progress_bar,
} }
} }
@ -294,13 +305,7 @@ impl NpmRegistryApi {
} }
let package_url = self.get_package_url(name); let package_url = self.get_package_url(name);
let _guard = self.progress_bar.update(package_url.as_str());
log::log!(
log::Level::Info,
"{} {}",
colors::green("Download"),
package_url,
);
let response = match reqwest::get(package_url).await { let response = match reqwest::get(package_url).await {
Ok(response) => response, Ok(response) => response,

View file

@ -25,6 +25,7 @@ use crate::node::NodeResolution;
use crate::npm::GlobalNpmPackageResolver; use crate::npm::GlobalNpmPackageResolver;
use crate::npm::NpmPackageReference; use crate::npm::NpmPackageReference;
use crate::npm::NpmPackageResolver; use crate::npm::NpmPackageResolver;
use crate::progress_bar::ProgressBar;
use crate::resolver::ImportMapResolver; use crate::resolver::ImportMapResolver;
use crate::resolver::JsxResolver; use crate::resolver::JsxResolver;
use crate::tools::check; use crate::tools::check;
@ -88,6 +89,7 @@ pub struct Inner {
maybe_file_watcher_reporter: Option<FileWatcherReporter>, maybe_file_watcher_reporter: Option<FileWatcherReporter>,
pub npm_resolver: GlobalNpmPackageResolver, pub npm_resolver: GlobalNpmPackageResolver,
pub cjs_resolutions: Mutex<HashSet<ModuleSpecifier>>, pub cjs_resolutions: Mutex<HashSet<ModuleSpecifier>>,
progress_bar: ProgressBar,
} }
impl Deref for ProcState { impl Deref for ProcState {
@ -147,6 +149,7 @@ impl ProcState {
let http_cache = http_cache::HttpCache::new(&deps_cache_location); let http_cache = http_cache::HttpCache::new(&deps_cache_location);
let root_cert_store = cli_options.resolve_root_cert_store()?; let root_cert_store = cli_options.resolve_root_cert_store()?;
let cache_usage = cli_options.cache_setting(); let cache_usage = cli_options.cache_setting();
let progress_bar = ProgressBar::default();
let file_fetcher = FileFetcher::new( let file_fetcher = FileFetcher::new(
http_cache, http_cache,
cache_usage, cache_usage,
@ -156,6 +159,7 @@ impl ProcState {
cli_options cli_options
.unsafely_ignore_certificate_errors() .unsafely_ignore_certificate_errors()
.map(ToOwned::to_owned), .map(ToOwned::to_owned),
Some(progress_bar.clone()),
)?; )?;
let lockfile = cli_options let lockfile = cli_options
@ -224,6 +228,7 @@ impl ProcState {
// don't do the unstable error when in the lsp // don't do the unstable error when in the lsp
|| matches!(cli_options.sub_command(), DenoSubcommand::Lsp), || matches!(cli_options.sub_command(), DenoSubcommand::Lsp),
cli_options.no_npm(), cli_options.no_npm(),
progress_bar.clone(),
); );
let emit_options: deno_ast::EmitOptions = ts_config_result.ts_config.into(); let emit_options: deno_ast::EmitOptions = ts_config_result.ts_config.into();
@ -250,6 +255,7 @@ impl ProcState {
maybe_file_watcher_reporter, maybe_file_watcher_reporter,
npm_resolver, npm_resolver,
cjs_resolutions: Default::default(), cjs_resolutions: Default::default(),
progress_bar,
}))) })))
} }
@ -411,6 +417,8 @@ impl ProcState {
self.prepare_node_std_graph().await?; self.prepare_node_std_graph().await?;
} }
self.progress_bar.clear();
// type check if necessary // type check if necessary
if self.options.type_check_mode() != TypeCheckMode::None { if self.options.type_check_mode() != TypeCheckMode::None {
let maybe_config_specifier = self.options.maybe_config_file_specifier(); let maybe_config_specifier = self.options.maybe_config_file_specifier();

132
cli/progress_bar.rs Normal file
View file

@ -0,0 +1,132 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::colors;
use deno_core::parking_lot::Mutex;
use indexmap::IndexSet;
use std::sync::Arc;
use std::time::Duration;
#[derive(Clone, Debug, Default)]
pub struct ProgressBar(Arc<Mutex<ProgressBarInner>>);
#[derive(Debug)]
struct ProgressBarInner {
pb: Option<indicatif::ProgressBar>,
is_tty: bool,
in_flight: IndexSet<String>,
}
impl Default for ProgressBarInner {
fn default() -> Self {
Self {
pb: None,
is_tty: colors::is_tty(),
in_flight: IndexSet::default(),
}
}
}
impl ProgressBarInner {
fn get_or_create_pb(&mut self) -> indicatif::ProgressBar {
if let Some(pb) = self.pb.as_ref() {
return pb.clone();
}
let pb = indicatif::ProgressBar::new_spinner();
pb.enable_steady_tick(Duration::from_millis(120));
pb.set_prefix("Download");
pb.set_style(
indicatif::ProgressStyle::with_template(
"{prefix:.green} {spinner:.green} {msg}",
)
.unwrap()
.tick_strings(&["", "", "", "", "", "", "", "", "", ""]),
);
self.pb = Some(pb);
self.pb.as_ref().unwrap().clone()
}
fn add_in_flight(&mut self, msg: &str) {
if self.in_flight.contains(msg) {
return;
}
self.in_flight.insert(msg.to_string());
}
/// Returns if removed "in-flight" was last entry and progress
/// bar needs to be updated.
fn remove_in_flight(&mut self, msg: &str) -> bool {
if !self.in_flight.contains(msg) {
return false;
}
let mut is_last = false;
if let Some(last) = self.in_flight.last() {
is_last = last == msg;
}
self.in_flight.remove(msg);
is_last
}
fn update_progress_bar(&mut self) {
let pb = self.get_or_create_pb();
if let Some(msg) = self.in_flight.last() {
pb.set_message(msg.clone());
}
}
}
pub struct UpdateGuard {
pb: ProgressBar,
msg: String,
noop: bool,
}
impl Drop for UpdateGuard {
fn drop(&mut self) {
if self.noop {
return;
}
let mut inner = self.pb.0.lock();
if inner.remove_in_flight(&self.msg) {
inner.update_progress_bar();
}
}
}
impl ProgressBar {
pub fn update(&self, msg: &str) -> UpdateGuard {
let mut guard = UpdateGuard {
pb: self.clone(),
msg: msg.to_string(),
noop: false,
};
let mut inner = self.0.lock();
// If we're not running in TTY we're just gonna fallback
// to using logger crate.
if !inner.is_tty {
log::log!(log::Level::Info, "{} {}", colors::green("Download"), msg);
guard.noop = true;
return guard;
}
inner.add_in_flight(msg);
inner.update_progress_bar();
guard
}
pub fn clear(&self) {
let mut inner = self.0.lock();
match inner.pb.as_ref() {
Some(pb) => {
pb.finish_and_clear();
inner.pb = None;
}
None => {}
};
}
}