tiefdownlib 0.10.0

A library to manage and convert TiefDown projects.
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
use crate::{
    consts::CURRENT_MANIFEST_VERSION,
    manifest_model::{
        Manifest, MarkdownProject, PreProcessor, PreProcessors, Processor, Processors, Profile,
        Template, upgrade_manifest,
    },
    template_management::{self, add_lix_filters, get_template_path, get_template_type_from_path},
    template_type::TemplateType,
};
use color_eyre::eyre::{Result, eyre};
use fs_extra::dir;
use log::{debug, error, info};
use std::{env::current_dir, fs, path::PathBuf, process::Command};
use toml::{Table, Value};

/// Initializes a new TiefDown project.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `template_names` - A list of preset template names to initialize the project with.
///   * Defaults to the default TeX template if not provided.
/// * `no_templates` - A flag indicating whether to skip initializing the templates.
/// * `force` - A flag indicating whether to force initialization even if the project already exists.
/// * `markdown_dir` - The path to the markdown directory.
/// * `smart_clean` - A flag indicating whether to enable smart clean.
/// * `smart_clean_threshold` - The threshold for smart clean.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn init(
    project: Option<PathBuf>,
    template_names: Option<Vec<String>>,
    no_templates: bool,
    force: bool,
    markdown_dir: Option<String>,
    smart_clean: bool,
    smart_clean_threshold: Option<u32>,
) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));

    if project.clone().exists() && force {
        if project == current_dir()? {
            return Err(eyre!(
                "Cannot force initialization in the current directory."
            ));
        }
        std::fs::remove_dir_all(project.clone())?;
    }

    if !project.clone().exists() {
        std::fs::create_dir(project.clone())?;
        debug!("Created project directory at '{}'.", project.display());
    }

    let manifest_path = project.join("manifest.toml");
    if manifest_path.exists() {
        return Err(eyre!(
            "Manifest file already exists. Please remove it before initializing a new project or use the --force flag."
        ));
    }

    let mut templates: Vec<Template> = Vec::new();

    if !no_templates {
        templates.extend(
            template_names
                .unwrap_or(vec!["template.tex".to_string()])
                .iter()
                .map(get_template_mapping_for_preset)
                .collect::<Result<Vec<_>>>()?,
        );
    }

    let markdown_dir = markdown_dir.clone().unwrap_or("Markdown".to_string());

    let markdown_dir_path = project.join(&markdown_dir);
    if !markdown_dir_path.exists() {
        std::fs::create_dir(&markdown_dir_path)?;
        std::fs::write(
            markdown_dir_path.join("Chapter 1 - Introduction.md"),
            r#"# Test Document
This is a simple test document for you to edit or overwrite."#,
        )?;
        debug!(
            "Initialized markdown directory at '{}'.",
            markdown_dir_path.display()
        );
    }

    let smart_clean_value = if smart_clean { Some(true) } else { None };

    debug!(
        "Initializing templates ({}): {:?}",
        templates.len(),
        templates.iter().map(|t| t.name.clone()).collect::<Vec<_>>()
    );
    create_templates(&project, &templates)?;

    let manifest: Manifest = Manifest {
        version: CURRENT_MANIFEST_VERSION,
        markdown_projects: Some(vec![MarkdownProject {
            name: markdown_dir.clone(),
            path: PathBuf::from(markdown_dir.clone()),
            output: PathBuf::from("."),
            metadata_fields: None,
            default_profile: None,
            resources: None,
        }]),
        templates: templates.clone(),
        custom_processors: Processors {
            preprocessors: Vec::new(),
            processors: Vec::new(),
        },
        smart_clean: smart_clean_value,
        smart_clean_threshold,
        shared_metadata: None,
        metadata_settings: None,
        profiles: None,
        injections: None,
    };

    std::fs::write(manifest_path.clone(), toml::to_string(&manifest)?)?;
    debug!("Wrote manifest to '{}'.", manifest_path.display());

    Ok(())
}

fn get_template_mapping_for_preset(template: &String) -> Result<Template> {
    // NOTE: As this is just the preset templates, we set the minimal implementation.
    let mut template = Template {
        name: template.clone(),
        template_type: get_template_type_from_path(template)?,
        output: None,
        template_file: None,
        filters: None,
        preprocessors: None,
        processor: None,
        header_injections: None,
        body_injections: None,
        footer_injections: None,
    };

    add_lix_filters(&mut template);

    Ok(template)
}

