specsync 4.1.3

Bidirectional spec-to-code validation with schema column checking — 11 languages, single binary
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
use colored::Colorize;
use std::fs;
use std::path::{Path, PathBuf};
use std::process;

use crate::config::load_config;
use crate::generator;
use crate::github;
use crate::importer;

/// Result of a single import attempt (used for batch summary).
#[derive(Default)]
struct BatchStats {
    imported: usize,
    skipped: usize,
    errors: usize,
}

pub fn cmd_import(
    root: &Path,
    source: Option<&str>,
    id: Option<&str>,
    repo_override: Option<&str>,
    all_issues: bool,
    label: Option<&str>,
    from_dir: Option<&Path>,
) {
    // Route to batch or single import
    if all_issues {
        cmd_import_all_issues(root, repo_override, label);
        return;
    }
    if let Some(dir) = from_dir {
        cmd_import_from_dir(root, dir);
        return;
    }

    // Single import — source and id are required
    let source = source.unwrap_or_else(|| {
        eprintln!(
            "{} SOURCE is required. Use: specsync import <source> <id>",
            "Error:".red()
        );
        eprintln!(
            "  Or use {} or {} for batch import.",
            "--all-issues".bold(),
            "--from-dir".bold()
        );
        process::exit(1);
    });
    let id = id.unwrap_or_else(|| {
        eprintln!(
            "{} ID is required. Use: specsync import <source> <id>",
            "Error:".red()
        );
        process::exit(1);
    });

    cmd_import_single(root, source, id, repo_override);
}

fn cmd_import_single(root: &Path, source: &str, id: &str, repo_override: Option<&str>) {
    let config = load_config(root);
    let specs_dir = root.join(&config.specs_dir);

    let result = match source.to_lowercase().as_str() {
        "github" | "gh" => {
            let repo = repo_override
                .map(|r| r.to_string())
                .or_else(|| config.github.as_ref().and_then(|g| g.repo.clone()))
                .or_else(|| github::detect_repo(root))
                .unwrap_or_else(|| {
                    eprintln!(
                        "{} Cannot determine GitHub repo. Use --repo or set github.repo in specsync.json.",
                        "Error:".red()
                    );
                    process::exit(1);
                });

            let number: u64 = id.parse().unwrap_or_else(|_| {
                eprintln!("{} Invalid issue number: {id}", "Error:".red());
                process::exit(1);
            });

            println!(
                "  {} Fetching GitHub issue #{number} from {repo}...",
                "".blue()
            );
            importer::import_github_issue(&repo, number)
        }
        "jira" => {
            println!("  {} Fetching Jira issue {id}...", "".blue());
            importer::import_jira_issue(id)
        }
        "confluence" | "wiki" => {
            println!("  {} Fetching Confluence page {id}...", "".blue());
            importer::import_confluence_page(id)
        }
        _ => {
            eprintln!(
                "{} Unknown source '{}'. Supported: github, jira, confluence",
                "Error:".red(),
                source
            );
            process::exit(1);
        }
    };

    let item = match result {
        Ok(item) => item,
        Err(e) => {
            eprintln!("{} {e}", "Error:".red());
            process::exit(1);
        }
    };

    println!("  {} Imported: {}", "".green(), item.purpose);
    if !item.requirements.is_empty() {
        println!(
            "  {} Extracted {} requirement(s)",
            "i".blue(),
            item.requirements.len()
        );
    }

    let spec_dir = specs_dir.join(&item.module_name);
    let spec_file = spec_dir.join(format!("{}.spec.md", item.module_name));

    if spec_file.exists() {
        eprintln!(
            "{} Spec already exists: {}",
            "!".yellow(),
            spec_file.strip_prefix(root).unwrap_or(&spec_file).display()
        );
        process::exit(1);
    }

    let spec_content = importer::render_spec(&item);

    if let Err(e) = fs::create_dir_all(&spec_dir) {
        eprintln!("Failed to create {}: {e}", spec_dir.display());
        process::exit(1);
    }

    match fs::write(&spec_file, &spec_content) {
        Ok(_) => {
            let rel = spec_file.strip_prefix(root).unwrap_or(&spec_file).display();
            println!("  {} Created {rel}", "".green());
            generator::generate_companion_files_for_spec(&spec_dir, &item.module_name);
            println!(
                "\n{} Run {} to validate and fill in the details.",
                "Tip:".cyan().bold(),
                "specsync check".bold()
            );
        }
        Err(e) => {
            eprintln!("Failed to write {}: {e}", spec_file.display());
            process::exit(1);
        }
    }
}

