alp-cli 0.1.5

The native `alp` CLI for ALP SDK embedded projects: board.yaml validate/generate, project scaffolding, toolchain bootstrap, and west build/flash with a stable JSON envelope.
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
// SPDX-License-Identifier: Apache-2.0
//! `alp sdk <list|install|current|switch>` — manage local SDK installs.
//!
//! Mirrors TS `runSdkCommand`: `list` queries the GitHub releases API,
//! `install <version>` git-clones into the cache, `current` reports the active
//! SDK, and `switch <version|path>` repoints it. Network/filesystem effects
//! live here; parsing + readiness logic is in `alp-core::sdk`.

use std::path::{Path, PathBuf};
use std::process::Command;

use alp_core::{
    GITHUB_RELEASES_URL, SdkReadinessReport, SdkReadinessState, SdkRelease, check_sdk_readiness,
    parse_remote_sdk_releases, resolve_active_sdk,
};
use serde::Serialize;

use super::CommandRun;
use crate::cli::{GlobalArgs, SdkArgs};
use crate::envelope::{Envelope, Issue, Project};
use crate::exit::ExitCode;
use crate::util::generated_at_iso;

const SDK_GIT_URL: &str = "https://github.com/alplabai/alp-sdk.git";

#[derive(Serialize)]
struct ListData {
    subcommand: &'static str,
    releases: Vec<SdkRelease>,
}

#[derive(Serialize)]
struct InstallData {
    subcommand: &'static str,
    version: String,
    #[serde(rename = "sdkPath")]
    sdk_path: String,
    readiness: SdkReadinessReport,
}

#[derive(Serialize)]
struct CurrentData {
    subcommand: &'static str,
    #[serde(rename = "sdkPath")]
    sdk_path: Option<String>,
    readiness: Option<SdkReadinessReport>,
}

#[derive(Serialize)]
struct SwitchData {
    subcommand: &'static str,
    #[serde(rename = "sdkPath")]
    sdk_path: String,
    version: Option<String>,
}

pub fn run(g: &GlobalArgs, args: &SdkArgs) -> CommandRun {
    match args.subcommand.as_deref() {
        Some("list") => run_list(g),
        Some("install") => run_install(g, args),
        Some("current") => run_current(g),
        Some("switch") => run_switch(g, args),
        other => {
            let sub = other.unwrap_or("(none)");
            emit_failure(
                g,
                ListData {
                    subcommand: "list",
                    releases: Vec::new(),
                },
                ExitCode::RuntimeFailure,
                "unknown-subcommand",
                format!("Unknown sdk subcommand: {sub}"),
                vec![
                    format!("sdk: unknown subcommand '{sub}'."),
                    "Available subcommands: list, install <version>, current, switch <version>"
                        .to_string(),
                ],
            )
        }
    }
}

// ── alp sdk list ────────────────────────────────────────────────────────────

fn run_list(g: &GlobalArgs) -> CommandRun {
    let pb = crate::progress::spinner(g, "Fetching ALP SDK releases…");
    let result = fetch_releases();
    pb.finish_and_clear();
    let releases = match result {
        Ok(r) => r,
        Err(message) => {
            return emit_failure(
                g,
                ListData {
                    subcommand: "list",
                    releases: Vec::new(),
                },
                ExitCode::RuntimeFailure,
                "fetch-failed",
                message.clone(),
                vec!["sdk list: failed to fetch releases.".to_string(), message],
            );
        }
    };
    let text = format_release_table(&releases);
    emit_success(
        g,
        ListData {
            subcommand: "list",
            releases,
        },
        ExitCode::Success,
        text,
    )
}

fn fetch_releases() -> Result<Vec<SdkRelease>, String> {
    let response = ureq::get(GITHUB_RELEASES_URL)
        .set("User-Agent", "alp-cli/0")
        .set("Accept", "application/vnd.github+json")
        .set("X-GitHub-Api-Version", "2022-11-28")
        .call()
        .map_err(|e| e.to_string())?;
    let value: serde_json::Value = response.into_json().map_err(|e| e.to_string())?;
    parse_remote_sdk_releases(&value)
}

fn format_release_table(releases: &[SdkRelease]) -> Vec<String> {
    if releases.is_empty() {
        return vec!["No SDK releases found.".to_string()];
    }
    let mut lines = vec![format!("ALP SDK releases ({})", releases.len())];
    for rel in releases {
        let date: String = rel.published_at.chars().take(10).collect();
        let notes = if rel.release_notes_summary.is_empty() {
            String::new()
        } else {
            format!("   {}", truncate(&rel.release_notes_summary, 60))
        };
        lines.push(format!("  {:<12} {date}{notes}", rel.tag));
    }
    lines
}

// ── alp sdk install <version> ────────────────────────────────────────────────