/// Adds a new template to the TiefDown project.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `template_name` - The name of the template to add.
/// * `template_type` - The type of the template.
/// * `template_file` - The path to the template file.
/// * `output` - The output file for the template.
/// * `filters` - A list of lua-filters to apply to the template.
///   * Can be either a file or directory.
/// * `preprocessor` - The name of the preprocessor to use.
/// * `processor` - The name of the processor to use.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn add_template(
    project: Option<PathBuf>,
    template_name: String,
    template_type: Option<TemplateType>,
    template_file: Option<PathBuf>,
    output: Option<PathBuf>,
    filters: Option<Vec<String>>,
    preprocessors: Option<Vec<String>>,
    preprocessor_output: Option<PathBuf>,
    processor: Option<String>,
    header_injections: Option<Vec<String>>,
    body_injections: Option<Vec<String>>,
    footer_injections: Option<Vec<String>>,
) -> Result<()> {
    debug!(
        "Adding template '{}' (type: {:?})...",
        template_name, template_type
    );
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    if manifest.templates.iter().any(|t| t.name == template_name) {
        return Err(eyre!(
            "Template with name '{}' already exists.",
            template_name
        ));
    }

    let template_type = match template_type {
        Some(t) => t,
        None => {
            get_template_type_from_path(get_template_path(template_file.clone(), &template_name))?
        }
    };

    if preprocessors.is_some() && preprocessor_output.is_none() {
        return Err(eyre!(
            "Cannot set preprocessors without setting a combined output."
        ));
    }

    let mut template_preprocessors = None;
    if preprocessor_output.is_some() {
        template_preprocessors = Some(PreProcessors {
            preprocessors: preprocessors.unwrap_or(vec![]),
            combined_output: PathBuf::from(preprocessor_output.unwrap()),
        });
    }

    let mut template = Template {
        name: template_name.clone(),
        template_type,
        output,
        template_file,
        filters,
        preprocessors: template_preprocessors,
        processor,
        header_injections,
        body_injections,
        footer_injections,
    };

    create_templates(&project, &vec![template.clone()])?;
    add_lix_filters(&mut template);

    manifest.templates.extend([template.clone()]);

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;
    debug!("Template '{}' added and manifest updated.", template_name);

    Ok(())
}

/// Removes a template from the TiefDown project.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `template_name` - The name of the template to remove.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn remove_template(project: Option<PathBuf>, template_name: String) -> Result<()> {
    debug!("Removing template '{}'...", template_name);
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    if let Some(pos) = manifest
        .templates
        .iter()
        .position(|t| t.name == template_name)
    {
        let removed_template = manifest.templates.swap_remove(pos);

        let manifest_content = toml::to_string(&manifest)?;
        std::fs::write(&manifest_path, manifest_content)?;

        let template_dir = project.join("template");
        let template_path = template_dir.join(
            removed_template
                .template_file
                .as_ref()
                .unwrap_or(&PathBuf::from(&removed_template.name)),
        );

        if template_path.is_dir() {
            std::fs::remove_dir_all(&template_path)?;
        } else {
            fs::remove_file(template_path)?;
        }
        debug!("Removed template resources for '{}'.", template_name);
    } else {
        return Err(eyre!(
            "Template {} could not be found in the project.",
            template_name
        ));
    }

    Ok(())
}

