Skip to content

Commit

Permalink
Add command to migrate tarmac-manifest.toml (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampfkarren authored Dec 26, 2024
1 parent 1114b0f commit f84cdd3
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ asphalt sync --dry-run

Lists asset paths from the lockfile and their corresponding Roblox asset IDs.

### `asphalt migrate-tarmac-manifest`

Will migrate over an existing `tarmac-manifest.toml` to `asphalt.lock.toml`.

## Configuration

Asphalt is configured with a project file called `asphalt.toml`. It is required for the program to run.
Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub enum Commands {

/// Initialize a new configuration.
Init,

/// Migrate an existing tarmac-manifest.toml to a lockfile.
MigrateTarmacManifest(crate::commands::migrate_tarmac_manifest::MigrateTarmacManifestArgs),
}

#[derive(ValueEnum, Clone)]
Expand Down
84 changes: 84 additions & 0 deletions src/commands/migrate_tarmac_manifest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::{collections::BTreeMap, path::PathBuf, sync::Arc};

use anyhow::Context;
use resvg::usvg::fontdb::Database;
use serde::{Deserialize, Serialize};

use crate::asset::Asset;

#[derive(Debug, Serialize, Deserialize)]
struct TarmacManifest {
inputs: BTreeMap<PathBuf, TarmacEntry>,
}

#[derive(Debug, Serialize, Deserialize)]
struct TarmacEntry {
id: u64,
}

pub async fn migrate_manifest(args: MigrateTarmacManifestArgs) -> anyhow::Result<()> {
let tarmac_manifest_contents = std::fs::read_to_string(&args.manifest_path)
.context("Failed to open tarmac-manifest.toml")?;
let tarmac_manifest: TarmacManifest = toml::from_str(&tarmac_manifest_contents)
.context("Failed to parse tarmac-manifest.toml")?;

let mut lockfile = crate::LockFile::default();

for (path, entry) in tarmac_manifest.inputs {
let content_path = args.manifest_path.with_file_name(&path);
let content = match std::fs::read(&content_path) {
Ok(content) => content,
Err(error) => {
if error.kind() == std::io::ErrorKind::NotFound {
log::warn!(
"Content file {} not found, skipping",
content_path.display()
);

continue;
} else {
return Err(error).with_context(|| {
format!("Failed to read content file {}", content_path.display())
});
}
}
};

let font_db = Arc::new(Database::new());

let asset = Asset::new(
path.to_string_lossy().to_string(),
content,
&path.extension().unwrap_or_default().to_string_lossy(),
font_db,
)
.await
.with_context(|| format!("Failed to create asset for {}", path.to_string_lossy()))?;

lockfile.entries.insert(
path.to_string_lossy().to_string(),
crate::FileEntry {
asset_id: entry.id,
hash: asset.hash(),
},
);
}

lockfile
.write(
&args
.manifest_path
.with_file_name(crate::lockfile::FILE_NAME),
)
.await
.context("Failed to write Asphalt lockfile")?;

Ok(())
}

#[derive(clap::Args)]
pub struct MigrateTarmacManifestArgs {
/// The path to the tarmac-manifest.toml file.
#[clap(default_value = "tarmac-manifest.toml")]
pub manifest_path: PathBuf,
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod init;
pub mod list;
pub mod migrate_tarmac_manifest;
pub mod sync;
2 changes: 1 addition & 1 deletion src/commands/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub async fn sync(args: SyncArgs, existing_lockfile: LockFile) -> anyhow::Result
if let SyncTarget::Cloud = state.target {
state
.new_lockfile
.write()
.write(Path::new(crate::lockfile::FILE_NAME))
.await
.context("Failed to write lockfile")?;
}
Expand Down
8 changes: 4 additions & 4 deletions src/lockfile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::{collections::BTreeMap, path::Path};
use tokio::fs::{read_to_string, write};

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -13,7 +13,7 @@ pub struct LockFile {
pub entries: BTreeMap<String, FileEntry>,
}

static FILE_NAME: &str = "asphalt.lock.toml";
pub static FILE_NAME: &str = "asphalt.lock.toml";

impl LockFile {
pub async fn read() -> anyhow::Result<Self> {
Expand All @@ -24,9 +24,9 @@ impl LockFile {
}
}

pub async fn write(&self) -> anyhow::Result<()> {
pub async fn write(&self, filename: &Path) -> anyhow::Result<()> {
let content = toml::to_string(self)?;
write(FILE_NAME, content).await?;
write(filename, content).await?;

Ok(())
}
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ async fn main() -> anyhow::Result<()> {
.context("Failed to sync"),
Commands::List => list(existing_lockfile).await.context("Failed to list"),
Commands::Init => init().await.context("Failed to initialize"),
Commands::MigrateTarmacManifest(args) => {
commands::migrate_tarmac_manifest::migrate_manifest(args)
.await
.context("Failed to migrate tarmac-manifest.toml")
}
}
}

0 comments on commit f84cdd3

Please sign in to comment.