-
Notifications
You must be signed in to change notification settings - Fork 98
fix(generate): add missing typescript types and fix issues with bindings array in dfx.json #2616
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -118,16 +118,6 @@ pub trait CanisterBuilder { | |
| ) | ||
| })?; | ||
| } | ||
| std::fs::create_dir_all(&generate_output_dir).with_context(|| { | ||
| format!( | ||
| "Failed to create dir: {}", | ||
| generate_output_dir.to_string_lossy() | ||
| ) | ||
| })?; | ||
|
|
||
| let generated_idl_path = self.generate_idl(pool, info, config)?; | ||
|
|
||
| let (env, ty) = check_candid_file(generated_idl_path.as_path())?; | ||
|
|
||
| let bindings = info | ||
| .get_declarations_config() | ||
|
|
@@ -145,6 +135,17 @@ pub trait CanisterBuilder { | |
| ); | ||
| } | ||
|
|
||
| std::fs::create_dir_all(&generate_output_dir).with_context(|| { | ||
| format!( | ||
| "Failed to create dir: {}", | ||
| generate_output_dir.to_string_lossy() | ||
| ) | ||
| })?; | ||
|
|
||
| let generated_idl_path = self.generate_idl(pool, info, config)?; | ||
|
|
||
| let (env, ty) = check_candid_file(generated_idl_path.as_path())?; | ||
|
|
||
| // Typescript | ||
| if bindings.contains(&"ts".to_string()) { | ||
| let output_did_ts_path = generate_output_dir | ||
|
|
@@ -158,6 +159,8 @@ pub trait CanisterBuilder { | |
| ) | ||
| })?; | ||
| eprintln!(" {}", &output_did_ts_path.display()); | ||
|
|
||
| compile_handlebars_files("ts", info, generate_output_dir)?; | ||
| } | ||
|
|
||
| // Javascript | ||
|
|
@@ -174,78 +177,8 @@ pub trait CanisterBuilder { | |
| ) | ||
| })?; | ||
| eprintln!(" {}", &output_did_js_path.display()); | ||
| // index.js | ||
| let mut language_bindings = crate::util::assets::language_bindings() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this to a separate function that takes a param "ts" or "js" and will only generate the files for the provided extension. |
||
| .context("Failed to get language bindings archive.")?; | ||
| for f in language_bindings | ||
| .entries() | ||
| .context("Failed to read language bindings archive entries.")? | ||
| { | ||
| let mut file = f.context("Failed to read language bindings archive entry.")?; | ||
|
|
||
| let pathname: PathBuf = file | ||
| .path() | ||
| .context("Failed to read language bindings entry path name.")? | ||
| .to_path_buf(); | ||
| let extension = pathname.extension(); | ||
| let is_template = matches! (extension, Some (ext ) if ext == OsStr::new("hbs")); | ||
|
|
||
| if is_template { | ||
| let mut file_contents = String::new(); | ||
| file.read_to_string(&mut file_contents) | ||
| .context("Failed to read language bindings archive file content.")?; | ||
|
|
||
| // create the handlebars registry | ||
| let handlebars = Handlebars::new(); | ||
|
|
||
| let mut data: BTreeMap<String, &String> = BTreeMap::new(); | ||
|
|
||
| let canister_name = &info.get_name().to_string(); | ||
|
|
||
| let node_compatibility = info.get_declarations_config().node_compatibility; | ||
|
|
||
| // Insert only if node outputs are specified | ||
| let actor_export = if node_compatibility { | ||
| // leave empty for nodejs | ||
| "".to_string() | ||
| } else { | ||
| format!( | ||
| r#" | ||
|
|
||
| /** | ||
| * A ready-to-use agent for the {0} canister | ||
| * @type {{import("@dfinity/agent").ActorSubclass<import("./{0}.did.js")._SERVICE>}} | ||
| */ | ||
| export const {0} = createActor(canisterId);"#, | ||
| canister_name | ||
| ) | ||
| .to_string() | ||
| }; | ||
|
|
||
| data.insert("canister_name".to_string(), canister_name); | ||
| data.insert("actor_export".to_string(), &actor_export); | ||
|
|
||
| let process_string: String = match &info.get_declarations_config().env_override | ||
| { | ||
| Some(s) => format!(r#""{}""#, s.clone()), | ||
| None => { | ||
| format!( | ||
| "process.env.{}{}", | ||
| &canister_name.to_ascii_uppercase(), | ||
| "_CANISTER_ID" | ||
| ) | ||
| } | ||
| }; | ||
|
|
||
| data.insert("canister_name_process_env".to_string(), &process_string); | ||
|
|
||
| let new_file_contents = | ||
| handlebars.render_template(&file_contents, &data).unwrap(); | ||
| let new_path = generate_output_dir.join(pathname.with_extension("")); | ||
| std::fs::write(&new_path, new_file_contents) | ||
| .with_context(|| format!("Failed to write to {}.", new_path.display()))?; | ||
| } | ||
| } | ||
| compile_handlebars_files("js", info, generate_output_dir)?; | ||
| } | ||
|
|
||
| // Motoko | ||
|
|
@@ -268,6 +201,7 @@ export const {0} = createActor(canisterId);"#, | |
| } else { | ||
| eprintln!(" {}", &generated_idl_path.display()); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -281,6 +215,87 @@ export const {0} = createActor(canisterId);"#, | |
| } | ||
| } | ||
|
|
||
| fn compile_handlebars_files( | ||
| lang: &str, | ||
| info: &CanisterInfo, | ||
| generate_output_dir: &Path, | ||
| ) -> DfxResult { | ||
| // index.js | ||
| let mut language_bindings = crate::util::assets::language_bindings() | ||
| .context("Failed to get language bindings archive.")?; | ||
| for f in language_bindings | ||
| .entries() | ||
| .context("Failed to read language bindings archive entries.")? | ||
| { | ||
| let mut file = f.context("Failed to read language bindings archive entry.")?; | ||
|
|
||
| let pathname: PathBuf = file | ||
| .path() | ||
| .context("Failed to read language bindings entry path name.")? | ||
| .to_path_buf(); | ||
| let file_extension = format!("{}.hbs", lang); | ||
| let is_template = pathname | ||
| .to_str() | ||
| .map_or(false, |name| name.ends_with(&file_extension)); | ||
|
|
||
| if is_template { | ||
| let mut file_contents = String::new(); | ||
| file.read_to_string(&mut file_contents) | ||
| .context("Failed to read language bindings archive file content.")?; | ||
|
|
||
| // create the handlebars registry | ||
| let handlebars = Handlebars::new(); | ||
|
|
||
| let mut data: BTreeMap<String, &String> = BTreeMap::new(); | ||
|
|
||
| let canister_name = &info.get_name().to_string(); | ||
|
|
||
| let node_compatibility = info.get_declarations_config().node_compatibility; | ||
|
|
||
| // Insert only if node outputs are specified | ||
| let actor_export = if node_compatibility { | ||
| // leave empty for nodejs | ||
| "".to_string() | ||
| } else { | ||
| format!( | ||
| r#" | ||
|
|
||
| /** | ||
| * A ready-to-use agent for the {0} canister | ||
| * @type {{import("@dfinity/agent").ActorSubclass<import("./{0}.did.js")._SERVICE>}} | ||
| */ | ||
| export const {0} = createActor(canisterId);"#, | ||
| canister_name | ||
| ) | ||
| .to_string() | ||
| }; | ||
|
|
||
| data.insert("canister_name".to_string(), canister_name); | ||
| data.insert("actor_export".to_string(), &actor_export); | ||
|
|
||
| let process_string: String = match &info.get_declarations_config().env_override { | ||
| Some(s) => format!(r#""{}""#, s.clone()), | ||
| None => { | ||
| format!( | ||
| "process.env.{}{}", | ||
| &canister_name.to_ascii_uppercase(), | ||
| "_CANISTER_ID" | ||
| ) | ||
| } | ||
| }; | ||
|
|
||
| data.insert("canister_name_process_env".to_string(), &process_string); | ||
|
|
||
| let new_file_contents = handlebars.render_template(&file_contents, &data).unwrap(); | ||
| let new_path = generate_output_dir.join(pathname.with_extension("")); | ||
| std::fs::write(&new_path, new_file_contents) | ||
| .with_context(|| format!("Failed to write to {}.", new_path.display()))?; | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| // TODO: this function was copied from src/lib/models/canister.rs | ||
| fn ensure_trailing_newline(s: String) -> String { | ||
| if s.ends_with('\n') { | ||
|
|
||
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.
This code was generating the declarations directory and the DID file before checking if the bindings array is empty.
There's some code lower down in this file that deletes the DID file, but we never get that far because of the early return for an empty bindings array.
So the old flow was like this:
Now it's like this: