Skip to content

Commit

Permalink
--
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Sep 24, 2024
1 parent 7c8129c commit bd2a920
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 39 deletions.
9 changes: 0 additions & 9 deletions crates/infra/cli/src/commands/check/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::path::Path;

use anyhow::Result;
use clap::{Parser, ValueEnum};
use infra_utils::cargo::CargoWorkspaceCommands;
use infra_utils::commands::Command;
use infra_utils::paths::PathExtensions;
use infra_utils::terminal::Terminal;
use strum::IntoEnumIterator;

Expand Down Expand Up @@ -79,12 +76,6 @@ fn check_npm() -> Result<()> {
package.build()?;
}

let root_project = Path::repo_path("tsconfig.json");

Command::new("tsc")
.property("--build", root_project.unwrap_str())
.run();

Ok(())
}

Expand Down
11 changes: 11 additions & 0 deletions crates/infra/cli/src/commands/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ enum LintCommand {
Rustfmt,
/// Check for violations in Bash files.
Shellcheck,
/// Check for type errors in TypeScript files.
Tsc,
/// Check for violations issues in Yaml files.
Yamllint,
}
Expand All @@ -51,6 +53,7 @@ impl OrderedCommand for LintCommand {
LintCommand::MarkdownLint => run_markdown_lint()?,
LintCommand::Rustfmt => run_rustfmt(),
LintCommand::Shellcheck => run_shellcheck()?,
LintCommand::Tsc => run_tsc(),
LintCommand::Yamllint => run_yamllint()?,
};

Expand Down Expand Up @@ -131,6 +134,14 @@ fn run_shellcheck() -> Result<()> {
Ok(())
}

fn run_tsc() {
let root_project = Path::repo_path("tsconfig.json");

Command::new("tsc")
.property("--build", root_project.unwrap_str())
.run();
}