/// Updates a template in the TiefDown project.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `template_name` - The name of the template to update.
/// * `template_type` - The type of the template.
///   * It is advised to not update template types, but rather create a new template.
/// * `template_file` - The path to the template file.
/// * `output` - The output file for the template.
/// * `filters` - A list of lua-filters to apply to the template.
///   * Can be either a file or directory.
///   * Overwrites existing filters!
/// * `add_filters` - A list of lua-filters to add to the template.
///   * Can be either a file or directory.
/// * `remove_filters` - A list of lua-filters to remove from the template.
///   * Can be either a file or directory.
/// * `preprocessor` - The name of the preprocessor to use.
/// * `processor` - The name of the processor to use.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn update_template(
    project: Option<PathBuf>,
    template_name: String,
    template_type: Option<TemplateType>,
    template_file: Option<PathBuf>,
    output: Option<PathBuf>,
    filters: Option<Vec<String>>,
    add_filters: Option<Vec<String>>,
    remove_filters: Option<Vec<String>>,
    preprocessors: Option<Vec<String>>,
    add_preprocessors: Option<Vec<String>>,
    remove_preprocessors: Option<Vec<String>>,
    preprocessor_output: Option<PathBuf>,
    processor: Option<String>,
    header_injections: Option<Vec<String>>,
    body_injections: Option<Vec<String>>,
    footer_injections: Option<Vec<String>>,
) -> Result<()> {
    debug!(
        "Updating template '{}' (fields provided: type={:?}, file={:?}, output={:?})",
        template_name, template_type, template_file, output
    );
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    if let Some(index) = manifest
        .templates
        .iter()
        .position(|t| t.name == template_name)
    {
        let template = &mut manifest.templates[index];

        template.template_type = template_type.unwrap_or(template.template_type.clone());
        template.output = output.or(template.output.clone());
        template.template_file = template_file.or(template.template_file.clone());
        if let Some(filters) = filters {
            template.filters = Some(filters);
        } else if let Some(add_filters) = add_filters {
            if add_filters.iter().any(|filter| filter.is_empty()) {
                return Err(eyre!(
                    "Cannot add an empty filter to the template '{}'.",
                    template_name
                ));
            }

            if let Some(filters) = &mut template.filters {
                filters.extend(add_filters);
            } else {
                template.filters = Some(add_filters);
            }
        } else if let Some(remove_filters) = remove_filters {
            if remove_filters.iter().any(|filter| filter.is_empty()) {
                return Err(eyre!(
                    "Cannot remove an empty filter from the template '{}'.",
                    template_name
                ));
            }

            if let Some(filters) = &mut template.filters {
                filters.retain(|filter| !remove_filters.contains(filter));
            }
        }

        if let Some(preprocessor_output) = preprocessor_output {
            if let Some(preprocessors) = &mut template.preprocessors {
                preprocessors.combined_output = PathBuf::from(preprocessor_output);
            } else {
                template.preprocessors = Some(PreProcessors {
                    preprocessors: vec![],
                    combined_output: PathBuf::from(preprocessor_output),
                });
            }
        }

        if let Some(preprocessors) = preprocessors {
            if let Some(template_preprocessors) = &mut template.preprocessors {
                template_preprocessors.preprocessors = preprocessors;
            } else {
                return Err(eyre!(
                    "Preprocessor cannot be set as no combined output is set for the template '{}'. Please set a combined output first.",
                    template_name
                ));
            }
        } else if let Some(add_preprocessors) = add_preprocessors {
            if add_preprocessors.iter().any(|filter| {
                manifest
                    .custom_processors
                    .preprocessors
                    .iter()
                    .all(|p| p.name != *filter)
            }) {
                return Err(eyre!(
                    "Preprocessor '{}' cannot be added as it does not exist or is invalid.",
                    template_name
                ));
            }

            if let Some(preprocessors) = &mut template.preprocessors {
                preprocessors.preprocessors.extend(add_preprocessors);
            } else {
                return Err(eyre!(
                    "Preprocessor cannot be set as no combined output is set for the template '{}'. Please set a combined output first.",
                    template_name
                ));
            }
        } else if let Some(remove_preprocessors) = remove_preprocessors {
            if remove_preprocessors.iter().any(|filter| filter.is_empty()) {
                return Err(eyre!(
                    "Cannot remove an empty preprocessor from the template '{}'.",
                    template_name
                ));
            }

            if let Some(preprocessors) = &mut template.preprocessors {
                preprocessors
                    .preprocessors
                    .retain(|filter| !remove_preprocessors.contains(filter));
            }
        }

        template.processor = processor.or(template.processor.clone());

        template.header_injections = header_injections.or(template.header_injections.clone());
        template.body_injections = body_injections.or(template.body_injections.clone());
        template.footer_injections = footer_injections.or(template.footer_injections.clone());
    } else {
        return Err(eyre!(
            "Template with name '{}' does not exist.",
            template_name
        ));
    }

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;
    debug!("Template '{}' updated and manifest saved.", template_name);

    Ok(())
}

/// Updates the globally managed settings of a TiefDown project.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `smart_clean` - Whether to enable smart cleaning.
/// * `smart_clean_threshold` - The threshold for smart cleaning.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn update_settings(
    project: Option<PathBuf>,
    smart_clean: Option<bool>,
    smart_clean_threshold: Option<u32>,
) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    if let Some(smart_clean_value) = smart_clean {
        let smart_clean_value = if smart_clean_value { Some(true) } else { None };
        manifest.smart_clean = smart_clean_value;
    }

    if let Some(smart_clean_threshold) = smart_clean_threshold {
        manifest.smart_clean_threshold = Some(smart_clean_threshold);
    }

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;

    Ok(())
}

