Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
feat(plugin): Add commands for plugins (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Nov 1, 2021
1 parent fe87872 commit 31f523b
Show file tree
Hide file tree
Showing 30 changed files with 1,698 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
.DS_Store
4 changes: 4 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
format_strings = true
merge_imports = true
use_field_init_shorthand = true
wrap_comments = true
20 changes: 20 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/*.bk": true,
"**/node_modules": true,
"**/target": true
},
"[rust]": {
"editor.formatOnSave": true
},
"[typescript]": {
"editor.formatOnSave": true
},
"editor.formatOnSave": true,
"git.ignoreLimitWarning": true
}
35 changes: 35 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[workspace]
members = [
"templates/new-plugin",
]

[package]
authors = ["강동윤 <[email protected]>"]
description = "Command line for writting swc plugins in rust"
documentation = "https://rustdoc.swc.rs/swc_plugin/"
edition = "2018"
license = "Apache-2.0/MIT"
name = "swd"
repository = "https://github.com/swc-project/swc.git"
version = "0.0.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ahash = "0.7.5"
anyhow = "1.0.41"
cached = "0.25.0"
cargo-edit = "0.8.0"
cargo_metadata = "0.14.0"
clap = "2.33.3"
indexmap = {version = "1.7.0", features = ["rayon"]}
once_cell = "1.8.0"
rayon = "1.5.1"
serde = {version = "1.0.130", features = ["derive"]}
serde_json = "1.0.68"
structopt = "0.3.21"
swc_node_arch = {path = "./arch/"}
swc_node_base = "0.4.0"
tracing = "0.1.26"
tracing-subscriber = "0.2.20"
url = "2"
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# swd
# swd

# Usage

```sh
# Dependency
cargo install cargo-edit
cargo install swd
```

## Managing plugins

```
swc-dev plugin --help
```
16 changes: 16 additions & 0 deletions arch/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
authors = ["강동윤 <[email protected]>"]
description = "Node js architectures to rust target triple"
documentation = "https://rustdoc.swc.rs/swc_node_arch/"
edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_node_arch"
repository = "https://github.com/swc-project/swc.git"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"
serde = {version = "1", features = ["derive"]}
string_enum = "0.3.1"
155 changes: 155 additions & 0 deletions arch/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use anyhow::{bail, Context, Error};
use std::{fmt::Display, str::FromStr};
use string_enum::StringEnum;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, StringEnum)]
pub enum NodeArch {
/// `arm`
Arm,

/// `arm64`
Arm64,

/// `ia32`
Ia32,

/// `mips`
Mips,

/// `mipsel`
MipSel,

/// `ppc`
Ppc,

/// `ppc64`
Ppc64,

/// `s390`
S390,

/// `s390x`
S390x,

/// `x32`
X32,

/// `x64`
X64,
}

impl NodeArch {
pub fn from_cpu(cpu: &str) -> Result<Self, Error> {
match cpu {
"x86_64" => Ok(NodeArch::X64),
"aarch64" => Ok(NodeArch::Arm64),
"i686" => Ok(NodeArch::Ia32),
"armv7" => Ok(NodeArch::Arm),
_ => {
let s = cpu.parse();

match s {
Ok(v) => Ok(v),
Err(_) => bail!("failed to parse node arch from cpu `{}`", cpu),
}
}
}
}
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, StringEnum)]
pub enum NodePlatform {
/// `linux`
Linux,
/// `freebsd`
Freebsd,
/// `darwin`
Darwin,
/// `win32`
Windows,
/// `android`
Android,
}

impl NodePlatform {
pub fn from_sys(s: &str) -> Result<Self, Error> {
match s {
"linux" => Ok(NodePlatform::Linux),
"freebsd" => Ok(NodePlatform::Freebsd),
"darwin" => Ok(NodePlatform::Darwin),
"windows" => Ok(NodePlatform::Windows),

_ => s
.parse()
.ok()
.ok_or_else(|| anyhow::anyhow!("failed to parse `{}` as NodePlatform", s)),
}
}

/// Get extension for a dynamic library in this platform.
///
/// This include a leading dot.
pub fn cdylib_ext(self) -> &'static str {
match self {
NodePlatform::Android | NodePlatform::Linux | NodePlatform::Freebsd => ".so",
NodePlatform::Darwin => ".dylib",
NodePlatform::Windows => ".dll",
}
}
}

//// https://github.com/napi-rs/napi-rs/blob/main/cli/src/parse-triple.ts
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PlatformDetail {
pub platform: NodePlatform,
pub platform_arch_abi: String,
pub arch: NodeArch,

pub raw: String,
pub abi: Option<String>,
}

//// https://github.com/napi-rs/napi-rs/blob/7e7b4f0a82da0be2a6288c3b88e3456568284714/cli/src/parse-triple.ts#L73
impl FromStr for PlatformDetail {
type Err = Error;

fn from_str(raw: &str) -> Result<Self, Self::Err> {
(|| -> Result<_, Error> {
let triples = raw.split('-').collect::<Vec<_>>();

let (platform, arch, abi) = match &*triples {
[cpu, _, sys, abi] => (
NodePlatform::from_sys(sys)?,
NodeArch::from_cpu(cpu)?,
Some(abi.to_string()),
),
[cpu, _, sys] => (NodePlatform::from_sys(sys)?, NodeArch::from_cpu(cpu)?, None),
[cpu, sys, ..] => (NodePlatform::from_sys(sys)?, NodeArch::from_cpu(cpu)?, None),
_ => {
bail!("invalid format")
}
};

let platform_arch_abi = if let Some(abi) = &abi {
format!("{}-{}-{}", platform, arch, abi)
} else {
format!("{}-{}", platform, arch)
};

Ok(PlatformDetail {
platform,
platform_arch_abi,
arch,
raw: raw.to_string(),
abi,
})
})()
.with_context(|| format!("failed to parse `{}` as platform detail", raw))
}
}

impl Display for PlatformDetail {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.platform_arch_abi, f)
}
}
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2021-09-30
8 changes: 8 additions & 0 deletions scripts/invoke.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -eu

cargo install --path . --debug
export RUST_LOG=debug

(cd plugins/packages/jest && swd $@)
41 changes: 41 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
extern crate swc_node_base;

use anyhow::Error;
use plugin::PluginCommand;
use std::time::Instant;
use structopt::StructOpt;
use tracing::info;
use tracing_subscriber::EnvFilter;

mod plugin;
mod util;

#[derive(Debug, StructOpt)]

pub enum Cmd {
Plugin(PluginCommand),
}

fn main() -> Result<(), Error> {
let logger = tracing_subscriber::FmtSubscriber::builder()
.without_time()
.with_target(false)
.with_ansi(true)
.with_env_filter(EnvFilter::from_default_env())
.pretty()
.finish();

tracing::subscriber::set_global_default(logger)?;

let cmd = Cmd::from_args();

let start = Instant::now();
match cmd {
Cmd::Plugin(cmd) => {
cmd.run()?;
}
}
info!("Done in {:?}", start.elapsed());

Ok(())
}
Loading

0 comments on commit 31f523b

Please sign in to comment.