@@ -4,7 +4,7 @@ use anyhow::{anyhow, Context, Result};
4
4
use std:: collections:: HashMap ;
5
5
use std:: fs:: { self , create_dir_all, read_to_string} ;
6
6
use std:: path:: { Path , PathBuf } ;
7
- use tinted_builder:: { Scheme , SchemeSystem , Template } ;
7
+ use tinted_builder:: { Base16Scheme , Scheme , SchemeSystem , Template } ;
8
8
use utils:: { get_scheme_files, parse_filename, ParsedFilename , SchemeFile , TemplateConfig } ;
9
9
10
10
use crate :: helpers:: write_to_file;
@@ -82,7 +82,7 @@ pub fn build(
82
82
) ) ;
83
83
}
84
84
85
- let template_config_content = read_to_string ( template_config_path) ?;
85
+ let template_config_content = read_to_string ( & template_config_path) ?;
86
86
let template_config: HashMap < String , TemplateConfig > =
87
87
serde_yaml:: from_str ( & template_config_content) ?;
88
88
@@ -105,42 +105,118 @@ pub fn build(
105
105
106
106
// For each template definition in the templates/config.yaml file
107
107
for ( template_item_config_name, template_item_config_value) in template_config. iter ( ) {
108
- let template_item_scheme_files: Vec < ( PathBuf , Scheme ) > = all_scheme_files
109
- . iter ( )
110
- . filter_map ( |( path, scheme) | {
111
- if template_item_config_value
112
- . supported_systems
113
- . clone ( )
114
- . unwrap_or ( vec ! [ SchemeSystem :: default ( ) ] )
115
- . contains ( & scheme. get_scheme_system ( ) )
116
- {
117
- Some ( ( path. clone ( ) , scheme. clone ( ) ) )
118
- } else {
119
- None
120
- }
121
- } )
122
- . collect ( ) ;
123
-
124
- generate_themes_for_config (
125
- template_item_config_name,
126
- template_item_config_value,
127
- & theme_template_path,
128
- & template_item_scheme_files,
129
- is_quiet,
130
- ) ?;
108
+ let supported_systems = template_item_config_value
109
+ . supported_systems
110
+ . clone ( )
111
+ . unwrap_or ( vec ! [ SchemeSystem :: default ( ) ] ) ;
112
+
113
+ if supported_systems. contains ( & SchemeSystem :: List ) {
114
+ render_list (
115
+ & theme_template_path,
116
+ ( template_item_config_name, template_item_config_value) ,
117
+ all_scheme_files. clone ( ) ,
118
+ is_quiet,
119
+ ) ?;
120
+ } else {
121
+ let template_item_scheme_files: Vec < ( PathBuf , Scheme ) > = all_scheme_files
122
+ . iter ( )
123
+ . filter_map ( |( path, scheme) | {
124
+ if supported_systems. contains ( & scheme. get_scheme_system ( ) ) {
125
+ Some ( ( path. clone ( ) , scheme. clone ( ) ) )
126
+ } else {
127
+ None
128
+ }
129
+ } )
130
+ . collect ( ) ;
131
+
132
+ generate_themes_for_config (
133
+ template_item_config_name,
134
+ template_item_config_value,
135
+ & theme_template_path,
136
+ & template_item_scheme_files,
137
+ is_quiet,
138
+ ) ?;
139
+ }
131
140
}
132
141
133
142
Ok ( ( ) )
134
143
}
135
144
136
- fn generate_themes_for_config (
137
- config_name : & str ,
138
- config_value : & TemplateConfig ,
139
- theme_template_path : impl AsRef < Path > ,
140
- scheme_files : & Vec < ( PathBuf , Scheme ) > ,
145
+ fn render_list (
146
+ template_path : impl AsRef < Path > ,
147
+ ( config_name, config_value) : ( & str , & TemplateConfig ) ,
148
+ all_scheme_files : Vec < ( PathBuf , Scheme ) > ,
141
149
is_quiet : bool ,
142
150
) -> Result < ( ) > {
143
- let filename = match (
151
+ let supported_systems = config_value
152
+ . supported_systems
153
+ . clone ( )
154
+ . unwrap_or ( vec ! [ SchemeSystem :: default ( ) ] ) ;
155
+ let filename = get_filename ( config_value, is_quiet) ?;
156
+ let mustache_template_path = template_path
157
+ . as_ref ( )
158
+ . join ( format ! ( "templates/{}.mustache" , config_name) ) ;
159
+ let template_content = read_to_string ( & mustache_template_path) . context ( format ! (
160
+ "Mustache template missing: {}" ,
161
+ mustache_template_path. display( )
162
+ ) ) ?;
163
+ let mut data: HashMap < & str , Vec < Base16Scheme > > = HashMap :: new ( ) ;
164
+ data. insert (
165
+ "schemes" ,
166
+ all_scheme_files
167
+ . clone ( )
168
+ . into_iter ( )
169
+ . filter_map ( |( _, scheme) | match scheme {
170
+ Scheme :: Base16 ( scheme) => {
171
+ if supported_systems. contains ( & SchemeSystem :: Base16 ) {
172
+ Some ( scheme)
173
+ } else {
174
+ None
175
+ }
176
+ }
177
+ Scheme :: Base24 ( scheme) => {
178
+ if supported_systems. contains ( & SchemeSystem :: Base24 ) {
179
+ Some ( scheme)
180
+ } else {
181
+ None
182
+ }
183
+ }
184
+ _ => None ,
185
+ } )
186
+ . collect :: < Vec < Base16Scheme > > ( ) ,
187
+ ) ;
188
+ let data = serde_yaml:: to_string ( & data) . unwrap_or_default ( ) ;
189
+ let output = ribboncurls:: render ( & template_content, & data, None ) ?;
190
+ let parsed_filename = parse_filename ( & template_path, & filename) ?;
191
+ let output_path = parsed_filename. get_path ( ) ;
192
+
193
+ if !parsed_filename. directory . exists ( ) {
194
+ create_dir_all ( & parsed_filename. directory ) ?
195
+ }
196
+
197
+ write_to_file ( & output_path, & output) ?;
198
+
199
+ if !is_quiet {
200
+ println ! (
201
+ "Successfully generated \" {}\" list with filename \" {}\" " ,
202
+ supported_systems
203
+ . iter( )
204
+ . filter_map( |item| if * item == SchemeSystem :: List {
205
+ None
206
+ } else {
207
+ Some ( item. as_str( ) . to_string( ) )
208
+ } )
209
+ . collect:: <Vec <String >>( )
210
+ . join( ", " ) ,
211
+ template_path. as_ref( ) . join( filename) . display( ) ,
212
+ ) ;
213
+ }
214
+
215
+ Ok ( ( ) )
216
+ }
217
+
218
+ fn get_filename ( config_value : & TemplateConfig , is_quiet : bool ) -> Result < String > {
219
+ match (
144
220
& config_value. filename ,
145
221
#[ allow ( deprecated) ]
146
222
& config_value. extension ,
@@ -182,7 +258,17 @@ fn generate_themes_for_config(
182
258
_ => Err ( anyhow ! (
183
259
"Config file is missing \" filepath\" or \" extension\" and \" output\" properties"
184
260
) ) ,
185
- } ?;
261
+ }
262
+ }
263
+
264
+ fn generate_themes_for_config (
265
+ config_name : & str ,
266
+ config_value : & TemplateConfig ,
267
+ theme_template_path : impl AsRef < Path > ,
268
+ scheme_files : & Vec < ( PathBuf , Scheme ) > ,
269
+ is_quiet : bool ,
270
+ ) -> Result < ( ) > {
271
+ let filename = get_filename ( config_value, is_quiet) ?;
186
272
let mustache_template_path = theme_template_path
187
273
. as_ref ( )
188
274
. join ( format ! ( "templates/{}.mustache" , config_name) ) ;
0 commit comments