Skip to content

Commit

Permalink
refactor(symlink): removed unecessary symlink (#16)
Browse files Browse the repository at this point in the history
* refactor(symlink): removed unecessary symlink

* fix(fmt) inserted '#[rustfmt::skip]' wrongly deleted before

* fix(fmt) fixed the formatting issue
  • Loading branch information
MatthewS8 authored Dec 3, 2024
1 parent 4d35a7c commit 25740d5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 56 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ You can then have a very simple `build.rs` as follows:
use flatbuffers_build::BuilderOptions;

BuilderOptions::new_with_files(["schemas/weapon.fbs", "schemas/example.fbs"])
.set_symlink_directory("src/gen_flatbuffers")
.compile()
.expect("flatbuffer compilation failed");
```
Expand All @@ -55,12 +54,14 @@ Note here that `weapon.fbs` and `example.fbs` are based on the schemas provided
`flatbuffers` as an example. The namespace is `MyGame.Sample` and it contains multiple tables
and structs, including a `Monster` table.

This will just compile the flatbuffers and drop them in `${OUT_DIR}/flatbuffers` and will
create a symlink under `src/gen_flatbuffers`. You can then use them in `lib.rs` like so:
This will just compile the flatbuffers and drop them in `${OUT_DIR}/flatbuffers`.
You can then use them in `lib.rs` like so:

```rust
#[allow(warnings)]
mod gen_flatbuffers;
mod gen_flatbuffers {
include!(concat!(env!("OUT_DIR"), "/flatbuffers/mod.rs"));
}

use gen_flatbuffers::my_game::sample::Monster;

Expand All @@ -69,8 +70,6 @@ fn some_fn() {
}
```

Note that since this will generate a symlink under `src/gen_flatbuffers`, you need to add this
file to your gitignore as this symlink will dynamically change at runtime.

## On file ordering

Expand Down
19 changes: 18 additions & 1 deletion flatbuffers-build-example/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flatbuffers-build-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ anyhow = "1"
flatbuffers = "=24.3.25"

[build-dependencies]
flatbuffers-build = { version = "0.1.2", path = "..", features = ["vendored"] }
flatbuffers-build = { version = "0.2.1", path = "..", features = ["vendored"] }
1 change: 0 additions & 1 deletion flatbuffers-build-example/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use flatbuffers_build::BuilderOptions;

fn main() {
BuilderOptions::new_with_files(["schemas/weapon.fbs", "schemas/example.fbs"])
.set_symlink_directory("src/gen_flatbuffers")
.compile()
.expect("flatbuffer compilation failed")
}
6 changes: 4 additions & 2 deletions flatbuffers-build-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#[allow(warnings)]
#[rustfmt::skip]
mod gen_flatbuffers;

mod gen_flatbuffers {
// for a cleaner code, you could do this in a separate module
include!(concat!(env!("OUT_DIR"), "/flatbuffers/mod.rs"));
}
use gen_flatbuffers::my_game::sample::{Monster, MonsterArgs, Vec3};

fn main() -> anyhow::Result<()> {
Expand Down
50 changes: 5 additions & 45 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
//! use flatbuffers_build::BuilderOptions;
//!
//! BuilderOptions::new_with_files(["schemas/weapon.fbs", "schemas/example.fbs"])
//! .set_symlink_directory("src/gen_flatbuffers")
//! .compile()
//! .expect("flatbuffer compilation failed");
//! ```
Expand All @@ -50,12 +49,14 @@
//! `flatbuffers` as an example. The namespace is `MyGame.Sample` and it contains multiple tables
//! and structs, including a `Monster` table.
//!
//! This will just compile the flatbuffers and drop them in `${OUT_DIR}/flatbuffers` and will
//! create a symlink under `src/gen_flatbuffers`. You can then use them in `lib.rs` like so:
//! This will just compile the flatbuffers and drop them in `${OUT_DIR}/flatbuffers`
//! You can then use them in `lib.rs` like so:
//!
//! ```rust,ignore
//! #[allow(warnings)]
//! mod gen_flatbuffers;
//! mod gen_flatbuffers {
//! include!(concat!(env!("OUT_DIR"), "/flatbuffers/mod.rs"));
//! }
//!
//! use gen_flatbuffers::my_game::sample::Monster;
//!
Expand All @@ -64,8 +65,6 @@
//! }
//! ```
//!
//! Note that since this will generate a symlink under `src/gen_flatbuffers`, you need to add this
//! file to your gitignore as this symlink will dynamically change at runtime.
//!
//! ## On file ordering
//!
Expand Down Expand Up @@ -121,11 +120,6 @@ pub enum Error {
"output directory was not set. Either call .set_output_path() or set the `OUT_DIR` env var"
)]
OutputDirNotSet,
/// Returned when an issue arrises when creating the symlink. Typically this will be things
/// like permissions, a directory existing already at the file location, or other filesystem
/// errors.
#[error("failed to create symlink path requested: {0}")]
SymlinkCreationFailure(#[source] std::io::Error),
}

