labt 0.3.9

Lab-t Lightweight Android build tool
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
use std::{
    collections::{HashMap, HashSet},
    fmt::Display,
    path::{Path, PathBuf},
    rc::Rc,
    str::FromStr,
};

use anyhow::{bail, Context};
use glob::glob;
use serde::{Deserialize, Serialize};
use toml_edit::{value, Document};

use crate::{
    config::repository::{ChannelType, Revision},
    get_project_root,
    pom::VersionRange,
    submodules::{
        build::Step,
        sdk::{toml_strings, GOOGLE_REPO_NAME_STR},
        sdkmanager::{installed_list::RepositoryInfo, ToId, ToIdLong},
    },
};

use super::Plugin;

pub(super) const NAME: &str = "name";
pub(super) const VERSION: &str = "version";
pub(super) const LABT: &str = "labt";
pub(super) const STAGE: &str = "stage";
pub(super) const FILE: &str = "file";
pub(super) const PRIORITY: &str = "priority";
pub(super) const INPUTS: &str = "inputs";
pub(super) const OUTPUTS: &str = "outputs";
pub(super) const PACKAGE_PATHS: &str = "package_paths";
pub(super) const SDK: &str = "sdk";
pub(super) const REPO: &str = "repo";
pub(super) const PATH: &str = "path";
pub(super) const CHANNEL: &str = "channel";
pub(super) const UNSAFE: &str = "unsafe";

const PRE: &str = "pre";
const AAPT: &str = "aapt";
const COMPILE: &str = "compile";
const DEX: &str = "dex";
const BUNDLE: &str = "bundle";
const POST: &str = "post";

/// The sdk entries that this plugin requires
#[derive(PartialEq, Debug, Clone)]
pub struct SdkEntry {
    pub repo: String,
    pub name: String,
    pub path: String,
    pub version: Revision,
    pub channel: ChannelType,
}

impl Default for SdkEntry {
    fn default() -> Self {
        SdkEntry {
            repo: GOOGLE_REPO_NAME_STR.to_string(),
            name: String::default(),
            path: String::default(),
            version: Revision::default(),
            channel: ChannelType::Unset,
        }
    }
}

impl ToId for SdkEntry {
    fn create_id(&self) -> (&String, &Revision, &ChannelType) {
        (&self.path, &self.version, &self.channel)
    }
}
impl ToIdLong for SdkEntry {
    fn create_id(&self) -> (&String, &String, &Revision, &ChannelType) {
        (&self.repo, &self.path, &self.version, &self.channel)
    }
}

#[derive(Default, Debug)]
pub struct PluginToml {
    /// plugin name
    pub name: String,
    /// plugin version
    pub version: String,
    /// plugin states
    pub stages: HashMap<Step, PluginStage>,

    /// A list of plugin specified repositories
    pub sdk_repo: HashMap<String, RepositoryInfo>,

    /// plugin sdk dependencies
    pub sdk: Vec<SdkEntry>,

    pub path: PathBuf,
    /// Paths to search for required lua modules
    pub package_paths: Option<Vec<PathBuf>>,
    /// Enable unsafe lua api for entire plugin
    pub enable_unsafe: bool,
    /// required Labt version
    pub labt: Option<VersionRange>,
}

#[derive(Default, Debug, Deserialize, Serialize)]
pub struct Stage {
    /// Pre build state, used in generating code or building external
    /// dependency used in next steps
    pub pre: Option<PluginStage>,
    /// Complie application res folder and generate required R.java files
    pub aapt: Option<PluginStage>,
    /// Compile java/kotlin files to produce java jar files of the project
    pub compile: Option<PluginStage>,
    /// Dex jar files to produce android classes.dex files,
    pub dex: Option<PluginStage>,
    /// Bundles all the compiled app files into a zip with .apk extension,
    /// should also sign the bundle
    pub bundle: Option<PluginStage>,
    /// Do anything with the resulting built app file, deploy a release, install, run etc.
    pub post: Option<PluginStage>,
}

