axoproject 0.32.0

project detection logic for various axo.dev applications
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
//! Support for generic projects with dist build instructions

use axoasset::{AxoassetError, SourceFile};
use camino::{Utf8Path, Utf8PathBuf};
use serde::Deserialize;

use crate::{
    errors::GenericManifestParseError, PackageInfo, Result, Version, WorkspaceInfo,
    WorkspaceSearch, WorkspaceStructure,
};

const DIST_PACKAGE_TOML: &str = "dist.toml";
const DIST_WORKSPACE_TOML: &str = "dist-workspace.toml";
const DIST_TARGET_DIR: &str = "target";

const MEMBER_GENERIC: &str = "dist";
#[cfg(feature = "cargo-projects")]
const MEMBER_CARGO: &str = "cargo";
#[cfg(feature = "npm-projects")]
const MEMBER_NPM: &str = "npm";

#[derive(Deserialize, Debug)]
struct WorkspaceManifest {
    workspace: Option<Workspace>,
    package: Option<Package>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
struct Workspace {
    members: Vec<WorkspaceMember>,
    /// If non-empty, reject the false gods of `dist = true/false`
    /// and force distability of packages based on name-matching this list.
    #[serde(default)]
    packages: Vec<String>,
    /// If set, force every package to have this version
    /// (note that the true_version still exists to remember the truth).
    version: Option<semver::Version>,
}

#[derive(Debug)]
enum WorkspaceMember {
    Generic(Utf8PathBuf),
    #[cfg(feature = "cargo-projects")]
    Cargo(Utf8PathBuf),
    #[cfg(feature = "npm-projects")]
    Npm(Utf8PathBuf),
}

impl std::str::FromStr for WorkspaceMember {
    type Err = GenericManifestParseError;
    fn from_str(member: &str) -> std::result::Result<Self, GenericManifestParseError> {
        let Some((kind, path)) = member.split_once(':') else {
            return Err(GenericManifestParseError::NoPrefix {
                val: member.to_owned(),
            });
        };
        let output = match kind {
            MEMBER_GENERIC => WorkspaceMember::Generic(path.into()),
            #[cfg(feature = "cargo-projects")]
            MEMBER_CARGO => WorkspaceMember::Cargo(path.into()),
            #[cfg(feature = "npm-projects")]
            MEMBER_NPM => WorkspaceMember::Npm(path.into()),
            other => {
                return Err(GenericManifestParseError::UnknownPrefix {
                    prefix: other.to_owned(),
                    val: member.to_owned(),
                });
            }
        };
        Ok(output)
    }
}

impl std::fmt::Display for WorkspaceMember {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WorkspaceMember::Generic(path) => write!(f, "{MEMBER_GENERIC}:{path}"),
            #[cfg(feature = "cargo-projects")]
            WorkspaceMember::Cargo(path) => write!(f, "{MEMBER_CARGO}/{path}"),
            #[cfg(feature = "npm-projects")]
            WorkspaceMember::Npm(path) => write!(f, "${MEMBER_NPM}/{path}"),
        }
    }
}

impl serde::Serialize for WorkspaceMember {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> serde::Deserialize<'de> for WorkspaceMember {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Error;

        let path = String::deserialize(deserializer)?;
        path.parse().map_err(|e| D::Error::custom(format!("{e}")))
    }
}

#[derive(Deserialize, Debug)]
struct PackageManifest {
    package: Option<Package>,
}

#[derive(Deserialize, Debug, Default)]
#[serde(rename_all = "kebab-case")]
struct Package {
    name: Option<String>,
    repository: Option<String>,
    homepage: Option<String>,
    documentation: Option<String>,
    description: Option<String>,
    readme: Option<Utf8PathBuf>,
    authors: Option<Vec<String>>,
    binaries: Option<Vec<String>>,
    out_dir: Option<String>,
    license: Option<String>,
    changelog: Option<Utf8PathBuf>,
    license_files: Option<Vec<Utf8PathBuf>>,
    cstaticlibs: Option<Vec<String>>,
    cdylibs: Option<Vec<String>>,
    build_command: Option<Vec<String>>,
    version: Option<semver::Version>,
}