/// Alias for a Result that uses [`Error`] as the default error type.
Expand Down Expand Up @@ -157,7 +151,6 @@ pub struct BuilderOptions {
files: Vec<PathBuf>,
compiler: Option<String>,
output_path: Option<PathBuf>,
symlink_path: Option<PathBuf>,
supress_buildrs_directives: bool,
}

Expand All @@ -184,7 +177,6 @@ impl BuilderOptions {
files: files.into_iter().map(|f| f.as_ref().into()).collect(),
compiler: None,
output_path: None,
symlink_path: None,
supress_buildrs_directives: false,
}
}
Expand Down Expand Up @@ -217,20 +209,6 @@ impl BuilderOptions {
}
}

/// Set a path to create a symlink that points to the output files. This is commonly used to
/// symlink to a folder under `src` so you can normally pull in the generated code as a module.
/// We recommend always calling this and setting it to `src/generated` or something similar.
///
/// # Arguments
/// * `symlink_path` - Path to generate the symlink to.
#[must_use]
pub fn set_symlink_directory<P: AsRef<Path>>(self, symlink_path: P) -> Self {
BuilderOptions {
symlink_path: Some(symlink_path.as_ref().into()),
..self
}
}

/// Set this if you're not running from a `build.rs` script and don't want us to print the
/// build.rs instructions/directives that we would otherwise print in stdout.
#[must_use]
Expand Down Expand Up @@ -292,13 +270,6 @@ fn compile(builder_options: BuilderOptions) -> Result {
args.extend(files_str);
run_flatc(&compiler, &args)?;

if let Some(symlink_path) = builder_options.symlink_path {
generate_symlink(&symlink_path, PathBuf::from(output_path))?;
if !builder_options.supress_buildrs_directives {
println!("cargo::rerun-if-changed={}", symlink_path.display());
}
}

if !builder_options.supress_buildrs_directives {
for file in builder_options.files {
println!("cargo::rerun-if-changed={}", file.display());
Expand All @@ -307,17 +278,6 @@ fn compile(builder_options: BuilderOptions) -> Result {
Ok(())
}

fn generate_symlink<P: AsRef<Path>, Q: AsRef<Path>>(symlink_path: P, output_path: Q) -> Result {
if symlink_path.as_ref().exists() {
std::fs::remove_file(&symlink_path).map_err(Error::SymlinkCreationFailure)?;
}
#[cfg(unix)]
std::os::unix::fs::symlink(output_path, symlink_path).map_err(Error::SymlinkCreationFailure)?;
#[cfg(windows)]
junction::create(output_path, symlink_path).map_err(Error::SymlinkCreationFailure)?;
Ok(())
}

fn confirm_flatc_version(compiler: &str) -> Result {
// Output shows up in stdout
let output = run_flatc(compiler, ["--version"])?;
Expand Down

0 comments on commit 25740d5

Please sign in to comment.