#[derive(Default, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct PluginStage {
    /// file containing the entry point
    pub file: PathBuf,
    /// plugin priority
    pub priority: i32,
    /// The input files that we should watch for changes
    pub inputs: Option<Vec<String>>,
    /// The output files that we should ensure that it is uptodate
    pub outputs: Option<Vec<String>>,
    /// Enable unsafe lua api
    #[serde(rename = "unsafe", default)]
    pub enable_unsafe: bool,
}

impl PluginToml {
    /// Maps PluginToml stages into their [`Plugin`] representation.
    pub fn get_steps(self) -> anyhow::Result<Vec<Plugin>> {
        let mut steps = vec![];
        let sdk_rc = Rc::new(self.sdk.clone());

        /// because i cant accurately copy & paste these mappings
        /// from PluginToml stage to Plugin Step without creating a bug,
        /// let the macro repeat it, maybe im lazy
        macro_rules! map_plugin {
            [$($j:expr),*] => {
                $(
                // check if $i is set, if set then create a sub plugin
                if let Some(s) = &self.stages.get(&$j) {
                    // get this plugin root directory
                    let mut path = self.path.clone();
                    // push the plugin source path to path
                    path.push(s.file.clone());
                    // create a plugin and set its step as $j
                    let mut plugin = Plugin::new(self.name.clone(), self.version.clone(), path, $j);
                    plugin.sdk_dependencies = Rc::clone(&sdk_rc);
                    plugin.priority = s.priority;
                    plugin.unsafe_mode = self.enable_unsafe || s.enable_unsafe;
                    plugin.package_paths = if let Some(package_paths) = &self.package_paths{
                            load_package_paths(package_paths, &self.path)
                        }else{
                            load_package_paths(&[], &self.path)
                        };

                    if s.inputs.is_some() && s.outputs.is_some() {
                        // both have items, so add them to the output
                        plugin.dependents = Some((expand_globs(s.inputs.clone().unwrap()).context("Unable to expand global patterns specified by the inputs dependents")?,
                                expand_globs(s.outputs.clone().unwrap()).context("Unable to expand global patterns specified by the outputs dependents")?));
                    }
                    // add the plugin to the list of plugins
                    steps.push(plugin);
                }
               )*
            };
        }

        map_plugin![
            Step::PRE,
            Step::AAPT,
            Step::COMPILE,
            Step::DEX,
            Step::BUNDLE,
            Step::POST
        ];

        Ok(steps)
    }
}
#[derive(Debug)]
enum PluginTomlErrorKind {
    /// name key is missing
    MissingKey(&'static str),
    /// name key is missing
    MissingTableKey(&'static str, String, Option<usize>),
    /// Failed to convert a value to string
    ToStringErr(&'static str, Option<&'static str>, Option<usize>),
    /// Failed to convert a value to bool
    ToBoolErr(&'static str, Option<&'static str>, Option<usize>),
    /// Invalid sdk string entry
    InvalidSdkKey(String, String),
    /// Invalid version string
    InvalidSdkVersionString(String),
    /// Invalid channel name
    InvalidChannel(String),
}
#[derive(Debug)]
struct PluginTomlError {
    kind: PluginTomlErrorKind,
}

impl PluginTomlError {
    pub fn new(kind: PluginTomlErrorKind) -> Self {
        Self { kind }
    }
}
impl std::error::Error for PluginTomlError {}

impl Display for PluginTomlError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            PluginTomlErrorKind::MissingKey(key) => {
                write!(f, "Missing {} which is required!", key)
            }
            PluginTomlErrorKind::MissingTableKey(key, table, position) => {
                if let Some(position) = position {
                    write!(
                        f,
                        "Missing {} for array table \"{}\" at position {} which is required",
                        key, table, position
                    )
                } else {
                    write!(
                        f,
                        "Missing {} for table \"{}\" which is required.",
                        key, table
                    )
                }
            }
            PluginTomlErrorKind::ToStringErr(key, Some(table), Some(position)) => {
                write!(
                    f,
                    "Failed to convert {} value as string in the table \"{}\" at position {}.",
                    key, table, position
                )
            }
            PluginTomlErrorKind::ToStringErr(key, None, _) => {
                write!(f, "Failed to convert {} value as string.", key)
            }
            PluginTomlErrorKind::ToBoolErr(key, Some(table), Some(position)) => {
                write!(
                    f,
                    "Failed to convert {} value as Boolean in the table \"{}\" at position {}.",
                    key, table, position
                )
            }
            PluginTomlErrorKind::ToBoolErr(key, None, _) => {
                write!(f, "Failed to convert {} value as boolean.", key)
            }
            PluginTomlErrorKind::InvalidSdkVersionString(key) => {
                write!(f, "Invalid version string for sdk dependency {}", key)
            }
            PluginTomlErrorKind::InvalidSdkKey(key, value) => {
                write!(
                    f,
                    "Invalid sdk key value for {}. Expected format is path:version:channel but found {}.",
                    key,value
                )
            }
            PluginTomlErrorKind::InvalidChannel(key) => {
                write!(f, "Invalid channel name for {} sdk dependency", key)
            }
            _ => {
                write!(f, "Unhandled error occured while parsing plugin.toml")
            }
        }
    }
}

