gdenv-lib 1.1.0

The best command-line tool to install and switch between multiple versions of Godot.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Utilities for generating a `.gdextension` file for Godot.

use crate::path_extension::PathExt;
use anyhow::{Context, Result, bail};
use pathdiff::diff_paths;
use std::path::{Path, PathBuf};

/// A validated GDExtension configuration ready to be writen to a `.gdextension` file.
/// Construct me using the builder `GdExtensionConfig::start`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ValidGdExtensionConfig {
    config_file_name: String,
    compatability_version: String,
    entry_symbol: String,
    reloadable: bool,
    release_target: Option<String>,
    debug_target: Option<String>,
    godot_project_path: PathBuf,
    relative_target_path: String,
    library_name: String,
}

/// Used to configure a `.gdextension` file for Godot that can be written to disk.
/// This class is a builder for `ValidGdExtensionConfig`.
///
/// Example usage:
/// ```rust,no_run
/// # fn example() -> anyhow::Result<()> {
/// # use std::path::PathBuf;
/// # use gdenv_lib::gdextension_config::GdExtensionConfig;
/// # let crate_name = "test-library";
/// # let godot_project_path = &PathBuf::from("/home/user/projects/godot_project_path");
/// # let target_directory = &PathBuf::from("/home/user/.cache/cargo/target");
/// # let working_dir = &PathBuf::from("/home/user/projects/godot_project_path");
/// GdExtensionConfig::start(crate_name, godot_project_path, target_directory)
///     .build(working_dir)?
///     .write()?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GdExtensionConfig {
    config_file_name: String,
    compatability_version: String,
    entry_symbol: String,
    reloadable: bool,
    release_target: Option<String>,
    debug_target: Option<String>,
    target_path: Option<PathBuf>,
    godot_project_path: Option<PathBuf>,
    library_name: Option<String>,
}

impl Default for GdExtensionConfig {
    fn default() -> Self {
        Self {
            config_file_name: "rust.gdextension".to_string(),
            compatability_version: "4.1".to_string(),
            entry_symbol: "gdext_rust_init".to_string(),
            reloadable: true,
            release_target: Some("release".to_string()),
            debug_target: Some("debug".to_string()),
            target_path: None,
            godot_project_path: None,
            library_name: None,
        }
    }
}

impl GdExtensionConfig {
    /// Start building a `ValidGdExtensionConfig` from the given parameters.
    ///
    /// Note: `crate_name` will have dashes replaced with underscores
    /// to match cargo file naming conventions.
    pub fn start(crate_name: &str, godot_project_path: &Path, target_directory: &Path) -> Self {
        Self {
            library_name: Some(crate_name.replace("-", "_")),
            target_path: Some(target_directory.to_path_buf()),
            godot_project_path: Some(godot_project_path.to_path_buf()),
            ..Self::default()
        }
    }

    /// Validate builder parameters and return a `ValidGdExtensionConfig`.
    pub fn build(&self, working_dir: &Path) -> Result<ValidGdExtensionConfig> {
        let target_path = self
            .target_path
            .as_deref()
            .context("Missing target path")?
            .to_absolute(working_dir)
            .with_context(|| {
                format!(
                    "Failed to calculate absolute target path: {:?}",
                    self.target_path
                )
            })?;
        let godot_project_path = self
            .godot_project_path
            .as_ref()
            .context("Missing godot project path")?
            .to_absolute(working_dir)
            .with_context(|| {
                format!(
                    "Failed to calculate absolute godot project path: {:?}",
                    self.godot_project_path
                )
            })?;
        let library_name = self.library_name.as_ref().context("Missing library name")?;
        let relative_target_path = diff_paths(&target_path, &godot_project_path)
            .with_context(|| {
                format!(
                    "Failed to calculate relative target path: target={:?} -> godot_project={:?}",
                    target_path, godot_project_path
                )
            })?
            .to_str()
            .context("Failed to convert relative target path to string")?
            .to_string()
            .replace('\\', "/"); // Godot res:// paths are always forward slashes.

        Ok(ValidGdExtensionConfig {
            config_file_name: self.config_file_name.clone(),
            reloadable: self.reloadable,
            compatability_version: self.compatability_version.clone(),
            entry_symbol: self.entry_symbol.clone(),
            release_target: self.release_target.clone(),
            debug_target: self.debug_target.clone(),
            godot_project_path,
            relative_target_path,
            library_name: library_name.clone(),
        })
    }

    /// Only include 'release' library configuration.
    /// The default is to include both 'release' and 'debug'.
    pub fn release_target(self, name: Option<String>) -> Self {
        Self {
            release_target: name,
            ..self
        }
    }

