pyro-artifacts 0.1.0

Cli commands for pyroduct
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
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use crate::artifacts::{
    Artifact, Artifacts, CapBinary, CapabilityBinary, CapabilitySource, Interface,
};
use crate::cache::{CacheError, CacheManager};
use crate::cargo::{CapabilityIdent, ProjectManifest};
use crate::debug::{self, CapabilityDebug, ModuleDebug};
use crate::{
    build::BuildError,
    command::{CommandError, format_syn_error, run_command},
};
use pyro_macro::{ffi::generate_capability, module::generate_module};
use pyro_spec::InterfaceSpec;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use thiserror::Error;
use tokio::fs;
use tokio::process::Command;

#[derive(Error, Debug)]
pub enum EnvironmentError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Cargo metadata failed: {0}")]
    Metadata(String),

    #[error(transparent)]
    CommandError(#[from] CommandError),

    #[error("Failed to parse or write: {0}")]
    Serde(String),

    #[error("Missing target directory in metadata")]
    MissingTargetDir,

    #[error("Artifact not found: {0}")]
    ArtifactNotFound(PathBuf),

    #[error("Utf8 error: {0}")]
    Utf8(#[from] std::string::FromUtf8Error),

    #[error("Failed to parse manifest: {0}")]
    ParseManifest(String),

    #[error("Interface generation failed: {0}")]
    InterfaceGeneration(String),

    #[error("Source not found: {0}")]
    SourceNotFound(PathBuf),

    #[error("Cache error: {0}")]
    Cache(#[from] CacheError),

    #[error("Build error: {0}")]
    Build(#[from] BuildError),
}

impl From<serde_json::Error> for EnvironmentError {
    fn from(value: serde_json::Error) -> Self {
        Self::Serde(value.to_string())
    }
}

pub type EnvResult<T> = std::result::Result<T, EnvironmentError>;

/// Central context to manage cargo compilation environment
pub struct Environment {
    pub root: PathBuf,
    pub target_dir: PathBuf,
    pub manifest: crate::cargo::ProjectManifest,
    pub cache_manager: Arc<CacheManager>,
}

impl Environment {
    /// Create a new Environment by fetching metadata and detecting manifests from the given root
    pub async fn new(root: PathBuf, cache_manager: Arc<CacheManager>) -> EnvResult<Self> {
        let manifest = Self::load_manifest(&root).await?;
        Self::ensure_cargo_toml(&root, &manifest, &cache_manager).await?;
        let target_dir = Self::get_target_dir(&root).await?;
        Ok(Self {
            root,
            target_dir,
            manifest,
            cache_manager,
        })
    }

    /// Write Cargo.toml from Module.toml or Capability.toml if it doesn't exist
    async fn ensure_cargo_toml(
        root: &Path,
        manifest: &crate::cargo::ProjectManifest,
        cache_manager: &CacheManager,
    ) -> EnvResult<()> {
        let cargo_toml_path = root.join("Cargo.toml");
        if cargo_toml_path.exists() {
            return Ok(());
        }
        let contents =
            toml::to_string_pretty(&manifest.clone().to_cargo_manifest(Some(cache_manager)))
                .map_err(|e| EnvironmentError::ParseManifest(e.to_string()))?;
        fs::write(&cargo_toml_path, contents).await?;
        Ok(())
    }

    pub fn name(&self) -> String {
        self.manifest.ident().name.clone()
    }

    pub fn version(&self) -> String {
        self.manifest.ident().version.clone()
    }

    pub fn author(&self) -> String {
        self.manifest.ident().author.clone()
    }

    /// Detect Capability.toml or Module.toml to extract name and version
    async fn load_manifest(root: &Path) -> EnvResult<ProjectManifest> {
        tracing::debug!("Loading manifest from {:?}", root);
        let capability_toml = root.join("Capability.toml");
        if capability_toml.exists() {
            let content = tokio::fs::read_to_string(&capability_toml).await?;
            let manifest: crate::cargo::CapabilityManifest = toml::from_str(&content)
                .map_err(|e| EnvironmentError::ParseManifest(format!("Capability.toml: {}", e)))?;
            return Ok(crate::cargo::ProjectManifest::Capability(manifest));
        }

        let module_toml = root.join("Module.toml");
        if module_toml.exists() {
            let content = tokio::fs::read_to_string(&module_toml).await?;
            let manifest: crate::cargo::ModuleManifest = toml::from_str(&content)
                .map_err(|e| EnvironmentError::ParseManifest(format!("Module.toml: {}", e)))?;
            return Ok(crate::cargo::ProjectManifest::Module(manifest));
        }

        // Default for anon compilations or when no package section is found
        Err(EnvironmentError::ParseManifest(
            "No manifest found".to_string(),
        ))
    }

    /// Run `cargo metadata` to find the target directory
    pub async fn get_target_dir(path: &Path) -> EnvResult<PathBuf> {
        let output = Command::new("cargo")
            .args(["metadata", "--format-version=1", "--no-deps"])
            .current_dir(path)
            .output()
            .await?;

        if !output.status.success() {
            return Err(EnvironmentError::Metadata(
                String::from_utf8_lossy(&output.stderr).to_string(),
            ));
        }

        let metadata: serde_json::Value = serde_json::from_slice(&output.stdout)?;

        metadata["target_directory"]
            .as_str()
            .map(PathBuf::from)
            .ok_or(EnvironmentError::MissingTargetDir)
    }

    pub async fn generate_lockfile(&self) -> EnvResult<String> {
        run_command(&self.root, &["generate-lockfile"], true).await?;

        Ok(fs::read_to_string(self.root.join("Cargo.lock")).await?)
    }

    /// Compile the project (defaults to release)
    pub async fn compile(&self, extra_args: &[&str], capture: bool) -> EnvResult<()> {
        let mut args = vec!["build", "--release"];
        args.extend_from_slice(extra_args);
        run_command(&self.root, &args, capture).await?;
        Ok(())
    }

    /// Get path to the compiled wasm artifact
    pub fn get_wasm_artifact(&self, name: &str) -> EnvResult<PathBuf> {
        let path = self
            .target_dir
            .join("wasm32-unknown-unknown")
            .join("release")
            .join(format!("{}.wasm", name.replace('-', "_")));

        if path.exists() {
            Ok(path)
        } else {
            Err(EnvironmentError::ArtifactNotFound(path))
        }
    }

    /// Get path to the compiled library artifact (dylib/so/dll)
    pub async fn get_library_artifact(&self, name: &str) -> EnvResult<CapBinary> {
        let ext = dylib_extension();
        let path =
            self.target_dir
                .join("release")
                .join(format!("lib{}.{}", name.replace('-', "_"), ext));
        if path.exists() {
            match ext {
                "dylib" => Ok(CapBinary::MachO(fs::read(&path).await?)),
                "so" => Ok(CapBinary::Elf(fs::read(&path).await?)),
                "dll" => Ok(CapBinary::Pe(fs::read(&path).await?)),
                _ => Err(EnvironmentError::ArtifactNotFound(path)),
            }
        } else {
            Err(EnvironmentError::ArtifactNotFound(path))
        }
    }

    /// Load artifacts from an existing target directory without compiling
    pub async fn load_artifacts_from_target(&self, target_dir: &Path) -> EnvResult<Vec<Artifacts>> {
        tracing::info!("Loading artifacts from target directory: {:?}", target_dir);

        let name = self.name();
        let version = self.version();
        let author = self.author();

        let src_path = self.root.join("src").join("lib.rs");
        let src_lib_rs = if src_path.exists() {
            fs::read_to_string(&src_path).await?
        } else {
            String::new()
        };

        match &self.manifest {
            crate::cargo::ProjectManifest::Capability(cap_manifest) => {
                let lib = self.get_library_artifact(&name).await.ok();

                let lock_path = self.root.join("Cargo.lock");
                let cargo_lock = if lock_path.exists() {
                    fs::read_to_string(&lock_path).await?
                } else {
                    String::new()
                };

                let (interface_rs, interface) =
                    pyro_macro::ffi::generate_interface(&src_lib_rs, &name, &version).map_err(
                        |r| EnvironmentError::InterfaceGeneration(format_syn_error(&src_lib_rs, r)),
                    )?;

                let interface_rs = prettyplease::unparse(&interface_rs);

                let mut artifacts = vec![
                    Artifacts::CapabilitySource(CapabilitySource {
                        manifest: cap_manifest.clone(),
                        cargo_toml: toml::to_string_pretty(
                            &cap_manifest.clone().to_capability_manifest(),
                        )
                        .map_err(|e| EnvironmentError::ParseManifest(e.to_string()))?,
                        cargo_lock,
                        src_lib_rs,
                    }),
                    Artifacts::Interface(Interface {
                        manifest: cap_manifest.clone(),
                        src_lib_rs: interface_rs,
                        interface: interface.clone(),
                    }),
                ];

                if let Some(lib) = lib {
                    artifacts.push(Artifacts::CapabilityBinary(CapabilityBinary {
                        ident: CapabilityIdent {
                            name,
                            version,
                            author,
                        },
                        libs: vec![lib],
                        interface: interface.clone(),
                    }));
                }

                Ok(artifacts)
            }
            crate::cargo::ProjectManifest::Module(module_manifest) => {
                let wasm_path = self.get_wasm_artifact(&name).ok();

                let source = crate::artifacts::ModuleSource {
                    dependencies: crate::artifacts::ModuleDependencies {
                        dependencies: module_manifest.dependencies.clone(),
                        capabilities: module_manifest.capabilities.values().cloned().collect(),
                    },
                    source: src_lib_rs.clone(),
                };
                let hash = source.hash();

                let mut artifacts =
                    vec![Artifacts::Module(crate::artifacts::Module::Source(source))];

                if let Some(path) = wasm_path {
                    let spec = pyro_macro::module::generate_module_spec(&src_lib_rs)
                        .map_err(|e| EnvironmentError::InterfaceGeneration(e.to_string()))?
                        .map(|func| crate::artifacts::ModuleSpec {
                            hash,
                            func,
                            capabilities: module_manifest.capabilities.values().cloned().collect(),
                        })
                        .ok_or_else(|| {
                            EnvironmentError::InterfaceGeneration(
                                "Module main function missing".to_string(),
                            )
                        })?;

                    let binary = crate::artifacts::ModuleBinary {
                        wasm: fs::read(path).await?,
                        spec,
                    };
                    artifacts.push(Artifacts::Module(crate::artifacts::Module::Binary(binary)));
                }

                Ok(artifacts)
            }
        }
    }

    pub async fn package(&self, capture: bool) -> EnvResult<Vec<Artifacts>> {
        let name = self.name();
        let version = self.version();
        let author = self.author();

        match &self.manifest {
            crate::cargo::ProjectManifest::Capability(cap_manifest) => {
                tracing::info!("Packaging capability: {:?}", self.root);

                let cargo_toml =
                    toml::to_string_pretty(&cap_manifest.clone().to_capability_manifest())
                        .map_err(|e| EnvironmentError::ParseManifest(e.to_string()))?;

                tracing::info!("Compiling capability binary...");
                self.compile(&["--features", "capability", "-p", &name], capture)
                    .await?;

                let lib = self.get_library_artifact(&name).await?;

                let lock_path = self.root.join("Cargo.lock");
                let cargo_lock = if lock_path.exists() {
                    fs::read_to_string(&lock_path).await?
                } else {
                    String::new()
                };

                let src_path = self.root.join("src").join("lib.rs");
                let src_lib_rs = if src_path.exists() {
                    fs::read_to_string(&src_path).await?
                } else {
                    String::new()
                };

                let (interface_rs, interface) =
                    pyro_macro::ffi::generate_interface(&src_lib_rs, &name, &version).map_err(
                        |r| EnvironmentError::InterfaceGeneration(format_syn_error(&src_lib_rs, r)),
                    )?;

                let interface_rs = prettyplease::unparse(&interface_rs);

                Ok(vec![
                    Artifacts::CapabilitySource(CapabilitySource {
                        manifest: cap_manifest.clone(),
                        cargo_toml,
                        cargo_lock,
                        src_lib_rs,
                    }),
                    Artifacts::CapabilityBinary(CapabilityBinary {
                        ident: CapabilityIdent {
                            name,
                            version,
                            author,
                        },
                        libs: vec![lib],
                        interface: interface.clone(),
                    }),
                    Artifacts::Interface(Interface {
                        manifest: cap_manifest.clone(),
                        src_lib_rs: interface_rs,
                        interface: interface.clone(),
                    }),
                ])
            }
            crate::cargo::ProjectManifest::Module(module_manifest) => {
                tracing::info!("Packaging module: {:?}", self.root);

                tracing::info!("Compiling module binary...");
                self.compile(&["--features", "module", "-p", &name], capture)
                    .await?;

                let wasm_artifact = self.get_wasm_artifact(&name)?;

                let src_path = self.root.join("src").join("lib.rs");
                let src_lib_rs = if src_path.exists() {
                    fs::read_to_string(&src_path).await?
                } else {
                    String::new()
                };

                let source = crate::artifacts::ModuleSource {
                    dependencies: crate::artifacts::ModuleDependencies {
                        dependencies: module_manifest.dependencies.clone(),
                        capabilities: module_manifest.capabilities.values().cloned().collect(),
                    },
                    source: src_lib_rs.clone(),
                };
                let hash = source.hash();

                let spec = pyro_macro::module::generate_module_spec(&src_lib_rs)
                    .map_err(|e| EnvironmentError::InterfaceGeneration(e.to_string()))?
                    .map(|func| crate::artifacts::ModuleSpec {
                        hash,
                        func,
                        capabilities: module_manifest.capabilities.values().cloned().collect(),
                    })
                    .ok_or_else(|| {
                        EnvironmentError::InterfaceGeneration(
                            "Module main function missing".to_string(),
                        )
                    })?;

                let binary = crate::artifacts::ModuleBinary {
                    wasm: fs::read(wasm_artifact).await?,
                    spec,
                };

                Ok(vec![
                    Artifacts::Module(crate::artifacts::Module::Source(source)),
                    Artifacts::Module(crate::artifacts::Module::Binary(binary)),
                ])
            }
        }
    }

    pub async fn expand_debug(&self) -> EnvResult<()> {
        let debug_dir = self.root.join("debug");
        fs::create_dir_all(&debug_dir).await?;

        match &self.manifest {
            crate::cargo::ProjectManifest::Capability(cap_manifest) => {
                tracing::info!("Generating debug info for capability: {}", self.name());
                let name = self.name();
                let version = self.version();

                let lib = self.get_library_artifact(&name).await?;

                let src_path = self.root.join("src").join("lib.rs");
                let src_lib_rs = fs::read_to_string(&src_path)
                    .await
                    .map_err(|_| EnvironmentError::SourceNotFound(src_path))?;

                let (_, interface) =
                    pyro_macro::ffi::generate_interface(&src_lib_rs, &name, &version).map_err(
                        |r| EnvironmentError::InterfaceGeneration(format_syn_error(&src_lib_rs, r)),
                    )?;

                let binary = CapabilityBinary {
                    ident: cap_manifest.capability.clone(),
                    libs: vec![lib],
                    interface,
                };

                let symbols = debug::symbols(&binary);

                let code = generate_capability(&src_lib_rs, &name, &version)
                    .map_err(|e| EnvironmentError::InterfaceGeneration(e.to_string()))?;

                let cap_rs = Some(prettyplease::unparse(&code));
                let debug_info = CapabilityDebug { symbols, cap_rs };
                debug_info.write_to_directory(&debug_dir).await?;
            }
            crate::cargo::ProjectManifest::Module(module_manifest) => {
                tracing::info!("Generating debug info for module: {}", self.name());
                let name = self.name();

                let wasm_path = self.get_wasm_artifact(&name)?;
                let wasm_bytes = fs::read(wasm_path).await?;

                let src_path = self.root.join("src").join("lib.rs");
                let src_lib_rs = fs::read_to_string(&src_path)
                    .await
                    .map_err(|_| EnvironmentError::SourceNotFound(src_path))?;

                let spec = pyro_macro::module::generate_module_spec(&src_lib_rs)
                    .map_err(|e| EnvironmentError::InterfaceGeneration(e.to_string()))?
                    .ok_or_else(|| {
                        EnvironmentError::InterfaceGeneration(
                            "Module main function missing".to_string(),
                        )
                    })?;

                let source = crate::artifacts::ModuleSource {
                    dependencies: crate::artifacts::ModuleDependencies {
                        dependencies: module_manifest.dependencies.clone(),
                        capabilities: module_manifest.capabilities.values().cloned().collect(),
                    },
                    source: src_lib_rs.clone(),
                };
                let hash = source.hash();

                let binary = crate::artifacts::ModuleBinary {
                    wasm: wasm_bytes,
                    spec: crate::artifacts::ModuleSpec {
                        hash,
                        func: spec,
                        capabilities: vec![], // Capabilities not needed for WAT/RS generation
                    },
                };

                let wat =
                    debug::wat(&binary).map_err(|e| EnvironmentError::InterfaceGeneration(e))?;

                let generated_code = generate_module(&src_lib_rs).map_err(|e| {
                    EnvironmentError::InterfaceGeneration(format!(
                        "Module code generation error: {}",
                        e
                    ))
                })?;
                let cap_rs = Some(prettyplease::unparse(&generated_code));

                let debug_info = ModuleDebug {
                    wat: Some(wat),
                    cap_rs,
                };
                debug_info.write_to_directory(&debug_dir).await?;
            }
        }

        Ok(())
    }

    pub async fn capability_spec(&self) -> EnvResult<InterfaceSpec<'static>> {
        let name = self.name();
        let version = self.version();
        let src_path = self.root.join("src").join("lib.rs");
        let src_lib_rs = fs::read_to_string(&src_path)
            .await
            .map_err(|_| EnvironmentError::SourceNotFound(src_path))?;

        match &self.manifest {
            crate::cargo::ProjectManifest::Capability(_) => {
                let (_, interface) =
                    pyro_macro::ffi::generate_interface(&src_lib_rs, &name, &version).map_err(
                        |r| EnvironmentError::InterfaceGeneration(format_syn_error(&src_lib_rs, r)),
                    )?;
                Ok(interface)
            }
            crate::cargo::ProjectManifest::Module(_) => Err(EnvironmentError::InterfaceGeneration(
                "Capability spec is only supported for capabilities".to_string(),
            )),
        }
    }

    pub async fn module_spec(&self) -> EnvResult<crate::artifacts::ModuleSpec> {
        let src_path = self.root.join("src").join("lib.rs");
        let src_lib_rs = fs::read_to_string(&src_path)
            .await
            .map_err(|_| EnvironmentError::SourceNotFound(src_path))?;

        match &self.manifest {
            crate::cargo::ProjectManifest::Module(module_manifest) => {
                let source = crate::artifacts::ModuleSource {
                    dependencies: crate::artifacts::ModuleDependencies {
                        dependencies: module_manifest.dependencies.clone(),
                        capabilities: module_manifest.capabilities.values().cloned().collect(),
                    },
                    source: src_lib_rs.clone(),
                };
                let hash = source.hash();

                pyro_macro::module::generate_module_spec(&src_lib_rs)
                    .map_err(|e| EnvironmentError::InterfaceGeneration(e.to_string()))?
                    .map(|func| crate::artifacts::ModuleSpec {
                        hash,
                        func,
                        capabilities: module_manifest.capabilities.values().cloned().collect(),
                    })
                    .ok_or_else(|| {
                        EnvironmentError::InterfaceGeneration(
                            "Module main function missing".to_string(),
                        )
                    })
            }
            crate::cargo::ProjectManifest::Capability(_) => {
                Err(EnvironmentError::InterfaceGeneration(
                    "Module spec is only supported for modules".to_string(),
                ))
            }
        }
    }
}

pub fn dylib_extension() -> &'static str {
    if cfg!(target_os = "macos") {
        "dylib"
    } else if cfg!(target_os = "windows") {
        "dll"
    } else {
        "so"
    }
}