Skip to content

Commit 3824b86

Browse files
committed
Automatically include copyright headers in generated files
1 parent 54f6dd2 commit 3824b86

2 files changed

Lines changed: 22 additions & 25 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,9 @@ cargo clippy --all-features -- -D warnings -A clippy::drop_non_drop # Lint (CI
4141

4242
```bash
4343
RUSTFLAGS="--cfg genproto" cargo build -p ldk-server-protos
44+
cargo fmt --all
4445
```
4546

46-
After regenerating, you must manually readd the copyright header to each generated file in `ldk-server-protos/src/` (
47-
`api.rs`, `types.rs`, `events.rs`, `error.rs`):
48-
49-
```
50-
// This file is Copyright its original authors, visible in version control
51-
// history.
52-
//
53-
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
54-
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
55-
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
56-
// You may not use this file except in accordance with one or both of these
57-
// licenses.
58-
```
59-
60-
Then run `cargo fmt --all` to format the generated code.
61-
6247
## Adding a New API Endpoint
6348

6449
1. Define request/response messages in `ldk-server-protos/src/proto/api.proto`

ldk-server-protos/build.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ extern crate prost_build;
1313
#[cfg(genproto)]
1414
use std::{env, fs, path::Path};
1515

16+
#[cfg(genproto)]
17+
const COPYRIGHT_HEADER: &str =
18+
"// This file is Copyright its original authors, visible in version control
19+
// history.
20+
//
21+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
22+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
23+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
24+
// You may not use this file except in accordance with one or both of these
25+
// licenses.
26+
27+
";
28+
1629
/// To generate updated proto objects, run `RUSTFLAGS="--cfg genproto" cargo build`
1730
fn main() {
1831
#[cfg(genproto)]
@@ -38,13 +51,12 @@ fn generate_protos() {
3851
&["src/proto/"],
3952
)
4053
.expect("protobuf compilation failed");
41-
println!("OUT_DIR: {}", &env::var("OUT_DIR").unwrap());
42-
let from_path = Path::new(&env::var("OUT_DIR").unwrap()).join("api.rs");
43-
fs::copy(from_path, "src/api.rs").unwrap();
44-
let from_path = Path::new(&env::var("OUT_DIR").unwrap()).join("types.rs");
45-
fs::copy(from_path, "src/types.rs").unwrap();
46-
let from_path = Path::new(&env::var("OUT_DIR").unwrap()).join("events.rs");
47-
fs::copy(from_path, "src/events.rs").unwrap();
48-
let from_path = Path::new(&env::var("OUT_DIR").unwrap()).join("error.rs");
49-
fs::copy(from_path, "src/error.rs").unwrap();
54+
let out_dir = env::var("OUT_DIR").unwrap();
55+
println!("OUT_DIR: {}", &out_dir);
56+
for file in &["api.rs", "types.rs", "events.rs", "error.rs"] {
57+
let from_path = Path::new(&out_dir).join(file);
58+
let content = fs::read_to_string(&from_path).unwrap();
59+
let with_header = format!("{}{}", COPYRIGHT_HEADER, content);
60+
fs::write(Path::new("src").join(file), with_header).unwrap();
61+
}
5062
}

0 commit comments

Comments
 (0)