Skip to content

Commit

Permalink
added CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
alshdavid committed Mar 2, 2024
1 parent a6cbe65 commit 07e5356
Show file tree
Hide file tree
Showing 114 changed files with 365 additions and 363 deletions.
7 changes: 4 additions & 3 deletions .github/scripts/cmd/run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export function main(args) {
const FIXTURE_DIR = path.join(Paths.Fixtures, FIXTURE)

const [CARGO_ARGS] = args._raw.replace(FIXTURE, '').split(' -- ')
const [,MACH_ARGS] = process.argv.join(' ').split(' -- ')
const [,MACH_ARGS = ''] = process.argv.join(' ').split(' -- ')

const { binary_path } = build(parse(CARGO_ARGS.split(' ')))

try {
child_process.execSync(`${binary_path} ${MACH_ARGS}`, { cwd: FIXTURE_DIR, stdio: 'inherit' })
} catch (error) {}
child_process.execSync(`${binary_path} ${MACH_ARGS || ''}`, { cwd: FIXTURE_DIR, stdio: 'inherit' })
} catch (error) {
}
}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# builds
dist/
build/
publish/
temp/
target/
Expand Down
4 changes: 3 additions & 1 deletion crates/mach/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[package]
name = "mach"
version = "0.0.0-local"
description = "mustang"
repository = "https://github.com/alshdavid/mach"
bugreports = "test"
description = "Bundler For The Modern Web"
edition = "2021"

[dependencies]
Expand Down
9 changes: 0 additions & 9 deletions crates/mach/src/adapters/node_js/mod.rs

This file was deleted.

3 changes: 0 additions & 3 deletions crates/mach/src/bundling/mod.rs

This file was deleted.

4 changes: 4 additions & 0 deletions crates/mach/src/cmd/build/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use clap::Parser;

#[derive(Parser, Debug)]
pub struct BuildCommand {}
5 changes: 5 additions & 0 deletions crates/mach/src/cmd/build/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use super::BuildCommand;

pub fn main(command: BuildCommand) {
println!("running build");
}
5 changes: 5 additions & 0 deletions crates/mach/src/cmd/build/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod main;
mod config;

pub use crate::cmd::build::main::*;
pub use crate::cmd::build::config::*;
4 changes: 4 additions & 0 deletions crates/mach/src/cmd/dev/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use clap::Parser;

#[derive(Parser, Debug)]
pub struct DevCommand {}
6 changes: 6 additions & 0 deletions crates/mach/src/cmd/dev/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::DevCommand;

pub fn main(_command: DevCommand) {
println!("Not implemented yet");
println!("Coming Soon");
}
5 changes: 5 additions & 0 deletions crates/mach/src/cmd/dev/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod main;
mod config;

pub use crate::cmd::dev::main::*;
pub use crate::cmd::dev::config::*;
4 changes: 4 additions & 0 deletions crates/mach/src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod version;
pub mod build;
pub mod watch;
pub mod dev;
4 changes: 4 additions & 0 deletions crates/mach/src/cmd/version/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use clap::Parser;

#[derive(Parser, Debug)]
pub struct VersionCommand {}
31 changes: 31 additions & 0 deletions crates/mach/src/cmd/version/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::fs;

use super::VersionCommand;

#[allow(non_upper_case_globals)]
const color_red: &str = "\x1B[31m";
#[allow(non_upper_case_globals)]
const color_reset: &str = "\x1B[39m";
#[allow(non_upper_case_globals)]
const style_bold: &str = "\x1B[1m";
#[allow(non_upper_case_globals)]
const style_reset: &str = "\x1B[0m";

const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
const VERSION: &str = env!("CARGO_PKG_VERSION");
const REPOSITORY: &str = env!("CARGO_PKG_REPOSITORY");