fn run_install(g: &GlobalArgs, args: &SdkArgs) -> CommandRun {
    let Some(version) = args.arg.clone() else {
        return emit_failure(
            g,
            InstallData {
                subcommand: "install",
                version: String::new(),
                sdk_path: String::new(),
                readiness: empty_readiness(""),
            },
            ExitCode::RuntimeFailure,
            "missing-version",
            "Version argument is required.".to_string(),
            vec![
                "sdk install: version argument is required.".to_string(),
                "Usage: alp sdk install <version>".to_string(),
            ],
        );
    };

    let cache_root = args.destination.clone().unwrap_or_else(default_cache_root);
    let dest_path = Path::new(&cache_root).join(&version);
    let dest_str = dest_path.to_string_lossy().to_string();

    let already_installed = dest_path.join("scripts").join("alp_project.py").exists();
    if !already_installed {
        let pb = crate::progress::spinner(g, &format!("Cloning alp-sdk {version}"));
        let clone_result = git_clone(&version, &dest_path);
        pb.finish_and_clear();
        if let Err(message) = clone_result {
            return emit_failure(
                g,
                InstallData {
                    subcommand: "install",
                    version: version.clone(),
                    sdk_path: dest_str.clone(),
                    readiness: empty_readiness(&dest_str),
                },
                ExitCode::RuntimeFailure,
                "install-failed",
                message.clone(),
                vec!["sdk install: installation failed.".to_string(), message],
            );
        }
    }

    let readiness = readiness_for(&dest_str);
    // Process exit reflects readiness; the envelope field stays success (TS parity).
    let exit = if readiness.state == SdkReadinessState::Missing {
        ExitCode::RuntimeFailure
    } else {
        ExitCode::Success
    };
    let text = format_readiness_block(&format!("SDK {version} installed"), &readiness);
    emit_success(
        g,
        InstallData {
            subcommand: "install",
            version,
            sdk_path: dest_str,
            readiness,
        },
        exit,
        text,
    )
}

fn git_clone(version: &str, dest: &Path) -> Result<(), String> {
    if let Some(parent) = dest.parent() {
        std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
    }
    // Capture output (--quiet) rather than inherit: the spinner owns the
    // terminal while cloning, and JSON/non-interactive runs stay output-free.
    // git's stderr is surfaced verbatim only on failure.
    let output = Command::new("git")
        .args([
            "clone",
            "--quiet",
            "--branch",
            version,
            "--depth",
            "1",
            SDK_GIT_URL,
        ])
        .arg(dest)
        .output()
        .map_err(|e| format!("Alp: git clone failed to start: {e}"))?;
    if !output.status.success() {
        let detail = String::from_utf8_lossy(&output.stderr);
        let detail = detail.trim();
        if detail.is_empty() {
            return Err(format!(
                "Alp: git clone failed with exit code {}.",
                output
                    .status
                    .code()
                    .map(|c| c.to_string())
                    .unwrap_or_else(|| "unknown".to_string())
            ));
        }
        return Err(format!("Alp: git clone failed: {detail}"));
    }
    Ok(())
}

// ── alp sdk current ──────────────────────────────────────────────────────────

fn run_current(g: &GlobalArgs) -> CommandRun {
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let sdk_path = resolve_active_sdk(
        &cwd.to_string_lossy(),
        |p| Path::new(p).exists(),
        |p| std::fs::read_to_string(p).ok(),
    );
    let readiness = sdk_path.as_deref().map(readiness_for);

    let text = match (&sdk_path, &readiness) {
        (Some(_), Some(report)) => format_readiness_block("Active SDK", report),
        _ => vec![
            "No active SDK configured for this workspace.".to_string(),
            "Run 'alp sdk install <version>' to get started.".to_string(),
        ],
    };
    emit_success(
        g,
        CurrentData {
            subcommand: "current",
            sdk_path,
            readiness,
        },
        ExitCode::Success,
        text,
    )
}

// ── alp sdk switch <version|path> ────────────────────────────────────────────