fn run_yamllint() -> Result<()> {
let config_file = Path::repo_path(".yamllint.yml");

Expand Down
18 changes: 18 additions & 0 deletions crates/infra/cli/src/commands/setup/npm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
use std::path::Path;

use infra_utils::commands::Command;
use infra_utils::github::GitHub;
use infra_utils::paths::PathExtensions;

pub fn setup_npm() {
if GitHub::is_running_in_ci() {
Command::new("npm").arg("ci").run();
} else {
Command::new("npm").arg("install").run();
}

Command::new("npm")
.current_dir(Path::repo_path("submodules/jco"))
.args(["install"])
.run();

Command::new("npm")
.current_dir(Path::repo_path("submodules/jco"))
.args(["run", "build"])
.run();

Command::new("npm")
.current_dir(Path::repo_path("submodules/jco"))
.args(["run", "build:release"])
.run();
}
89 changes: 59 additions & 30 deletions crates/infra/cli/src/toolchains/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use infra_utils::codegen::CodegenFileSystem;
use infra_utils::commands::Command;
use infra_utils::paths::{FileWalker, PathExtensions};
use strum_macros::EnumIter;
use tempfile::TempDir;

pub const WASM_TARGET: &str = "wasm32-wasi";

Expand All @@ -22,9 +21,9 @@ impl WasmPackage {
pub fn build(self) -> Result<()> {
let wasm_component = self.generate_component()?;

let transpiled_dir = self.transpile_sources(&wasm_component)?;
self.transpile_wasm(&wasm_component)?;

self.copy_transpiled(transpiled_dir.path())?;
self.transpile_sources()?;

Ok(())
}
Expand Down Expand Up @@ -74,52 +73,53 @@ impl WasmPackage {
Ok(wasm_component)
}

fn transpile_sources(self, wasm_component: &Path) -> Result<TempDir> {
let wasm_crate = self.wasm_crate();

let jco_config = CargoWorkspace::locate_source_crate(wasm_crate)?.join(self.config_file());

let temp_dir = tempfile::tempdir()?;
fn transpile_wasm(self, wasm_component: &Path) -> Result<()> {
let temp_dir_handle = tempfile::tempdir()?;
let temp_dir = temp_dir_handle.path();

Command::new("jco")
.args(["transpile", wasm_component.unwrap_str()])
.property("--configuration-file", jco_config.unwrap_str())
.property("--out-dir", temp_dir.path().unwrap_str())
.property("--base64-cutoff", "0") // disable inlining core Wasm binaries as base64
.flag("--no-namespaced-exports") // disable namespaced exports for typescript compatibility
.flag("--valid-lifting-optimization") // optimize component binary validations assuming all lifted values are valid
.run();

Ok(temp_dir)
}
{
let wasm_crate = self.wasm_crate();
let jco_config =
CargoWorkspace::locate_source_crate(wasm_crate)?.join(self.config_file());

Command::new("npm")
.current_dir(Path::repo_path("submodules/jco"))
.args(["exec", "--"])
.args(["jco", "transpile", wasm_component.unwrap_str()])
.property("--configuration-file", jco_config.unwrap_str())
.property("--out-dir", temp_dir.unwrap_str())
.property("--base64-cutoff", "0") // disable inlining core Wasm binaries as base64
.flag("--no-namespaced-exports") // disable namespaced exports for typescript compatibility
.flag("--valid-lifting-optimization") // optimize component binary validations assuming all lifted values are valid
.run();
}

fn copy_transpiled(self, transpiled_dir: &Path) -> Result<()> {
let npm_crate = self.npm_crate();
let output_dir = CargoWorkspace::locate_source_crate(npm_crate)?.join("wasm/generated");

let mut fs = CodegenFileSystem::new(&output_dir)?;

for transpiled in FileWalker::from_directory(transpiled_dir).find_all()? {
let output = transpiled.replace_prefix(transpiled_dir, &output_dir);
for temp_path in FileWalker::from_directory(temp_dir).find_all()? {
let output_path = temp_path.replace_prefix(temp_dir, &output_dir);

match transpiled.unwrap_ext() {
match temp_path.unwrap_ext() {
// Copy '.d.ts' definition files as-is:
"ts" => {
let contents = transpiled.read_to_string()?;
fs.write_file(output, contents)?;
let contents = temp_path.read_to_string()?;
fs.write_file(output_path, contents)?;
}

// Disable type checking for JS, since we have no control over the generated output:
"js" => {
let mut contents = transpiled.read_to_string()?;
let mut contents = temp_path.read_to_string()?;
contents.insert_str(0, "// @ts-nocheck\n\n");
fs.write_file(output, contents)?;
fs.write_file(output_path, contents)?;
}

// Binary file, that doesn't need to go through our codegen/formatting APIs:
"wasm" => {
std::fs::copy(&transpiled, &output)?;
fs.mark_generated_file(output)?;
std::fs::copy(&temp_path, &output_path)?;
fs.mark_generated_file(output_path)?;
}

other => panic!("Unexpected file extension: {other}"),
Expand All @@ -137,6 +137,35 @@ impl WasmPackage {
Ok(())
}

fn transpile_sources(self) -> Result<()> {
let npm_crate = self.npm_crate();
let project_dir = CargoWorkspace::locate_source_crate(npm_crate)?;

let temp_dir_handle = tempfile::tempdir()?;
let temp_dir = temp_dir_handle.path();

Command::new("tsc")
.property("--project", project_dir.join("tsconfig.json").unwrap_str())
.property("--outDir", temp_dir.unwrap_str())
.property("--noEmit", "false")
.run();

// __SLANG_NPM_PACKAGE_EXPORTS__ (keep in sync)
let temp_dir = temp_dir.join("src/generated");
let output_dir = project_dir.join("target/generated");

std::fs::remove_dir_all(&output_dir)?; // remove any old generated files

for temp_path in FileWalker::from_directory(&temp_dir).find_all()? {
let output_path = temp_path.replace_prefix(&temp_dir, &output_dir);

std::fs::create_dir_all(output_path.unwrap_parent())?;
std::fs::copy(&temp_path, &output_path)?;
}

Ok(())
}

pub fn wasm_crate(self) -> &'static str {
match self {
Self::Codegen => "codegen_runtime_cargo_wasm",
Expand Down

0 comments on commit bd2a920

Please sign in to comment.