Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
VoltagedDebunked committed Oct 16, 2024
1 parent d45cfca commit 9ce1540
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 144 deletions.
8 changes: 4 additions & 4 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN mkdir src
# Copy the source code
COPY src ./src

# Install required target (for example, x86_64-unknown-linux-gnu and x86_64-pc-windows-gnu)
# Install required target
RUN rustup target add x86_64-unknown-linux-gnu x86_64-pc-windows-gnu

# Build the application for Linux
Expand All @@ -26,10 +26,10 @@ RUN cargo build --release --target x86_64-pc-windows-gnu
FROM debian:bullseye-slim AS runtime

# Copy the built Linux binary from the builder stage
COPY --from=builder /usr/src/Zen/target/x86_64-unknown-linux-gnu/release/Zen /usr/local/bin/Zen-linux
COPY --from=builder /usr/src/zen/target/x86_64-unknown-linux-gnu/release/zen /usr/local/bin/zen-linux

# Copy the built Windows binary from the builder stage (if needed)
COPY --from=builder /usr/src/Zen/target/x86_64-pc-windows-gnu/release/Zen.exe /usr/local/bin/Zen.exe
COPY --from=builder /usr/src/zen/target/x86_64-pc-windows-gnu/release/zen.exe /usr/local/bin/zen.exe

# Set the entry point for the Linux binary
ENTRYPOINT ["Zen-linux"]
ENTRYPOINT ["zen-linux"]
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build = "build.rs"

[dependencies]
colored = "2.1.0"
deno_core = "0.313.0"
deno_core = "0.314.0"
tokio = { version = "1.40.0", features = ["full"] }
regex = "1.11.0"
url = "2.5.2"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Zen is a high-performance JavaScript runtime built in Rust. It executes code fas
### Prerequisites