/// Batch import all open GitHub issues as spec drafts.
fn cmd_import_all_issues(root: &Path, repo_override: Option<&str>, label: Option<&str>) {
    let config = load_config(root);
    let specs_dir = root.join(&config.specs_dir);

    let repo = repo_override
        .map(|r| r.to_string())
        .or_else(|| config.github.as_ref().and_then(|g| g.repo.clone()))
        .or_else(|| github::detect_repo(root))
        .unwrap_or_else(|| {
            eprintln!(
                "{} Cannot determine GitHub repo. Use --repo or set github.repo in specsync.json.",
                "Error:".red()
            );
            process::exit(1);
        });

    let label_display = label.map(|l| format!(" (label: {l})")).unwrap_or_default();
    println!(
        "\n--- {} -----------------------------------------------",
        "Batch Import: GitHub Issues".bold()
    );
    println!(
        "  {} Fetching open issues from {repo}{label_display}...",
        "".blue()
    );

    let issues = match github::list_issues(&repo, label) {
        Ok(issues) => issues,
        Err(e) => {
            eprintln!("{} {e}", "Error:".red());
            process::exit(1);
        }
    };

    if issues.is_empty() {
        println!("  {} No open issues found.", "i".blue());
        return;
    }

    println!(
        "  {} Found {} issue(s) to import\n",
        "i".blue(),
        issues.len()
    );

    let mut stats = BatchStats::default();
    let total = issues.len();

    for (idx, issue) in issues.iter().enumerate() {
        let progress = format!("[{}/{}]", idx + 1, total);
        print!("  {} ", progress.dimmed());

        let result = importer::import_github_issue(&repo, issue.number);
        let item = match result {
            Ok(item) => item,
            Err(e) => {
                println!("{} #{}: {}", "".red(), issue.number, e);
                stats.errors += 1;
                continue;
            }
        };

        let spec_dir = specs_dir.join(&item.module_name);
        let spec_file = spec_dir.join(format!("{}.spec.md", item.module_name));

        if spec_file.exists() {
            println!(
                "{} #{} skipped — spec already exists: {}",
                "~".yellow(),
                issue.number,
                item.module_name
            );
            stats.skipped += 1;
            continue;
        }

        let spec_content = importer::render_spec(&item);

        if let Err(e) = fs::create_dir_all(&spec_dir) {
            println!("{} #{}: Failed to create dir: {e}", "".red(), issue.number);
            stats.errors += 1;
            continue;
        }

        match fs::write(&spec_file, &spec_content) {
            Ok(_) => {
                let rel = spec_file.strip_prefix(root).unwrap_or(&spec_file).display();
                println!("{} #{}{}", "".green(), issue.number, rel);
                generator::generate_companion_files_for_spec(&spec_dir, &item.module_name);
                stats.imported += 1;
            }
            Err(e) => {
                println!("{} #{}: Failed to write spec: {e}", "".red(), issue.number);
                stats.errors += 1;
            }
        }
    }

    print_batch_summary("import", &stats);
}

