dprint 0.55.1

Binary for dprint code formatter—a pluggable and configurable code formatting platform.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use indexmap::IndexMap;
use serde::Serialize;
use std::collections::HashMap;
use std::path::Path;

use super::Environment;
use super::TestEnvironment;
use crate::test_helpers;
use crate::test_helpers::TestProcessPluginFile;
use crate::test_helpers::WASM_PLUGIN_0_1_0_BYTES;
use crate::utils::get_sha256_checksum;

pub struct TestConfigFileBuilder {
  environment: TestEnvironment,
  incremental: Option<bool>,
  inherit: Option<bool>,
  includes: Option<Vec<String>>,
  excludes: Option<Vec<String>>,
  plugins: Option<Vec<String>>,
  sections: IndexMap<String, String>,
}

impl TestConfigFileBuilder {
  pub fn new(environment: TestEnvironment) -> Self {
    TestConfigFileBuilder {
      environment,
      incremental: None,
      inherit: None,
      includes: None,
      excludes: None,
      plugins: None,
      sections: Default::default(),
    }
  }

  pub fn to_string(&self) -> String {
    let mut parts = Vec::new();
    if let Some(inherit) = self.inherit.as_ref() {
      parts.push(format!(r#""inherit": {}"#, inherit));
    }
    for (key, value) in self.sections.iter() {
      parts.push(format!("\"{}\": {}", key, value));
    }
    if let Some(incremental) = self.incremental.as_ref() {
      parts.push(format!(r#""incremental": {}"#, incremental));
    }
    // todo: reduce code duplication... was lazy
    if let Some(plugins) = self.plugins.as_ref() {
      let plugins_text = plugins.iter().map(|name| format!("  \"{}\"", name)).collect::<Vec<_>>().join(",\n");
      parts.push(format!("\"plugins\": [\n{}\n]", plugins_text))
    }
    if let Some(includes) = self.includes.as_ref() {
      let text = includes.iter().map(|v| format!("  \"{}\"", v)).collect::<Vec<_>>().join(",\n");
      parts.push(format!("\"includes\": [\n{}\n]", text))
    }
    if let Some(excludes) = self.excludes.as_ref() {
      let text = excludes.iter().map(|v| format!("  \"{}\"", v)).collect::<Vec<_>>().join(",\n");
      parts.push(format!("\"excludes\": [\n{}\n]", text))
    }
    format!(
      "{{\n{}\n}}",
      parts.join(",\n").lines().map(|l| format!("  {}", l)).collect::<Vec<_>>().join("\n")
    )
  }

  pub fn set_incremental(&mut self, value: bool) -> &mut Self {
    self.incremental = Some(value);
    self
  }

  pub fn set_inherit(&mut self, value: bool) -> &mut Self {
    self.inherit = Some(value);
    self
  }

  pub fn add_local_wasm_plugin(&mut self) -> &mut Self {
    self.add_plugin("/plugins/test-plugin.wasm")
  }

  pub fn add_remote_wasm_plugin(&mut self) -> &mut Self {
    self.add_plugin("https://plugins.dprint.dev/test-plugin.wasm")
  }

  pub fn add_remote_wasm_plugin_with_checksum(&mut self, checksum: &str) -> &mut Self {
    self.add_plugin(&format!("https://plugins.dprint.dev/test-plugin.wasm@{}", checksum))
  }

  /// This is a v3 old Wasm plugin.
  pub fn add_remote_wasm_plugin_0_1_0(&mut self) -> &mut Self {
    self.add_plugin("https://plugins.dprint.dev/test-plugin-0.1.0.wasm")
  }

  pub fn add_remote_wasm_plugin_0_1_0_with_checksum(&mut self) -> &mut Self {
    self.add_plugin(&format!(
      "https://plugins.dprint.dev/test-plugin-0.1.0.wasm@{}",
      &crate::utils::get_sha256_checksum(&WASM_PLUGIN_0_1_0_BYTES)
    ))
  }

  pub fn add_config_section(&mut self, name: &str, text: &str) -> &mut Self {
    self.sections.insert(name.to_string(), text.to_string());
    self
  }

  pub fn add_remote_process_plugin(&mut self) -> &mut Self {
    // get the process plugin file and check its checksum
    let remote_file_text = self
      .environment
      .get_remote_file("https://plugins.dprint.dev/test-process.json")
      .unwrap()
      .unwrap();
    let checksum = get_sha256_checksum(&remote_file_text);
    self.add_remote_process_plugin_with_checksum(&checksum)
  }

  pub fn add_remote_process_plugin_with_checksum(&mut self, checksum: &str) -> &mut Self {
    let url = "https://plugins.dprint.dev/test-process.json";
    if checksum.is_empty() {
      self.add_plugin(url)
    } else {
      self.add_plugin(&format!("{}@{}", url, checksum))
    }
  }

  pub fn ensure_plugins_section(&mut self) -> &mut Self {
    if self.plugins.is_none() {
      self.plugins = Some(Vec::new());
    }
    self
  }

  pub fn clear_plugins(&mut self) -> &mut Self {
    self.plugins = None;
    self
  }

  pub fn add_plugin(&mut self, plugin: &str) -> &mut Self {
    let mut plugins = self.plugins.take().unwrap_or_else(Vec::new);
    plugins.push(plugin.to_string());
    self.plugins = Some(plugins);
    self
  }

  pub fn add_includes(&mut self, includes_item: &str) -> &mut Self {
    let mut includes = self.includes.take().unwrap_or_else(Vec::new);
    includes.push(includes_item.to_string());
    self.includes = Some(includes);
    self
  }

  pub fn add_excludes(&mut self, excludes_item: &str) -> &mut Self {
    let mut excludes = self.excludes.take().unwrap_or_else(Vec::new);
    excludes.push(excludes_item.to_string());
    self.excludes = Some(excludes);
    self
  }
}

#[derive(Default)]
pub struct TestInfoFileBuilder {
  plugin_schema_version: Option<usize>,
  plugins: Vec<TestInfoFilePlugin>,
}

#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct TestInfoFilePlugin {
  pub name: String,
  pub version: String,
  pub url: String,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub file_names: Option<Vec<String>>,
  pub file_extensions: Vec<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub config_key: Option<String>,
  pub config_excludes: Vec<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub checksum: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub default_config: Option<serde_json::Value>,
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub config_items: Vec<TestInfoFileConfigItem>,
}

#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct TestInfoFileConfigItem {
  #[serde(rename = "match")]
  pub file_match: TestInfoFileMatch,
  pub config: serde_json::Value,
}

#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct TestInfoFileMatch {
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub file_extensions: Vec<String>,
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub file_names: Vec<String>,
}

impl TestInfoFileBuilder {
  pub fn add_plugin(&mut self, plugin: TestInfoFilePlugin) -> &mut Self {
    self.plugins.push(plugin);
    self
  }