    /// Only include 'debug' library configuration.
    /// The default is to include both 'release' and 'debug'.
    pub fn debug_target(self, name: Option<String>) -> Self {
        Self {
            debug_target: name,
            ..self
        }
    }

    /// Configure the minimum compatibility version for the generated `.gdextension` file.
    /// The default is `4.1`.
    pub fn compatability_version(self, version: &str) -> Self {
        Self {
            compatability_version: version.to_string(),
            ..self
        }
    }

    /// Configure the name of the entry symbol for the generated `.gdextension` file.
    /// The default is `gdext_rust_init`.
    pub fn entry_symbol(self, symbol: &str) -> Self {
        Self {
            entry_symbol: symbol.to_string(),
            ..self
        }
    }

    /// Configure the name of the generated `.gdextension` file.
    /// The default is `rust.gdextension`.
    pub fn config_file_name(self, name: &str) -> Self {
        Self {
            config_file_name: name.to_string(),
            ..self
        }
    }

    /// Configure whether the `.gdextension` library is hot reloadable.
    /// The default is `true`.
    pub fn reloadable(self, reloadable: bool) -> Self {
        Self { reloadable, ..self }
    }
}

impl ValidGdExtensionConfig {
    /// Generate a `.gdextension` file as a string.
    pub fn create(&self) -> String {
        let release = if let Some(release_target) = &self.release_target {
            format!(
                r#"
linux.release.x86_64 =   "res://{target}/{release_target}/lib{pkgname}.so"
windows.release.x86_64 = "res://{target}/{release_target}/{pkgname}.dll"
macos.release =          "res://{target}/{release_target}/lib{pkgname}.dylib"
macos.release.arm64 =    "res://{target}/{release_target}/lib{pkgname}.dylib"
"#,
                target = self.relative_target_path,
                release_target = release_target,
                pkgname = self.library_name,
            )
            .trim_start()
            .to_string()
        } else {
            "".to_string()
        };

        let debug = if let Some(debug_target) = &self.debug_target {
            format!(
                r#"
linux.debug.x86_64 =     "res://{target}/{debug_target}/lib{pkgname}.so"
windows.debug.x86_64 =   "res://{target}/{debug_target}/{pkgname}.dll"
macos.debug =            "res://{target}/{debug_target}/lib{pkgname}.dylib"
macos.debug.arm64 =      "res://{target}/{debug_target}/lib{pkgname}.dylib"
"#,
                target = self.relative_target_path,
                debug_target = debug_target,
                pkgname = self.library_name,
            )
            .trim_start()
            .to_string()
        } else {
            "".to_string()
        };

        let preamble = format!(
            r#"
[configuration]
entry_symbol = "{entry_symbol}"
compatibility_minimum = {compatability_version}
reloadable = {reloadable}

[libraries]
"#,
            entry_symbol = self.entry_symbol,
            compatability_version = self.compatability_version,
            reloadable = if self.reloadable { "true" } else { "false" },
        )
        .trim_start()
        .to_string();

        preamble + &release + &debug
    }

    /// The full path to the generated `.gdextension` file including the file name.
    pub fn full_config_path(&self) -> PathBuf {
        self.godot_project_path.join(&self.config_file_name)
    }