impl Display for PluginToml {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut doc = Document::new();
        doc.insert(NAME, value(self.name.as_str()));
        doc.insert(VERSION, value(self.version.as_str()));

        let mut sdk_table = toml_edit::Table::new();
        for sdk in &self.sdk {
            let mut table = toml_edit::InlineTable::new();
            table.insert(PATH, sdk.path.as_str().into());
            table.insert(VERSION, sdk.version.to_string().into());
            table.insert(CHANNEL, sdk.channel.to_string().into());
            sdk_table.insert(&sdk.name, value(table));
        }
        let mut stages = toml_edit::Table::new();
        let mut show_stage = |stage: Step| {
            if let Some(s) = self.stages.get(&stage) {
                let mut table = toml_edit::Table::new();
                table.insert(FILE, value(s.file.to_string_lossy().to_string().as_str()));
                table.insert(PRIORITY, value(s.priority as i64));
                if let Some(inputs) = &s.inputs {
                    let array = toml_edit::Array::from_iter(inputs.iter().map(|p| p.as_str()));
                    table.insert(INPUTS, value(array));
                }
                if let Some(outputs) = &s.outputs {
                    let array = toml_edit::Array::from_iter(outputs.iter().map(|p| p.as_str()));
                    table.insert(OUTPUTS, value(array));
                }
                if s.enable_unsafe {
                    table.insert(UNSAFE, value(true));
                }
                stages.insert(stage.to_string().as_str(), toml_edit::Item::Table(table));
            }
        };
        show_stage(Step::PRE);
        show_stage(Step::AAPT);
        show_stage(Step::COMPILE);
        show_stage(Step::DEX);
        show_stage(Step::BUNDLE);
        show_stage(Step::POST);

        doc.insert(SDK, toml_edit::Item::Table(sdk_table));
        doc.insert(STAGE, toml_edit::Item::Table(stages));
        write!(f, "{}", doc)
    }
}

impl FromStr for PluginToml {
    type Err = anyhow::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let doc: Document = s.parse().context("Failed to parse plugin.toml file")?;

        let name = if doc.contains_key(NAME) {
            doc[NAME]
                .as_str()
                .ok_or_else(|| {
                    PluginTomlError::new(PluginTomlErrorKind::ToStringErr(NAME, None, None))
                })?
                .to_string()
        } else {
            bail!(PluginTomlError::new(PluginTomlErrorKind::MissingKey(NAME)));
        };

        let version = if doc.contains_key(VERSION) {
            doc[VERSION]
                .as_str()
                .ok_or_else(|| {
                    PluginTomlError::new(PluginTomlErrorKind::ToStringErr(NAME, None, None))
                })?
                .to_string()
        } else {
            bail!(PluginTomlError::new(PluginTomlErrorKind::MissingKey(
                VERSION
            )));
        };

        let enable_unsafe = if doc.contains_key(UNSAFE) {
            doc[UNSAFE].as_bool().ok_or_else(|| {
                PluginTomlError::new(PluginTomlErrorKind::ToBoolErr(NAME, None, None))
            })?
        } else {
            false
        };

