Skip to content

Commit f84cdd3

Browse files
authored
Add command to migrate tarmac-manifest.toml (#88)
1 parent 1114b0f commit f84cdd3

File tree

7 files changed

+102
-5
lines changed

7 files changed

+102
-5
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ asphalt sync --dry-run
6868

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

71+
### `asphalt migrate-tarmac-manifest`
72+
73+
Will migrate over an existing `tarmac-manifest.toml` to `asphalt.lock.toml`.
74+
7175
## Configuration
7276

7377
Asphalt is configured with a project file called `asphalt.toml`. It is required for the program to run.

src/cli.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ pub enum Commands {
2121

2222
/// Initialize a new configuration.
2323
Init,
24+
25+
/// Migrate an existing tarmac-manifest.toml to a lockfile.
26+
MigrateTarmacManifest(crate::commands::migrate_tarmac_manifest::MigrateTarmacManifestArgs),
2427
}
2528

2629
#[derive(ValueEnum, Clone)]
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

src/commands/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod init;
22
pub mod list;
3+
pub mod migrate_tarmac_manifest;
34
pub mod sync;

src/commands/sync/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub async fn sync(args: SyncArgs, existing_lockfile: LockFile) -> anyhow::Result
168168
if let SyncTarget::Cloud = state.target {
169169
state
170170
.new_lockfile
171-
.write()
171+
.write(Path::new(crate::lockfile::FILE_NAME))
172172
.await
173173
.context("Failed to write lockfile")?;
174174
}

src/lockfile.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::{Deserialize, Serialize};
2-
use std::collections::BTreeMap;
2+
use std::{collections::BTreeMap, path::Path};
33
use tokio::fs::{read_to_string, write};
44

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

16-
static FILE_NAME: &str = "asphalt.lock.toml";
16+
pub static FILE_NAME: &str = "asphalt.lock.toml";
1717

1818
impl LockFile {
1919
pub async fn read() -> anyhow::Result<Self> {
@@ -24,9 +24,9 @@ impl LockFile {
2424
}
2525
}
2626

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

3131
Ok(())
3232
}

src/main.rs

+5
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@ async fn main() -> anyhow::Result<()> {
3434
.context("Failed to sync"),
3535
Commands::List => list(existing_lockfile).await.context("Failed to list"),
3636
Commands::Init => init().await.context("Failed to initialize"),
37+
Commands::MigrateTarmacManifest(args) => {
38+
commands::migrate_tarmac_manifest::migrate_manifest(args)
39+
.await
40+
.context("Failed to migrate tarmac-manifest.toml")
41+
}
3742
}
3843
}

0 commit comments

Comments
 (0)