/// Try to find a generic workspace at start_dir, walking up
/// ancestors as necessary until we reach clamp_to_dir (or run out of ancestors).
///
/// Behaviour is unspecified if only part of the workspace is nested in clamp_to_dir
/// We might find the workspace, or we might not. This is generally assumed to be fine,
/// since we typically clamp to a git repo, if at all.
pub fn get_workspace(start_dir: &Utf8Path, clamp_to_dir: Option<&Utf8Path>) -> WorkspaceSearch {
    // First search for a workspace file
    match crate::find_file(DIST_WORKSPACE_TOML, start_dir, clamp_to_dir) {
        // Found a workspace file, read it
        Ok(manifest_path) => match workspace_from(&manifest_path) {
            Ok(info) => WorkspaceSearch::Found(info),
            Err(e) => WorkspaceSearch::Broken {
                manifest_path,
                cause: e,
            },
        },
        // No workspace file, maybe there's just a package?
        Err(_) => match crate::find_file(DIST_PACKAGE_TOML, start_dir, clamp_to_dir) {
            // Ok, make a faux-workspace from that
            Ok(manifest_path) => match single_package_workspace_from(&manifest_path) {
                Ok(info) => WorkspaceSearch::Found(info),
                Err(e) => WorkspaceSearch::Broken {
                    manifest_path,
                    cause: e,
                },
            },
            Err(e) => WorkspaceSearch::Missing(e),
        },
    }
}

// Load and process a dist-workspace.toml, and its child packages
fn workspace_from(manifest_path: &Utf8Path) -> Result<WorkspaceStructure> {
    use serde::de::Error;

    let manifest = load_workspace_dist_toml(manifest_path)?;

    // Create dummy src and span for field errors
    let source = SourceFile::new(manifest_path.as_str(), String::new());
    let span = source.span_for_line_col(1, 1);

    if let Some(workspace) = manifest.workspace {
        // dist-workspace.toml for generic projects with
        // both [workspace] and [package]
        if let Some(package) = manifest.package {
            let package = process_package(manifest_path, package, false)?;
            upgrade_package_to_workspace(package)
        } else {
            process_virtual_workspace(manifest_path, workspace)
        }
    } else if let Some(package) = manifest.package {
        let package = process_package(manifest_path, package, true)?;
        upgrade_package_to_workspace(package)
    } else {
        Err(AxoassetError::Toml {
            source,
            span,
            details: axoasset::toml::de::Error::custom(
                "dist-workspace.toml must have one of [workspace] or [package]",
            ),
        })?
    }
}

fn process_virtual_workspace(
    manifest_path: &Utf8Path,
    workspace: Workspace,
) -> Result<WorkspaceStructure> {
    let workspace_dir = manifest_path.parent().unwrap().to_path_buf();
    let root_auto_includes = crate::find_auto_includes(&workspace_dir)?;

    let mut package_info = vec![];
    let mut sub_workspaces = vec![];
    for member in &workspace.members {
        match member {
            WorkspaceMember::Generic(member_reldir) => {
                let member_dir = workspace_dir.join(member_reldir);
                let member_manifest_path = member_dir.join(DIST_PACKAGE_TOML);
                let mut package = package_from(&member_manifest_path)?;
                crate::merge_auto_includes(&mut package, &root_auto_includes);

                // If `workspace.packages` is set, set distability overrides
                if !workspace.packages.is_empty() {
                    package.dist = Some(workspace.packages.contains(&package.name));
                }
                // If `workspace.version` is set, force every package to have this version
                if let Some(version_override) = &workspace.version {
                    package.version = Some(Version::Cargo(version_override.clone()));
                }

                package_info.push(package);
            }
            #[cfg(feature = "cargo-projects")]
            WorkspaceMember::Cargo(member_reldir) => {
                let cargo_workspace_dir = workspace_dir.join(member_reldir);
                let search =
                    crate::rust::get_workspace(&cargo_workspace_dir, Some(&cargo_workspace_dir))
                        .into_result()?;
                sub_workspaces.push(search);
            }
            #[cfg(feature = "npm-projects")]
            WorkspaceMember::Npm(member_reldir) => {
                // First load the npm package(s)
                let npm_workspace_dir = workspace_dir.join(member_reldir);
                let search =
                    crate::javascript::get_workspace(&npm_workspace_dir, Some(&npm_workspace_dir))
                        .into_result()?;
                sub_workspaces.push(search);
            }
        }
    }
    for sub_workspace in &mut sub_workspaces {
        // Process packages
        for package in &mut sub_workspace.packages {
            // If there's a dist.toml in the same dir, load it with less validation
            // and merge the results into the npm package
            let paired_manifest = package.package_root.join(DIST_PACKAGE_TOML);
            if paired_manifest.exists() {
                let generic = raw_package_from(&paired_manifest)?;
                merge_package_with_raw_generic(package, generic, paired_manifest);
            }
            crate::merge_auto_includes(package, &root_auto_includes);

            // If `workspace.packages` is set, set distability overrides
            if !workspace.packages.is_empty() {
                package.dist = Some(workspace.packages.contains(&package.name));
            }
            // If `workspace.version` is set, force every package to have this version
            if let Some(version_override) = &workspace.version {
                package.version = Some(Version::Cargo(version_override.clone()));
            }
        }
    }

    Ok(WorkspaceStructure {
        sub_workspaces,
        packages: package_info,
        workspace: WorkspaceInfo {
            kind: crate::WorkspaceKind::Generic,
            target_dir: workspace_dir.join(DIST_TARGET_DIR),
            workspace_dir,
            manifest_path: manifest_path.to_owned(),
            dist_manifest_path: Some(manifest_path.to_owned()),
            root_auto_includes,
            #[cfg(feature = "cargo-projects")]
            cargo_metadata_table: None,
            #[cfg(feature = "cargo-projects")]
            cargo_profiles: crate::rust::CargoProfiles::new(),
        },
    })
}

