vorpal-sdk 0.2.2

Rust SDK for building Vorpal artifacts.
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
use crate::{
    api::artifact::{ArtifactStepSecret, ArtifactSystem},
    artifact::{
        get_env_key, protoc::Protoc, rust_toolchain, rust_toolchain::RustToolchain, step, Artifact,
        ArtifactSource, DevelopmentEnvironment,
    },
    context::ConfigContext,
};
use anyhow::{bail, Result};
use indoc::formatdoc;
use serde::Deserialize;
use std::fs::read_to_string;
use toml::from_str;

#[derive(Debug, Deserialize)]
struct RustCargoToml {
    bin: Option<Vec<RustCargoTomlBinary>>,
    package: Option<RustCargoTomlPackage>,
    workspace: Option<RustCargoTomlWorkspace>,
}

#[derive(Debug, Deserialize)]
struct RustCargoTomlBinary {
    name: String,
    path: String,
}

#[derive(Debug, Deserialize)]
struct RustCargoTomlPackage {
    name: String,
    // version: String,
}

#[derive(Debug, Deserialize)]
struct RustCargoTomlWorkspace {
    members: Option<Vec<String>>,
}

pub struct Rust<'a> {
    artifacts: Vec<String>,
    bins: Vec<String>,
    build: bool,
    check: bool,
    environments: Vec<&'a str>,
    excludes: Vec<&'a str>,
    format: bool,
    includes: Vec<&'a str>,
    lint: bool,
    name: &'a str,
    packages: Vec<String>,
    secrets: Vec<ArtifactStepSecret>,
    source: Option<String>,
    tests: bool,
    systems: Vec<ArtifactSystem>,
}

fn parse_cargo(path: &str) -> Result<RustCargoToml> {
    let contents = read_to_string(path).expect("Failed to read Cargo.toml");

    Ok(from_str(&contents).expect("Failed to parse Cargo.toml"))
}

impl<'a> Rust<'a> {
    pub fn new(name: &'a str, systems: Vec<ArtifactSystem>) -> Self {
        Self {
            artifacts: vec![],
            bins: vec![],
            build: true,
            check: false,
            environments: vec![],
            excludes: vec![],
            format: false,
            includes: vec![],
            lint: false,
            name,
            packages: vec![],
            secrets: vec![],
            source: None,
            tests: false,
            systems,
        }
    }

    pub fn with_artifacts(mut self, artifacts: Vec<String>) -> Self {
        self.artifacts = artifacts;
        self
    }

    pub fn with_bins(mut self, bins: Vec<&str>) -> Self {
        self.bins = bins.iter().map(|s| s.to_string()).collect();
        self
    }

    pub fn with_check(mut self, check: bool) -> Self {
        self.check = check;
        self
    }

    pub fn with_environments(mut self, environments: Vec<&'a str>) -> Self {
        self.environments = environments;
        self
    }

    pub fn with_excludes(mut self, excludes: Vec<&'a str>) -> Self {
        self.excludes = excludes;
        self
    }

    pub fn with_format(mut self, format: bool) -> Self {
        self.format = format;
        self
    }

    pub fn with_includes(mut self, includes: Vec<&'a str>) -> Self {
        self.includes = includes;
        self
    }

    pub fn with_lint(mut self, lint: bool) -> Self {
        self.lint = lint;
        self
    }

    pub fn with_packages(mut self, packages: Vec<&'a str>) -> Self {
        self.packages = packages.iter().map(|s| s.to_string()).collect();
        self
    }

    pub fn with_secrets(mut self, secrets: Vec<(&str, &str)>) -> Self {
        for (name, value) in secrets {
            if !self.secrets.iter().any(|s| s.name == name) {
                self.secrets.push(ArtifactStepSecret {
                    name: name.to_string(),
                    value: value.to_string(),
                });
            }
        }

        self
    }

    pub fn with_source(mut self, source: String) -> Self {
        self.source = Some(source);
        self
    }

