gdext_gen/gdext/deps.rs
1//! Module for the generation of the dependencies section of the `.gdextension` file.
2
3#[allow(unused_imports)]
4use std::{
5 collections::HashMap,
6 path::{Path, PathBuf},
7};
8
9use toml_edit::{Decor, InlineTable, Key};
10
11use super::GDExtension;
12use crate::{
13 args::BaseDirectory,
14 features::{sys::System, target::Target},
15};
16
17impl GDExtension {
18 /// Generates the dependencies section of the [`GDExtension`].
19 ///
20 /// # Parameters
21 ///
22 /// * `base_dir` - The base directory to use for the paths of the dependencies in the `.gdextension` file.
23 /// * `dependencies` - Map of dependencies, where the key is the target and the value is a [`Vec`] with the paths to the dependencies **relative** to the *`base_dir`*. For example, if the `base_dir` is [`ProjectFolder`](crate::args::BaseDirectory::ProjectFolder), the path for `Godot` would be `"res://path/to/dep"` and the path provided must be `"path/to/dep"`. If the path contains non valid Unicode, it will be stored calling [`to_string_lossy`](Path::to_string_lossy).
24 ///
25 /// # Returns
26 ///
27 /// The [`Vec`] of targets and their dependencies to add well formatted to the [`toml_edit::DocumentMut`].
28 pub fn generate_deps(
29 base_dir: BaseDirectory,
30 dependencies: HashMap<Target, Vec<PathBuf>>,
31 ) -> Vec<(String, InlineTable)> {
32 let mut dependencies_vector = Vec::new();
33 // Decor for the formatting of the inline keys.
34 let leaf_decor = Decor::new("\n ", " ");
35
36 for (target, paths) in dependencies {
37 let target_name = target.get_godot_target();
38 let mut current_dependencies = InlineTable::new();
39 for path in paths {
40 current_dependencies.insert_formatted(
41 &Key::from(format!(
42 "{}{}",
43 base_dir.as_str(),
44 path.to_string_lossy().replace('\\', "/")
45 ))
46 .with_leaf_decor(leaf_decor.clone()),
47 match target.0 {
48 System::MacOS => "Contents/Frameworks",
49 _ => "",
50 }
51 .into(),
52 );
53 }
54
55 // There should at least be one target-dependencies, and thus, a newline can be safely added.
56 current_dependencies
57 .iter_mut()
58 .last()
59 .unwrap()
60 .1
61 .decor_mut()
62 .set_suffix("\n");
63
64 dependencies_vector.push((target_name, current_dependencies));
65 }
66
67 // Generating the empty table where the dependencies will be formatted in. This is not needed anymore, it's generated using toml_edit instead.
68 //self.dependencies = Some(toml::Table::new());
69
70 dependencies_vector
71 }
72}