kcl-lib 0.2.186

KittyCAD Language implementation and tools
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
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
//! Run all the KCL samples in the `kcl_samples` directory.
use std::fs;
use std::panic::AssertUnwindSafe;
use std::panic::catch_unwind;
use std::path::Path;
use std::path::PathBuf;

use ahash::AHashSet;
use anyhow::Result;
use anyhow::bail;
use serde::Deserialize;
use serde::Serialize;
use walkdir::WalkDir;

use super::Test;
use crate::simulation_tests::TestConfig;
use crate::tooling::render_artifacts::RENDERED_MODEL_NAME;

const ALLOWED_FILETYPES: [&str; 3] = ["kcl", "stp", "step"];
const SAMPLE_CATEGORIES: [&str; 16] = [
    "Aerospace",
    "Architecture & Construction",
    "Art & Design",
    "Automotive & Transportation",
    "Electronics & Computing",
    "Home & Lifestyle",
    "Industrial & Manufacturing",
    "Mechanical Components",
    "Medical & Assistive",
    "Musical Instruments",
    "Robotics & Automation",
    "Science & Education",
    "Toys, Games, & Recreation",
    "3D Printable",
    "Parametric",
    "Tools",
];

lazy_static::lazy_static! {
    /// The directory containing the KCL samples source.
    static ref INPUTS_DIR: PathBuf = Path::new("../../public/kcl-samples").to_path_buf();
    /// The directory containing the expected output.  We keep them isolated in
    /// their own directory, separate from other simulation tests, so that we
    /// know whether we've checked them all.
    static ref OUTPUTS_DIR: PathBuf = Path::new("tests/kcl_samples").to_path_buf();
}

#[kcl_directory_test_macro::test_all_dirs("../public/kcl-samples")]
fn parse(dir_name: &str, dir_path: &Path) {
    let t = test(dir_name, dir_path.join("main.kcl"));
    let write_new = matches!(
        std::env::var("INSTA_UPDATE").as_deref(),
        Ok("auto" | "always" | "new" | "unseen")
    );
    if write_new {
        // Ensure the directory exists for new tests.
        std::fs::create_dir_all(t.output_dir.clone()).unwrap();
    }
    super::parse_test(&t);
}

#[kcl_directory_test_macro::test_all_dirs("../public/kcl-samples")]
async fn unparse(dir_name: &str, dir_path: &Path) {
    let t = test(dir_name, dir_path.join("main.kcl"));
    unparse_test(&t).await;
}

/// This is different from the rest of the simulation tests because we want to write
/// back out to the original file.
async fn unparse_test(test: &Test) {
    let kcl_files = crate::unparser::walk_dir(&test.input_dir).await.unwrap();
    let futures = kcl_files
        .into_iter()
        .filter(|file| file.extension().is_some_and(|ext| ext == "kcl")) // We only care about kcl
        // files here.
        .map(|file| {
            tokio::spawn(async move {
                let contents = tokio::fs::read_to_string(&file).await.unwrap();
                eprintln!("{}", file.display());
                let program = crate::Program::parse_no_errs(&contents).unwrap();
                let recast = program.recast_with_options(&Default::default());

                catch_unwind(AssertUnwindSafe(|| {
                    expectorate::assert_contents(&file, &recast.to_string());
                }))
            })
        })
        .collect::<Vec<_>>();

    // Join all futures and await their completion.
    for future in futures {
        future.await.unwrap().unwrap();
    }
}

#[kcl_directory_test_macro::test_all_dirs("../public/kcl-samples", exclude = ["walkie-talkie"])]
async fn kcl_test_execute(dir_name: &str, dir_path: &Path) {
    let t = test(dir_name, dir_path.join("main.kcl"));
    super::execute_test(&t, true).await;
}

/// The current engine times out on the walkie-talkie's exact 143-tool speaker
/// grille. Keep the real-engine regression available to run explicitly while
/// engine#4948 is in progress, without making unrelated sample CI intermittent.
#[tokio::test(flavor = "multi_thread")]
#[ignore = "blocked by https://github.com/KittyCAD/engine/issues/4948"]
async fn kcl_test_execute_walkie_talkie() {
    let dir_path = INPUTS_DIR.join("walkie-talkie");
    let t = test("walkie-talkie", dir_path.join("main.kcl"));
    super::execute_test(&t, true).await;
}

