-
Notifications
You must be signed in to change notification settings - Fork 6
Add cargo-spatial-codegen tool #49
Add cargo-spatial-codegen tool #49
Conversation
Looks like the travis build failed, but I'm unclear on what the error is? It seems like the environment variable |
Ah that's because the PR is coming from a fork and the |
Looks like I'll have to manually run the CI suite locally and override the failing check when we go to merge. Will take a look at the code 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.
I like the change from sh
-> cargo
command as its very idiomatic and one less language/complexity to worry about.
However, I feel that such a tool would be better placed as a standalone binary in spatialos-sdk-tools
since it can be made generic for all projects (given a set of input schema directories and an output directory).
Conflating the worker binary and the setup for the worker binary feels a little strange at first glance.
If you are up for it - I'd love it if you could refactor project-example/setup.rs
into a standalone binary in the spatialos-sdk-tools
crate.
In the future, it would probably be wrapped into a codegen
tool which would compile the schema into the descriptor as well as generate Rust code.
I agree that splitting the setup script into its own is a good idea. I've been trying to setup my own project based on the example in this repo, and I'm finding that I need this setup script in order to compile the schema files. Splitting it off means that I can install it on its own, rather than having to copy-paste the script into my project. |
* Revert changes to argument_parsing.rs and update project-example. * Update README.md with new instructions for running setup. * Tweak logic in setup to not be hard coded for the project-example structure.
btw I'm marking this as WIP, I want to do a cleanup pass and make sure errors are being handled properly before this gets merged. |
I've cleaned up the error handling logic (i.e. not just panicking on every error) and removed the WIP marker. |
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.
One small nit - but mostly LGTM
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.
Nice! LGTM - will run the code through "CI" later this evening 👍
So running the command listed in the adjusted README gives me the following: λ cargo run --bin setup -- spatialos-sdk/examples/project-example/spatialos/schema
Compiling spatialos-sdk-tools v0.0.0 (C:\Workspace\personal\spatialos-sdk-rs\spatialos-sdk-tools)
Finished dev [unoptimized + debuginfo] target(s) in 1.36s
Running `target\debug\setup.exe spatialos-sdk/examples/project-example/spatialos/schema`
C:/Workspace\personal\spatialos-sdk-rs\dependencies\std-lib\improbable\standard_library.schema: Input file is not contained in any --schema_path. One --schema_path argument must be an exact prefix of the input file.
C:/Workspace\personal\spatialos-sdk-rs\dependencies\std-lib\improbable\vector3.schema: Input file is not contained in any --schema_path. One --schema_path argument must be an exact prefix of the input file. Which looks like an error from the I think you've omitted the EDIT: Actually you haven't. Not sure what's happening here 🤔 |
Hmmmmm, I was running into an issue similar to this before. It seems like
In that example You can actually see what is (I think) the cause in the example you posted:
The first separator is Ultimately, this is an issue with |
I was able to reproduce the issue by using the As an aside, there's a tracking issue for adding path normalization logic to std, but it hasn't landed on stable yet 😭 |
Generates the bundle.json file needed as part of code generation in #6.
@jamiebrynes7 @davedissian I've updated this to add support for codegen in the setup script. I've also deleted the |
@@ -0,0 +1,2 @@ | |||
# Generated bindings to SpatialOS components, shouldn't be committed. | |||
generated_code.rs |
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.
There's an argument that while codegen is under heavy development, having some example code checked in makes it a lot easier to review rather than t4 templates
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.
I think that's a fair point, though I think it would make more sense to have small, controlled examples as part of the spatialos-sdk-code-generator crate, rather than using the example project for that purpose. That said, I'm happy to restore generated_code.rs
if you'd prefer 🙂
let descriptor_out_arg = OsString::from("--descriptor_set_out=") | ||
.tap(|arg| arg.push(normalize(output_dir.join("schema.descriptor")))); | ||
let mut command = Command::new(&schema_compiler_path); | ||
command |
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.
Can combine the above line with this block:
let mut command = Command::new(&schema_compiler_path)
.arg(...)
...
.arg(...);
Also a line of whitespace between the last path constructor and the command declaration would help with readability :)
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.
Unfortunately, I don't think I can combine these. On line 87 I conditionally add arguments to command
(in a loop), which means I need the command bound to a variable. But if I try to bind the return value of arg()
to a variable, I'll get a compiler error, e.g.
let mut command = Command::new(...)
.arg(...)
.arg(...);
gives the error:
error[E0716]: temporary value dropped while borrowed
--> spatialos-sdk-tools\src/codegen/main.rs:79:23
|
79 | let mut command = Command::new(&schema_compiler_path)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
...
84 | .arg("--load_all_schema_on_schema_path");
| - temporary value is freed at the end of this statement
...
88 | command.arg(&arg);
| ------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
The only way to make this work is to bind the result of Command::new()
to a variable, then call arg()
on it in a chain.
As an aside, this is why I use the tap crate and avoid writing methods that return Self
. There's no really good way to do method chaining by returning Self
in Rust, and tap is the only robust solution I've found.
/// | ||
/// If not specified, will not perform code generation. | ||
#[structopt(long, short = "c", parse(from_os_str))] | ||
codegen: Option<PathBuf>, |
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.
Maybe codegen_out_path
?
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.
Hrmmm, so I originally had it as something like that, but changed it to --codegen
because I wanted it to more clearly indicate that specifying that flag was also enabling codegen. I can change it back to --codegen-out-path
if you think that's better, I don't have super strong feelings about it either way. Though, if you think --codegen
is fine as the CLI flag and you just want the variable to be codegen_out_path
, I can also do that 😁
|
||
/// The path the output directory for the project | ||
#[structopt(long, short, parse(from_os_str))] | ||
output_dir: PathBuf, |
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.
Maybe descriptor_out_dir
?
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.
output_dir
is currently used for both schema.descriptor
and bundle.json
. Does it make sense to call it descriptor_out_dir
when it's used for both? Or should those two be split into separate flags?
Do we even have to list all the schema files when invoking |
I'm going to close this PR in favor of working on a more robust |
This PR replaces the
setup.sh
helper script used in the project-example example with a newsetup
subcommand in the example application itself. This provides better cross-platform support for running the example, specifically it makes it easier to run the example on Windows.Additional details:
setup.rs
module in project-example that replicates the logic insetup.sh
.setup
subcommand inargument_parsing.rs
.main()
to handle the two different ways the example can be run (i.e. launching a worker or performing setup).Please let me know if there's anything I need to change!