|
| 1 | +use std::{collections::BTreeMap, path::PathBuf, sync::Arc}; |
| 2 | + |
| 3 | +use anyhow::Context; |
| 4 | +use resvg::usvg::fontdb::Database; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +use crate::asset::Asset; |
| 8 | + |
| 9 | +#[derive(Debug, Serialize, Deserialize)] |
| 10 | +struct TarmacManifest { |
| 11 | + inputs: BTreeMap<PathBuf, TarmacEntry>, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Debug, Serialize, Deserialize)] |
| 15 | +struct TarmacEntry { |
| 16 | + id: u64, |
| 17 | +} |
| 18 | + |
| 19 | +pub async fn migrate_manifest(args: MigrateTarmacManifestArgs) -> anyhow::Result<()> { |
| 20 | + let tarmac_manifest_contents = std::fs::read_to_string(&args.manifest_path) |
| 21 | + .context("Failed to open tarmac-manifest.toml")?; |
| 22 | + let tarmac_manifest: TarmacManifest = toml::from_str(&tarmac_manifest_contents) |
| 23 | + .context("Failed to parse tarmac-manifest.toml")?; |
| 24 | + |
| 25 | + let mut lockfile = crate::LockFile::default(); |
| 26 | + |
| 27 | + for (path, entry) in tarmac_manifest.inputs { |
| 28 | + let content_path = args.manifest_path.with_file_name(&path); |
| 29 | + let content = match std::fs::read(&content_path) { |
| 30 | + Ok(content) => content, |
| 31 | + Err(error) => { |
| 32 | + if error.kind() == std::io::ErrorKind::NotFound { |
| 33 | + log::warn!( |
| 34 | + "Content file {} not found, skipping", |
| 35 | + content_path.display() |
| 36 | + ); |
| 37 | + |
| 38 | + continue; |
| 39 | + } else { |
| 40 | + return Err(error).with_context(|| { |
| 41 | + format!("Failed to read content file {}", content_path.display()) |
| 42 | + }); |
| 43 | + } |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + let font_db = Arc::new(Database::new()); |
| 48 | + |
| 49 | + let asset = Asset::new( |
| 50 | + path.to_string_lossy().to_string(), |
| 51 | + content, |
| 52 | + &path.extension().unwrap_or_default().to_string_lossy(), |
| 53 | + font_db, |
| 54 | + ) |
| 55 | + .await |
| 56 | + .with_context(|| format!("Failed to create asset for {}", path.to_string_lossy()))?; |
| 57 | + |
| 58 | + lockfile.entries.insert( |
| 59 | + path.to_string_lossy().to_string(), |
| 60 | + crate::FileEntry { |
| 61 | + asset_id: entry.id, |
| 62 | + hash: asset.hash(), |
| 63 | + }, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + lockfile |
| 68 | + .write( |
| 69 | + &args |
| 70 | + .manifest_path |
| 71 | + .with_file_name(crate::lockfile::FILE_NAME), |
| 72 | + ) |
| 73 | + .await |
| 74 | + .context("Failed to write Asphalt lockfile")?; |
| 75 | + |
| 76 | + Ok(()) |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(clap::Args)] |
| 80 | +pub struct MigrateTarmacManifestArgs { |
| 81 | + /// The path to the tarmac-manifest.toml file. |
| 82 | + #[clap(default_value = "tarmac-manifest.toml")] |
| 83 | + pub manifest_path: PathBuf, |
| 84 | +} |
0 commit comments