Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
xtask to download and parse js libs (#1816)
Browse files Browse the repository at this point in the history
* download and parse js libs
  • Loading branch information
xunilrj committed Nov 24, 2021
1 parent eebed7d commit b3108d3
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 3 deletions.
108 changes: 106 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ schemars = "0.8"
yastl = "0.1"
num_cpus = "1.13"
ungrammar = "1.14.9"
ureq = { version = "2.3.1" }
url = "2.2.2"
1 change: 1 addition & 0 deletions xtask/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod compare;
pub mod coverage;
pub mod docgen;
pub mod glue;
pub mod libs;

use std::{
env,
Expand Down
2 changes: 2 additions & 0 deletions xtask/src/libs/libs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.prod.js
60 changes: 60 additions & 0 deletions xtask/src/libs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::{path::PathBuf, str::FromStr};

fn err_to_string<E: std::fmt::Debug>(e: E) -> String {
format!("{:?}", e)
}

pub fn get_code(lib: &str) -> Result<String, String> {
let url = url::Url::from_str(lib).map_err(err_to_string)?;
let segments = url
.path_segments()
.ok_or_else(|| "lib url has no segments".to_string())?;
let filename = segments
.last()
.ok_or_else(|| "lib url has no segments".to_string())?;

let mut file = PathBuf::from_str("target").map_err(err_to_string)?;
file.push(filename);

match std::fs::read_to_string(&file) {
Ok(code) => {
println!("[{}] - using [{}]", filename, file.display());
Ok(code)
}
Err(_) => {
println!(
"[{}] - Downloading [{}] to [{}]",
filename,
lib,
file.display()
);
match ureq::get(lib).call() {
Ok(response) => {
let mut reader = response.into_reader();

let _ = std::fs::remove_file(&file);
let mut writer = std::fs::File::create(&file).map_err(err_to_string)?;
let _ = std::io::copy(&mut reader, &mut writer);

std::fs::read_to_string(&file).map_err(err_to_string)
}
Err(e) => Err(format!("{:?}", e)),
}
}
}
}

pub fn run() {
let libs = include_str!("libs.txt").lines();
for lib in libs {
let code = get_code(lib);
match code {
Ok(code) => {
let _ = std::panic::catch_unwind(|| rslint_parser::parse_module(code.as_str(), 0));
}
Err(e) => println!("{:?}", e),
}
}

println!("end");
}
7 changes: 6 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ fn main() -> Result<()> {
coverage::run(query, yastl::Pool::with_config(num_cpus::get(), pool), json);
Ok(())
}
"coverage-libs" => {
xtask::libs::run();
Ok(())
}
_ => {
eprintln!(
"\
Expand All @@ -63,7 +67,8 @@ SUBCOMMANDS:
syntax
docgen
coverage [--json]
compare [--markdown]
coverage-libs
compare [--markdown]
OPTIONS
--markdown Emits supported output into markdown format. Supported by compare subcommand
--json Emits supported output into json format. Supported by coverage subcommand
Expand Down

0 comments on commit b3108d3

Please sign in to comment.