Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI initial concept #304

Open
wants to merge 49 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
4570d9e
CLI initial concept
escritorio-gustavo Apr 9, 2024
7c15180
Add --add-features flag, fix feature! macro and bson-uuid-impl
escritorio-gustavo Apr 9, 2024
1518c18
Remove *-impl features from CLI
escritorio-gustavo Apr 10, 2024
257d04e
Gate test generation behind cargo feature
escritorio-gustavo Apr 10, 2024
27669f1
Update Github action
escritorio-gustavo Apr 10, 2024
ca3eb23
Make the --output-directory flag an Option instead of having a defaul…
escritorio-gustavo Apr 10, 2024
7914bea
Initial draft on generating index.ts
escritorio-gustavo Apr 12, 2024
58df89d
Add missing file
escritorio-gustavo Apr 12, 2024
67f9631
Update Github action
escritorio-gustavo Apr 12, 2024
f092001
Check for empty index and emit codegen warning
escritorio-gustavo Apr 12, 2024
7192a8b
Save relative paths in netadata file
escritorio-gustavo Apr 12, 2024
2b09065
Remove unused import
escritorio-gustavo Apr 12, 2024
d18bf79
Remove unused read file access
escritorio-gustavo Apr 12, 2024
af0d6d3
Add naming collision warning
escritorio-gustavo Apr 12, 2024
41cf034
Add imports
escritorio-gustavo Apr 12, 2024
2ea928f
MAke warnings colorful
escritorio-gustavo Apr 12, 2024
9557f9f
Also highlight type names
escritorio-gustavo Apr 12, 2024
4ec1679
Make type names bold
escritorio-gustavo Apr 12, 2024
9c45249
Remove commented dependency
escritorio-gustavo Apr 12, 2024
98b0c01
Print Rust type name in collision warning
escritorio-gustavo Apr 12, 2024
b80f988
Capture cargo test stderr and stdout
escritorio-gustavo Apr 12, 2024
363f7c8
Refactor CLI code
escritorio-gustavo Apr 12, 2024
7894b4b
Commit untracked files
escritorio-gustavo Apr 12, 2024
65524d5
Export all files, not just the first metadata entry
escritorio-gustavo Apr 12, 2024
7560098
Delete metadata file
escritorio-gustavo Apr 12, 2024
770d6ea
Add metadata file to .gitignore
escritorio-gustavo Apr 12, 2024
e52085c
Enable clippy pedantic and nursery
escritorio-gustavo Apr 12, 2024
275b15d
Merge branch 'main' into cli
escritorio-gustavo Apr 15, 2024
b402bdc
Merge branch 'main' into cli
escritorio-gustavo Apr 15, 2024
b1b5cbe
Clippy warning in test
escritorio-gustavo Apr 17, 2024
6790173
Remove duplicated infer_as test file
escritorio-gustavo Apr 17, 2024
50bfc35
Add error message about naming collisions
escritorio-gustavo Apr 17, 2024
88fc140
Gate metadata file behind feature
escritorio-gustavo Apr 17, 2024
66c94dd
No longer capture stderr as that would silence serde warnings
escritorio-gustavo Apr 17, 2024
1461d3f
Remove changes to infer_as test
escritorio-gustavo Apr 17, 2024
b6c5c65
Make --output-directory required
escritorio-gustavo Apr 17, 2024
c8a6b14
Make casing consistent
escritorio-gustavo Apr 17, 2024
fcaa80f
Merge branch 'main' into cli
escritorio-gustavo Apr 18, 2024
c930c0f
Avoid extra collection allocation
escritorio-gustavo Apr 22, 2024
9b19079
Improve parsing of Metadata
escritorio-gustavo Apr 23, 2024
8ff6688
Replace new with try_from for clarity
escritorio-gustavo Apr 23, 2024
f229f92
Add option to export all types to a single file
escritorio-gustavo May 9, 2024
c9b29aa
Missing imports
escritorio-gustavo May 9, 2024
89430eb
Fix Syntax Error
escritorio-gustavo May 9, 2024
147ea87
Merge branch 'main' into cli
escritorio-gustavo May 20, 2024
00f14af
Merge branch 'main' into cli
gustavo-shigueo Jul 24, 2024
7c9643e
Merge branch 'main' into cli
gustavo-shigueo Aug 1, 2024
5d72d97
Merge branch 'main' into cli
gustavo-shigueo Oct 10, 2024
5abdda9
Fix lockfile
gustavo-shigueo Oct 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"