    pub fn with_tests(mut self, tests: bool) -> Self {
        self.tests = tests;
        self
    }

    pub async fn build(mut self, context: &mut ConfigContext) -> Result<String> {
        // Sort for deterministic output
        self.secrets.sort_by(|a, b| a.name.cmp(&b.name));

        let protoc = Protoc::new().build(context).await?;

        // Parse source path

        let context_path = context.get_artifact_context_path();

        let source_path = match self.source {
            Some(ref source) => source,
            None => ".",
        };

        let context_path_source = context_path.join(source_path);

        if !context_path_source.exists() {
            bail!("`source.{}.path` not found: {}", self.name, source_path);
        }

        // Parse cargo.toml

        let source_cargo_path = context_path_source.join("Cargo.toml");

        if !source_cargo_path.exists() {
            bail!("Cargo.toml not found: {:?}", source_cargo_path);
        }

        let source_cargo = parse_cargo(source_cargo_path.to_str().unwrap())?;

        // Get list of bin targets

        let mut packages = vec![];
        let mut packages_bin_names = vec![];
        let mut packages_manifests = vec![];
        let mut packages_targets = vec![];

        if let Some(workspace) = source_cargo.workspace {
            if let Some(members) = workspace.members {
                for member in members {
                    let package_path = context_path_source.join(member.clone());
                    let package_cargo_path = package_path.join("Cargo.toml");

                    if !package_cargo_path.exists() {
                        bail!("Cargo.toml not found: {:?}", package_cargo_path);
                    }

                    let package_cargo = parse_cargo(package_cargo_path.to_str().unwrap())?;

                    if !self.packages.is_empty() {
                        if let Some(ref package) = package_cargo.package {
                            if !self.packages.contains(&package.name) {
                                continue;
                            }
                        }
                    }

                    let mut package_target_paths = vec![];

                    if let Some(bins) = package_cargo.bin {
                        for bin in bins {
                            package_target_paths.push(package_path.join(bin.path));

                            if self.bins.is_empty() || self.bins.contains(&bin.name) {
                                let manifest_path = package_cargo_path.display().to_string();

                                if !packages_manifests.contains(&manifest_path) {
                                    packages_manifests.push(manifest_path);
                                }

                                packages_bin_names.push(bin.name);
                            }
                        }
                    }

                    if package_target_paths.is_empty() {
                        package_target_paths.push(package_path.join("src/lib.rs"));
                    }

                    for member_target_path in package_target_paths.iter() {
                        let member_target_path_relative = member_target_path
                            .strip_prefix(&context_path_source)
                            .unwrap_or(member_target_path)
                            .to_path_buf();

                        packages_targets.push(member_target_path_relative);
                    }

                    packages.push(member);
                }
            }
        }

        // 2. CREATE ARTIFACTS

        // Get rust toolchain artifact

        let rust_toolchain = RustToolchain::new().build(context).await?;
        let rust_toolchain_target = rust_toolchain::target(context.get_system())?;
        let rust_toolchain_version = rust_toolchain::version();
        let rust_toolchain_name = format!("{rust_toolchain_version}-{rust_toolchain_target}");

        // Set environment variables

        let mut step_artifacts = vec![rust_toolchain.clone()];

        let mut step_environments = vec![
            "HOME=$VORPAL_WORKSPACE/home".to_string(),
            format!(
                "PATH={}",
                format!(
                    "{}/toolchains/{}/bin",
                    get_env_key(&rust_toolchain),
                    rust_toolchain_name
                )
            ),
            format!("RUSTUP_HOME={}", get_env_key(&rust_toolchain)),
            format!("RUSTUP_TOOLCHAIN={}", rust_toolchain_name),
        ];

        for environment in self.environments {
            step_environments.push(environment.to_string());
        }

        // Create vendor artifact

        let mut vendor_cargo_paths = vec!["Cargo.toml".to_string(), "Cargo.lock".to_string()];

        for package in packages.iter() {
            vendor_cargo_paths.push(format!("{package}/Cargo.toml"));
        }

        let mut vendor_step_script = formatdoc! {r#"
            mkdir -p $HOME

            pushd ./source/{name}-vendor"#,
            name = self.name,
        };

        if !packages.is_empty() {
            vendor_step_script = formatdoc! {r#"
                {vendor_step_script}

                cat > Cargo.toml << "EOF"
                [workspace]
                members = [{packages}]
                resolver = "2"
                EOF

                target_paths=({target_paths})

                for target_path in ${{target_paths[@]}}; do
                    mkdir -p $(dirname ${{target_path}})
                    touch ${{target_path}}
                done"#,
                packages = packages.iter().map(|s| format!("\"{s}\"")).collect::<Vec<_>>().join(","),
                target_paths = packages_targets.iter().map(|s| format!("\"{}\"", s.display())).collect::<Vec<_>>().join(" "),
            };
        } else {
            vendor_step_script = formatdoc! {r#"
                {vendor_step_script}

                mkdir -p src
                touch src/main.rs"#,
            };
        }

        vendor_step_script = formatdoc! {r#"
            {vendor_step_script}

            mkdir -p $VORPAL_OUTPUT/vendor

            cargo_vendor=$(cargo vendor --versioned-dirs $VORPAL_OUTPUT/vendor)

            echo "$cargo_vendor" > $VORPAL_OUTPUT/config.toml"#,
        };

        let vendor_steps = vec![
            step::shell(
                context,
                step_artifacts.clone(),
                step_environments.clone(),
                vendor_step_script,
                self.secrets.clone(),
            )
            .await?,
        ];

        let vendor_name = format!("{}-vendor", self.name);

        let vendor_source = ArtifactSource::new(vendor_name.as_str(), source_path)
            .with_includes(vendor_cargo_paths.clone())
            .build();

        let vendor = Artifact::new(vendor_name.as_str(), vendor_steps, self.systems.clone())
            .with_sources(vec![vendor_source])
            .build(context)
            .await?;

        step_artifacts.push(vendor.clone());
        step_artifacts.push(protoc);

        // Create source

        let mut source_includes = vec![];
        let mut source_excludes = vec!["target".to_string()];

        for exclude in self.excludes {
            source_excludes.push(exclude.to_string());
        }

        for include in self.includes {
            source_includes.push(include.to_string());
        }

        let source = ArtifactSource::new(self.name, source_path)
            .with_includes(source_includes)
            .with_excludes(source_excludes)
            .build();

        // Create step

        let mut step_script = formatdoc! {r#"
            mkdir -p $HOME

            pushd ./source/{name}

            mkdir -p .cargo
            mkdir -p $VORPAL_OUTPUT/bin

            ln -s {vendor}/config.toml .cargo/config.toml"#,
            name = self.name,
            vendor = get_env_key(&vendor),
        };

        if !self.packages.is_empty() {
            step_script = formatdoc! {r#"
                {step_script}

                cat > Cargo.toml << "EOF"
                [workspace]
                members = [{packages}]
                resolver = "2"
                EOF"#,
                packages = packages.iter().map(|s| format!("\"{s}\"")).collect::<Vec<_>>().join(","),
            };
        }

        if packages_bin_names.is_empty() {
            packages_bin_names.push(self.name.to_string());
        }

        if packages_manifests.is_empty() {
            packages_manifests.push(source_cargo_path.display().to_string());
        }

        step_script = formatdoc! {r#"
            {step_script}

            bin_names=({bin_names})
            manifest_paths=({manifest_paths})

            if [ "{enable_format}" = "true" ]; then
                echo "Running formatter..."
                cargo --offline fmt --all --check
            fi

            for manifest_path in ${{manifest_paths[@]}}; do
                if [ "{enable_lint}" = "true" ]; then
                    echo "Running linter..."
                    cargo --offline clippy --manifest-path ${{manifest_path}} -- --deny warnings
                fi
            done

            for bin_name in ${{bin_names[@]}}; do
                if [ "{enable_check}" = "true" ]; then
                    echo "Running check..."
                    cargo --offline check --bin ${{bin_name}} --release
                fi

                if [ "{enable_build}" = "true" ]; then
                    echo "Running build..."
                    cargo --offline build --bin ${{bin_name}} --release
                fi

                if [ "{enable_tests}" = "true" ]; then
                    echo "Running tests..."
                    cargo --offline test --bin ${{bin_name}} --release
                fi

                cp -p ./target/release/${{bin_name}} $VORPAL_OUTPUT/bin/
            done"#,
            bin_names = packages_bin_names.join(" "),
            enable_build = if self.build { "true" } else { "false" },
            enable_check = if self.check { "true" } else { "false" },
            enable_format = if self.format { "true" } else { "false" },
            enable_lint = if self.lint { "true" } else { "false" },
            enable_tests = if self.tests { "true" } else { "false" },
            manifest_paths = packages_manifests.join(" "),
        };

        let steps = vec![
            step::shell(
                context,
                [step_artifacts.clone(), self.artifacts.clone()].concat(),
                step_environments,
                step_script,
                self.secrets,
            )
            .await?,
        ];

        // Create artifact

        Artifact::new(self.name, steps, self.systems)
            .with_sources(vec![source])
            .build(context)
            .await
    }
}

