gdsuite 0.1.1

Workflow CLI for Godot projects: installs and coordinates gdtoolkit, gdtest, gdhealth and gdtypecheck
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use std::{
    fs,
    path::{Path, PathBuf},
    sync::OnceLock,
};

use anyhow::{Context, Result, anyhow};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GdsuiteConfig {
    #[serde(default)]
    pub project: ProjectConfig,
    #[serde(default)]
    pub tools: ToolsConfig,
    #[serde(default)]
    pub commands: CommandsConfig,
    #[serde(default)]
    pub reports: ReportsConfig,
    #[serde(default)]
    pub check: CheckConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectConfig {
    #[serde(default = "default_root")]
    pub root: String,
    pub godot_exe: Option<PathBuf>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolsConfig {
    #[serde(default = "default_gdtoolkit_version")]
    pub gdtoolkit: String,
    #[serde(default = "default_gdtest_source")]
    pub gdtest_source: String,
    #[serde(default = "default_gdhealth_source")]
    pub gdhealth_source: String,
    #[serde(default = "default_gdtypecheck_source")]
    pub gdtypecheck_source: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandsConfig {
    #[serde(default = "default_test_target")]
    pub default_test_target: String,
    #[serde(default = "default_health_subcommand")]
    pub default_health_subcommand: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReportsConfig {
    #[serde(default = "default_report_format")]
    pub default: String,
    #[serde(default = "default_artifacts_dir")]
    pub artifacts_dir: PathBuf,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckConfig {
    #[serde(default = "default_check_steps")]
    pub steps: Vec<CheckStep>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum CheckStep {
    Fmt,
    Lint,
    Typecheck,
    Test,
    Health,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Manifest {
    pub godot_exe: Option<PathBuf>,
    pub python: Option<PathBuf>,
    pub gdformat: Option<PathBuf>,
    pub gdlint: Option<PathBuf>,
    pub gdtest: Option<PathBuf>,
    pub gdhealth: Option<PathBuf>,
    pub gdtypecheck: Option<PathBuf>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReleaseManifest {
    pub suite: ReleaseSuite,
    pub tools: ReleaseTools,
    pub contracts: ReleaseContracts,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReleaseSuite {
    pub version: String,
    pub manifest_version: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReleaseTools {
    pub gdtoolkit: ReleaseTool,
    pub gdtest: ReleaseTool,
    pub gdhealth: ReleaseTool,
    pub gdtypecheck: ReleaseTool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReleaseTool {
    pub acquire: ToolAcquireSpec,
    pub tested: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReleaseContracts {
    pub gdtest: GdtestContract,
    pub gdhealth: GdhealthContract,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GdtestContract {
    pub required_harness: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GdhealthContract {
    pub required_schema_version: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ToolAcquireSpec {
    Pip {
        package: String,
        spec: String,
    },
    Crate {
        #[serde(rename = "crate")]
        crate_name: String,
        version: String,
    },
    Path {
        path: String,
    },
    Git {
        url: String,
        rev: String,
        subdir: Option<String>,
    },
    GithubRelease {
        url: String,
    },
    GithubReleaseRepo {
        owner: String,
        repo: String,
        tag: String,
    },
}

#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolPolicy {
    pub tested: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstalledState {
    pub suite_version: String,
    pub manifest_version: u32,
    pub tools: InstalledToolsState,
    pub contracts: InstalledContractsState,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstalledToolsState {
    pub gdtoolkit: InstalledToolState,
    pub gdtest: InstalledToolState,
    pub gdhealth: InstalledToolState,
    pub gdtypecheck: InstalledToolState,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstalledToolState {
    pub requested: ToolAcquireSpec,
    pub resolved_version: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct InstalledContractsState {
    pub gdtest_harness_version: Option<String>,
    pub gdhealth_schema_version: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum CompatibilityVerdict {
    Ok,
    Warning,
    Error,
}

impl Default for ProjectConfig {
    fn default() -> Self {
        Self {
            root: default_root(),
            godot_exe: None,
        }
    }
}

impl Default for ToolsConfig {
    fn default() -> Self {
        Self {
            gdtoolkit: default_gdtoolkit_version(),
            gdtest_source: default_gdtest_source(),
            gdhealth_source: default_gdhealth_source(),
            gdtypecheck_source: default_gdtypecheck_source(),
        }
    }
}

impl Default for CommandsConfig {
    fn default() -> Self {
        Self {
            default_test_target: default_test_target(),
            default_health_subcommand: default_health_subcommand(),
        }
    }
}

impl Default for ReportsConfig {
    fn default() -> Self {
        Self {
            default: default_report_format(),
            artifacts_dir: default_artifacts_dir(),
        }
    }
}

impl Default for CheckConfig {
    fn default() -> Self {
        Self {
            steps: default_check_steps(),
        }
    }
}

impl GdsuiteConfig {
    pub fn load(path: &Path) -> Result<Self> {
        let text = fs::read_to_string(path)
            .with_context(|| format!("failed to read config {}", path.display()))?;
        let value: toml::Value = toml::from_str(&text)
            .with_context(|| format!("failed to parse config {}", path.display()))?;
        reject_legacy_sections(path, &value)?;
        value
            .try_into()
            .with_context(|| format!("failed to parse config {}", path.display()))
    }

    pub fn write(&self, path: &Path) -> Result<()> {
        fs::write(path, render_gdsuite_config(self))
            .with_context(|| format!("failed to write config {}", path.display()))
    }

    pub fn write_if_missing(path: &Path, godot_exe: Option<&Path>) -> Result<()> {
        if path.exists() {
            return Ok(());
        }

        fs::write(path, default_config_template(godot_exe))
            .with_context(|| format!("failed to write config {}", path.display()))
    }
}

impl Manifest {
    pub fn load(path: &Path) -> Result<Self> {
        let text = fs::read_to_string(path)
            .with_context(|| format!("failed to read manifest {}", path.display()))?;
        serde_json::from_str(&text)
            .with_context(|| format!("failed to parse manifest {}", path.display()))
    }

    pub fn write(&self, path: &Path) -> Result<()> {
        fs::write(path, serde_json::to_string_pretty(self)?)
            .with_context(|| format!("failed to write manifest {}", path.display()))
    }
}

impl ReleaseManifest {
    pub fn bundled() -> &'static Self {
        static RELEASE_MANIFEST: OnceLock<ReleaseManifest> = OnceLock::new();
        RELEASE_MANIFEST.get_or_init(|| {
            toml::from_str(include_str!("../.gdsuite-release.toml"))
                .expect("bundled release manifest must parse")
        })
    }
}

impl InstalledState {
    #[allow(dead_code)]
    pub fn load(path: &Path) -> Result<Self> {
        let text = fs::read_to_string(path)
            .with_context(|| format!("failed to read installation state {}", path.display()))?;
        serde_json::from_str(&text)
            .with_context(|| format!("failed to parse installation state {}", path.display()))
    }

    pub fn write(&self, path: &Path) -> Result<()> {
        fs::write(path, serde_json::to_string_pretty(self)?)
            .with_context(|| format!("failed to write installation state {}", path.display()))
    }
}

impl ToolAcquireSpec {
    pub fn gdtoolkit_spec(&self) -> Option<&str> {
        match self {
            Self::Pip { spec, .. } => Some(spec),
            _ => None,
        }
    }

    pub fn render_requested(&self) -> String {
        match self {
            Self::Pip { spec, .. } => spec.clone(),
            Self::Crate {
                crate_name,
                version,
            } => format!("crate:{crate_name}@{version}"),
            Self::Path { path } => format!("path:{path}"),
            Self::Git { url, rev, subdir } => {
                if let Some(subdir) = subdir {
                    format!("git:{url}?rev={rev}&subdir={subdir}")
                } else {
                    format!("git:{url}?rev={rev}")
                }
            }
            Self::GithubRelease { url } => format!("github:{url}"),
            Self::GithubReleaseRepo { owner, repo, tag } => {
                format!("github:{owner}/{repo}@{tag}")
            }
        }
    }
}

fn reject_legacy_sections(path: &Path, value: &toml::Value) -> Result<()> {
    let Some(table) = value.as_table() else {
        return Ok(());
    };

    let legacy = [
        (
            "lint",
            "create `gdlintrc` with your native gdlint settings",
        ),
        (
            "health",
            "remove suite-owned health settings; use gdhealth CLI args or `gdhealth.toml`",
        ),
        (
            "gdtest",
            "create `gdtest.toml` with your native gdtest settings",
        ),
        (
            "gdhealth",
            "create `gdhealth.toml` with your native gdhealth settings",
        ),
        (
            "typecheck",
            "configure the delegated typecheck tool directly; gdsuite no longer owns typecheck config",
        ),
        (
            "fix",
            "remove suite-owned fixers; use the delegated tool behavior instead",
        ),
    ]
    .into_iter()
    .filter(|(key, _)| table.contains_key(*key))
    .collect::<Vec<_>>();

    if legacy.is_empty() {
        return Ok(());
    }

    let guidance = legacy
        .into_iter()
        .map(|(key, message)| format!("[{key}] is no longer supported: {message}"))
        .collect::<Vec<_>>()
        .join("; ");
    Err(anyhow!(
        "unsupported legacy sections in {}: {}. Run `gdsuite migrate` to rewrite the file.",
        path.display(),
        guidance
    ))
}

fn default_root() -> String {
    ".".to_string()
}

fn default_gdtoolkit_version() -> String {
    ReleaseManifest::bundled()
        .tools
        .gdtoolkit
        .acquire
        .gdtoolkit_spec()
        .expect("gdtoolkit release manifest must use pip")
        .to_string()
}

fn default_gdtest_source() -> String {
    ReleaseManifest::bundled()
        .tools
        .gdtest
        .acquire
        .render_requested()
}

fn default_gdhealth_source() -> String {
    ReleaseManifest::bundled()
        .tools
        .gdhealth
        .acquire
        .render_requested()
}

fn default_gdtypecheck_source() -> String {
    ReleaseManifest::bundled()
        .tools
        .gdtypecheck
        .acquire
        .render_requested()
}

fn default_test_target() -> String {
    "test/".to_string()
}

fn default_health_subcommand() -> String {
    "health".to_string()
}

fn default_report_format() -> String {
    "text".to_string()
}

fn default_artifacts_dir() -> PathBuf {
    PathBuf::from("reports/gdsuite")
}

fn default_check_steps() -> Vec<CheckStep> {
    vec![
        CheckStep::Fmt,
        CheckStep::Lint,
        CheckStep::Typecheck,
        CheckStep::Health,
    ]
}

fn default_config_template(godot_exe: Option<&Path>) -> String {
    let release = ReleaseManifest::bundled();
    let godot_exe = godot_exe
        .map(|path| {
            format!(
                "godot_exe = \"{}\"\n",
                path.display().to_string().replace('\\', "\\\\")
            )
        })
        .unwrap_or_default();

    format!(
        r#"[project]
root = "."
{godot_exe}[tools]
gdtoolkit = "{}"
gdtest_source = "{}"
gdhealth_source = "{}"
gdtypecheck_source = "{}"

[commands]
default_test_target = "test/"
default_health_subcommand = "health"

[reports]
default = "text"
artifacts_dir = "reports/gdsuite"

[check]
steps = ["fmt", "lint", "typecheck", "health"]
"#,
        release
            .tools
            .gdtoolkit
            .acquire
            .gdtoolkit_spec()
            .expect("gdtoolkit release manifest must use pip"),
        release.tools.gdtest.acquire.render_requested(),
        release.tools.gdhealth.acquire.render_requested(),
        release.tools.gdtypecheck.acquire.render_requested(),
    )
}

fn render_gdsuite_config(config: &GdsuiteConfig) -> String {
    let godot_exe = config
        .project
        .godot_exe
        .as_ref()
        .map(|path| {
            format!(
                "godot_exe = \"{}\"\n",
                path.display().to_string().replace('\\', "\\\\")
            )
        })
        .unwrap_or_default();

    format!(
        r#"[project]
root = "{}"
{godot_exe}[tools]
gdtoolkit = "{}"
gdtest_source = "{}"
gdhealth_source = "{}"
gdtypecheck_source = "{}"

[commands]
default_test_target = "{}"
default_health_subcommand = "{}"

[reports]
default = "{}"
artifacts_dir = "{}"

[check]
steps = {}
"#,
        escape_string(&config.project.root),
        escape_string(&config.tools.gdtoolkit),
        escape_string(&config.tools.gdtest_source),
        escape_string(&config.tools.gdhealth_source),
        escape_string(&config.tools.gdtypecheck_source),
        escape_string(&config.commands.default_test_target),
        escape_string(&config.commands.default_health_subcommand),
        escape_string(&config.reports.default),
        escape_path(&config.reports.artifacts_dir),
        render_check_steps(&config.check.steps),
    )
}

fn render_check_steps(steps: &[CheckStep]) -> String {
    let values = steps
        .iter()
        .map(|step| match step {
            CheckStep::Fmt => "\"fmt\"",
            CheckStep::Lint => "\"lint\"",
            CheckStep::Typecheck => "\"typecheck\"",
            CheckStep::Test => "\"test\"",
            CheckStep::Health => "\"health\"",
        })
        .collect::<Vec<_>>();
    format!("[{}]", values.join(", "))
}

fn escape_string(value: &str) -> String {
    value.replace('\\', "\\\\")
}

fn escape_path(path: &Path) -> String {
    escape_string(&path.display().to_string())
}

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

    #[test]
    fn test_github_release_repo_render_requested() {
        let spec = ToolAcquireSpec::GithubReleaseRepo {
            owner: "q-rault".to_string(),
            repo: "gdtest".to_string(),
            tag: "v0.1.0".to_string(),
        };
        assert_eq!(spec.render_requested(), "github:q-rault/gdtest@v0.1.0");
    }
}