Skip to content

Commit

Permalink
refactor(ast_tools): handle paths as strings (oxc-project#6925)
Browse files Browse the repository at this point in the history
We were converting back and forwards between strings and `Path`s for no purpose. Just use strings.
  • Loading branch information
overlookmotel authored and Orenbek committed Oct 28, 2024
1 parent 2818a10 commit 65c28de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
26 changes: 10 additions & 16 deletions tasks/ast_tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,27 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
.run()?;

if !cli_options.dry_run {
let side_effects = outputs
let paths = outputs
.into_iter()
.map(|it| {
let path = it.path();
log!("Writing {path}...");
it.write_to_file().unwrap();
.map(|output| {
log!("Writing {}...", &output.path);
output.write_to_file().unwrap();
logln!(" Done!");
path
output.path
})
.collect();
write_ci_filter(SOURCE_PATHS, side_effects, ".github/.generated_ast_watch_list.yml")?;
write_ci_filter(SOURCE_PATHS, paths, ".github/.generated_ast_watch_list.yml")?;
}

if let CliOptions { schema: Some(schema_path), dry_run: false, .. } = cli_options {
let path = schema_path.to_str().expect("invalid path for schema output.");
let schema = serde_json::to_string_pretty(&schema.defs).normalize()?;
write_all_to(schema.as_bytes(), path)?;
write_all_to(schema.as_bytes(), schema_path)?;
}

Ok(())
}

fn write_ci_filter(
inputs: &[&str],
side_effects: Vec<String>,
output_path: &str,
) -> std::io::Result<()> {
fn write_ci_filter(inputs: &[&str], paths: Vec<String>, output_path: &str) -> std::io::Result<()> {
let file = file!().replace('\\', "/");
let mut output = format!(
"\
Expand All @@ -127,8 +121,8 @@ fn write_ci_filter(
push_item(input);
}

for side_effect in side_effects {
push_item(side_effect.as_str());
for path in paths {
push_item(path.as_str());
}

push_item("tasks/ast_tools/src/**");
Expand Down
32 changes: 13 additions & 19 deletions tasks/ast_tools/src/output/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{fs, io::Write, path::PathBuf};
use std::{
fs,
io::{self, Write},
path::Path,
};

use proc_macro2::TokenStream;

Expand All @@ -11,8 +15,8 @@ use rust::print_rust;
///
/// Can be either Rust or Javascript.
pub enum Output {
Rust { path: PathBuf, tokens: TokenStream },
Javascript { path: PathBuf, code: String },
Rust { path: String, tokens: TokenStream },
Javascript { path: String, code: String },
}

impl Output {
Expand All @@ -34,34 +38,24 @@ impl Output {
/// A raw output from codegen.
#[derive(Debug)]
pub struct RawOutput {
pub path: PathBuf,
pub path: String,
pub content: Vec<u8>,
}

impl RawOutput {
/// Get path of output as a string.
pub fn path(&self) -> String {
let path = self.path.to_string_lossy();
path.replace('\\', "/")
}

/// Write output to file
pub fn write_to_file(self) -> std::io::Result<()> {
let Self { path, content } = self;
let path = path.into_os_string();
let path = path.to_str().unwrap();
write_all_to(&content, path)?;
Ok(())
pub fn write_to_file(&self) -> io::Result<()> {
write_all_to(&self.content, &self.path)
}
}

/// Get path for an output.
pub fn output_path(krate: &str, path: &str) -> PathBuf {
std::path::PathBuf::from_iter([krate, "src", "generated", path])
pub fn output_path(krate: &str, path: &str) -> String {
format!("{krate}/src/generated/{path}")
}

/// Write data to file.
pub fn write_all_to<S: AsRef<std::path::Path>>(data: &[u8], path: S) -> std::io::Result<()> {
pub fn write_all_to<P: AsRef<Path>>(data: &[u8], path: P) -> io::Result<()> {
let path = path.as_ref();
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
Expand Down

0 comments on commit 65c28de

Please sign in to comment.