dotnet_parser/
outdated.rs

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
//! This parses the output of dotnet-outdated
use std::process::Command;
use std::str::from_utf8;
use tracing::{debug, trace, warn};

/// should upgrades be locked to a specific major/minor/patch level only
#[derive(Debug, Clone, Default, clap::ValueEnum)]
pub enum VersionLock {
    /// do not lock the version when considering upgrades
    #[default]
    None,
    /// lock the version to the current major version (i.e. only consider minor versions and patch levels)
    Major,
    /// lock the version to the current minor version (i.e. only consider patch levels)
    Minor,
}

impl std::fmt::Display for VersionLock {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VersionLock::None => {
                write!(f, "None")
            }
            VersionLock::Major => {
                write!(f, "Major")
            }
            VersionLock::Minor => {
                write!(f, "Minor")
            }
        }
    }
}

/// Should dotnet-outdated look for pre-release versions of packages?
#[derive(Debug, Clone, Default, clap::ValueEnum)]
pub enum PreRelease {
    /// Never look for pre-releases
    Never,
    /// automatically let dotnet-outdated determine if pre-releases are appropriate to look for
    #[default]
    Auto,
    /// Always look for pre-releases
    Always,
}

impl std::fmt::Display for PreRelease {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PreRelease::Never => {
                write!(f, "Never")
            }
            PreRelease::Auto => {
                write!(f, "Auto")
            }
            PreRelease::Always => {
                write!(f, "Always")
            }
        }
    }
}

/// These are options to modify the behaviour of the program.
#[derive(Debug, Default, clap::Parser)]
pub struct DotnetOutdatedOptions {
    /// Include auto referenced packages
    #[clap(
        short = 'i',
        long = "include-auto-references",
        help = "Include auto-referenced packages"
    )]
    include_auto_references: bool,
    /// Should dotnet-outdated look for pre-release version of packages
    #[clap(
        long = "pre-release",
        value_name = "VALUE",
        default_value = "auto",
        help = "Should dotnet-outdated look for pre-release versions of packages",
        value_enum
    )]
    pre_release: PreRelease,
    /// Dependencies that should be included in the consideration
    #[clap(
        long = "include",
        value_name = "PACKAGE_NAME",
        number_of_values = 1,
        help = "Dependencies that should be included in the consideration"
    )]
    include: Vec<String>,
    /// Dependencies that should be excluded from consideration
    #[clap(
        long = "exclude",
        value_name = "PACKAGE_NAME",
        number_of_values = 1,
        help = "Dependencies that should be excluded from consideration"
    )]
    exclude: Vec<String>,
    /// should transitive dependencies be considered
    #[clap(
        short = 't',
        long = "transitive",
        help = "Should dotnet-outdated consider transitiv dependencies"
    )]
    transitive: bool,
    /// if transitive dependencies are considered, to which depth
    #[clap(
        long = "transitive-depth",
        value_name = "DEPTH",
        default_value = "1",
        requires = "transitive",
        help = "If transitive dependencies are considered, to which depth in the dependency tree"
    )]
    transitive_depth: u64,
    /// should we consider all upgrades or limit to minor and/or patch levels only
    #[clap(
        long = "version-lock",
        value_name = "LOCK",
        default_value = "none",
        help = "Should we consider all updates or just minor versions and/or patch levels",
        value_enum
    )]
    version_lock: VersionLock,
    /// path to pass to dotnet-outdated, defaults to current directory
    #[clap(
        long = "input-dir",
        value_name = "DIRECTORY",
        help = "The input directory to pass to dotnet outdated"
    )]
    input_dir: Option<std::path::PathBuf>,
}

/// Outer structure for parsing donet-outdated output
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DotnetOutdatedData {
    /// one per .csproj file (e.g. binaries, tests,...)
    pub projects: Vec<Project>,
}

/// Per project data
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Project {
    /// Name of the project
    pub name: String,
    /// absolute path to the .csproj file for it
    pub file_path: String,
    /// frameworks this targets with dependencies
    pub target_frameworks: Vec<Framework>,
}