/// Adds a preprocessor to the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `name` - The name of the preprocessor.
/// * `combined_output` - The output file for the preprocessor.
/// * `extension_filter` - The file extension the preprocessor should be applied to.
/// * `cli` - The program to call as the preprocessor.
/// * `cli_args` - The arguments for the preprocessor.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn add_preprocessor(
    project: Option<PathBuf>,
    name: String,
    extension_filter: Option<String>,
    cli: Option<String>,
    cli_args: Vec<String>,
) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    let preprocessor = PreProcessor {
        name,
        extension_filter,
        cli,
        cli_args,
    };
    manifest.custom_processors.preprocessors.push(preprocessor);

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;

    Ok(())
}

/// Removes a preprocessor from the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `name` - The name of the preprocessor to remove.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn remove_preprocessor(project: Option<PathBuf>, name: String) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    if let Some(pos) = manifest
        .custom_processors
        .preprocessors
        .iter()
        .position(|p| p.name == name)
    {
        manifest.custom_processors.preprocessors.remove(pos);
    } else {
        return Err(eyre!("Preprocessor with name '{}' does not exist.", name));
    }

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;

    Ok(())
}

/// Adds a processor to the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `name` - The name of the processor.
/// * `processor_args` - The arguments for the processor.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn add_processor(
    project: Option<PathBuf>,
    name: String,
    processor_args: Vec<String>,
) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    let processor = Processor {
        name,
        processor_args,
    };
    manifest.custom_processors.processors.push(processor);

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;

    Ok(())
}

/// Removes a processor from the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `name` - The name of the processor to remove.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn remove_processor(project: Option<PathBuf>, name: String) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    if let Some(pos) = manifest
        .custom_processors
        .processors
        .iter()
        .position(|p| p.name == name)
    {
        manifest.custom_processors.processors.remove(pos);
    } else {
        return Err(eyre!("Processor with name '{}' does not exist.", name));
    }

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;

    Ok(())
}

/// Retrieves the list of processors from the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
///
/// # Returns
///
/// A Result containing either an error or a vector of Processor objects.
pub fn get_processors(project: Option<PathBuf>) -> Result<Vec<Processor>> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let manifest = load_and_convert_manifest(&manifest_path)?;

    Ok(manifest.custom_processors.processors)
}

/// Adds a profile to the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `name` - The name of the profile.
/// * `templates` - A vector of template names.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn add_profile(project: Option<PathBuf>, name: String, templates: Vec<String>) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    let profile = Profile { name, templates };

    if manifest.profiles.is_none() {
        manifest.profiles = Some(vec![]);
    }

    manifest.profiles.as_mut().unwrap().push(profile);

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;

    Ok(())
}

/// Removes a profile from the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
/// * `name` - The name of the profile to remove.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn remove_profile(project: Option<PathBuf>, name: String) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let mut manifest = load_and_convert_manifest(&manifest_path)?;

    if let Some(profiles) = &mut manifest.profiles {
        if let Some(pos) = profiles.iter().position(|p| p.name == name) {
            profiles.remove(pos);
        } else {
            return Err(eyre!("Profile with name '{}' does not exist.", name));
        }
    } else {
        return Err(eyre!("Profile with name '{}' does not exist.", name));
    }

    let manifest_content = toml::to_string(&manifest)?;
    std::fs::write(&manifest_path, manifest_content)?;

    Ok(())
}

/// Retrieves the list of templates from the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
///
/// # Returns
///
/// A Result containing either an error or a vector of TemplateMapping objects.
pub fn get_templates(project: Option<PathBuf>) -> Result<Vec<Template>> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let manifest = load_and_convert_manifest(&manifest_path)?;

    Ok(manifest.templates)
}

/// Retrieves the list of profiles from the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
///
/// # Returns
///
/// A Result containing either an error or a vector of Profile objects.
pub fn get_profiles(project: Option<PathBuf>) -> Result<Vec<Profile>> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let manifest = load_and_convert_manifest(&manifest_path)?;

    let profiles = manifest.profiles;

    Ok(profiles.unwrap_or_default())
}

/// Retrieves the list of preprocessors from the project's manifest.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
///
/// # Returns
///
/// A Result containing either an error or a vector of PreProcessor objects.
pub fn get_preprocessors(project: Option<PathBuf>) -> Result<Vec<PreProcessor>> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");

    let manifest = load_and_convert_manifest(&manifest_path)?;

    Ok(manifest.custom_processors.preprocessors)
}

/// Cleans the project's output directories.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn clean(project: Option<PathBuf>) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");
    let _ = load_and_convert_manifest(&manifest_path)?;

    run_smart_clean(&project, 0)?;

    Ok(())
}