        let labt_version = if doc.contains_key(LABT) {
            let v = doc[LABT]
                .as_str()
                .ok_or_else(|| {
                    PluginTomlError::new(PluginTomlErrorKind::ToStringErr(NAME, None, None))
                })?
                .to_string();

            Some(v.parse::<VersionRange>()?)
        } else {
            None
        };

        let package_paths = doc
            .get(PACKAGE_PATHS)
            .and_then(|f| f.as_array())
            .map(|paths| paths.iter().map(|p| PathBuf::from(p.to_string())).collect());

        let mut stages_map: HashMap<Step, PluginStage> = HashMap::new();
        if let Some(stages) = doc.get(STAGE).and_then(|s| s.as_table()) {
            let load_stage = |stage_name: &'static str, stages: &toml_edit::Table| {
                if let Some(stage) = stages.get(stage_name).and_then(|f| f.as_table()) {
                    let file = if let Some(file) = stage.get(FILE) {
                        PathBuf::from(
                            file.as_str()
                                .ok_or_else(|| {
                                    PluginTomlError::new(PluginTomlErrorKind::ToStringErr(
                                        FILE,
                                        Some(STAGE),
                                        None,
                                    ))
                                })?
                                .to_string(),
                        )
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::MissingTableKey(
                            FILE,
                            stage_name.to_string(),
                            None
                        )))
                    };

