|
| 1 | +use std::{ |
| 2 | + fs, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +use serde::Serialize; |
1 | 7 | use tucana::shared::{ |
2 | | - FlowType, FlowTypeSetting, FunctionDefinition, Module, ParameterDefinition, RuntimeFlowType, RuntimeFlowTypeSetting, RuntimeFunctionDefinition, RuntimeParameterDefinition |
| 8 | + DefinitionDataType, FlowType, FlowTypeSetting, FunctionDefinition, Module, |
| 9 | + ModuleConfigurationDefinition, ParameterDefinition, RuntimeFlowType, RuntimeFlowTypeSetting, |
| 10 | + RuntimeFunctionDefinition, RuntimeParameterDefinition, Translation, |
| 11 | +}; |
| 12 | + |
| 13 | +use crate::{ |
| 14 | + analyser::core::Analyser, |
| 15 | + command::parse_errors::fail_on_parser_errors, |
| 16 | + parser::{DefinitionModule, Parser}, |
3 | 17 | }; |
4 | 18 |
|
5 | | -use crate::{analyser::core::Analyser, command::parse_errors::fail_on_parser_errors, parser::{DefinitionModule, Parser}}; |
| 19 | +#[derive(Serialize)] |
| 20 | +struct ModuleMeta<'a> { |
| 21 | + identifier: &'a str, |
| 22 | + name: &'a [Translation], |
| 23 | + description: &'a [Translation], |
| 24 | + documentation: &'a str, |
| 25 | + author: &'a str, |
| 26 | + icon: &'a str, |
| 27 | + version: &'a str, |
| 28 | +} |
6 | 29 |
|
7 | 30 | fn runtime_function_into_function( |
8 | 31 | runtime_function: &RuntimeFunctionDefinition, |
@@ -110,27 +133,138 @@ fn apply_version_to_module(mut module: Module, version: String) -> Module { |
110 | 133 | fn configure_module(definition_module: &DefinitionModule, version: String) -> Module { |
111 | 134 | let mut module = definition_module.clone().into_module(); |
112 | 135 |
|
113 | | - module.function_definitions = module.runtime_function_definitions.iter().map(|x| runtime_function_into_function(x)).collect(); |
114 | | - module.flow_types = module.runtime_flow_types.iter().map(|x| runtime_flow_type_into_flow_type(x)).collect(); |
| 136 | + module.function_definitions = module |
| 137 | + .runtime_function_definitions |
| 138 | + .iter() |
| 139 | + .map(|x| runtime_function_into_function(x)) |
| 140 | + .collect(); |
| 141 | + module.flow_types = module |
| 142 | + .runtime_flow_types |
| 143 | + .iter() |
| 144 | + .map(|x| runtime_flow_type_into_flow_type(x)) |
| 145 | + .collect(); |
115 | 146 |
|
116 | 147 | apply_version_to_module(module, version) |
117 | 148 | } |
118 | 149 |
|
119 | | -pub async fn publish(version: String, path: Option<String>) { |
120 | | - let dir_path = path.unwrap_or_else(|| "./definitions".to_string()); |
| 150 | +fn safe_file_name(identifier: &str) -> String { |
| 151 | + identifier.replace("::", "_").replace(['/', '\\', ':'], "_") |
| 152 | +} |
| 153 | + |
| 154 | +fn write_json_file<T: Serialize>(path: &Path, value: &T) { |
| 155 | + let json = serde_json::to_string_pretty(value).expect("Error serializing definition"); |
| 156 | + fs::write(path, json).expect("Error writing definition"); |
| 157 | +} |
| 158 | + |
| 159 | +fn write_definition_collection<T, F>( |
| 160 | + module_path: &Path, |
| 161 | + folder_name: &str, |
| 162 | + definitions: &[T], |
| 163 | + identifier: F, |
| 164 | +) where |
| 165 | + T: Serialize, |
| 166 | + F: Fn(&T) -> &str, |
| 167 | +{ |
| 168 | + if definitions.is_empty() { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + let folder_path = module_path.join(folder_name); |
| 173 | + fs::create_dir_all(&folder_path).expect("Error creating definition folder"); |
121 | 174 |
|
122 | | - let mut analyzer = Analyser::new(dir_path.as_str()); |
| 175 | + for definition in definitions { |
| 176 | + let definition_path = |
| 177 | + folder_path.join(format!("{}.json", safe_file_name(identifier(definition)))); |
| 178 | + write_json_file(&definition_path, definition); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +fn write_module(module: &Module, out_dir_path: &Path) { |
| 183 | + let module_path = out_dir_path.join(safe_file_name(&module.identifier)); |
| 184 | + fs::create_dir_all(&module_path).expect("Error creating module folder"); |
| 185 | + |
| 186 | + let meta = ModuleMeta { |
| 187 | + identifier: &module.identifier, |
| 188 | + name: &module.name, |
| 189 | + description: &module.description, |
| 190 | + documentation: &module.documentation, |
| 191 | + author: &module.author, |
| 192 | + icon: &module.icon, |
| 193 | + version: &module.version, |
| 194 | + }; |
| 195 | + write_json_file(&module_path.join("meta.json"), &meta); |
| 196 | + |
| 197 | + write_definition_collection::<DefinitionDataType, _>( |
| 198 | + &module_path, |
| 199 | + "data_types", |
| 200 | + &module.definition_data_types, |
| 201 | + |definition| definition.identifier.as_str(), |
| 202 | + ); |
| 203 | + write_definition_collection::<FlowType, _>( |
| 204 | + &module_path, |
| 205 | + "flow_types", |
| 206 | + &module.flow_types, |
| 207 | + |definition| definition.identifier.as_str(), |
| 208 | + ); |
| 209 | + write_definition_collection::<RuntimeFlowType, _>( |
| 210 | + &module_path, |
| 211 | + "runtime_flow_types", |
| 212 | + &module.runtime_flow_types, |
| 213 | + |definition| definition.identifier.as_str(), |
| 214 | + ); |
| 215 | + write_definition_collection::<FunctionDefinition, _>( |
| 216 | + &module_path, |
| 217 | + "functions", |
| 218 | + &module.function_definitions, |
| 219 | + |definition| definition.runtime_name.as_str(), |
| 220 | + ); |
| 221 | + write_definition_collection::<RuntimeFunctionDefinition, _>( |
| 222 | + &module_path, |
| 223 | + "runtime_functions", |
| 224 | + &module.runtime_function_definitions, |
| 225 | + |definition| definition.runtime_name.as_str(), |
| 226 | + ); |
| 227 | + write_definition_collection::<ModuleConfigurationDefinition, _>( |
| 228 | + &module_path, |
| 229 | + "configurations", |
| 230 | + &module.configurations, |
| 231 | + |definition| definition.identifier.as_str(), |
| 232 | + ); |
| 233 | +} |
| 234 | + |
| 235 | +fn write_modules(modules: &[Module], out_dir_path: &str) { |
| 236 | + let out_dir_path = PathBuf::from(out_dir_path); |
| 237 | + |
| 238 | + if out_dir_path.exists() { |
| 239 | + fs::remove_dir_all(&out_dir_path).expect("Error deleting output folder"); |
| 240 | + } |
| 241 | + fs::create_dir_all(&out_dir_path).expect("Error creating output folder"); |
| 242 | + |
| 243 | + for module in modules { |
| 244 | + write_module(module, &out_dir_path); |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +pub async fn publish(version: String, in_path: Option<String>, out_path: Option<String>) { |
| 249 | + let in_dir_path = in_path.unwrap_or_else(|| "./definitions".to_string()); |
| 250 | + let out_dir_path = out_path.unwrap_or_else(|| "./out".to_string()); |
| 251 | + |
| 252 | + let mut analyzer = Analyser::new(in_dir_path.as_str()); |
123 | 253 | analyzer.report(false, true); |
124 | 254 |
|
125 | | - let parser = match Parser::from_path(dir_path.as_str()) { |
| 255 | + let parser = match Parser::from_path(in_dir_path.as_str()) { |
126 | 256 | Some(parser) => parser, |
127 | 257 | None => { |
128 | 258 | panic!("Error reading definitions"); |
129 | 259 | } |
130 | 260 | }; |
131 | 261 | fail_on_parser_errors(&parser); |
132 | 262 |
|
133 | | - let modules: Vec<Module> = parser.modules.into_iter().map(|x| configure_module(&x, version.clone())).collect(); |
| 263 | + let modules: Vec<Module> = parser |
| 264 | + .modules |
| 265 | + .into_iter() |
| 266 | + .map(|x| configure_module(&x, version.clone())) |
| 267 | + .collect(); |
134 | 268 |
|
135 | | - //TODO: Gen out files |
| 269 | + write_modules(&modules, out_dir_path.as_str()); |
136 | 270 | } |
0 commit comments