fn run_switch(g: &GlobalArgs, args: &SdkArgs) -> CommandRun {
    let Some(version_or_path) = args.arg.clone() else {
        return emit_failure(
            g,
            SwitchData {
                subcommand: "switch",
                sdk_path: String::new(),
                version: None,
            },
            ExitCode::RuntimeFailure,
            "missing-version",
            "Version or path argument is required.".to_string(),
            vec![
                "sdk switch: version or path argument is required.".to_string(),
                "Usage: alp sdk switch <version|path>".to_string(),
            ],
        );
    };

    let sdk_path = if Path::new(&version_or_path).is_absolute() {
        version_or_path.clone()
    } else {
        g.sdk_root.clone().unwrap_or_else(|| {
            Path::new(&default_cache_root())
                .join(&version_or_path)
                .to_string_lossy()
                .to_string()
        })
    };

    if !Path::new(&sdk_path).exists() {
        return emit_failure(
            g,
            SwitchData {
                subcommand: "switch",
                sdk_path: sdk_path.clone(),
                version: None,
            },
            ExitCode::RuntimeFailure,
            "path-not-found",
            format!("SDK path not found: {sdk_path}"),
            vec![
                format!("sdk switch: SDK path does not exist: {sdk_path}"),
                "Run 'alp sdk install <version>' first.".to_string(),
            ],
        );
    }

    if let Err(message) = write_active_pointer(&sdk_path) {
        return emit_failure(
            g,
            SwitchData {
                subcommand: "switch",
                sdk_path: sdk_path.clone(),
                version: None,
            },
            ExitCode::RuntimeFailure,
            "switch-failed",
            message.clone(),
            vec![
                "sdk switch: failed to update active SDK pointer.".to_string(),
                message,
            ],
        );
    }

    let readiness = readiness_for(&sdk_path);
    let text = vec![
        format!(
            "Switched active SDK to {}.",
            readiness
                .version
                .clone()
                .unwrap_or_else(|| sdk_path.clone())
        ),
        format!("  path    {sdk_path}"),
        format!("  state   {}", state_label(readiness.state)),
    ];
    emit_success(
        g,
        SwitchData {
            subcommand: "switch",
            sdk_path,
            version: readiness.version,
        },
        ExitCode::Success,
        text,
    )
}

fn write_active_pointer(sdk_path: &str) -> Result<(), String> {
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let dir = cwd.join(".alp");
    std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
    let pointer = serde_json::json!({ "sdkPath": sdk_path, "updatedAt": generated_at_iso() });
    let content = format!(
        "{}\n",
        serde_json::to_string_pretty(&pointer).expect("pointer is serializable")
    );
    std::fs::write(dir.join("sdk-path"), content).map_err(|e| e.to_string())
}

// ── helpers ───────────────────────────────────────────────────────────────

fn readiness_for(sdk_path: &str) -> SdkReadinessReport {
    check_sdk_readiness(
        sdk_path,
        |p| Path::new(p).exists(),
        |p| std::fs::read_to_string(p).ok(),
    )
}

fn empty_readiness(sdk_path: &str) -> SdkReadinessReport {
    SdkReadinessReport {
        sdk_path: sdk_path.to_string(),
        version: None,
        loader_script_present: false,
        metadata_present: false,
        state: SdkReadinessState::Missing,
        issues: Vec::new(),
    }
}

fn default_cache_root() -> String {
    let home = std::env::var_os(if cfg!(windows) { "USERPROFILE" } else { "HOME" })
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("."));
    home.join(".alp")
        .join("sdk-cache")
        .to_string_lossy()
        .to_string()
}

fn state_label(state: SdkReadinessState) -> &'static str {
    match state {
        SdkReadinessState::Ready => "ready",
        SdkReadinessState::Partial => "partial",
        SdkReadinessState::Missing => "missing",
    }
}

fn format_readiness_block(header: &str, report: &SdkReadinessReport) -> Vec<String> {
    let mut lines = vec![
        header.to_string(),
        format!("  path    {}", report.sdk_path),
        format!(
            "  version {}",
            report
                .version
                .clone()
                .unwrap_or_else(|| "(unknown)".to_string())
        ),
        format!("  state   {}", state_label(report.state)),
    ];
    if !report.issues.is_empty() {
        lines.push("  issues:".to_string());
        for issue in &report.issues {
            lines.push(format!("    - {issue}"));
        }
    }
    lines
}

fn truncate(text: &str, max: usize) -> String {
    if text.chars().count() <= max {
        text.to_string()
    } else {
        text.chars().take(max).collect()
    }
}

fn null_project() -> Project {
    Project {
        root: None,
        board_yaml: None,
    }
}

fn emit_success<T: Serialize>(
    g: &GlobalArgs,
    data: T,
    exit: ExitCode,
    text_lines: Vec<String>,
) -> CommandRun {
    let text = if g.is_json() { Vec::new() } else { text_lines };
    // The success-path envelope always carries exitCode 0 (mirrors TS
    // createEnvelope); the process exit may still differ (install → missing).
    let json = g.is_json().then(|| {
        Envelope::new(
            "sdk",
            null_project(),
            data,
            Vec::new(),
            ExitCode::Success.code(),
        )
        .to_json()
    });
    CommandRun { exit, text, json }
}

fn emit_failure<T: Serialize>(
    g: &GlobalArgs,
    data: T,
    exit: ExitCode,
    code: &str,
    message: String,
    text_lines: Vec<String>,
) -> CommandRun {
    let issues = vec![Issue {
        code: format!("sdk.{code}"),
        severity: "error".to_string(),
        message,
    }];
    let text = if g.is_json() { Vec::new() } else { text_lines };
    let json = g
        .is_json()
        .then(|| Envelope::new("sdk", null_project(), data, issues, exit.code()).to_json());
    CommandRun { exit, text, json }
}