// Load and process a dist.toml, and treat it as an entire workspace
//
// This is slated to be deprecated! (we'll auto-migrate these to dist-workspace.toml)
fn single_package_workspace_from(manifest_path: &Utf8Path) -> Result<WorkspaceStructure> {
    use serde::de::Error;

    // Pretend this is a dist-workspace.toml so that we can notice if `[workspace]` is set
    // and then error because that's not supported
    let manifest = load_workspace_dist_toml(manifest_path)?;

    // Create dummy src and span for field errors
    let source = SourceFile::new(manifest_path.as_str(), String::new());
    let span = source.span_for_line_col(1, 1);

    if manifest.workspace.is_some() {
        Err(AxoassetError::Toml {
            source,
            span,
            details: axoasset::toml::de::Error::custom(
                "dist.toml can't have a [workspace], only dist-workspace.toml can",
            ),
        })?
    } else if let Some(package) = manifest.package {
        let package = process_package(manifest_path, package, true)?;
        upgrade_package_to_workspace(package)
    } else {
        Err(AxoassetError::Toml {
            source,
            span,
            details: axoasset::toml::de::Error::custom("standalone dist.toml must have [package]\n(we think this is a workspace dist.toml, which is deprecated -- rename it to dist-workspace.toml?)"),
        })?
    }
}

/// Given a package, upgrade it to a workspace
fn upgrade_package_to_workspace(package: PackageInfo) -> Result<WorkspaceStructure> {
    let root_auto_includes = crate::find_auto_includes(&package.package_root)?;
    Ok(WorkspaceStructure {
        workspace: WorkspaceInfo {
            kind: crate::WorkspaceKind::Generic,
            target_dir: package.package_root.join(DIST_TARGET_DIR),
            workspace_dir: package.package_root.clone(),
            manifest_path: package.manifest_path.clone(),
            dist_manifest_path: Some(package.manifest_path.clone()),
            root_auto_includes,

            #[cfg(feature = "cargo-projects")]
            cargo_metadata_table: None,
            #[cfg(feature = "cargo-projects")]
            cargo_profiles: Default::default(),
        },
        sub_workspaces: vec![],
        packages: vec![package],
    })
}

fn raw_package_from(manifest_path: &Utf8Path) -> Result<Package> {
    let manifest = load_package_dist_toml(manifest_path)?;
    Ok(manifest.package.unwrap_or_default())
}

// Load and process a dist.toml
fn package_from(manifest_path: &Utf8Path) -> Result<PackageInfo> {
    let package = raw_package_from(manifest_path)?;
    process_package(manifest_path, package, true)
}