#[test]
fn test_after_engine_ensure_kcl_samples_manifest_etc() {
    let tests = kcl_samples_inputs();
    let expected_outputs = kcl_samples_outputs();

    // Ensure that inputs aren't missing.
    let input_names = AHashSet::from_iter(tests.iter().map(|t| t.name.clone()));
    let missing = expected_outputs
        .into_iter()
        .filter(|name| !input_names.contains(name))
        .collect::<Vec<_>>();
    assert!(
        missing.is_empty(),
        "Expected input kcl-samples for the following. If these are no longer tests, delete the expected output directories for them in {}: {missing:?}",
        OUTPUTS_DIR.to_string_lossy()
    );

    // We want to move the screenshot for the inputs to the public/kcl-samples
    // directory so that they can be used as inputs for the next run.
    // First ensure each directory exists.
    let public_screenshot_dir = INPUTS_DIR.join("screenshots");
    for dir in [&public_screenshot_dir] {
        if !dir.exists() {
            std::fs::create_dir_all(dir).unwrap();
        }
    }
    for tests in &tests {
        let screenshot_file = OUTPUTS_DIR.join(&tests.name).join(RENDERED_MODEL_NAME);
        if !screenshot_file.exists() {
            panic!("Missing screenshot for test: {}", tests.name);
        }
        std::fs::copy(
            screenshot_file,
            public_screenshot_dir.join(format!("{}.png", tests.name)),
        )
        .unwrap();
    }

    // Update the README.md with the new screenshots.
    let mut new_content = String::new();
    for test in tests {
        // Format:
        new_content.push_str(&format!(
            r#"#### [{}]({}/main.kcl) ([screenshot](screenshots/{}.png))
[![{}](screenshots/{}.png)]({}/main.kcl)
"#,
            test.name, test.name, test.name, test.name, test.name, test.name,
        ));
    }
    update_readme(&INPUTS_DIR, &new_content).unwrap();
}

#[test]
fn test_after_engine_generate_manifest() {
    // Generate the manifest.json
    generate_kcl_manifest(&INPUTS_DIR).unwrap();

    let manifest_path = INPUTS_DIR.join(MANIFEST_FILE);
    // Check that the JSON written was valid.
    let _manifest: Vec<KclMetadata> = serde_json::from_str(&fs::read_to_string(&manifest_path).unwrap()).unwrap();
}

#[test]
fn parse_sample_categories_preserves_commas_in_names() {
    assert_eq!(
        parse_sample_categories("Toys, Games, & Recreation; 3D Printable"),
        vec!["Toys, Games, & Recreation", "3D Printable"]
    );
}

fn test(test_name: &str, entry_point: std::path::PathBuf) -> Test {
    let parent = std::fs::canonicalize(entry_point.parent().unwrap()).unwrap();
    let inputs_dir = std::fs::canonicalize(INPUTS_DIR.as_path()).unwrap();
    let relative_path = parent.strip_prefix(inputs_dir).unwrap();
    let output_dir = std::fs::canonicalize(OUTPUTS_DIR.as_path()).unwrap();
    let test_config = TestConfig::from_file(&output_dir.join(test_name)).unwrap_or_default();
    let TestConfig {
        redact_uuids,
        kcl_versions,
    } = test_config;
    let mut relative_output_dir = output_dir.join(relative_path);
    if !kcl_versions.is_empty() {
        relative_output_dir = relative_output_dir.join("output");
    }

    // Ensure the output directory exists.
    if !relative_output_dir.exists() {
        std::fs::create_dir_all(&relative_output_dir).unwrap();
    }
    Test {
        redact_uuids,
        name: test_name.to_owned(),
        entry_point,
        input_dir: parent.to_path_buf(),
        output_dir: relative_output_dir,
        // Skip is temporary while we have non-deterministic output.
        skip_assert_artifact_graph: true,
        snapshot_physical_properties: true,
        expected_deprecation_warnings: Some(0),
        kcl_versions,
    }
}

fn kcl_samples_inputs() -> Vec<Test> {
    let mut tests = Vec::new();

    // Collect all directory entries first and sort them by name for consistent ordering
    let mut entries: Vec<_> = INPUTS_DIR
        .read_dir()
        .unwrap()
        .filter_map(Result::ok)
        .filter(|e| e.path().is_dir())
        .collect();

    // Sort directories by name for consistent ordering
    entries.sort_by_key(|a| a.file_name());

    for entry in entries {
        let path = entry.path();
        if !path.is_dir() {
            // We're looking for directories only.
            continue;
        }
        let Some(dir_name) = path.file_name() else {
            continue;
        };
        let dir_name_str = dir_name.to_string_lossy();
        if dir_name_str.starts_with('.') {
            // Skip hidden directories.
            continue;
        }
        if matches!(dir_name_str.as_ref(), "screenshots") {
            // Skip output directories.
            continue;
        }
        // Look for the entry point inside the directory.
        let sub_dir = INPUTS_DIR.join(dir_name);
        let main_kcl_path = sub_dir.join("main.kcl");
        let entry_point = if main_kcl_path.exists() {
            main_kcl_path
        } else {
            panic!("No main.kcl found in {sub_dir:?}");
        };
        tests.push(test(&dir_name_str, entry_point));
    }

    tests
}

fn kcl_samples_outputs() -> Vec<String> {
    let mut outputs = Vec::new();

    for entry in OUTPUTS_DIR.read_dir().unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();
        if !path.is_dir() {
            // We're looking for directories only.
            continue;
        }
        let Some(dir_name) = path.file_name() else {
            continue;
        };
        let dir_name_str = dir_name.to_string_lossy();
        if dir_name_str.starts_with('.') {
            // Skip hidden.
            continue;
        }

        outputs.push(dir_name_str.into_owned());
    }

    outputs
}

