-
Notifications
You must be signed in to change notification settings - Fork 111
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
escritorio-gustavo
wants to merge
49
commits into
main
Choose a base branch
from
cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
CLI initial concept #304
Changes from 7 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
4570d9e
CLI initial concept
escritorio-gustavo 7c15180
Add --add-features flag, fix feature! macro and bson-uuid-impl
escritorio-gustavo 1518c18
Remove *-impl features from CLI
escritorio-gustavo 257d04e
Gate test generation behind cargo feature
escritorio-gustavo 27669f1
Update Github action
escritorio-gustavo ca3eb23
Make the --output-directory flag an Option instead of having a defaul…
escritorio-gustavo 7914bea
Initial draft on generating index.ts
escritorio-gustavo 58df89d
Add missing file
escritorio-gustavo 67f9631
Update Github action
escritorio-gustavo f092001
Check for empty index and emit codegen warning
escritorio-gustavo 7192a8b
Save relative paths in netadata file
escritorio-gustavo 2b09065
Remove unused import
escritorio-gustavo d18bf79
Remove unused read file access
escritorio-gustavo af0d6d3
Add naming collision warning
escritorio-gustavo 41cf034
Add imports
escritorio-gustavo 2ea928f
MAke warnings colorful
escritorio-gustavo 9557f9f
Also highlight type names
escritorio-gustavo 4ec1679
Make type names bold
escritorio-gustavo 9c45249
Remove commented dependency
escritorio-gustavo 98b0c01
Print Rust type name in collision warning
escritorio-gustavo b80f988
Capture cargo test stderr and stdout
escritorio-gustavo 363f7c8
Refactor CLI code
escritorio-gustavo 7894b4b
Commit untracked files
escritorio-gustavo 65524d5
Export all files, not just the first metadata entry
escritorio-gustavo 7560098
Delete metadata file
escritorio-gustavo 770d6ea
Add metadata file to .gitignore
escritorio-gustavo e52085c
Enable clippy pedantic and nursery
escritorio-gustavo 275b15d
Merge branch 'main' into cli
escritorio-gustavo b402bdc
Merge branch 'main' into cli
escritorio-gustavo b1b5cbe
Clippy warning in test
escritorio-gustavo 6790173
Remove duplicated infer_as test file
escritorio-gustavo 50bfc35
Add error message about naming collisions
escritorio-gustavo 88fc140
Gate metadata file behind feature
escritorio-gustavo 66c94dd
No longer capture stderr as that would silence serde warnings
escritorio-gustavo 1461d3f
Remove changes to infer_as test
escritorio-gustavo b6c5c65
Make --output-directory required
escritorio-gustavo c8a6b14
Make casing consistent
escritorio-gustavo fcaa80f
Merge branch 'main' into cli
escritorio-gustavo c930c0f
Avoid extra collection allocation
escritorio-gustavo 9b19079
Improve parsing of Metadata
escritorio-gustavo 8ff6688
Replace new with try_from for clarity
escritorio-gustavo f229f92
Add option to export all types to a single file
escritorio-gustavo c9b29aa
Missing imports
escritorio-gustavo 89430eb
Fix Syntax Error
escritorio-gustavo 147ea87
Merge branch 'main' into cli
escritorio-gustavo 00f14af
Merge branch 'main' into cli
gustavo-shigueo 7c9643e
Merge branch 'main' into cli
gustavo-shigueo 5d72d97
Merge branch 'main' into cli
gustavo-shigueo 5abdda9
Fix lockfile
gustavo-shigueo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
members = ["macros", "ts-rs", "example"] | ||
members = ["macros", "ts-rs", "example", "cli"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "cargo-ts" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
clap = { version = "4", features = ["derive"]} | ||
color-eyre = "0.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::{io::Write, path::PathBuf}; | ||
|
||
use clap::Parser; | ||
use color_eyre::Result; | ||
|
||
mod path; | ||
|
||
#[derive(Parser, Debug)] | ||
struct Args { | ||
/// Defines where your TS bindings will be saved by setting TS_RS_EXPORT_DIR | ||
#[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)] | ||
no_warnings: bool, | ||
|
||
/// Adds the ".js" extension to import paths | ||
#[arg(long)] | ||
esm_imports: bool, | ||
|
||
/// Formats the generated TypeScript files | ||
#[arg(long)] | ||
format: bool, | ||
|
||
#[arg(long = "index")] | ||
generate_index_ts: bool, | ||
} | ||
|
||
macro_rules! feature { | ||
($cargo_invocation: expr, $args: expr, { $($field: ident => $feature: literal),* $(,)? }) => { | ||
$( | ||
if $args.$field { | ||
$cargo_invocation | ||
.arg("--features") | ||
.arg(format!("ts-rs/{}", $feature)); | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
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") | ||
.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()?.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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 pathsThere was a problem hiding this comment.
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?