veryl-metadata 0.20.0

A modern hardware description language
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
use crate::build::{Build, Target};
use crate::build_info::BuildInfo;
use crate::doc::Doc;
use crate::format::Format;
use crate::git::Git;
use crate::lint::Lint;
use crate::lockfile::Lockfile;
use crate::project::Project;
use crate::pubfile::{Pubfile, Release};
use crate::publish::Publish;
use crate::synth::Synth;
use crate::test::Test;
use crate::{FilelistType, MetadataError, SourceMapTarget};
use log::{debug, info, warn};
use once_cell::sync::Lazy;
use regex::Regex;
use semver::VersionReq;
use serde::{Deserialize, Serialize};
use spdx::Expression;
use std::collections::HashMap;
use std::env;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::SystemTime;
use url::Url;
use veryl_path::{PathSet, ignore_already_exists};

#[derive(Clone, Copy, Debug)]
pub enum BumpKind {
    Major,
    Minor,
    Patch,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Metadata {
    pub project: Project,
    #[serde(default)]
    pub build: Build,
    #[serde(default)]
    pub format: Format,
    #[serde(default)]
    pub lint: Lint,
    #[serde(default)]
    pub publish: Publish,
    #[serde(default)]
    pub doc: Doc,
    #[serde(default)]
    pub test: Test,
    #[serde(default)]
    pub synth: Synth,
    #[serde(default)]
    pub dependencies: HashMap<String, Dependency>,
    #[serde(skip)]
    pub metadata_path: PathBuf,
    #[serde(skip)]
    pub pubfile_path: PathBuf,
    #[serde(skip)]
    pub pubfile: Pubfile,
    #[serde(skip)]
    pub lockfile_path: PathBuf,
    #[serde(skip)]
    pub lockfile: Lockfile,
    #[serde(skip)]
    pub build_info: BuildInfo,
}

#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[serde(untagged)]
pub enum UrlPath {
    Url(Url),
    Path(PathBuf),
}

impl fmt::Display for UrlPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            UrlPath::Url(x) => x.fmt(f),
            UrlPath::Path(x) => {
                let text = x.to_string_lossy();
                text.fmt(f)
            }
        }
    }
}

static VALID_PROJECT_NAME: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^[a-zA-Z_][0-9a-zA-Z_]*$").unwrap());

fn check_project_name(name: &str) -> Result<(), MetadataError> {
    if !VALID_PROJECT_NAME.is_match(name) {
        return Err(MetadataError::InvalidProjectName(name.to_string()));
    }
    if name.starts_with("__") {
        return Err(MetadataError::ReservedProjectName(name.to_string()));
    }
    Ok(())
}

impl Metadata {
    pub fn search_from_current() -> Result<PathBuf, MetadataError> {
        Metadata::search_from(
            env::current_dir().map_err(|x| MetadataError::file_io(x, &PathBuf::from(".")))?,
        )
    }

    pub fn search_from<T: AsRef<Path>>(from: T) -> Result<PathBuf, MetadataError> {
        for path in from.as_ref().ancestors() {
            let path = path.join("Veryl.toml");
            if path.is_file() {
                return Ok(path);
            }
        }

        Err(MetadataError::FileNotFound)
    }

    pub fn load<T: AsRef<Path>>(path: T) -> Result<Self, MetadataError> {
        let path = path
            .as_ref()
            .canonicalize()
            .map_err(|x| MetadataError::file_io(x, path.as_ref()))?;
        let text = fs::read_to_string(&path).map_err(|x| MetadataError::file_io(x, &path))?;
        let mut metadata: Metadata = Self::from_str(&text)?;
        metadata.metadata_path.clone_from(&path);
        metadata.pubfile_path = path.with_file_name("Veryl.pub");
        metadata.lockfile_path = path.with_file_name("Veryl.lock");
        metadata.check()?;

        if metadata.pubfile_path.exists() {
            metadata.pubfile = Pubfile::load(&metadata.pubfile_path)?;
        }

        let dot_build = metadata.project_dot_build_path();
        if !dot_build.exists() {
            ignore_already_exists(fs::create_dir(&dot_build))
                .map_err(|x| MetadataError::file_io(x, &dot_build))?;
        }

        let build_info = metadata.project_build_info_path();
        if build_info.exists() {
            if let Ok(info) = BuildInfo::load(&build_info) {
                metadata.build_info = info;
            } else {
                // ignore failure of loading BuildInfo
                info!("Discarded incompatible .build/info.toml");
            }
        }

        debug!(
            "Loaded metadata ({})",
            metadata.metadata_path.to_string_lossy()
        );
        Ok(metadata)
    }

