easy_install/
install.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
use crate::{artifact::Artifacts, download::download, env::get_install_dir};
use atomic_file_install::atomic_install;
use binstalk_downloader::{
    download::{Download, PkgFmt},
    remote::Client,
};
use binstalk_registry::Registry;
use detect_targets::detect_targets;
use regex::Regex;
use semver::VersionReq;
use std::{num::NonZeroU16, path::Path};
use tempfile::tempdir;
use tracing::trace;

pub async fn install(url: &str) {
    trace!("install {}", url);
    if is_github(url) {
        install_from_github(url).await
    } else if is_url(url) && is_file(url) {
        install_from_url(url).await;
    } else {
        install_from_crate(url).await;
    }
}

async fn install_from_crate(crate_name: &str) {
    trace!("install_from_crate {}", crate_name);
    let client = Client::new(
        concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")),
        None,
        NonZeroU16::new(10).unwrap(),
        1.try_into().unwrap(),
        [],
    )
    .unwrap();
    let version_req = &VersionReq::STAR;
    let sparse_registry: Registry = Registry::crates_io_sparse_registry();
    let manifest_from_sparse = sparse_registry
        .fetch_crate_matched(client, crate_name, version_req)
        .await
        .unwrap();
    if let Some(pkg) = manifest_from_sparse.package {
        if let Some(repo) = pkg.repository() {
            if is_github(repo) {
                install_from_github(repo).await;
            }
        }
    }
}

async fn install_from_url(url: &str) {
    trace!("install_from_url {}", url);
    let fmt = PkgFmt::guess_pkg_format(url).unwrap();
    let files = download(url).await;
    install_from_download_file(fmt, files).await;
}

async fn install_from_download_file(fmt: PkgFmt, download: Download<'static>) {
    trace!("install_from_download_file");
    let out_dir = tempdir().unwrap();
    let files = download.and_extract(fmt, &out_dir).await.unwrap();
    let dir = files.get_dir(Path::new(".")).unwrap();
    let install_dir = get_install_dir();
    let src_dir = out_dir.path().to_path_buf();

    let mut v = vec![];
    for i in dir {
        let mut dst = install_dir.clone();
        let mut src = src_dir.clone();
        let s = i.to_str().unwrap();

        src.push(s);
        dst.push(s);
        atomic_install(src.as_path(), dst.as_path()).unwrap();

        v.push(format!("{} -> {}", s, dst.to_str().unwrap()));
    }
    println!("Installation Successful");
    println!("{}", v.join("\n"));
}

async fn get_artifact_api(url: &str) -> Option<String> {
    trace!("get_artifact_api {}", url);
    let re_gh_tag = Regex::new(
        r"https?://github\.com/(?P<owner>[^/]+)/(?P<repo>[^/]+)/releases/tag/(?P<tag>[^/]+)",
    )
    .unwrap();

    let re_gh_releases =
        Regex::new(r"http?s://github\.com/(?P<owner>[^/]+)/(?P<repo>[^/]+)").unwrap();

    if let Some(captures) = re_gh_tag.captures(url) {
        if let (Some(owner), Some(repo), Some(tag)) = (
            captures.name("owner"),
            captures.name("repo"),
            captures.name("tag"),
        ) {
            return Some(format!(
                "https://api.github.com/repos/{}/{}/releases/tags/{}",
                owner.as_str(),
                repo.as_str(),
                tag.as_str()
            ));
        }
    }

    if let Some(captures) = re_gh_releases.captures(url) {
        if let (Some(owner), Some(repo)) = (captures.name("owner"), captures.name("repo")) {
            return Some(format!(
                "https://api.github.com/repos/{}/{}/releases/latest",
                owner.as_str(),
                repo.as_str(),
            ));
        }
    }
    None
}