    /// Write a generated `.gdextension` file to disk.
    pub fn write(&self) -> Result<()> {
        if !self
            .full_config_path()
            .parent()
            .is_some_and(|parent| parent.exists())
        {
            bail!(
                "Destination directory of GDExtension file does not exist: {}",
                self.full_config_path().display()
            );
        }
        std::fs::write(self.full_config_path(), self.create()).context(format!(
            "Failed to write GDExtension file: {}",
            self.full_config_path().display()
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;

    fn create_test_directories() -> Result<(tempfile::TempDir, PathBuf, PathBuf)> {
        let tempdir = tempfile::Builder::new().prefix("gdenv-test").tempdir()?;
        let godot_project_path = tempdir.path().join("home/user/projects/godot_project_path");
        std::fs::create_dir_all(&godot_project_path)?;
        let target_path = tempdir.path().join("home/user/.cache/cargo/target");
        std::fs::create_dir_all(&target_path)?;

        Ok((tempdir, godot_project_path, target_path))
    }

    #[test]
    fn test_create() -> Result<()> {
        let (tempdir, godot_project_path, target_path) = create_test_directories()?;
        let config = GdExtensionConfig::start("test_library", &godot_project_path, &target_path)
            .build(tempdir.path())
            .expect("Successful build");
        let file_string = config.create();

        assert!(!file_string.contains('\\'));
        assert_eq!(
            file_string,
            r#"
[configuration]
entry_symbol = "gdext_rust_init"
compatibility_minimum = 4.1
reloadable = true

[libraries]
linux.release.x86_64 =   "res://../../.cache/cargo/target/release/libtest_library.so"
windows.release.x86_64 = "res://../../.cache/cargo/target/release/test_library.dll"
macos.release =          "res://../../.cache/cargo/target/release/libtest_library.dylib"
macos.release.arm64 =    "res://../../.cache/cargo/target/release/libtest_library.dylib"
linux.debug.x86_64 =     "res://../../.cache/cargo/target/debug/libtest_library.so"
windows.debug.x86_64 =   "res://../../.cache/cargo/target/debug/test_library.dll"
macos.debug =            "res://../../.cache/cargo/target/debug/libtest_library.dylib"
macos.debug.arm64 =      "res://../../.cache/cargo/target/debug/libtest_library.dylib"
"#
            .trim_start()
            .to_string()
        );
        Ok(())
    }

    #[test]
    fn test_create_release_only() -> Result<()> {
        let (tempdir, godot_project_path, target_path) = create_test_directories()?;
        let config = GdExtensionConfig::start("test_library", &godot_project_path, &target_path)
            .release_target(Some("release".to_string()))
            .debug_target(None)
            .build(tempdir.path())
            .expect("Successful build");
        let file_string = config.create();

        assert!(!file_string.contains('\\'));
        assert_eq!(
            file_string,
            r#"
[configuration]
entry_symbol = "gdext_rust_init"
compatibility_minimum = 4.1
reloadable = true

[libraries]
linux.release.x86_64 =   "res://../../.cache/cargo/target/release/libtest_library.so"
windows.release.x86_64 = "res://../../.cache/cargo/target/release/test_library.dll"
macos.release =          "res://../../.cache/cargo/target/release/libtest_library.dylib"
macos.release.arm64 =    "res://../../.cache/cargo/target/release/libtest_library.dylib"
"#
            .trim_start()
            .to_string()
        );
        Ok(())
    }

    #[test]
    fn test_create_debug_only() -> Result<()> {
        let (tempdir, godot_project_path, target_path) = create_test_directories()?;
        let config = GdExtensionConfig::start("test_library", &godot_project_path, &target_path)
            .release_target(None)
            .debug_target(Some("debug".to_string()))
            .build(tempdir.path())
            .expect("Successful build");
        let file_string = config.create();

        assert!(!file_string.contains('\\'));
        assert_eq!(
            file_string,
            r#"
[configuration]
entry_symbol = "gdext_rust_init"
compatibility_minimum = 4.1
reloadable = true

[libraries]
linux.debug.x86_64 =     "res://../../.cache/cargo/target/debug/libtest_library.so"
windows.debug.x86_64 =   "res://../../.cache/cargo/target/debug/test_library.dll"
macos.debug =            "res://../../.cache/cargo/target/debug/libtest_library.dylib"
macos.debug.arm64 =      "res://../../.cache/cargo/target/debug/libtest_library.dylib"
"#
            .trim_start()
            .to_string()
        );
        Ok(())
    }

    #[test]
    fn test_entry_symbol() -> Result<()> {
        let (tempdir, godot_project_path, target_path) = create_test_directories()?;
        let config = GdExtensionConfig::start("test_library", &godot_project_path, &target_path)
            .entry_symbol("custom_entry_point")
            .build(tempdir.path())
            .expect("Successful build");
        let file_string = config.create();

        assert!(!file_string.contains('\\'));
        assert_eq!(
            file_string,
            r#"
[configuration]
entry_symbol = "custom_entry_point"
compatibility_minimum = 4.1
reloadable = true

[libraries]
linux.release.x86_64 =   "res://../../.cache/cargo/target/release/libtest_library.so"
windows.release.x86_64 = "res://../../.cache/cargo/target/release/test_library.dll"
macos.release =          "res://../../.cache/cargo/target/release/libtest_library.dylib"
macos.release.arm64 =    "res://../../.cache/cargo/target/release/libtest_library.dylib"
linux.debug.x86_64 =     "res://../../.cache/cargo/target/debug/libtest_library.so"
windows.debug.x86_64 =   "res://../../.cache/cargo/target/debug/test_library.dll"
macos.debug =            "res://../../.cache/cargo/target/debug/libtest_library.dylib"
macos.debug.arm64 =      "res://../../.cache/cargo/target/debug/libtest_library.dylib"
"#
            .trim_start()
            .to_string()
        );
        Ok(())
    }
}