  pub fn set_plugin_schema_version(&mut self, version: usize) -> &mut Self {
    self.plugin_schema_version = Some(version);
    self
  }

  pub fn to_string(&self) -> String {
    let mut parts = Vec::new();
    parts.push("\"schemaVersion\": 4".to_string());
    parts.push(format!("\"pluginSystemSchemaVersion\": {}", self.plugin_schema_version.unwrap_or(4)));
    let plugins_text = serde_json::to_string_pretty(&self.plugins).unwrap();
    parts.push(format!("\"latest\": {}", plugins_text));
    format!("{{\n{}\n}}", parts.join(",\n"))
  }
}

pub struct TestEnvironmentBuilder {
  environment: TestEnvironment,
  config_files: HashMap<String, TestConfigFileBuilder>,
  info_file: Option<TestInfoFileBuilder>,
}

impl TestEnvironmentBuilder {
  pub fn new() -> Self {
    Self {
      environment: TestEnvironment::new(),
      config_files: HashMap::new(),
      info_file: None,
    }
  }

  pub fn with_remote_wasm_plugin() -> TestEnvironmentBuilder {
    let mut builder = TestEnvironmentBuilder::new();
    builder.add_remote_wasm_plugin();
    builder
  }

  pub fn with_initialized_remote_wasm_plugin() -> TestEnvironmentBuilder {
    let mut builder = TestEnvironmentBuilder::new();
    builder
      .add_remote_wasm_plugin()
      .with_default_config(|config_file| {
        config_file.add_remote_wasm_plugin();
      })
      .initialize();
    builder
  }

  pub fn with_remote_process_plugin() -> TestEnvironmentBuilder {
    let mut builder = TestEnvironmentBuilder::new();
    builder.add_remote_process_plugin();
    builder
  }

  pub fn with_initialized_remote_process_plugin() -> TestEnvironmentBuilder {
    let mut builder = TestEnvironmentBuilder::new();
    builder
      .add_remote_process_plugin()
      .with_default_config(|config_file| {
        config_file.add_remote_process_plugin();
      })
      .initialize();
    builder
  }

  pub fn with_initialized_remote_wasm_and_process_plugin() -> TestEnvironmentBuilder {
    let mut builder = TestEnvironmentBuilder::new();
    builder
      .add_remote_process_plugin()
      .add_remote_wasm_plugin()
      .with_default_config(|config_file| {
        config_file.add_remote_wasm_plugin().add_remote_process_plugin();
      })
      .initialize();
    builder
  }

  pub fn initialize(&mut self) -> &mut Self {
    test_helpers::run_test_cli(vec!["license"], &self.environment).unwrap(); // cause initialization
    self.environment.clear_logs();
    self
  }

  pub fn build(&mut self) -> TestEnvironment {
    self.environment.clone()
  }

  pub fn add_remote_wasm_plugin(&mut self) -> &mut Self {
    self.add_remote_wasm_plugin_at_url("https://plugins.dprint.dev/test-plugin.wasm");
    self
  }