const MANIFEST_FILE: &str = "manifest.json";
const COMMENT_PREFIX: &str = "//";

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct KclMetadata {
    file: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    categories: Vec<String>,
    path_from_project_directory_to_first_file: String,
    multiple_files: bool,
    title: String,
    description: String,
    files: Vec<String>,
}

// Function to read and parse .kcl files
fn get_kcl_metadata(project_path: &Path, files: &[String]) -> Option<KclMetadata> {
    // Prefer the sample's root main.kcl
    let primary_kcl_file = files.iter().find(|file| file.contains("main.kcl"))?;

    let full_path_to_primary_kcl = project_path.join(primary_kcl_file);

    // Read the file content
    let content = match fs::read_to_string(&full_path_to_primary_kcl) {
        Ok(content) => content,
        Err(_) => return None,
    };

    let lines: Vec<&str> = content.lines().collect();

    if lines.len() < 2 {
        return None;
    }

    // Extract title, description, and categories from the first three lines
    let title = lines[0].trim_start_matches(COMMENT_PREFIX).trim().to_string();
    let description = lines[1].trim_start_matches(COMMENT_PREFIX).trim().to_string();
    let categories = if let Some(third_line) = lines.get(2)
        && let Some(categories_line) = third_line
            .trim_start_matches(COMMENT_PREFIX)
            .trim()
            .strip_prefix("Categories: ")
    {
        parse_sample_categories(categories_line)
    } else {
        Vec::new()
    };

    // Get the relative path from the project directory to the primary KCL file
    let path_from_project_dir = full_path_to_primary_kcl
        .strip_prefix(INPUTS_DIR.as_path())
        .unwrap_or(&full_path_to_primary_kcl)
        .to_string_lossy()
        .to_string();

    let mut files = files.to_vec();
    files.sort();

    Some(KclMetadata {
        file: primary_kcl_file.to_owned(),
        path_from_project_directory_to_first_file: path_from_project_dir,
        multiple_files: files.len() > 1,
        title,
        description,
        files,
        categories,
    })
}

fn parse_sample_categories(categories_line: &str) -> Vec<String> {
    categories_line
        .split(';')
        .map(str::trim)
        .filter(|category| !category.is_empty())
        .map(str::to_owned)
        .collect()
}

// Function to scan the directory and generate the manifest.json
fn generate_kcl_manifest(kcl_samples_root_dir: &Path) -> Result<()> {
    let mut manifest = Vec::new();

    // Collect all directory entries first
    let mut entries: Vec<_> = kcl_samples_root_dir.read_dir()?.filter_map(|e| e.ok()).collect();

    // Sort directories by name for consistent ordering
    entries.sort_by_key(|a| a.file_name().to_string_lossy().to_string());

    // Loop through top-level sample directories and add to manifest if KCL sample
    for entry in entries {
        let path = entry.path();

        if path.is_dir() {
            // Collect allowed files, preserving nested paths relative to sample dir.
            let files: Vec<String> = WalkDir::new(&path)
                .into_iter()
                .filter_map(Result::ok)
                .filter(|e| {
                    if let Some(ext) = e.path().extension() {
                        let ext = ext.to_string_lossy().to_lowercase();
                        ALLOWED_FILETYPES.contains(&ext.as_str())
                    } else {
                        false
                    }
                })
                .map(|e| {
                    e.path()
                        .strip_prefix(&path)
                        .unwrap_or(e.path())
                        .to_string_lossy()
                        .replace('\\', "/")
                })
                .collect();

            if files.is_empty() {
                continue;
            }

            if let Some(metadata) = get_kcl_metadata(&path, &files) {
                if let Some(category) = metadata
                    .categories
                    .iter()
                    .find(|category| !SAMPLE_CATEGORIES.contains(&category.as_str()))
                {
                    bail!("Unknown category {category:?} in {}", full_path_for_error(&path));
                }
                manifest.push(metadata);
            }
        }
    }

    // Write the manifest.json
    let output_path = kcl_samples_root_dir.join(MANIFEST_FILE);
    expectorate::assert_contents(&output_path, &serde_json::to_string_pretty(&manifest).unwrap());

    println!(
        "Manifest of {} items written to {}",
        manifest.len(),
        output_path.display()
    );

    Ok(())
}

fn full_path_for_error(path: &Path) -> String {
    path.join("main.kcl").to_string_lossy().into_owned()
}

/// Updates README.md by finding a specific search string and replacing all content after it
/// with the new content provided.
fn update_readme(dir: &Path, new_content: &str) -> Result<()> {
    let search_str = "---\n";
    let readme_path = dir.join("README.md");

    // Read the file content
    let content = fs::read_to_string(&readme_path)?;

    // Find the line containing the search string
    let Some(index) = content.find(search_str) else {
        anyhow::bail!(
            "Search string '{}' not found in `{}`",
            search_str,
            readme_path.display()
        );
    };

    // Get the position just after the search string
    let position = index + search_str.len();

    // Create the updated content
    let updated_content = format!("{}{}\n", &content[..position], new_content);

    // Write the modified content back to the file
    expectorate::assert_contents(&readme_path, &updated_content);

    Ok(())
}