pub async fn get_artifact_url(url: &str) -> Option<String> {
    trace!("get_artifact_url {}", url);
    let api = get_artifact_api(url).await.unwrap();
    trace!("get_artifact_url api {}", api);

    let client = reqwest::Client::new();
    let response = client
        .get(&api)
        .header("User-Agent", "reqwest")
        .send()
        .await
        .unwrap();

    let artifacts: Artifacts = response.json().await.unwrap();

    let targets = detect_targets().await;

    for i in artifacts.assets {
        for pat in &targets {
            if i.name.contains(pat) {
                return Some(i.browser_download_url);
            }
        }
    }

    None
}

async fn install_from_github(url: &str) {
    trace!("install_from_git {}", url);
    let artifact_url = get_artifact_url(url).await.unwrap();
    install_from_url(&artifact_url).await;
}

fn is_file(s: &str) -> bool {
    use PkgFmt::*;
    let is_windows = cfg!(target_os = "windows");

    for i in [Tar, Tbz2, Tgz, Txz, Tzstd, Zip, Bin] {
        for ext in i.extensions(is_windows) {
            if !ext.is_empty() && s.ends_with(ext) {
                return true;
            }
        }
    }

    false
}

fn is_url(s: &str) -> bool {
    s.starts_with("http://") || s.starts_with("https://")
}

fn is_github(s: &str) -> bool {
    s.starts_with("http://github.com/") || s.starts_with("https://github.com/")
}

#[cfg(test)]
mod test {
    use std::path::Path;

    use binstalk_downloader::download::PkgFmt;
    use tempfile::tempdir;

    use crate::{
        download::download,
        env::IS_WINDOWS,
        install::{is_file, is_github, is_url},
    };

    use super::{get_artifact_api, get_artifact_url};

    #[test]
    fn test_is_file() {
        assert!(!is_file("https://github.com/ahaoboy/ansi2"));

        assert!(!is_file(
            "https://api.github.com/repos/ahaoboy/ansi2/releases/latest"
        ));
        assert!(!is_file(
            "https://github.com/ahaoboy/ansi2/releases/tag/v0.2.11"
        ));
        assert!(is_file("https://github.com/ahaoboy/ansi2/releases/download/v0.2.11/ansi2-x86_64-unknown-linux-musl.tar.gz"));
        assert!(is_file("https://github.com/ahaoboy/ansi2/releases/download/v0.2.11/ansi2-x86_64-pc-windows-msvc.zip"));
    }

    #[test]
    fn test_is_github() {
        assert!(is_github("https://github.com/ahaoboy/ansi2"));

        assert!(!is_github(
            "https://api.github.com/repos/ahaoboy/ansi2/releases/latest"
        ));
        assert!(is_github(
            "https://github.com/ahaoboy/ansi2/releases/tag/v0.2.11"
        ));
        assert!(is_github("https://github.com/ahaoboy/ansi2/releases/download/v0.2.11/ansi2-x86_64-unknown-linux-musl.tar.gz"));
        assert!(is_github("https://github.com/ahaoboy/ansi2/releases/download/v0.2.11/ansi2-x86_64-pc-windows-msvc.zip"));
    }

    #[test]
    fn test_is_url() {
        assert!(is_url("https://github.com/ahaoboy/ansi2"));
        assert!(!is_url("ansi2"));
    }

    #[tokio::test]
    async fn test_get_artifact_url() {
        let url = get_artifact_url("https://github.com/ahaoboy/ansi2/releases/tag/v0.2.11")
            .await
            .unwrap();
        let fmt = PkgFmt::guess_pkg_format(&url).unwrap();
        let files = download(&url).await;
        let out_dir = tempdir().unwrap();
        let files = files.and_extract(fmt, out_dir.path()).await.unwrap();
        assert!(files.has_file(Path::new(if IS_WINDOWS { "ansi2.exe" } else { "ansi2" })));
    }

    #[tokio::test]
    async fn test_get_artifact_api() {
        let url = get_artifact_api("https://github.com/ahaoboy/ansi2/releases/tag/v0.2.11")
            .await
            .unwrap();
        assert_eq!(
            url,
            "https://api.github.com/repos/ahaoboy/ansi2/releases/tags/v0.2.11"
        )
    }
}