  pub fn add_remote_wasm_0_1_0_plugin(&mut self) -> &mut Self {
    self
      .environment
      .add_remote_file("https://plugins.dprint.dev/test-plugin-0.1.0.wasm", test_helpers::WASM_PLUGIN_0_1_0_BYTES);
    self
  }

  pub fn add_remote_wasm_plugin_at_url(&mut self, url: &str) -> &mut Self {
    self.environment.add_remote_file(url, test_helpers::WASM_PLUGIN_BYTES);
    self
  }

  pub fn with_default_config(&mut self, func: impl FnMut(&mut TestConfigFileBuilder)) -> &mut Self {
    self.with_local_config("/dprint.json", func)
  }

  pub fn with_local_config(&mut self, file_path: impl AsRef<Path>, func: impl FnMut(&mut TestConfigFileBuilder)) -> &mut Self {
    let config_file_text = self.with_config_get_text(&file_path.as_ref().to_string_lossy(), func);
    self.write_file(file_path, &config_file_text)
  }

  pub fn with_remote_config(&mut self, url: &str, func: impl FnMut(&mut TestConfigFileBuilder)) -> &mut Self {
    let config_file_text = self.with_config_get_text(url, func);
    self.add_remote_file(url, &config_file_text)
  }

  pub fn with_global_config(&mut self, func: impl FnMut(&mut TestConfigFileBuilder)) -> &mut Self {
    // Set up the global config directory
    let global_config_dir = "/global-config";
    self.environment.set_env_var("DPRINT_CONFIG_DIR", Some(global_config_dir));

    // Create the global config file
    let config_file_text = self.with_config_get_text("__global_config__", func);
    let global_config_path = format!("{}/dprint.json", global_config_dir);
    self.write_file(&global_config_path, &config_file_text)
  }

  fn with_config_get_text(&mut self, key: &str, mut func: impl FnMut(&mut TestConfigFileBuilder)) -> String {
    let config_file = self.config_files.entry(key.to_string()).or_insert_with({
      let environment = self.environment.clone();
      || TestConfigFileBuilder::new(environment)
    });
    func(config_file);
    config_file.to_string()
  }

  pub fn with_info_file(&mut self, mut func: impl FnMut(&mut TestInfoFileBuilder)) -> &mut Self {
    if self.info_file.is_none() {
      self.info_file = Some(Default::default());
    }
    let info_file_builder = self.info_file.as_mut().unwrap();
    func(info_file_builder);
    self
      .environment
      .add_remote_file_bytes("https://plugins.dprint.dev/info.json", Vec::from(info_file_builder.to_string().as_bytes()));
    self
  }

  pub fn write_file(&mut self, file_path: impl AsRef<Path>, bytes: impl AsRef<[u8]>) -> &mut Self {
    let file_path = self.environment.clean_path(file_path);
    if let Some(parent) = file_path.parent() {
      self.environment.mk_dir_all(parent).unwrap();
    }
    self.environment.write_file_bytes(file_path, bytes.as_ref()).unwrap();
    self
  }

  pub fn add_staged_file(&mut self, file_path: impl AsRef<Path>) -> &mut Self {
    self.environment.set_staged_file(file_path);
    self
  }

  pub fn add_dirty_file(&mut self, file_path: impl AsRef<Path>) -> &mut Self {
    self.environment.set_dirty_file(file_path);
    self
  }

  pub fn add_remote_file(&mut self, path: &str, text: &str) -> &mut Self {
    self.environment.add_remote_file_bytes(path, text.to_string().into_bytes());
    self
  }

  pub fn add_remote_file_bytes(&mut self, path: &str, bytes: impl Into<Vec<u8>>) -> &mut Self {
    self.environment.add_remote_file_bytes(path, bytes.into());
    self
  }

  pub fn set_cwd(&mut self, dir_path: &str) -> &mut Self {
    self.environment.set_cwd(dir_path);
    self
  }

  pub fn add_local_wasm_plugin(&mut self) -> &mut Self {
    self.write_file("/plugins/test-plugin.wasm", test_helpers::WASM_PLUGIN_BYTES)
  }

  pub fn add_remote_process_plugin(&mut self) -> &mut Self {
    self.add_remote_process_plugin_at_url("https://plugins.dprint.dev/test-process.json", &TestProcessPluginFile::default())
  }

  pub fn add_remote_process_plugin_at_url(&mut self, url: &str, file_text: &TestProcessPluginFile) -> &mut Self {
    let zip_bytes = &test_helpers::PROCESS_PLUGIN_ZIP_BYTES;
    self.environment.add_remote_file_bytes(
      "https://github.com/dprint/test-process-plugin/releases/0.1.0/test-process-plugin.zip",
      zip_bytes.to_vec(),
    );
    self.environment.add_remote_file_bytes(url, file_text.text().to_string().into_bytes());
    self
  }
}