Parse VSCode theme files

This commit is contained in:
Marshall Bowers 2023-11-02 18:00:55 -04:00
parent 058981350c
commit e48332c81f
11 changed files with 118 additions and 85 deletions

View file

@ -49,8 +49,7 @@ pub struct ThemeFamily {
impl ThemeFamily {} impl ThemeFamily {}
pub struct ThemeVariant { pub struct ThemeVariant {
#[allow(dead_code)] pub id: String,
pub(crate) id: String,
pub name: SharedString, pub name: SharedString,
pub appearance: Appearance, pub appearance: Appearance,
pub styles: ThemeStyles, pub styles: ThemeStyles,

View file

@ -1,15 +1,21 @@
use std::borrow::Cow;
use std::fs::{self, File};
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use std::{borrow::Cow, fs::File};
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use convert_case::Case; use convert_case::Case;
use gpui::{AssetSource, SharedString}; use gpui::{serde_json, AssetSource, SharedString};
use log::LevelFilter; use log::LevelFilter;
use rust_embed::RustEmbed; use rust_embed::RustEmbed;
use serde::Deserialize; use serde::Deserialize;
use simplelog::SimpleLogger; use simplelog::SimpleLogger;
use theme::{default_color_scales, ThemeColorsRefinement, ThemeFamily}; use theme::{
default_color_scales, Appearance, GitStatusColors, PlayerColors, StatusColors, SyntaxTheme,
SystemColors, ThemeColors, ThemeColorsRefinement, ThemeFamily, ThemeStyles, ThemeVariant,
};
use crate::vscode::VsCodeTheme;
mod vscode; mod vscode;
@ -23,20 +29,21 @@ pub(crate) fn new_theme_family(name: String, author: String) -> ThemeFamily {
} }
} }
#[derive(Deserialize)] #[derive(Debug, Deserialize)]
struct FamilyJson { struct FamilyJson {
pub name: String, pub name: String,
pub author: String,
pub themes: Vec<ThemeVariantJson>, pub themes: Vec<ThemeVariantJson>,
} }
#[derive(Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
enum ThemeAppearanceJson { enum ThemeAppearanceJson {
Light, Light,
Dark, Dark,
} }
#[derive(Deserialize)] #[derive(Debug, Deserialize)]
struct ThemeVariantJson { struct ThemeVariantJson {
pub name: String, pub name: String,
pub appearance: ThemeAppearanceJson, pub appearance: ThemeAppearanceJson,
@ -69,55 +76,80 @@ fn main() -> Result<()> {
let themes_path = PathBuf::from_str("crates/theme2/src/themes")?; let themes_path = PathBuf::from_str("crates/theme2/src/themes")?;
let mut theme_modules = Vec::new(); let vscode_themes_path = PathBuf::from_str("assets/themes/src/vscode/")?;
for theme_path in Assets.list("themes/src/vsc/")? { let mut theme_families = Vec::new();
let (_, theme_name) = theme_path.split_once("themes/").unwrap();
if theme_name == ".gitkeep" { for theme_family_dir in fs::read_dir(&vscode_themes_path)? {
continue; let theme_family_dir = theme_family_dir?;
let theme_family_slug = theme_family_dir
.path()
.file_stem()
.ok_or(anyhow!("no file stem"))
.map(|stem| stem.to_string_lossy().to_string())?;
let family_metadata_file = File::open(theme_family_dir.path().join("family.json"))
.context(format!("no `family.json` found for '{theme_family_slug}'"))?;
let family_metadata: FamilyJson = serde_json::from_reader(family_metadata_file).context(
format!("failed to parse `family.json` for '{theme_family_slug}'"),
)?;
let mut themes = Vec::new();
for theme_entry in fs::read_dir(vscode_themes_path.join(theme_family_slug))? {
let theme_entry = theme_entry?;
let theme_file_path = theme_entry.path();
let file_name = theme_file_path
.file_name()
.ok_or(anyhow!("no file stem"))
.map(|file_name| file_name.to_string_lossy())?;
if !file_name.ends_with(".json") {
continue;
}
if file_name == "family.json" {
continue;
}
let theme_file = File::open(&theme_file_path)?;
let theme: VsCodeTheme = serde_json::from_reader(theme_file)
.context(format!("failed to parse theme {theme_file_path:?}"))?;
themes.push(theme);
} }
let theme_contents = Assets::get(&theme_path) let theme_family = ThemeFamily {
.with_context(|| format!("theme file not found: '{theme_path}'"))?; id: uuid::Uuid::new_v4().to_string(),
name: family_metadata.name.into(),
author: family_metadata.author.into(),
themes: themes
.into_iter()
.map(|theme| ThemeVariant {
id: uuid::Uuid::new_v4().to_string(),
name: "".into(),
appearance: Appearance::Dark,
styles: ThemeStyles {
system: SystemColors::default(),
colors: ThemeColors::default_dark(),
status: StatusColors::default(),
git: GitStatusColors::default(),
player: PlayerColors::default(),
syntax: SyntaxTheme::default_dark(),
},
})
.collect(),
scales: default_color_scales(),
};
// let json_theme: JsonTheme = theme_families.push(theme_family);
// serde_json::from_str(std::str::from_utf8(&theme_contents.data)?)
// .context("failed to parse legacy theme")?;
// let (json_theme, legacy_theme) = load_theme(&theme_path)?;
// let theme = convert_theme(json_theme, legacy_theme)?;
// let theme_slug = theme
// .metadata
// .name
// .as_ref()
// .replace("é", "e")
// .to_case(Case::Snake);
// let mut output_file = File::create(themes_path.join(format!("{theme_slug}.rs")))?;
// let theme_module = format!(
// r#"
// use gpui2::rgba;
// use crate::{{PlayerTheme, SyntaxTheme, Theme, ThemeMetadata}};
// pub fn {theme_slug}() -> Theme {{
// {theme_definition}
// }}
// "#,
// theme_definition = format!("{:#?}", ThemePrinter::new(theme))
// );
// output_file.write_all(theme_module.as_bytes())?;
theme_modules.push(theme_slug);
} }
println!("Hello, world!");
Ok(()) Ok(())
} }

View file

@ -5,21 +5,21 @@ use serde::Deserialize;
use theme::{default_color_scales, ColorScales, ThemeFamily}; use theme::{default_color_scales, ColorScales, ThemeFamily};
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct VSCodeTheme { pub struct VsCodeTheme {
#[serde(rename = "$schema")] #[serde(rename = "$schema")]
pub schema: String, pub schema: Option<String>,
pub name: String, pub name: Option<String>,
pub author: String, pub author: Option<String>,
pub maintainers: Vec<String>, pub maintainers: Option<Vec<String>>,
#[serde(rename = "semanticClass")] #[serde(rename = "semanticClass")]
pub semantic_class: String, pub semantic_class: Option<String>,
#[serde(rename = "semanticHighlighting")] #[serde(rename = "semanticHighlighting")]
pub semantic_highlighting: bool, pub semantic_highlighting: Option<bool>,
pub colors: VSCodeColors, pub colors: VsCodeColors,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct VSCodeColors { pub struct VsCodeColors {
#[serde(rename = "editor.foreground")] #[serde(rename = "editor.foreground")]
text: String, text: String,
#[serde(rename = "editor.background")] #[serde(rename = "editor.background")]
@ -27,36 +27,38 @@ pub struct VSCodeColors {
} }
pub(crate) fn new_theme_family_from_vsc(path: &Path) -> Result<ThemeFamily> { pub(crate) fn new_theme_family_from_vsc(path: &Path) -> Result<ThemeFamily> {
let path_str = path.to_str().unwrap(); todo!()
let family_name = path_str.split('/').last().unwrap();
let mut json_files: Vec<String> = Vec::new(); // let path_str = path.to_str().unwrap();
// let family_name = path_str.split('/').last().unwrap();
if path.is_dir() { // let mut json_files: Vec<String> = Vec::new();
for entry in std::fs::read_dir(path).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
if let Some(extension) = path.extension() {
if extension == "json" {
json_files.push(path.file_name().unwrap().to_str().unwrap().to_string());
}
}
}
}
} else {
anyhow::bail!("Path is not a directory");
}
let mut theme_family = ThemeFamily { // if path.is_dir() {
id: uuid::Uuid::new_v4().to_string(), // for entry in std::fs::read_dir(path).unwrap() {
name: family_name.into(), // let entry = entry.unwrap();
author: "New Theme Family".into(), // let path = entry.path();
themes: Vec::new(), // if path.is_file() {
scales: default_color_scales(), // if let Some(extension) = path.extension() {
}; // if extension == "json" {
// json_files.push(path.file_name().unwrap().to_str().unwrap().to_string());
// }
// }
// }
// }
// } else {
// anyhow::bail!("Path is not a directory");
// }
Ok(theme_family) // let mut theme_family = ThemeFamily {
// id: uuid::Uuid::new_v4().to_string(),
// name: family_name.into(),
// author: "New Theme Family".into(),
// themes: Vec::new(),
// scales: default_color_scales(),
// };
// Ok(theme_family)
} }
#[cfg(test)] #[cfg(test)]