    pub fn publish(&mut self) -> Result<(), MetadataError> {
        let prj_path = self.project_path();
        let git = Git::open(&prj_path)?;
        if !git.is_clean()? {
            return Err(MetadataError::ModifiedProject(prj_path.to_path_buf()));
        }

        let version = self
            .project
            .version
            .clone()
            .ok_or(MetadataError::MissingVersion)?;

        for release in &self.pubfile.releases {
            if release.version == version {
                return Err(MetadataError::PublishedVersion(version));
            }
        }

        let revision = git.get_revision()?;

        info!("Publishing release ({version} @ {revision})");

        let release = Release { version, revision };

        self.pubfile.releases.push(release);

        self.pubfile.save(&self.pubfile_path)?;
        info!("Writing metadata ({})", self.pubfile_path.to_string_lossy());

        if self.publish.publish_commit {
            git.add(&self.pubfile_path)?;
            git.commit(&self.publish.publish_commit_message)?;
            info!(
                "Committing metadata ({})",
                self.pubfile_path.to_string_lossy()
            );
        }

        Ok(())
    }

    pub fn check(&self) -> Result<(), MetadataError> {
        check_project_name(&self.project.name)?;

        if let Some(ref license) = self.project.license {
            let _ = Expression::parse(license)?;
        }

        Ok(())
    }

    pub fn bump_version(&mut self, kind: BumpKind) -> Result<(), MetadataError> {
        let prj_path = self.project_path();
        let git = Git::open(&prj_path)?;

        let current_version = self
            .project
            .version
            .as_ref()
            .ok_or(MetadataError::MissingVersion)?;

        let mut bumped_version = current_version.clone();

        match kind {
            BumpKind::Major => {
                bumped_version.major += 1;
                bumped_version.minor = 0;
                bumped_version.patch = 0;
            }
            BumpKind::Minor => {
                bumped_version.minor += 1;
                bumped_version.patch = 0;
            }
            BumpKind::Patch => bumped_version.patch += 1,
        }
        info!(
            "Bumping version ({} -> {})",
            current_version, bumped_version
        );

        self.project.version = Some(bumped_version.clone());

        let toml = fs::read_to_string(&self.metadata_path)
            .map_err(|x| MetadataError::file_io(x, &self.metadata_path))?;
        let re = Regex::new(r#"version\s+=\s+"([^"]*)""#).unwrap();
        let caps = re
            .captures(&toml)
            .expect("safely unwrap because metadata is valid");
        let bumped_field = caps[0].replace(&caps[1], &bumped_version.to_string());
        let bumped_toml = re.replace(&toml, bumped_field);
        fs::write(&self.metadata_path, bumped_toml.as_bytes())
            .map_err(|x| MetadataError::file_io(x, &self.metadata_path))?;
        info!(
            "Updating version field ({})",
            self.metadata_path.to_string_lossy()
        );

        if self.publish.bump_commit {
            git.add(&self.metadata_path)?;
            git.commit(&self.publish.bump_commit_message)?;
            info!(
                "Committing metadata ({})",
                self.metadata_path.to_string_lossy()
            );
        }

        Ok(())
    }

    pub fn update_lockfile(&mut self) -> Result<(), MetadataError> {
        let modified = if self.lockfile_path.exists() {
            let mut lockfile = Lockfile::load(self)?;
            let modified = lockfile.update(self, false)?;
            self.lockfile = lockfile;
            modified
        } else {
            self.lockfile = Lockfile::new(self)?;
            true
        };
        if modified {
            self.lockfile.save(&self.lockfile_path)?;
        }
        Ok(())
    }

    pub fn save_build_info(&mut self) -> Result<(), MetadataError> {
        let build_info = self.project_build_info_path();
        self.build_info.save(&build_info)
    }

    pub fn add_generated_file(&mut self, path: PathBuf) {
        self.build_info
            .generated_files
            .insert(path, SystemTime::now());
    }

    pub fn paths<T: AsRef<Path>>(
        &mut self,
        files: &[T],
        symlink: bool,
        include_dependencies: bool,
    ) -> Result<Vec<PathSet>, MetadataError> {
        let sources = if self.build.source.iter().count() > 0 {
            warn!(
                "[Veryl.toml] \"source\" field is deprecated. Replace it with \"sources\" field."
            );
            vec![self.build.source.clone()]
        } else {
            self.build.sources.clone()
        };

        let base = self.project_path();
        let mut ret = Vec::new();

        // Pre-canonicalize explicit file args once so we can route each to
        // the source dir it actually belongs to (without re-processing the
        // same file for every configured source dir).
        let canonical_files = if files.is_empty() {
            None
        } else {
            let mut v = Vec::new();
            for file in files {
                v.push(
                    fs::canonicalize(file.as_ref())
                        .map_err(|x| MetadataError::file_io(x, file.as_ref()))?,
                );
            }
            Some(v)
        };
        let mut explicit_routed = canonical_files.as_ref().map(|v| vec![false; v.len()]);

        for source in &sources {
            let src_base = base.join(source);

            let src_files = if let Some(cf) = canonical_files.as_ref() {
                // Only keep files that live under this source dir; other
                // source dirs in `sources` will pick them up.
                let mut ret = Vec::new();
                for (i, path) in cf.iter().enumerate() {
                    if path.starts_with(&src_base) {
                        ret.push(path.clone());
                        if let Some(ref mut flags) = explicit_routed {
                            flags[i] = true;
                        }
                    }
                }
                ret
            } else {
                veryl_path::gather_files_with_extension(&src_base, "veryl", symlink)?
            };

            for src in src_files {
                let Ok(src_relative) = src.strip_prefix(&src_base) else {
                    return Err(MetadataError::InvalidSourceLocation(src));
                };
                let dst = match self.build.target {
                    Target::Source => src.with_extension("sv"),
                    Target::Directory { ref path } => {
                        base.join(path.join(src_relative.with_extension("sv")))
                    }
                    Target::Bundle { .. } => base.join(
                        PathBuf::from("target").join(src.with_extension("sv").file_name().unwrap()),
                    ),
                };
                let map = match &self.build.sourcemap_target {
                    SourceMapTarget::Directory { path } => {
                        if let Target::Directory { .. } = self.build.target {
                            base.join(path.join(src_relative.with_extension("sv.map")))
                        } else {
                            let dst = dst.strip_prefix(&base).unwrap();
                            base.join(path.join(dst.with_extension("sv.map")))
                        }
                    }
                    _ => {
                        let mut map = dst.clone();
                        map.set_extension("sv.map");
                        map
                    }
                };
                ret.push(PathSet {
                    prj: self.project.name.clone(),
                    src: src.to_path_buf(),
                    dst,
                    map,
                });
            }
        }

        // Any explicit file that wasn't claimed by a configured source dir
        // is outside the project — preserve the original error semantics.
        if let (Some(cf), Some(flags)) = (canonical_files.as_ref(), explicit_routed.as_ref())
            && let Some(pos) = flags.iter().position(|f| !f)
        {
            return Err(MetadataError::InvalidSourceLocation(cf[pos].clone()));
        }

        let base_dst = self.project_dependencies_path();
        if !base_dst.exists() {
            ignore_already_exists(fs::create_dir(&base_dst))
                .map_err(|x| MetadataError::file_io(x, &base_dst))?;
        }

        if include_dependencies {
            if !self.build.exclude_std {
                veryl_std::expand()?;
                ret.append(&mut veryl_std::paths(&base_dst)?);
            }

            self.update_lockfile()?;

            let mut deps = self.lockfile.paths(&base_dst)?;
            ret.append(&mut deps);
        }

        Ok(ret)
    }

    pub fn create_default_toml(name: &str) -> Result<String, MetadataError> {
        check_project_name(name)?;

        Ok(format!(
            r###"[project]
name = "{name}"
version = "0.1.0"
[build]
sources = ["src"]
target = {{type = "directory", path = "target"}}"###
        ))
    }

    pub fn create_default(name: &str) -> Result<Metadata, MetadataError> {
        let metadata: Metadata = toml::from_str(&Self::create_default_toml(name)?)?;
        Ok(metadata)
    }

    pub fn create_default_gitignore() -> &'static str {
        r#"# Build output
.build/
/target
/dependencies
*.f

# Verilator
obj_dir/
"#
    }

