use super::*;
pub(super) fn theme_import(
reference: &str,
theme: Option<&str>,
id: Option<&str>,
dry_run: bool,
) -> Result<()> {
let local = Path::new(reference);
let (source, _scratch) = if local.is_file() {
eprintln!("Importing from {}", local.display());
(local.to_path_buf(), None)
} else {
let (path, dir) = fetch_marketplace_theme(reference, theme)?;
(path, Some(dir))
};
let converted = crate::vscode_theme::convert_file(&source, id)?;
if dry_run {
print!("{}", converted.manifest);
return Ok(());
}
let installed = crate::vscode_theme::install(&converted)?;
println!(
"Imported \"{}\" as theme id {}",
converted.label, converted.id
);
println!(
" {}{}",
installed.path.display(),
if installed.replaced {
" (replaced the manifest already there)"
} else {
""
}
);
if !converted.notes.is_empty() {
println!(
"\n{} note{} about this import:",
converted.notes.len(),
if converted.notes.len() == 1 { "" } else { "s" }
);
for note in &converted.notes {
println!(" - {note}");
}
}
println!("\nPick it under the gear menu on croft's next launch.");
Ok(())
}
pub(super) fn scratch_nonce() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
^ (&scratch_nonce as *const _ as u128)
}
pub(super) fn fetch_marketplace_theme(
reference: &str,
wanted: Option<&str>,
) -> Result<(PathBuf, crate::marketplace::Scratch)> {
use crate::marketplace;
let id = marketplace::parse_ref(reference)?;
let scratch = marketplace::Scratch::new(std::env::temp_dir().join(format!(
"croft-theme-import-{}-{:x}",
std::process::id(),
scratch_nonce()
)))?;
let dir = scratch.path();
eprintln!(
"Downloading {}.{} from the marketplace…",
id.publisher, id.name
);
let vsix = marketplace::download_vsix(&id, dir)?;
let pkg = marketplace::read_member(&vsix, "extension/package.json", dir)?;
let nls = marketplace::read_member(&vsix, "extension/package.nls.json", dir).ok();
let themes = marketplace::contributed_themes(&pkg, nls.as_deref())?;
let picked = marketplace::pick_theme(&themes, wanted)?;
eprintln!(" taking \"{}\" ({})", picked.label, picked.path);
let entry = marketplace::extract_theme_chain(&vsix, &picked.path, dir)?;
Ok((entry, scratch))
}