                    let priority = if let Some(priority) = stage.get(PRIORITY) {
                        priority.as_integer().ok_or_else(|| {
                            PluginTomlError::new(PluginTomlErrorKind::ToStringErr(
                                PRIORITY,
                                Some(STAGE),
                                None,
                            ))
                        })? as i32
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::MissingTableKey(
                            PRIORITY,
                            STAGE.to_string(),
                            None
                        )))
                    };

                    let inputs: Option<Vec<String>> = if let Some(inputs) = stage.get(INPUTS) {
                        inputs.as_array().map(|array| {
                            array
                                .iter()
                                .map(|a| a.as_str().unwrap_or("").to_string())
                                .collect()
                        })
                    } else {
                        None
                    };

                    let outputs: Option<Vec<String>> = if let Some(outputs) = stage.get(OUTPUTS) {
                        outputs.as_array().map(|array| {
                            array
                                .iter()
                                .map(|a| a.as_str().unwrap_or("").to_string())
                                .collect()
                        })
                    } else {
                        None
                    };
                    let enabe_unsafe_stage = if let Some(unsafe_mode) = stage.get(UNSAFE) {
                        unsafe_mode.as_bool().ok_or_else(|| {
                            PluginTomlError::new(PluginTomlErrorKind::ToBoolErr(NAME, None, None))
                        })?
                    } else {
                        false
                    };

                    Ok(Some(PluginStage {
                        file,
                        priority,
                        inputs,
                        outputs,
                        enable_unsafe: enabe_unsafe_stage,
                    }))
                } else {
                    Ok(None)
                }
            };

            let mut map_stage = |step: Step, key: &'static str| {
                if let Some(stage) = load_stage(key, stages)? {
                    stages_map.insert(step, stage);
                }
                Ok::<(), anyhow::Error>(())
            };

            map_stage(Step::PRE, PRE)?;
            map_stage(Step::AAPT, AAPT)?;
            map_stage(Step::COMPILE, COMPILE)?;
            map_stage(Step::DEX, DEX)?;
            map_stage(Step::BUNDLE, BUNDLE)?;
            map_stage(Step::POST, POST)?;
        };

        let mut sdk_deps: Vec<SdkEntry> = Vec::new();
        if let Some(table) = doc.get(SDK).and_then(|s| s.as_table()) {
            for (key, value) in table.iter() {
                let mut sdk = SdkEntry {
                    name: key.to_string(),
                    ..Default::default()
                };
                if value.is_str() {
                    // must be a sdk package id path:version:channel
                    let value = value.as_str().unwrap();
                    let segments: Vec<&str> = value.splitn(4, ':').collect();
                    let length = segments.len();

                    let mut iter = segments.iter();
                    if length >= 4 {
                        // if length is at 4 then the first is
                        if let Some(repo) = iter.next() {
                            sdk.repo = repo.to_string();
                        } else {
                            bail!(PluginTomlError::new(PluginTomlErrorKind::InvalidSdkKey(
                                key.to_string(),
                                value.to_string()
                            )));
                        }
                    }

                    if let Some(path) = iter.next() {
                        sdk.path = path.to_string();
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::InvalidSdkKey(
                            key.to_string(),
                            value.to_string()
                        )));
                    }
                    // revision
                    if let Some(revision) = iter.next() {
                        sdk.version = revision.parse().context(PluginTomlError::new(
                            PluginTomlErrorKind::InvalidSdkVersionString(key.to_string()),
                        ))?;
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::InvalidSdkKey(
                            key.to_string(),
                            value.to_string()
                        )));
                    }
                    // channel
                    if let Some(channel) = iter.next() {
                        sdk.channel = channel.parse().context(PluginTomlError::new(
                            PluginTomlErrorKind::InvalidChannel(key.to_string()),
                        ))?;
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::InvalidSdkKey(
                            key.to_string(),
                            value.to_string()
                        )));
                    }
                } else if value.is_table_like() {
                    let value = value.as_table_like().unwrap();
                    if let Some(repo) = value.get(REPO).and_then(|p| p.as_str()) {
                        sdk.repo = repo.to_string();
                    }

                    if let Some(path) = value.get(PATH).and_then(|p| p.as_str()) {
                        sdk.path = path.to_string();
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::MissingTableKey(
                            PATH,
                            format!("sdk.{}", key),
                            None
                        )))
                    }

                    if let Some(version) = value.get(VERSION).and_then(|p| p.as_str()) {
                        sdk.version = version.parse().context(PluginTomlError::new(
                            PluginTomlErrorKind::InvalidSdkVersionString(key.to_string()),
                        ))?;
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::MissingTableKey(
                            VERSION,
                            format!("sdk.{}", key),
                            None
                        )))
                    }

                    if let Some(channel) = value.get(CHANNEL).and_then(|p| p.as_str()) {
                        sdk.channel = channel.parse().context(PluginTomlError::new(
                            PluginTomlErrorKind::InvalidChannel(key.to_string()),
                        ))?;
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::MissingTableKey(
                            CHANNEL,
                            format!("sdk.{}", key),
                            None
                        )))
                    }
                }
                sdk_deps.push(sdk);
            }
        }
        let mut repositories: HashMap<String, RepositoryInfo> = HashMap::new();
        if doc.contains_array_of_tables(toml_strings::REPOSITORY) {
            if let Some(repos) = doc[toml_strings::REPOSITORY].as_array_of_tables() {
                for (i, repo_table) in repos.iter().enumerate() {
                    let name = if let Some(name) = repo_table.get(toml_strings::NAME) {
                        name.as_str()
                            .ok_or_else(|| {
                                PluginTomlError::new(PluginTomlErrorKind::ToStringErr(
                                    toml_strings::NAME,
                                    Some(toml_strings::REPOSITORY),
                                    Some(i),
                                ))
                            })?
                            .to_string()
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::MissingTableKey(
                            toml_strings::NAME,
                            toml_strings::REPOSITORY.to_string(),
                            Some(i)
                        )));
                    };
                    let url = if let Some(url) = repo_table.get(toml_strings::URL) {
                        url.as_str()
                            .ok_or_else(|| {
                                PluginTomlError::new(PluginTomlErrorKind::ToStringErr(
                                    toml_strings::URL,
                                    None,
                                    Some(i),
                                ))
                            })?
                            .to_string()
                    } else {
                        bail!(PluginTomlError::new(PluginTomlErrorKind::MissingTableKey(
                            toml_strings::NAME,
                            toml_strings::REPOSITORY.to_string(),
                            Some(i)
                        )));
                    };

                    repositories.insert(
                        name,
                        RepositoryInfo {
                            url,
                            accepted_licenses: HashSet::new(),
                            path: PathBuf::default(),
                        },
                    );
                }
            }
        }

        Ok(Self {
            name,
            version,
            stages: stages_map,
            path: PathBuf::default(),
            package_paths,
            sdk: sdk_deps,
            enable_unsafe,
            labt: labt_version,
            sdk_repo: repositories,
        })
    }
}