    pub fn project_path(&self) -> PathBuf {
        self.metadata_path.parent().unwrap().to_path_buf()
    }

    pub fn project_dependencies_path(&self) -> PathBuf {
        self.project_path().join("dependencies")
    }

    pub fn project_dot_build_path(&self) -> PathBuf {
        self.project_path().join(".build")
    }

    pub fn project_build_info_path(&self) -> PathBuf {
        self.project_dot_build_path().join("info.toml")
    }

    pub fn filelist_path(&self) -> PathBuf {
        let filelist_name = match self.build.filelist_type {
            FilelistType::Absolute => format!("{}.f", self.project.name),
            FilelistType::Relative => format!("{}.f", self.project.name),
            FilelistType::Flgen => format!("{}.list.rb", self.project.name),
        };

        self.metadata_path.with_file_name(filelist_name)
    }

    pub fn doc_path(&self) -> PathBuf {
        self.metadata_path.parent().unwrap().join(&self.doc.path)
    }
}

impl FromStr for Metadata {
    type Err = MetadataError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let metadata: Metadata = toml::from_str(s)?;
        Ok(metadata)
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
#[serde(deny_unknown_fields)]
pub enum Dependency {
    Version(VersionReq),
    Entry(DependencyEntry),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DependencyEntry {
    pub version: Option<VersionReq>,
    pub git: Option<UrlPath>,
    pub github: Option<String>,
    pub project: Option<String>,
    pub path: Option<PathBuf>,
}