- Git
- Curl
- [Rust](https://www.rust-lang.org/tools/install)
- Docker (for containerization)

Expand Down Expand Up @@ -70,12 +71,12 @@ Zen can also be built and run using Docker. The provided Dockerfile allows you t

The Dockerfile consists of two main stages:

1. **Builder Stage**:
1. **Builder Stage**:
- Uses the official Rust image to build the Zen application.
- Copies the source code and required files.
- Compiles the application for both Linux and Windows targets.

2. **Runtime Stage**:
2. **Runtime Stage**:
- Uses a minimal Debian image to run the application.
- Copies the built binaries from the builder stage.

Expand Down
169 changes: 33 additions & 136 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,151 +1,48 @@
use std::fs;
use std::io::Error;
use regex::Regex;
use std::process::Command;
use std::io::{Error, ErrorKind};
use std::fs::metadata;

pub struct Formatter {
indent: String,
line_length: usize,
quote_style: QuoteStyle,
trailing_commas: bool,
}

#[allow(dead_code)]
#[derive(Clone, Copy)]
pub enum QuoteStyle {
Single,
Double,
}
pub struct Formatter;

impl Formatter {
pub fn new(indent_size: usize, line_length: usize, quote_style: QuoteStyle, trailing_commas: bool) -> Self {
Self {
indent: " ".repeat(indent_size),
line_length,
quote_style,
trailing_commas,
pub fn new() -> Result<Self, Error> {
Self::install_dprint()?;
Ok(Formatter)
}

fn install_dprint() -> Result<(), Error> {
let output = Command::new("cargo")
.arg("install")
.arg("dprint")
.output()?;

if !output.status.success() {
return Err(Error::new(
ErrorKind::Other,
String::from_utf8_lossy(&output.stderr),
));
}
}

pub fn format_js(&self, file_path: &str) -> Result<(), Error> {
let content = fs::read_to_string(file_path)?;
let formatted_content = self.format_code(&content);
fs::write(file_path, formatted_content)?;
Ok(())
}

fn format_code(&self, code: &str) -> String {
let mut result = String::new();
let mut indent_level = 0;
let mut inside_comment = false;

for line in code.lines() {
let trimmed_line = line.trim();

if trimmed_line.is_empty() {
result.push('\n');
continue;
}

if trimmed_line.starts_with("/*") {
inside_comment = true;
result.push_str(&format!("{}{}\n", self.indent.repeat(indent_level), trimmed_line));
continue;
}

if inside_comment {
result.push_str(&format!("{}{}\n", self.indent.repeat(indent_level), trimmed_line));
if trimmed_line.ends_with("*/") {
inside_comment = false;
}
continue;
}

if trimmed_line.starts_with("//") {
result.push_str(&format!("{}{}\n", self.indent.repeat(indent_level), trimmed_line));
continue;
}

if trimmed_line.ends_with('{') {
result.push_str(&format!("{}{}\n", self.indent.repeat(indent_level), trimmed_line));
indent_level += 1;
continue;
}

if trimmed_line == "}" {
indent_level -= 1;
result.push_str(&format!("{}{}\n", self.indent.repeat(indent_level), trimmed_line));
continue;
}

let formatted_line = self.format_line(trimmed_line);
result.push_str(&format!("{}{}\n", self.indent.repeat(indent_level), formatted_line));
}

self.break_lines(result)
}

fn format_line(&self, line: &str) -> String {
let mut formatted_line = line.to_string();
formatted_line = self.replace_quotes(formatted_line);
formatted_line = Regex::new(r"\s+").unwrap().replace_all(&formatted_line, " ").to_string();
formatted_line = self.format_arrow_functions(formatted_line);
formatted_line = self.ensure_semicolon(formatted_line);
if self.trailing_commas {
formatted_line = self.add_trailing_commas(formatted_line);
pub fn format_js(&self, file_path: &str) -> Result<(), Error> {
if metadata(file_path).is_err() {
return Err(Error::new(ErrorKind::NotFound, format!("File not found: {}", file_path)));
}
formatted_line.trim().to_string()
}

fn replace_quotes(&self, line: String) -> String {
let quote_char = match self.quote_style {
QuoteStyle::Single => "'",
QuoteStyle::Double => "\"",
};
let output = Command::new("dprint")
.arg("format")
.arg(file_path)
.output()?;

Regex::new(r#""([^"]*)""#)
.unwrap()
.replace_all(&line, quote_char)
.to_string()
}

fn format_arrow_functions(&self, line: String) -> String {
Regex::new(r"(\w+)\s*=>")
.unwrap()
.replace_all(&line, |caps: &regex::Captures| format!("{} =>", &caps[1]))
.to_string()
}

fn ensure_semicolon(&self, line: String) -> String {
if line.ends_with(';') || line.ends_with('}') || line.ends_with('{') {
line
} else {
format!("{};", line)
if !output.status.success() {
return Err(Error::new(
ErrorKind::Other,
String::from_utf8_lossy(&output.stderr),
));
}
}

fn add_trailing_commas(&self, line: String) -> String {
Regex::new(r#"(,\s*\})|(\s*})"#)
.unwrap()
.replace_all(&line, ", }")
.to_string()
}

fn break_lines(&self, content: String) -> String {
let mut result = String::new();
for line in content.lines() {
let mut current_line = String::new();
for word in line.split_whitespace() {
if current_line.len() + word.len() + 1 > self.line_length {
result.push_str(current_line.trim());
result.push('\n');
current_line.clear();
}
current_line.push_str(word);
current_line.push(' ');
}
result.push_str(current_line.trim());
result.push('\n');
}
result
Ok(())
}
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ async fn main() {
return;
}

let formatter = match Formatter::new() {
Ok(fmt) => fmt,
Err(e) => {
eprintln!("Error installing Dprint: {}", e.to_string().red().bold());
return;
}
};

match command.as_str() {
"run" => {
let handles: Vec<_> = args[2..]
Expand All @@ -79,7 +87,6 @@ async fn main() {
tasks::handle_task(&args[2]);
}
"fmt" => {
let formatter = Formatter::new(2, 80, fmt::QuoteStyle::Double, true);
for file in &args[2..] {
match formatter.format_js(file) {
Ok(_) => println!("Formatted {}", file),
Expand Down

0 comments on commit 9ce1540

Please sign in to comment.