fn expand_globs(patterns: Vec<String>) -> anyhow::Result<Vec<PathBuf>> {
    let mut paths: HashSet<PathBuf> = HashSet::new();
    for pattern in patterns {
        let path = PathBuf::from(pattern);
        let path = if path.is_relative() {
            // if is a relative path, append project root instead
            let mut root = get_project_root()
                .context("Failed to get project root directory")?
                .clone();
            root.push(path);
            root
        } else {
            path
        };
        // get the globs expansions and filter unreadable paths
        glob(path.to_str().unwrap_or_default())
            .context("Failed to match glob pattern")?
            .filter_map(Result::ok)
            .for_each(|p| {
                paths.insert(p);
            });
    }

    Ok(paths.iter().map(|p| p.to_owned()).collect())
}
/// tries to check is the provided package paths are relative, and adds
/// the plugin root dir to them to make a valid path
fn load_package_paths(paths: &[PathBuf], plugin_root: &Path) -> Vec<PathBuf> {
    let mut paths: Vec<PathBuf> = paths
        .iter()
        .map(|p| {
            if p.is_relative() {
                let mut new = PathBuf::from(plugin_root);
                new.push(p);
                new
            } else {
                p.to_owned()
            }
        })
        .collect();

    let mut lua_match = PathBuf::from(plugin_root);
    lua_match.push("?.lua");
    paths.push(lua_match);

    let mut lua_init_match = PathBuf::from(plugin_root);
    lua_init_match.push("?/init.lua");
    paths.push(lua_init_match);

    paths
}

