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
use std::collections::{BTreeSet, HashMap, HashSet};

use itertools::Itertools;
use serde::{Deserialize, Serialize};

use crate::{ast::Sol, msbuild};

/// Represents Visual Studio solution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Solution<'a> {
    /// Full path to solution file
    pub path: &'a str,
    /// Solution format
    pub format: &'a str,
    /// Solution product like Visual Studion 15 etc
    pub product: &'a str,
    /// Solution versions got from lines starts from # char at the beginning of solution file
    pub versions: Vec<Version<'a>>,
    /// Solution's projects
    pub projects: Vec<Project<'a>>,
    /// All solution's configuraion/platform pairs
    pub configurations: BTreeSet<SolutionConfiguration<'a>>,
    /// Dangling (projects with such ids not exist in the solution file) projects configurations inside solution
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dangling_project_configurations: Option<Vec<String>>,
}

/// Represnts [`Solution`] version. NOTE: [`Solution`] may have several versions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Version<'a> {
    pub name: &'a str,
    pub version: &'a str,
}

/// Represent project inside [`Solution`]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Project<'a> {
    pub type_id: &'a str,
    pub type_description: &'a str,
    pub id: &'a str,
    pub name: &'a str,
    pub path_or_uri: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub configurations: Option<BTreeSet<ProjectConfiguration<'a>>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<Vec<&'a str>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub depends_from: Option<Vec<&'a str>>,
}

/// Represents solution configuration/platform pair
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SolutionConfiguration<'a> {
    /// Solution's configuration name
    pub configuration: &'a str,
    /// Platform i.e. Any CPU, Win32, x86 etc.
    pub platform: &'a str,
}

/// Represents project configuration/platform pair
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ProjectConfiguration<'a> {
    /// Project configuration
    pub configuration: &'a str,
    /// Solution's configuration this project config belongs to
    pub solution_configuration: &'a str,
    /// Platform i.e. Asny CPU, Win32, x86 etc.
    pub platform: &'a str,
    /// Configuration tag
    pub tags: Vec<Tag>,
}

/// Represents project configuration tag
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Tag {
    /// Defines project configuration buildable
    #[default]
    Build,
    /// Defines project configuration deployable
    Deploy,
}

impl<'a> Solution<'a> {
    /// Creates new [`Solution`] instance from [`ast::Sol`] instance
    #[must_use]
    pub fn from(solution: &Sol<'a>) -> Self {
        Self {
            path: solution.path,
            format: solution.format,
            product: solution.product,
            versions: Self::versions(solution),
            projects: Self::projects(solution),
            configurations: Self::configurations(solution),
            dangling_project_configurations: Self::dangings(solution),
        }
    }

    /// Iterates all but solution folder projects inside [`Solution`]
    pub fn iterate_projects(&'a self) -> impl Iterator<Item = &'a Project<'a>> {
        self.projects
            .iter()
            .filter(|p| !msbuild::is_solution_folder(p.type_id))
    }

    /// Iterates all but solution folder and web site projects
    pub fn iterate_projects_without_web_sites(&'a self) -> impl Iterator<Item = &'a Project<'a>> {
        self.iterate_projects()
            .filter(|p| !msbuild::is_web_site_project(p.type_id))
    }

    fn versions(solution: &Sol<'a>) -> Vec<Version<'a>> {
        solution
            .versions
            .iter()
            .map(|v| Version {
                name: v.name,
                version: v.ver,
            })
            .collect()
    }

    fn configurations(solution: &Sol<'a>) -> BTreeSet<SolutionConfiguration<'a>> {
        solution
            .solution_configs
            .iter()
            .map(|c| SolutionConfiguration {
                configuration: c.config,
                platform: c.platform,
            })
            .collect()
    }

    fn projects(solution: &Sol<'a>) -> Vec<Project<'a>> {
        let project_configs = solution
            .project_configs
            .iter()
            .map(|c| {
                (
                    c.project_id,
                    c.configs
                        .iter()
                        .into_grouping_map_by(|pc| {
                            (pc.project_config, pc.solution_config, pc.platform)
                        })
                        .fold(
                            ProjectConfiguration::default(),
                            |mut pc, (p, s, plat), val| {
                                pc.configuration = p;
                                pc.solution_configuration = s;
                                pc.platform = plat;
                                match val.tag {
                                    crate::ast::ProjectConfigTag::ActiveCfg => {}
                                    crate::ast::ProjectConfigTag::Build => pc.tags.push(Tag::Build),
                                    crate::ast::ProjectConfigTag::Deploy => {
                                        pc.tags.push(Tag::Deploy);
                                    }
                                };
                                pc
                            },
                        )
                        .into_values()
                        .collect(),
                )
            })
            .collect::<HashMap<&str, BTreeSet<ProjectConfiguration>>>();
        solution
            .projects
            .iter()
            .map(|p| {
                let items = if p.items.is_empty() {
                    None
                } else {
                    Some(p.items.clone())
                };
                let depends_from = if p.depends_from.is_empty() {
                    None
                } else {
                    Some(p.depends_from.clone())
                };
                Project {
                    type_id: p.type_id,
                    type_description: p.type_descr,
                    id: p.id,
                    name: p.name,
                    path_or_uri: p.path_or_uri,
                    configurations: project_configs.get(p.id).cloned(),
                    items,
                    depends_from,
                }
            })
            .collect()
    }

    fn dangings(solution: &Sol<'a>) -> Option<Vec<String>> {
        let project_ids: HashSet<String> = solution
            .projects
            .iter()
            .filter(|p| !msbuild::is_solution_folder(p.type_id))
            .map(|p| p.id.to_uppercase())
            .collect();

        let dangilings = solution
            .project_configs
            .iter()
            .map(|p| p.project_id.to_uppercase())
            .collect::<HashSet<String>>()
            .difference(&project_ids)
            .cloned()
            .collect_vec();
        if dangilings.is_empty() {
            None
        } else {
            Some(dangilings)
        }
    }
}