1mod theme_printer;
2mod util;
3mod vscode;
4
5use std::fs::{self, File};
6use std::io::Write;
7use std::path::PathBuf;
8use std::str::FromStr;
9
10use anyhow::{anyhow, Context, Result};
11use convert_case::{Case, Casing};
12use gpui::serde_json;
13use log::LevelFilter;
14use serde::Deserialize;
15use simplelog::SimpleLogger;
16use theme::{default_color_scales, Appearance, ThemeFamily};
17use vscode::VsCodeThemeConverter;
18
19use crate::theme_printer::ThemeFamilyPrinter;
20use crate::vscode::VsCodeTheme;
21
22#[derive(Debug, Deserialize)]
23struct FamilyMetadata {
24 pub name: String,
25 pub author: String,
26 pub themes: Vec<ThemeMetadata>,
27}
28
29#[derive(Debug, Deserialize)]
30#[serde(rename_all = "snake_case")]
31pub enum ThemeAppearanceJson {
32 Light,
33 Dark,
34}
35
36impl From<ThemeAppearanceJson> for Appearance {
37 fn from(value: ThemeAppearanceJson) -> Self {
38 match value {
39 ThemeAppearanceJson::Light => Self::Light,
40 ThemeAppearanceJson::Dark => Self::Dark,
41 }
42 }
43}
44
45#[derive(Debug, Deserialize)]
46pub struct ThemeMetadata {
47 pub name: String,
48 pub file_name: String,
49 pub appearance: ThemeAppearanceJson,
50}
51
52fn main() -> Result<()> {
53 const SOURCE_PATH: &str = "assets/themes/src/vscode";
54 const OUT_PATH: &str = "crates/theme2/src/themes";
55
56 SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
57
58 println!("Loading themes source...");
59 let vscode_themes_path = PathBuf::from_str(SOURCE_PATH)?;
60 if !vscode_themes_path.exists() {
61 return Err(anyhow!(format!(
62 "Couldn't find {}, make sure it exists",
63 SOURCE_PATH
64 )));
65 }
66
67 let mut theme_families = Vec::new();
68
69 for theme_family_dir in fs::read_dir(&vscode_themes_path)? {
70 let theme_family_dir = theme_family_dir?;
71
72 if !theme_family_dir.file_type()?.is_dir() {
73 continue;
74 }
75
76 let theme_family_slug = theme_family_dir
77 .path()
78 .file_stem()
79 .ok_or(anyhow!("no file stem"))
80 .map(|stem| stem.to_string_lossy().to_string())?;
81
82 let family_metadata_file = File::open(theme_family_dir.path().join("family.json"))
83 .context(format!(
84 "no `family.json` found for '{}'",
85 theme_family_slug
86 ))?;
87
88 let license_file_path = theme_family_dir.path().join("LICENSE");
89
90 if !license_file_path.exists() {
91 println!("Skipping theme family '{}' because it does not have a LICENSE file. This theme will only be imported once a LICENSE file is provided.", theme_family_slug);
92 continue;
93 }
94
95 let family_metadata: FamilyMetadata = serde_json::from_reader(family_metadata_file)
96 .context(format!(
97 "failed to parse `family.json` for '{theme_family_slug}'"
98 ))?;
99
100 let mut themes = Vec::new();
101
102 for theme_metadata in family_metadata.themes {
103 let theme_file_path = theme_family_dir.path().join(&theme_metadata.file_name);
104
105 let theme_file = match File::open(&theme_file_path) {
106 Ok(file) => file,
107 Err(_) => {
108 println!("Failed to open file at path: {:?}", theme_file_path);
109 continue;
110 }
111 };
112
113 let vscode_theme: VsCodeTheme = serde_json::from_reader(theme_file)
114 .context(format!("failed to parse theme {theme_file_path:?}"))?;
115
116 let converter = VsCodeThemeConverter::new(vscode_theme, theme_metadata);
117
118 let theme = converter.convert()?;
119
120 themes.push(theme);
121 }
122
123 let theme_family = ThemeFamily {
124 id: uuid::Uuid::new_v4().to_string(),
125 name: family_metadata.name.into(),
126 author: family_metadata.author.into(),
127 themes,
128 scales: default_color_scales(),
129 };
130
131 theme_families.push(theme_family);
132 }
133
134 let themes_output_path = PathBuf::from_str(OUT_PATH)?;
135
136 if !themes_output_path.exists() {
137 println!("Creating directory: {:?}", themes_output_path);
138 fs::create_dir_all(&themes_output_path)?;
139 }
140
141 let mut mod_rs_file = File::create(themes_output_path.join(format!("mod.rs")))?;
142
143 let mut theme_modules = Vec::new();
144
145 for theme_family in theme_families {
146 let theme_family_slug = theme_family.name.to_string().to_case(Case::Snake);
147
148 let mut output_file =
149 File::create(themes_output_path.join(format!("{theme_family_slug}.rs")))?;
150 println!(
151 "Creating file: {:?}",
152 themes_output_path.join(format!("{theme_family_slug}.rs"))
153 );
154
155 let theme_module = format!(
156 r#"
157 use gpui::rgba;
158
159 use crate::{{
160 default_color_scales, Appearance, GitStatusColors, PlayerColor, PlayerColors, StatusColors,
161 SyntaxTheme, SystemColors, ThemeColors, ThemeFamily, ThemeStyles, ThemeVariant,
162 }};
163
164 pub fn {theme_family_slug}() -> ThemeFamily {{
165 {theme_family_definition}
166 }}
167 "#,
168 theme_family_definition = format!("{:#?}", ThemeFamilyPrinter::new(theme_family))
169 );
170
171 output_file.write_all(theme_module.as_bytes())?;
172
173 theme_modules.push(theme_family_slug);
174 }
175
176 let themes_vector_contents = format!(
177 r#"
178 use crate::ThemeFamily;
179
180 pub(crate) fn all_imported_themes() -> Vec<ThemeFamily> {{
181 vec![{all_themes}]
182 }}
183 "#,
184 all_themes = theme_modules
185 .iter()
186 .map(|module| format!("{}()", module))
187 .collect::<Vec<_>>()
188 .join(", ")
189 );
190
191 let mod_rs_contents = format!(
192 r#"
193 {mod_statements}
194
195 {use_statements}
196
197 {themes_vector_contents}
198 "#,
199 mod_statements = theme_modules
200 .iter()
201 .map(|module| format!("mod {module};"))
202 .collect::<Vec<_>>()
203 .join("\n"),
204 use_statements = theme_modules
205 .iter()
206 .map(|module| format!("pub use {module}::*;"))
207 .collect::<Vec<_>>()
208 .join("\n"),
209 themes_vector_contents = themes_vector_contents
210 );
211
212 mod_rs_file.write_all(mod_rs_contents.as_bytes())?;
213
214 Ok(())
215}