#[test]
fn parse_plugin_toml_from_string() {
    let toml = r#"
name="example"
version="0.1.0"
author="omentum"
labt=">=0.3.4"

[sdk]
build = "build-tools:33.0.2:stable"
platform = {path = "platform-tools", version="35.0.2.0",  channel="stable"}

[sdk.cmd]
path = "cmdline-tools;latest"
version = "16.0.0.1"
channel = "stable"

# pre build
[stage.pre]
file="pre.lua"
priority=1
unsafe = true

# android asset packaging tool step.
[stage.aapt]
file="aapt.lua"
priority=1

# java compilation
[stage.compile]
file="compile.lua"
priority=1

# dexing
[stage.dex]
file="dex.lua"
priority=1

# bundling
[stage.bundle]
file="bundle.lua"
priority=1

# post build
[stage.post]
file="post.lua"
priority=1       
"#;

    let plugin: PluginToml = toml.parse().unwrap();
    assert_eq!(plugin.name, String::from("example"));
    assert_eq!(plugin.version, String::from("0.1.0"));
    assert_eq!(plugin.path, PathBuf::default());
    assert_eq!(plugin.labt, Some(VersionRange::Ge(String::from("0.3.4"))));
    assert_eq!(plugin.sdk.len(), 3);

    let mut sdks = plugin.sdk.iter();
    assert_eq!(
        sdks.next(),
        Some(&SdkEntry {
            name: String::from("build"),
            path: String::from("build-tools"),
            version: "33.0.2".parse().unwrap(),
            channel: ChannelType::Stable,
            ..Default::default()
        })
    );
    assert_eq!(
        sdks.next(),
        Some(&SdkEntry {
            name: String::from("platform"),
            path: String::from("platform-tools"),
            version: "35.0.2.0".parse().unwrap(),
            channel: ChannelType::Stable,
            ..Default::default()
        })
    );
    assert_eq!(
        sdks.next(),
        Some(&SdkEntry {
            name: String::from("cmd"),
            path: String::from("cmdline-tools;latest"),
            version: "16.0.0.1".parse().unwrap(),
            channel: ChannelType::Stable,
            ..Default::default()
        })
    );
    assert_eq!(sdks.next(), None);

    assert_eq!(
        plugin.stages.get(&Step::AAPT),
        Some(&PluginStage {
            file: PathBuf::from("aapt.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        })
    );
    assert_eq!(
        plugin.stages.get(&Step::PRE),
        Some(&PluginStage {
            file: PathBuf::from("pre.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: true,
        })
    );
    assert_eq!(
        plugin.stages.get(&Step::COMPILE),
        Some(&PluginStage {
            file: PathBuf::from("compile.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        })
    );
    assert_eq!(
        plugin.stages.get(&Step::DEX),
        Some(&PluginStage {
            file: PathBuf::from("dex.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        })
    );
    assert_eq!(
        plugin.stages.get(&Step::BUNDLE),
        Some(&PluginStage {
            file: PathBuf::from("bundle.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        })
    );
    assert_eq!(
        plugin.stages.get(&Step::POST),
        Some(&PluginStage {
            file: PathBuf::from("post.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        })
    );
}

#[test]
fn plugin_toml_to_string() {
    let mut plugin: PluginToml = PluginToml {
        name: String::from("example"),
        version: String::from("0.1.0"),
        stages: HashMap::new(),
        sdk: Vec::new(),
        path: PathBuf::new(),
        package_paths: None,
        enable_unsafe: false,
        labt: None,
        sdk_repo: HashMap::new(),
    };

    plugin.sdk.push(SdkEntry {
        name: String::from("build"),
        path: String::from("build-tools"),
        version: "33.0.2".parse().unwrap(),
        channel: ChannelType::Stable,
        ..Default::default()
    });
    plugin.sdk.push(SdkEntry {
        name: String::from("platform"),
        path: String::from("platform-tools"),
        version: "35.0.2.0".parse().unwrap(),
        channel: ChannelType::Stable,
        ..Default::default()
    });
    plugin.sdk.push(SdkEntry {
        name: String::from("cmd"),
        path: String::from("cmdline-tools;latest"),
        version: "16.0.0.1".parse().unwrap(),
        channel: ChannelType::Stable,
        ..Default::default()
    });

    plugin.stages.insert(
        Step::AAPT,
        PluginStage {
            file: PathBuf::from("aapt.lua"),
            priority: 1,
            inputs: Some(vec![String::from("**/*.xml")]),
            outputs: Some(vec![String::from("build/res.apk")]),
            enable_unsafe: false,
        },
    );

    plugin.stages.insert(
        Step::PRE,
        PluginStage {
            file: PathBuf::from("pre.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        },
    );

    plugin.stages.insert(
        Step::COMPILE,
        PluginStage {
            file: PathBuf::from("compile.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        },
    );

    plugin.stages.insert(
        Step::DEX,
        PluginStage {
            file: PathBuf::from("dex.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        },
    );

    plugin.stages.insert(
        Step::BUNDLE,
        PluginStage {
            file: PathBuf::from("bundle.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: false,
        },
    );

    plugin.stages.insert(
        Step::POST,
        PluginStage {
            file: PathBuf::from("post.lua"),
            priority: 1,
            inputs: None,
            outputs: None,
            enable_unsafe: true,
        },
    );
    let toml = r#"name = "example"
version = "0.1.0"

[sdk]
build = { path = "build-tools", version = "33.0.2.0", channel = "stable" }
platform = { path = "platform-tools", version = "35.0.2.0", channel = "stable" }
cmd = { path = "cmdline-tools;latest", version = "16.0.0.1", channel = "stable" }

[stage]

[stage.pre]
file = "pre.lua"
priority = 1

[stage.aapt]
file = "aapt.lua"
priority = 1
inputs = ["**/*.xml"]
outputs = ["build/res.apk"]

[stage.compile]
file = "compile.lua"
priority = 1

[stage.dex]
file = "dex.lua"
priority = 1

[stage.bundle]
file = "bundle.lua"
priority = 1

[stage.post]
file = "post.lua"
priority = 1
unsafe = true
"#;
    assert_eq!(toml, plugin.to_string().as_str());
}