// ---------------------------------------------------------------------------
// Rust Development Environment
// ---------------------------------------------------------------------------

pub struct RustDevelopmentEnvironment<'a> {
    artifacts: Vec<String>,
    environments: Vec<String>,
    include_protoc: bool,
    name: &'a str,
    secrets: Vec<(&'a str, &'a str)>,
    systems: Vec<ArtifactSystem>,
}

impl<'a> RustDevelopmentEnvironment<'a> {
    pub fn new(name: &'a str, systems: Vec<ArtifactSystem>) -> Self {
        Self {
            artifacts: vec![],
            environments: vec![],
            include_protoc: true,
            name,
            secrets: vec![],
            systems,
        }
    }

    pub fn with_artifacts(mut self, artifacts: Vec<String>) -> Self {
        self.artifacts.extend(artifacts);
        self
    }

    pub fn with_environments(mut self, environments: Vec<String>) -> Self {
        self.environments.extend(environments);
        self
    }

    pub fn without_protoc(mut self) -> Self {
        self.include_protoc = false;
        self
    }

    pub fn with_secrets(mut self, secrets: Vec<(&'a str, &'a str)>) -> Self {
        for secret in secrets {
            if !self.secrets.iter().any(|(name, _)| *name == secret.0) {
                self.secrets.push(secret);
            }
        }
        self
    }