fn process_package(
    manifest_path: &Utf8Path,
    package: Package,
    use_workspace_manifest: bool,
) -> Result<PackageInfo> {
    use serde::de::Error;

    let version = package.version.map(Version::Generic);

    let manifest_path = manifest_path.to_path_buf();

    // Create dummy src and span for missing field errors
    let source = SourceFile::new(manifest_path.as_str(), String::new());
    let span = source.span_for_line_col(1, 1);
    let Some(build_command) = package.build_command else {
        return Err(AxoassetError::Toml {
            source,
            span,
            details: axoasset::toml::de::Error::custom("missing field 'build-command'"),
        })?;
    };
    let Some(name) = package.name else {
        return Err(AxoassetError::Toml {
            source,
            span,
            details: axoasset::toml::de::Error::custom("missing field 'name'"),
        })?;
    };

    let dist_manifest_path = use_workspace_manifest.then(|| manifest_path.clone());

    let mut info = PackageInfo {
        true_name: name.clone(),
        true_version: version.clone(),
        manifest_path: manifest_path.clone(),
        dist_manifest_path,
        package_root: manifest_path.parent().unwrap().to_owned(),
        name,
        version,
        description: package.description,
        authors: package.authors.unwrap_or_default(),
        license: package.license,
        publish: true,
        keywords: None,
        repository_url: package.repository.clone(),
        homepage_url: package.homepage,
        documentation_url: package.documentation,
        readme_file: package.readme,
        license_files: package.license_files.unwrap_or_default(),
        changelog_file: package.changelog,
        binaries: package.binaries.unwrap_or_default(),
        out_dir: package.out_dir,
        cstaticlibs: package.cstaticlibs.unwrap_or_default(),
        cdylibs: package.cdylibs.unwrap_or_default(),
        build_command: Some(build_command),
        #[cfg(feature = "cargo-projects")]
        cargo_metadata_table: None,
        #[cfg(feature = "cargo-projects")]
        cargo_package_id: None,
        npm_scope: None,
        axoupdater_versions: Default::default(),
        dist: None,
    };

    // Load and apply auto-includes
    let auto_includes = crate::find_auto_includes(&info.package_root)?;
    crate::merge_auto_includes(&mut info, &auto_includes);

    Ok(info)
}

/// Load the root workspace toml
fn load_workspace_dist_toml(manifest_path: &Utf8Path) -> Result<WorkspaceManifest> {
    let manifest_src = SourceFile::load_local(manifest_path)?;
    let manifest = manifest_src.deserialize_toml()?;
    Ok(manifest)
}

/// Load the a package toml
fn load_package_dist_toml(manifest_path: &Utf8Path) -> Result<PackageManifest> {
    let manifest_src = SourceFile::load_local(manifest_path)?;
    let manifest = manifest_src.deserialize_toml()?;
    Ok(manifest)
}

fn merge_package_with_raw_generic(
    package: &mut PackageInfo,
    generic: Package,
    generic_manifest_path: Utf8PathBuf,
) {
    let Package {
        name,
        repository,
        homepage,
        documentation,
        description,
        readme,
        authors,
        binaries,
        out_dir,
        license,
        changelog,
        license_files,
        cstaticlibs,
        cdylibs,
        build_command,
        version,
    } = generic;

    package.dist_manifest_path = Some(generic_manifest_path);

    if let Some(val) = name {
        package.name = val;
    }
    if let Some(val) = repository {
        package.repository_url = Some(val);
    }
    if let Some(val) = homepage {
        package.homepage_url = Some(val);
    }
    if let Some(val) = documentation {
        package.documentation_url = Some(val);
    }
    if let Some(val) = description {
        package.description = Some(val);
    }
    if let Some(val) = readme {
        package.readme_file = Some(val);
    }
    if let Some(val) = changelog {
        package.changelog_file = Some(val);
    }
    if let Some(val) = authors {
        package.authors = val;
    }
    if let Some(val) = binaries {
        package.binaries = val;
    }
    if let Some(val) = out_dir {
        package.out_dir = Some(val);
    }
    if let Some(val) = license {
        package.license = Some(val);
    }
    if let Some(val) = license_files {
        package.license_files = val;
    }
    if let Some(val) = cstaticlibs {
        package.cstaticlibs = val;
    }
    if let Some(val) = cdylibs {
        package.cdylibs = val;
    }
    if let Some(val) = build_command {
        package.build_command = Some(val);
    }
    if let Some(val) = version {
        package.version = Some(Version::Generic(val));
    }
}