Skip to content

Commit d272808

Browse files
authored
feat: Improve new program generation via cargo sails new (#1039)
1 parent aa5b63c commit d272808

File tree

8 files changed

+120
-31
lines changed

8 files changed

+120
-31
lines changed

rs/cli/src/program_new.rs

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,27 @@ use std::{
1313
const SAILS_VERSION: &str = env!("CARGO_PKG_VERSION");
1414

1515
#[derive(Template)]
16-
#[template(path = "app/build.askama")]
17-
struct AppBuild;
16+
#[template(path = "build.askama")]
17+
struct RootBuild {
18+
app_crate_name: String,
19+
program_struct_name: String,
20+
}
21+
22+
#[derive(Template)]
23+
#[template(path = "src/lib.askama")]
24+
struct RootLib {
25+
app_crate_name: String,
26+
}
27+
28+
#[derive(Template)]
29+
#[template(path = "readme.askama")]
30+
struct Readme {
31+
program_crate_name: String,
32+
app_crate_name: String,
33+
client_crate_name: String,
34+
service_name: String,
35+
app: bool,
36+
}
1837

1938
#[derive(Template)]
2039
#[template(path = "app/src/lib.askama")]
@@ -27,7 +46,7 @@ struct AppLib {
2746
#[derive(Template)]
2847
#[template(path = "client/build.askama")]
2948
struct ClientBuild {
30-
program_crate_name: String,
49+
app_crate_name: String,
3150
program_struct_name: String,
3251
}
3352

@@ -81,8 +100,27 @@ impl ProgramGenerator {
81100
}
82101
}
83102

84-
fn app_build(&self) -> AppBuild {
85-
AppBuild
103+
fn root_build(&self) -> RootBuild {
104+
RootBuild {
105+
app_crate_name: self.app_name().to_case(Case::Snake),
106+
program_struct_name: "Program".to_string(),
107+
}
108+
}
109+
110+
fn root_lib(&self) -> RootLib {
111+
RootLib {
112+
app_crate_name: self.app_name().to_case(Case::Snake),
113+
}
114+
}
115+
116+
fn root_readme(&self) -> Readme {
117+
Readme {
118+
program_crate_name: self.package_name.to_owned(),
119+
app_crate_name: self.app_name(),
120+
client_crate_name: self.client_name(),
121+
service_name: "Service".to_string(),
122+
app: self.app,
123+
}
86124
}
87125

88126
fn app_lib(&self) -> AppLib {
@@ -95,7 +133,7 @@ impl ProgramGenerator {
95133

96134
fn client_build(&self) -> ClientBuild {
97135
ClientBuild {
98-
program_crate_name: self.app_name().to_case(Case::Snake),
136+
app_crate_name: self.app_name().to_case(Case::Snake),
99137
program_struct_name: "Program".to_string(),
100138
}
101139
}
@@ -108,7 +146,7 @@ impl ProgramGenerator {
108146

109147
fn tests_gtest(&self) -> TestsGtest {
110148
TestsGtest {
111-
program_crate_name: self.app_name().to_case(Case::Snake),
149+
program_crate_name: self.package_name.to_case(Case::Snake),
112150
client_crate_name: self.client_name().to_case(Case::Snake),
113151
client_program_name: self.client_name().to_case(Case::Pascal),
114152
service_name: "Service".to_string(),
@@ -172,6 +210,7 @@ impl ProgramGenerator {
172210
self.generate_root()?;
173211
self.generate_app()?;
174212
self.generate_client()?;
213+
self.generate_build()?;
175214
self.generate_tests()?;
176215
}
177216
self.fmt()?;
@@ -185,10 +224,6 @@ impl ProgramGenerator {
185224

186225
// add sails-rs refs
187226
self.cargo_add_sails_rs(manifest_path, Normal, None)?;
188-
self.cargo_add_sails_rs(manifest_path, Build, Some("wasm-builder"))?;
189-
190-
let mut build_rs = File::create(build_rs_path(path))?;
191-
self.app_build().write_into(&mut build_rs)?;
192227

193228
let mut lib_rs = File::create(lib_rs_path(path))?;
194229
self.app_lib().write_into(&mut lib_rs)?;
@@ -200,7 +235,31 @@ impl ProgramGenerator {
200235
let path = &self.path;
201236
cargo_new(path, &self.package_name, self.offline)?;
202237

203-
cargo_toml_create_workspace(manifest_path(path))?;
238+
let manifest_path = &manifest_path(path);
239+
cargo_toml_create_workspace(manifest_path)?;
240+
241+
let mut readme_md = File::create(readme_path(path))?;
242+
self.root_readme().write_into(&mut readme_md)?;
243+
244+
Ok(())
245+
}
246+
247+
fn generate_build(&self) -> anyhow::Result<()> {
248+
let path = &self.path;
249+
let manifest_path = &manifest_path(path);
250+
251+
let mut lib_rs = File::create(lib_rs_path(path))?;
252+
self.root_lib().write_into(&mut lib_rs)?;
253+
254+
let mut build_rs = File::create(build_rs_path(path))?;
255+
self.root_build().write_into(&mut build_rs)?;
256+
257+
// add app ref
258+
cargo_add_by_path(manifest_path, self.app_path(), Normal, None, self.offline)?;
259+
cargo_add_by_path(manifest_path, self.app_path(), Build, None, self.offline)?;
260+
// add sails-rs refs
261+
self.cargo_add_sails_rs(manifest_path, Normal, None)?;
262+
self.cargo_add_sails_rs(manifest_path, Build, Some("build"))?;
204263

205264
Ok(())
206265
}
@@ -258,9 +317,6 @@ impl ProgramGenerator {
258317
self.offline,
259318
)?;
260319

261-
// delete ./src folder
262-
fs::remove_dir_all(src_path(path))?;
263-
264320
// add tests
265321
let test_path = &tests_path(path);
266322
fs::create_dir_all(test_path.parent().context("Parent should exists")?)?;
@@ -413,10 +469,6 @@ fn build_rs_path<P: AsRef<Path>>(path: P) -> PathBuf {
413469
path.as_ref().join("build.rs")
414470
}
415471

416-
fn src_path<P: AsRef<Path>>(path: P) -> PathBuf {
417-
path.as_ref().join("src")
418-
}
419-
420472
fn lib_rs_path<P: AsRef<Path>>(path: P) -> PathBuf {
421473
path.as_ref().join("src/lib.rs")
422474
}
@@ -425,6 +477,10 @@ fn tests_path<P: AsRef<Path>>(path: P) -> PathBuf {
425477
path.as_ref().join("tests/gtest.rs")
426478
}
427479

480+
fn readme_path<P: AsRef<Path>>(path: P) -> PathBuf {
481+
path.as_ref().join("README.md")
482+
}
483+
428484
fn cargo_command() -> String {
429485
std::env::var("CARGO").unwrap_or("cargo".into())
430486
}

rs/cli/templates/app/build.askama

Lines changed: 0 additions & 3 deletions
This file was deleted.

rs/cli/templates/app/src/lib.askama

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,3 @@ impl {{ program_struct_name }} {
3434
{{ service_name }}::new()
3535
}
3636
}
37-
38-
#[cfg(not(target_arch = "wasm32"))]
39-
pub use code::WASM_BINARY_OPT as WASM_BINARY;
40-
41-
#[cfg(not(target_arch = "wasm32"))]
42-
mod code {
43-
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
44-
}

rs/cli/templates/build.askama

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
if let Some((_, wasm_path)) = sails_rs::build_wasm() {
3+
sails_rs::ClientBuilder::<{{ app_crate_name }}::{{ program_struct_name }}>::from_wasm_path(wasm_path.with_extension("")).build_idl();
4+
}
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
sails_rs::build_client::<{{ program_crate_name }}::{{ program_struct_name }}>();
2+
sails_rs::build_client::<{{ app_crate_name }}::{{ program_struct_name }}>();
33
}

rs/cli/templates/readme.askama

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## The **{{ program_crate_name }}** program
2+
3+
The program workspace includes the following packages:{% if app %}
4+
- `{{ program_crate_name }}` is the package containing business logic for the program represented by the `{{ service_name }}` structure.
5+
{% else %}
6+
- `{{ program_crate_name }}` is the package allowing to build WASM binary for the program and IDL file for it.
7+
The package also includes integration tests for the program in the `tests` sub-folder
8+
- `{{ app_crate_name }}` is the package containing business logic for the program represented by the `{{ service_name }}` structure.
9+
- `{{ client_crate_name }}` is the package containing the client for the program allowing to interact with it from another program, tests, or off-chain client.
10+
{% endif %}

rs/cli/templates/src/lib.askama

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![no_std]
2+
3+
#[cfg(target_arch = "wasm32")]
4+
pub use {{ app_crate_name }}::wasm::*;
5+
6+
#[cfg(not(target_arch = "wasm32"))]
7+
pub use code::WASM_BINARY_OPT as WASM_BINARY;
8+
9+
#[cfg(not(target_arch = "wasm32"))]
10+
mod code {
11+
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
12+
}

rs/src/builder.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ impl<P: ProgramMeta> ClientBuilder<P> {
8787
}
8888
}
8989

90+
pub fn from_wasm_path<T: AsRef<Path>>(path: T) -> Self {
91+
let path = path.as_ref();
92+
let program_name = path
93+
.file_name()
94+
.expect("Invalid path to wasm")
95+
.to_string_lossy()
96+
.to_string();
97+
98+
Self {
99+
idl_path: Some(path.with_extension("idl")),
100+
client_path: Some(path.with_extension("rs")),
101+
program_name,
102+
no_std: false,
103+
_marker: Default::default(),
104+
}
105+
}
106+
90107
pub fn empty(program_name: String) -> Self {
91108
Self {
92109
idl_path: None,

0 commit comments

Comments
 (0)