/// Runs the smart clean operation on the project.
///
/// # Arguments
///
/// * `project` - The path to the project directory (relative or absolute).
///   * Defaults to the current directory if not provided.
pub fn smart_clean(project: Option<PathBuf>) -> Result<()> {
    let project = project.unwrap_or(PathBuf::from("."));
    let manifest_path = project.join("manifest.toml");
    let manifest = load_and_convert_manifest(&manifest_path)?;
    let smart_clean_threshold = manifest.smart_clean_threshold.unwrap_or(5);

    run_smart_clean(&project, smart_clean_threshold)?;

    Ok(())
}

pub(crate) fn run_smart_clean(project: &PathBuf, smart_clean_threshold: u32) -> Result<()> {
    debug!(
        "Running smart clean on project {} with threshold of {}.",
        project.display(),
        smart_clean_threshold
    );

    let regex = regex::Regex::new(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}$")?;

    let mut dirs_to_delete: Vec<_> = fs::read_dir(project)?
        .filter_map(Result::ok)
        .map(|entry| entry.path())
        .filter(|path| {
            path.is_dir()
                && path
                    .file_name()
                    .and_then(|name| name.to_str())
                    .map_or(false, |name| regex.is_match(name))
        })
        .collect();

    dirs_to_delete.sort_by(|a, b| a.cmp(b));
    dirs_to_delete.truncate(
        dirs_to_delete
            .len()
            .saturating_sub(smart_clean_threshold as usize),
    );

    for dir in dirs_to_delete {
        debug!("Deleting directory: {}", dir.display());

        dir::remove(dir)?;
    }

    Ok(())
}

/// Checks if the dependencies are installed.
///
/// # Arguments
///
/// * `dependencies` - A vector of strings representing the dependencies to check.
///
/// # Returns
///
/// A Result containing either an error or nothing.
pub fn check_dependencies(dependencies: Vec<&str>) -> Result<()> {
    debug!("Checking dependencies: {:?}", dependencies);
    let errors = get_missing_dependencies(dependencies)?;

    if !errors.is_empty() {
        for error in errors {
            error!("{}", error);
        }
        return Err(eyre!("Some dependencies are missing."));
    }

    info!("All dependencies are installed.");
    Ok(())
}

pub(crate) fn get_missing_dependencies(dependencies: Vec<&str>) -> Result<Vec<String>> {
    let mut errors = Vec::new();

    for dependency in dependencies {
        debug!("Probing dependency: '{}' --version", dependency);
        let output = Command::new(dependency).arg("--version").output();

        if !output.is_ok() {
            errors.push(format!(
                "Could not call {}:\n{}",
                dependency,
                output.unwrap_err()
            ));
        }
    }

    Ok(errors)
}

pub fn load_and_convert_manifest(manifest_path: &std::path::PathBuf) -> Result<Manifest> {
    if !manifest_path.exists() {
        return Err(eyre!(
            "Manifest file does not exist. Please initialize a project before editing it."
        ));
    }

    let manifest_content = fs::read_to_string(manifest_path)?;
    debug!(
        "Loading manifest from '{}' ({} bytes).",
        manifest_path.display(),
        manifest_content.len()
    );

    let mut manifest: Table = toml::from_str(&manifest_content)?;

    let current_manifest_version: u32 = manifest
        .get("version")
        .unwrap_or(&Value::Integer(0))
        .as_integer()
        .unwrap_or(0)
        .try_into()?;

    if current_manifest_version < CURRENT_MANIFEST_VERSION {
        upgrade_manifest(&mut manifest, current_manifest_version)?;
        debug!("Manifest upgraded to version {}.", CURRENT_MANIFEST_VERSION,);
    } else if current_manifest_version > CURRENT_MANIFEST_VERSION {
        return Err(eyre!(
            "Manifest file is from a newer version of the program. Please update the program."
        ));
    }

    let manifest = &toml::to_string(&manifest)?;
    fs::write(manifest_path, manifest)?;
    debug!(
        "Manifest written back after potential upgrade ({} bytes).",
        manifest.len()
    );

    let manifest: Manifest = toml::from_str(manifest)?;

    Ok(manifest)
}

fn create_templates(project: &std::path::Path, templates: &Vec<Template>) -> Result<()> {
    for template in templates {
        let template_creator = template_management::get_template_creator(template.name.as_str())?;

        debug!(
            "Creating template '{}' of type {}...",
            template.name, template.template_type
        );
        template_creator(project, template)?;
    }

    Ok(())
}