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
use anyhow::{ensure, Context as _};
use serde::Deserialize;
use std::{cmp, collections::BTreeMap};

use crate::Ctx;

#[derive(Deserialize, Debug)]
pub struct Payload {
    #[serde(rename = "fileName")]
    pub file_name: String,
    pub sha256: crate::util::Sha256,
    pub size: u64,
    pub url: String,
}

#[derive(Copy, Clone, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "snake_case")]
pub enum Chip {
    X86,
    X64,
    Arm,
    Arm64,
    Neutral,
}

#[derive(Copy, Clone, Deserialize, PartialEq, Debug)]
pub enum ItemKind {
    /// Unused.
    Bootstrapper,
    /// Unused.
    Channel,
    /// Unused.
    ChannelProduct,
    /// A composite package, no contents itself. Unused.
    Component,
    /// A single executable. Unused.
    Exe,
    /// Another kind of composite package without contents, and no localization. Unused.
    Group,
    /// Top level manifest
    Manifest,
    /// MSI installer
    Msi,
    /// Unused.
    Msu,
    /// Nuget package. Unused.
    Nupkg,
    /// Unused
    Product,
    /// A glorified zip file
    Vsix,
    /// Windows feature install/toggle. Unused.
    WindowsFeature,
    /// Unused.
    Workload,
    /// Plain zip file (ie not vsix). Unused.
    Zip,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct InstallSizes {
    pub target_drive: Option<u64>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ManifestItem {
    pub id: String,
    pub version: String,
    #[serde(rename = "type")]
    pub kind: ItemKind,
    pub chip: Option<Chip>,
    #[serde(default)]
    pub payloads: Vec<Payload>,
    #[serde(default)]
    pub dependencies: BTreeMap<String, serde_json::Value>,
    pub install_sizes: Option<InstallSizes>,
}

impl PartialEq for ManifestItem {
    #[inline]
    fn eq(&self, o: &Self) -> bool {
        self.cmp(o) == cmp::Ordering::Equal
    }
}

impl Eq for ManifestItem {}

impl cmp::Ord for ManifestItem {
    #[inline]
    fn cmp(&self, o: &Self) -> cmp::Ordering {
        self.id.cmp(&o.id)
    }
}

impl cmp::PartialOrd for ManifestItem {
    #[inline]
    fn partial_cmp(&self, o: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(o))
    }
}

#[derive(Deserialize)]
pub struct Manifest {
    #[serde(rename = "channelItems")]
    channel_items: Vec<ManifestItem>,
}

/// Retrieves the top-level manifest which contains license links as well as the
/// link to the actual package manifest which describes all of the contents
pub fn get_manifest(
    ctx: &Ctx,
    version: &str,
    channel: &str,
    progress: indicatif::ProgressBar,
) -> Result<Manifest, anyhow::Error> {
    let manifest_bytes = ctx.get_and_validate(
        format!("https://aka.ms/vs/{}/{}/channel", version, channel),
        &format!("manifest_{}.json", version),
        None,
        progress,
    )?;

    let manifest: Manifest = serde_json::from_slice(&manifest_bytes)?;

    Ok(manifest)
}

/// Retrieves the package manifest specified in the input manifest
pub fn get_package_manifest(
    ctx: &Ctx,
    manifest: &Manifest,
    progress: indicatif::ProgressBar,
) -> Result<PackageManifest, anyhow::Error> {
    let pkg_manifest = manifest
        .channel_items
        .iter()
        .find(|ci| ci.kind == ItemKind::Manifest && !ci.payloads.is_empty())
        .context("Unable to locate package manifest")?;

    // This always just a single payload, but ensure it stays that way in the future
    ensure!(
        pkg_manifest.payloads.len() == 1,
        "VS package manifest should have exactly 1 payload"
    );

    // While the payload includes a sha256 checksum for the payload it is actually
    // never correct (even though it is part of the url!) so we have to just download
    // it without checking, which is terrible but...¯\_(ツ)_/¯
    let payload = &pkg_manifest.payloads[0];

    let manifest_bytes = ctx.get_and_validate(
        payload.url.clone(),
        &format!("pkg_manifest_{}.vsman", payload.sha256),
        None,
        progress,
    )?;

    #[derive(Deserialize)]
    struct PkgManifest {
        packages: Vec<ManifestItem>,
    }

    let manifest: PkgManifest =
        serde_json::from_slice(&manifest_bytes).context("unable to parse manifest")?;

    let mut packages = BTreeMap::new();

    for pkg in manifest.packages {
        packages.insert(pkg.id.clone(), pkg);
    }

    Ok(PackageManifest { packages })
}

pub struct PackageManifest {
    pub packages: BTreeMap<String, ManifestItem>,
}