pub fn main(_command: VersionCommand) {
print!(r#"{color_red}{style_bold}"#);
println!(r#"___ ___ _ "#);
println!(r#"| \/ | | | "#);
println!(r#"| . . | __ _ ___| |__ "#);
println!(r#"| |\/| |/ _` |/ __| '_ \ "#);
println!(r#"| | | | (_| | (__| | | |"#);
println!(r#"\_| |_/\__,_|\___|_| |_|"#);
print!(r#"{color_reset}{style_reset}"#);
println!(r#""#);
println!(r#"{style_bold}Description{style_reset} {DESCRIPTION}"#);
println!(r#"{style_bold}Repository{style_reset} {REPOSITORY}"#);
println!(r#"{style_bold}Version{style_reset} {VERSION}"#);
}
5 changes: 5 additions & 0 deletions crates/mach/src/cmd/version/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod main;
mod config;

pub use crate::cmd::version::main::*;
pub use crate::cmd::version::config::*;
4 changes: 4 additions & 0 deletions crates/mach/src/cmd/watch/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use clap::Parser;

#[derive(Parser, Debug)]
pub struct WatchCommand {}
6 changes: 6 additions & 0 deletions crates/mach/src/cmd/watch/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::WatchCommand;

pub fn main(_command: WatchCommand) {
println!("Not implemented yet");
println!("Coming Soon");
}
5 changes: 5 additions & 0 deletions crates/mach/src/cmd/watch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod main;
mod config;

pub use crate::cmd::watch::main::*;
pub use crate::cmd::watch::config::*;
4 changes: 2 additions & 2 deletions crates/mach/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use clap::Parser;
use normalize_path::NormalizePath;

use crate::args::Args;
use crate::public::Config;
use crate::public::Machrc;
use crate::platform::public::Config;
use crate::platform::public::Machrc;

type FileIndex = HashMap<String, Vec<PathBuf>>;

Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions crates/mach/src/kit/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod hash;

pub use crate::kit::hash::hash::*;
2 changes: 2 additions & 0 deletions crates/mach/src/kit/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod hash;
pub mod swc;
File renamed without changes.
22 changes: 22 additions & 0 deletions crates/mach/src/kit/swc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![allow(dead_code)]
#![allow(unused_imports)]

mod convert;
mod lookup_property_access;
mod parse_module;
mod parse_program;
mod parse_script;
mod render_module;
mod render_program;
mod render_script;
mod render_stmts;

pub use crate::kit::swc::convert::*;
pub use crate::kit::swc::lookup_property_access::*;
pub use crate::kit::swc::parse_module::*;
pub use crate::kit::swc::parse_program::*;
pub use crate::kit::swc::parse_script::*;
pub use crate::kit::swc::render_module::*;
pub use crate::kit::swc::render_program::*;
pub use crate::kit::swc::render_script::*;
pub use crate::kit::swc::render_stmts::*;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
169 changes: 37 additions & 132 deletions crates/mach/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,136 +1,41 @@
mod adapters;
mod args;
mod bundling;
mod config;
mod emit;
mod packaging;
mod platform;
mod plugins;
mod public;
mod transformation;

use std::sync::Arc;

use crate::adapters::node_js::NodeAdapter;
use crate::bundling::bundle;
use crate::config::parse_config;
use crate::emit::emit;
use crate::packaging::package;
use crate::plugins::load_plugins;
use crate::public::AssetGraph;
use crate::public::AssetMap;
use crate::public::BundleGraph;
use crate::public::Bundles;
use crate::public::Config;
use crate::public::DependencyMap;
use crate::public::Packages;
use crate::transformation::transform;

async fn main_async(config: Config) {
// Bundle state
let mut asset_map = AssetMap::new();
let mut dependency_map = DependencyMap::new();
let mut asset_graph = AssetGraph::new();
let mut bundles = Bundles::new();
let mut bundle_graph = BundleGraph::new();
let mut packages = Packages::new();

// Adapters
let node_adapter = Arc::new(NodeAdapter::new(config.node_workers).await);

// TODO move this into a "reporter" plugin
println!("Entry: {}", config.entry_point.to_str().unwrap());
println!("Root: {}", config.project_root.to_str().unwrap());
if !&config.machrc.is_default {
println!(
"Mach Config: {}",
config.machrc.file_path.to_str().unwrap()
);
} else {
println!("Mach Config: Default");
}
println!("Out Dir: {}", config.dist_dir.to_str().unwrap());
println!("Optimize: {}", config.optimize);
println!("Threads: {}", config.threads);
println!("Node Workers: {}", config.node_workers);

// Initialize plugins
let Ok(plugins) = load_plugins(&config.machrc, node_adapter.clone()).await else {
panic!("Unable to initialize plugins");
};

// This phase reads source files and transforms them. New imports
// are discovered as files are parsed, looping until no more imports exist
if let Err(err) = transform(
&config,
&mut asset_map,
&mut dependency_map,
&mut asset_graph,
&plugins,
)
.await
{
println!("Transformation Error");
println!("{}", err);
return;
}

println!("Assets: {}", asset_map.len());

// dbg!(&asset_map);
// dbg!(&asset_graph);
// dbg!(&dependency_map);

if let Err(err) = bundle(
&config,
&mut asset_map,
&mut dependency_map,
&mut asset_graph,
&mut bundles,
&mut bundle_graph,
) {
println!("Bundling Error");
println!("{}", err);
return;
}

// dbg!(&bundles);
// dbg!(&bundle_graph);

if let Err(err) = package(
&config,
&mut asset_map,
&mut dependency_map,
&mut asset_graph,
&mut bundles,
&mut bundle_graph,
&mut packages,
) {
println!("Packaging Error");
println!("{}", err);
return;
}

// dbg!(&packages);

if let Err(err) = emit(&config, &mut bundles, &mut packages) {
println!("Packaging Error");
println!("{}", err);
return;
}
mod cmd;

use clap::Subcommand;
use clap::Parser;
use cmd::build::BuildCommand;
use cmd::dev::DevCommand;
use cmd::watch::WatchCommand;
use cmd::version::VersionCommand;

#[derive(Debug, Subcommand)]
pub enum CommandType {
Build(BuildCommand),
Dev(DevCommand),
Watch(WatchCommand),
Version(VersionCommand),
}

println!(
"Finished in: {:.3}s",
config.start_time.elapsed().unwrap().as_nanos() as f64 / 1_000_000 as f64 / 1000 as f64
);
#[derive(Parser, Debug)]
struct Commands {
#[clap(subcommand)]
command: CommandType
}

fn main() {
let config = parse_config().expect("Could not parse CLI args");
tokio::runtime::Builder::new_multi_thread()
.worker_threads(config.threads)
.enable_all()
.build()
.unwrap()
.block_on(main_async(config));
}
let command = Commands::parse();

match command.command {
CommandType::Build(command) => {
cmd::build::main(command);
},
CommandType::Dev(command) => {
cmd::dev::main(command);
},
CommandType::Watch(command) => {
cmd::watch::main(command);
},
CommandType::Version(command) => {
cmd::version::main(command);
},
}
}
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions crates/mach/src/platform/adapters/node_js/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod node_adapter;
mod node_worker;
pub mod requests;
mod resolve;
mod spawn;

pub use crate::platform::adapters::node_js::node_adapter::*;
pub use crate::platform::adapters::node_js::node_worker::*;
pub use crate::platform::adapters::node_js::resolve::*;
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::path::PathBuf;
use serde::Deserialize;
use serde::Serialize;

use crate::public::Config;
use crate::public::Dependency;
use crate::public::DependencyOptions;
use crate::platform::public::Config;
use crate::platform::public::Dependency;
use crate::platform::public::DependencyOptions;

#[derive(Serialize, Clone, Debug)]
pub struct LoadPluginRequest {
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 07e5356

Please sign in to comment.