[dependencies]
clap = { version = "4", features = ["derive"]}
color-eyre = "0.6"
56 changes: 46 additions & 10 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::path::PathBuf;
use std::{io::Write, path::PathBuf};

use clap::Parser;
use color_eyre::Result;

mod path;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to duplicate ts-rs/src/export/path.rs to make absolute paths

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make a ts-rs-core crate to share these types of functions?

#[derive(Parser, Debug)]
struct Args {
/// Defines where your TS bindings will be saved by setting TS_RS_EXPORT_DIR
#[arg(long, short)]
output_directory: Option<PathBuf>,
#[arg(long, short, default_value = "./bindings")]
output_directory: PathBuf,

escritorio-gustavo marked this conversation as resolved.
Show resolved Hide resolved
/// Disables warnings caused by using serde attributes that ts-rs cannot process
#[arg(long)]
Expand All @@ -19,6 +22,9 @@ struct Args {
/// Formats the generated TypeScript files
#[arg(long)]
format: bool,

#[arg(long = "index")]
generate_index_ts: bool,
}

macro_rules! feature {
Expand All @@ -33,26 +39,56 @@ macro_rules! feature {
};
}

fn main() {
fn main() -> Result<()> {
color_eyre::install()?;

let args = Args::parse();

let mut cargo_invocation = std::process::Command::new("cargo");

let metadata_path = args.output_directory.join("ts_rs.meta");
if metadata_path.exists() {
std::fs::remove_file(&metadata_path)?;
}

NyxCode marked this conversation as resolved.
Show resolved Hide resolved
cargo_invocation
.arg("test")
.arg("export_bindings_")
.arg("--features")
.arg("ts-rs/export");

if let Some(ref output_directory) = args.output_directory {
cargo_invocation.env("TS_RS_EXPORT_DIR", output_directory);
}
.arg("ts-rs/export")
.env("TS_RS_EXPORT_DIR", path::absolute(&args.output_directory)?);

feature!(cargo_invocation, args, {
no_warnings => "no-serde-warnings",
esm_imports => "import-esm",
format => "format",
});

cargo_invocation.spawn().unwrap().wait().unwrap();
cargo_invocation.spawn()?.wait()?;

if args.generate_index_ts {
let metadata = std::fs::read_to_string(&metadata_path)?
.lines()
.map(ToOwned::to_owned)
.collect::<std::collections::HashSet<_>>();

let index_path = args.output_directory.join("index.ts");

if index_path.exists() {
std::fs::remove_file(&index_path)?;
}

NyxCode marked this conversation as resolved.
Show resolved Hide resolved
let mut index = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(index_path)?;

for file in metadata.iter() {
index.write_fmt(format_args!("export * from {file:?};\n"))?;
}
}

std::fs::remove_file(&metadata_path)?;

Ok(())
}
15 changes: 14 additions & 1 deletion ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,27 @@ pub(crate) fn export_to<T: TS + ?Sized + 'static, P: AsRef<Path>>(
if let Some(parent) = path.as_ref().parent() {
std::fs::create_dir_all(parent)?;
}

let lock = FILE_LOCK.lock().unwrap();
{
// Manually write to file & call `sync_data`. Otherwise, calling `fs::read(path)`
// immediately after `T::export()` might result in an empty file.
use std::io::Write;
let mut file = File::create(path)?;
let mut file = File::create(&path)?;
file.write_all(buffer.as_bytes())?;
file.sync_data()?;

let bytes = path::absolute(path.as_ref())?
.to_string_lossy()
.bytes()
.chain([b'\n'])
.collect::<Box<[_]>>();
std::fs::OpenOptions::new()
.read(true)
.append(true)
.create(true)
.open(default_out_dir().join("ts_rs.meta"))?
.write_all(&bytes)?;
escritorio-gustavo marked this conversation as resolved.
Show resolved Hide resolved
}

drop(lock);
Expand Down
Loading