/// Batch import all markdown files from a directory as spec drafts.
fn cmd_import_from_dir(root: &Path, dir: &Path) {
    let config = load_config(root);
    let specs_dir = root.join(&config.specs_dir);

    let dir = if dir.is_absolute() {
        dir.to_path_buf()
    } else {
        root.join(dir)
    };

    if !dir.exists() {
        eprintln!("{} Directory not found: {}", "Error:".red(), dir.display());
        process::exit(1);
    }

    println!(
        "\n--- {} -----------------------------------------------",
        "Batch Import: Directory".bold()
    );
    println!(
        "  {} Scanning {} for markdown files...",
        "".blue(),
        dir.display()
    );

    // Collect all .md files in the directory (non-recursive by default)
    let md_files = collect_markdown_files(&dir);

    if md_files.is_empty() {
        println!(
            "  {} No markdown files found in {}",
            "i".blue(),
            dir.display()
        );
        return;
    }

    println!(
        "  {} Found {} file(s) to import\n",
        "i".blue(),
        md_files.len()
    );

    let mut stats = BatchStats::default();
    let total = md_files.len();

    for (idx, file_path) in md_files.iter().enumerate() {
        let progress = format!("[{}/{}]", idx + 1, total);
        let filename = file_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown");
        print!("  {} {} ", progress.dimmed(), filename);

        let content = match fs::read_to_string(file_path) {
            Ok(c) => c,
            Err(e) => {
                println!("{} Failed to read: {e}", "".red());
                stats.errors += 1;
                continue;
            }
        };

        let item = parse_markdown_as_import_item(filename, &content);

        let spec_dir = specs_dir.join(&item.module_name);
        let spec_file = spec_dir.join(format!("{}.spec.md", item.module_name));

        if spec_file.exists() {
            println!("{} skipped — spec already exists", "~".yellow());
            stats.skipped += 1;
            continue;
        }

        let spec_content = importer::render_spec(&item);

        if let Err(e) = fs::create_dir_all(&spec_dir) {
            println!("{} Failed to create dir: {e}", "".red());
            stats.errors += 1;
            continue;
        }

        match fs::write(&spec_file, &spec_content) {
            Ok(_) => {
                let rel = spec_file.strip_prefix(root).unwrap_or(&spec_file).display();
                println!("{}{}", "".green(), rel);
                generator::generate_companion_files_for_spec(&spec_dir, &item.module_name);
                stats.imported += 1;
            }
            Err(e) => {
                println!("{} Failed to write spec: {e}", "".red());
                stats.errors += 1;
            }
        }
    }

    print_batch_summary("import", &stats);
}

/// Collect all .md files in a directory (one level deep).
fn collect_markdown_files(dir: &Path) -> Vec<PathBuf> {
    let mut files = Vec::new();
    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return files,
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_file()
            && path
                .extension()
                .and_then(|e| e.to_str())
                .map(|e| e == "md")
                .unwrap_or(false)
        {
            files.push(path);
        }
    }
    files.sort();
    files
}

/// Parse a markdown file into an ImportedItem for spec generation.
fn parse_markdown_as_import_item(filename: &str, content: &str) -> importer::ImportedItem {
    // Extract title from first H1 heading, fall back to filename
    let title = content
        .lines()
        .find(|l| l.starts_with("# "))
        .map(|l| l.trim_start_matches("# ").trim().to_string())
        .unwrap_or_else(|| filename.to_string());

    // Purpose: first non-empty paragraph after the title (or the title itself)
    let purpose = content
        .lines()
        .skip_while(|l| l.starts_with("# ") || l.trim().is_empty())
        .find(|l| !l.trim().is_empty())
        .unwrap_or(&title)
        .trim()
        .to_string();

    let requirements = importer::extract_requirements_pub(content);
    let module_name = importer::slugify(filename);

    importer::ImportedItem {
        module_name,
        purpose,
        requirements,
        labels: Vec::new(),
        source_url: String::new(),
        issue_number: None,
        source_type: importer::ImportSource::Confluence, // closest semantic match for "doc"
    }
}

fn print_batch_summary(operation: &str, stats: &BatchStats) {
    let total = stats.imported + stats.skipped + stats.errors;
    println!(
        "\n{} Batch {operation} complete: {} imported, {} skipped, {} error(s) ({} total)",
        "".blue(),
        stats.imported.to_string().green(),
        stats.skipped.to_string().yellow(),
        if stats.errors > 0 {
            stats.errors.to_string().red().to_string()
        } else {
            stats.errors.to_string()
        },
        total
    );
    if stats.imported > 0 {
        println!(
            "\n{} Run {} to validate imported specs.",
            "Tip:".cyan().bold(),
            "specsync check".bold()
        );
    }
}