    pub async fn build(self, context: &mut ConfigContext) -> Result<String> {
        let rust_toolchain_digest = RustToolchain::new().build(context).await?;

        let mut artifacts = vec![];

        if self.include_protoc {
            let protoc = Protoc::new().build(context).await?;
            artifacts.push(protoc);
        }

        artifacts.push(rust_toolchain_digest.clone());
        artifacts.extend(self.artifacts);

        let toolchain_target = rust_toolchain::target(context.get_system())?;
        let toolchain_version = rust_toolchain::version();
        let toolchain_name = format!("{toolchain_version}-{toolchain_target}");
        let toolchain_bin = format!(
            "{}/toolchains/{toolchain_name}/bin",
            get_env_key(&rust_toolchain_digest)
        );

        let mut environments = vec![
            format!("PATH={toolchain_bin}"),
            format!("RUSTUP_HOME={}", get_env_key(&rust_toolchain_digest)),
            format!("RUSTUP_TOOLCHAIN={toolchain_name}"),
        ];

        environments.extend(self.environments);

        let mut devenv = DevelopmentEnvironment::new(self.name, self.systems)
            .with_artifacts(artifacts)
            .with_environments(environments);

        if !self.secrets.is_empty() {
            devenv = devenv.with_secrets(self.secrets);
        }

        devenv.build(context).await
    }
}