-
Notifications
You must be signed in to change notification settings - Fork 11
/
build.rs
108 lines (88 loc) 路 3.83 KB
/
build.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
use std::env;
use std::fs::{create_dir_all, File};
use std::io;
use std::io::Write;
use std::path::{Path, PathBuf};
use const_format::formatcp;
use sha2::{Digest, Sha256};
use zip::ZipArchive;
const DOCKER_VERSION: &str = "26.1.3";
const DOCKER_URL: &str =
formatcp!("https://download.docker.com/win/static/stable/x86_64/docker-{DOCKER_VERSION}.zip");
const DOCKER_SHA: &str = "bba77ac3c959387b0047dac25a4268d3312443e208228d4c1b5e5f4715ae152f";
const DOCKER_BUILDX_VERSION: &str = "0.14.1";
const DOCKER_BUILDX_URL: &str =
formatcp!("https://github.com/docker/buildx/releases/download/v{DOCKER_BUILDX_VERSION}/buildx-v{DOCKER_BUILDX_VERSION}.windows-amd64.exe");
const DOCKER_BUILDX_SHA: &str = "21830c62d2a43ef2568ad325c338e892f7d534e656795e1fa49f68a679ac5928";
const DOCKER_COMPOSE_VERSION: &str = "2.27.1";
const DOCKER_COMPOSE_URL: &str = formatcp!("https://github.com/docker/compose/releases/download/v{DOCKER_COMPOSE_VERSION}/docker-compose-windows-x86_64.exe");
const DOCKER_COMPOSE_SHA: &str = "354e903701dbd3e7ee3c4259de928367776864bb850efe677d129202638843db";
const WINCRED_VERSION: &str = "0.8.2";
const WINCRED_URL: &str = formatcp!("https://github.com/docker/docker-credential-helpers/releases/download/v{WINCRED_VERSION}/docker-credential-wincred-v{WINCRED_VERSION}.windows-amd64.exe");
const WINCRED_SHA: &str = "57d3ea7a97e73abd913f71b0ba4f497f729c640b022108716207b4bd47a9d658";
fn get_dest_dir() -> PathBuf {
//<root or manifest path>/target/<profile>/
let manifest_dir_string = env::var("CARGO_MANIFEST_DIR").unwrap();
let build_type = env::var("PROFILE").unwrap();
PathBuf::from(manifest_dir_string)
.join("target")
.join(build_type)
}
fn unzip(file: &Path, dest_dir: &Path) {
let compressed_data = File::open(file).unwrap();
let mut zip_archive = ZipArchive::new(compressed_data).unwrap();
for i in 0..zip_archive.len() {
let mut file = zip_archive.by_index(i).unwrap();
if file.is_dir() {
continue;
}
let path = dest_dir.join(file.enclosed_name().unwrap());
if let Some(p) = path.parent() {
create_dir_all(p).unwrap();
}
let mut outfile = File::create(&path).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}
}
fn build_docker(dest_dir: &Path) {
let compressed_path = dest_dir.join("docker.zip");
download_file(DOCKER_URL, DOCKER_SHA, &compressed_path);
unzip(&compressed_path, dest_dir);
}
fn build_wincred(dest_dir: &Path) {
let dest_path = dest_dir.join("docker-credential-wincred.exe");
download_file(WINCRED_URL, WINCRED_SHA, &dest_path);
}
fn download_file(uri: &str, expected_sha: &str, dest: &Path) {
if let Ok(mut file) = File::open(dest) {
let mut digest = Sha256::new();
io::copy(&mut file, &mut digest).unwrap();
let actual_sha = digest.finalize();
if expected_sha == format!("{actual_sha:x}") {
return;
}
}
let data = reqwest::blocking::get(uri).unwrap().bytes().unwrap();
let actual_sha = Sha256::digest(&data);
if format!("{actual_sha:x}") != expected_sha {
panic!("Checksum mismatch for {uri}: expected {expected_sha} but got {actual_sha:x}");
}
let mut outfile = File::create(dest).unwrap();
outfile.write_all(&data).unwrap();
}
fn build_docker_buildx(dest_dir: &Path) {
let dest_path = dest_dir.join("docker-buildx.exe");
download_file(DOCKER_BUILDX_URL, DOCKER_BUILDX_SHA, &dest_path);
}
fn build_docker_compose(dest_dir: &Path) {
let dest_path = dest_dir.join("docker-compose.exe");
download_file(DOCKER_COMPOSE_URL, DOCKER_COMPOSE_SHA, &dest_path);
}
fn main() {
let dest_dir = get_dest_dir();
build_docker(&dest_dir);
build_docker_buildx(&dest_dir);
build_docker_compose(&dest_dir);
build_wincred(&dest_dir);
println!("cargo:rerun-if-changed=build.rs");
}