/// Per project per target framework data
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Framework {
    /// Name of the framework, e.g. net5.0
    pub name: String,
    /// dependencies of the project when targeted for this framework
    pub dependencies: Vec<Dependency>,
}

/// Data about each outdated dependency
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Dependency {
    /// Name of the dependency
    pub name: String,
    /// the version that is currently in use
    pub resolved_version: String,
    /// the latest version as limited by the version lock parameter
    pub latest_version: String,
    /// severity of this upgrade
    pub upgrade_severity: Severity,
}

/// Severity of a required upgrade
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum Severity {
    /// a major version upgrade
    Major,
    /// a minor version uprade
    Minor,
    /// a patch level upgrade
    Patch,
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Severity::Major => {
                write!(f, "Major")
            }
            Severity::Minor => {
                write!(f, "Minor")
            }
            Severity::Patch => {
                write!(f, "Patch")
            }
        }
    }
}

/// What the exit code indicated about required updates
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum IndicatedUpdateRequirement {
    /// No update is required
    UpToDate,
    /// An update is required
    UpdateRequired,
}

impl std::fmt::Display for IndicatedUpdateRequirement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IndicatedUpdateRequirement::UpToDate => {
                write!(f, "up-to-date")
            }
            IndicatedUpdateRequirement::UpdateRequired => {
                write!(f, "update-required")
            }
        }
    }
}

/// main entry point for the dotnet-oudated call
pub fn outdated(
    options: &DotnetOutdatedOptions,
) -> Result<(IndicatedUpdateRequirement, DotnetOutdatedData), crate::Error> {
    let output_dir = tempfile::tempdir()?;
    let output_file = output_dir.path().join("outdated.json");
    let output_file = output_file
        .to_str()
        .ok_or(crate::Error::PathConversionError)?;

    let mut cmd = Command::new("dotnet");

    cmd.args([
        "outdated",
        "--fail-on-updates",
        "--output",
        output_file,
        "--output-format",
        "json",
    ]);

    if options.include_auto_references {
        cmd.args(["--include-auto-references"]);
    }

    cmd.args(["--pre-release", &options.pre_release.to_string()]);

    if !options.include.is_empty() {
        for i in &options.include {
            cmd.args(["--include", i]);
        }
    }

    if !options.exclude.is_empty() {
        for e in &options.exclude {
            cmd.args(["--exclude", e]);
        }
    }

    if options.transitive {
        cmd.args([
            "--transitive",
            "--transitive-depth",
            &options.transitive_depth.to_string(),
        ]);
    }

    cmd.args(["--version-lock", &options.version_lock.to_string()]);

    if let Some(ref input_dir) = options.input_dir {
        cmd.args([&input_dir]);
    }

    let output = cmd.output()?;

    if !output.status.success() {
        warn!(
            "dotnet outdated did not return with a successful exit code: {}",
            output.status
        );
        debug!("stdout:\n{}", from_utf8(&output.stdout)?);
        if !output.stderr.is_empty() {
            warn!("stderr:\n{}", from_utf8(&output.stderr)?);
        }
    }

    let update_requirement = if output.status.success() {
        IndicatedUpdateRequirement::UpToDate
    } else {
        IndicatedUpdateRequirement::UpdateRequired
    };

    let output_file_content = std::fs::read_to_string(output_file)?;

    trace!("Read output file content:\n{}", output_file_content);

    let jd = &mut serde_json::Deserializer::from_str(&output_file_content);
    let data: DotnetOutdatedData = serde_path_to_error::deserialize(jd)?;
    Ok((update_requirement, data))
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::Error;

    /// this test requires a .sln and/or .csproj files in the current
    /// directory (working dir of the tests)
    #[test]
    fn test_run_dotnet_outdated() -> Result<(), Error> {
        outdated(&Default::default())?;
        Ok(())
    }
}