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
use once_cell::sync::Lazy;
use semver::Version;
use sha2::Digest;
use std::{
ffi::OsString,
fs,
io::{Cursor, Write},
path::PathBuf,
};
#[cfg(target_family = "unix")]
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
mod error;
pub use error::SolcVmError;
mod platform;
pub use platform::platform;
mod releases;
pub use releases::{all_releases, Releases};
pub static SVM_HOME: Lazy<PathBuf> = Lazy::new(|| {
cfg_if::cfg_if! {
if #[cfg(test)] {
let dir = tempfile::tempdir().expect("could not create temp directory");
dir.path().join(".svm")
} else {
let mut user_home = home::home_dir().expect("could not detect user home directory");
user_home.push(".svm");
user_home
}
}
});
pub fn version_path(version: &str) -> PathBuf {
let mut version_path = SVM_HOME.to_path_buf();
version_path.push(&version);
version_path
}
pub fn global_version_path() -> PathBuf {
let mut global_version_path = SVM_HOME.to_path_buf();
global_version_path.push(".global-version");
global_version_path
}
pub fn current_version() -> Result<Option<Version>, SolcVmError> {
let v = fs::read_to_string(global_version_path().as_path())?;
Ok(Version::parse(v.trim_end_matches('\n').to_string().as_str()).ok())
}
pub fn use_version(version: &Version) -> Result<(), SolcVmError> {
let mut v = fs::File::create(global_version_path().as_path())?;
v.write_all(version.to_string().as_bytes())?;
Ok(())
}
pub fn unset_global_version() -> Result<(), SolcVmError> {
let mut v = fs::File::create(global_version_path().as_path())?;
v.write_all("".as_bytes())?;
Ok(())
}
pub fn installed_versions() -> Result<Vec<Version>, SolcVmError> {
let home_dir = SVM_HOME.to_path_buf();
let mut versions = vec![];
for v in fs::read_dir(&home_dir)? {
let v = v?;
if v.file_name() != OsString::from(".global-version".to_string()) {
versions.push(Version::parse(
v.path()
.file_name()
.ok_or(SolcVmError::UnknownVersion)?
.to_str()
.ok_or(SolcVmError::UnknownVersion)?
.to_string()
.as_str(),
)?);
}
}
versions.sort();
Ok(versions)
}
pub async fn all_versions() -> Result<Vec<Version>, SolcVmError> {
let mut releases = releases::all_releases(platform::platform())
.await?
.releases
.keys()
.cloned()
.collect::<Vec<Version>>();
releases.sort();
Ok(releases)
}
pub async fn install(version: &Version) -> Result<(), SolcVmError> {
setup_home()?;
let artifacts = releases::all_releases(platform::platform()).await?;
let artifact = artifacts
.releases
.get(version)
.ok_or(SolcVmError::UnknownVersion)?;
let download_url =
releases::artifact_url(platform::platform(), version, artifact.to_string().as_str())?;
let res = reqwest::get(download_url).await?;
let binbytes = res.bytes().await?;
let mut hasher = sha2::Sha256::new();
hasher.update(&binbytes);
let cs = &hasher.finalize()[..];
let checksum = artifacts
.get_checksum(version)
.unwrap_or_else(|| panic!("checksum not available: {:?}", version.to_string()));
if cs != checksum {
return Err(SolcVmError::ChecksumMismatch(version.to_string()));
}
let mut dest = {
setup_version(version.to_string().as_str())?;
let fname = version_path(version.to_string().as_str()).join(&format!("solc-{}", version));
let f = fs::File::create(fname)?;
#[cfg(target_family = "unix")]
f.set_permissions(Permissions::from_mode(0o777))?;
f
};
let mut content = Cursor::new(binbytes);
std::io::copy(&mut content, &mut dest)?;
Ok(())
}
pub fn remove_version(version: &Version) -> Result<(), SolcVmError> {
fs::remove_dir_all(version_path(version.to_string().as_str()))?;
Ok(())
}
pub fn setup_home() -> Result<PathBuf, SolcVmError> {
let home_dir = SVM_HOME.to_path_buf();
if !home_dir.as_path().exists() {
fs::create_dir_all(home_dir.clone())?;
}
let mut global_version = SVM_HOME.to_path_buf();
global_version.push(".global-version");
if !global_version.as_path().exists() {
fs::File::create(global_version.as_path())?;
}
Ok(home_dir)
}
fn setup_version(version: &str) -> Result<(), SolcVmError> {
let v = version_path(version);
if !v.exists() {
fs::create_dir_all(v.as_path())?
}
Ok(())
}
#[cfg(test)]
mod tests {
use crate::{
platform::Platform,
releases::{all_releases, artifact_url},
};
use rand::seq::SliceRandom;
use reqwest::Url;
use std::process::Command;
use std::process::Stdio;
use super::*;
#[tokio::test]
async fn test_artifact_url() {
let version = Version::new(0, 5, 0);
let artifact = "solc-v0.5.0";
assert_eq!(
artifact_url(Platform::LinuxAarch64, &version, artifact).unwrap(),
Url::parse(&format!(
"https://github.com/nikitastupin/solc/raw/main/linux/aarch64/{}",
artifact
))
.unwrap(),
)
}
#[tokio::test]
async fn test_install() {
let versions = all_releases(platform::platform())
.await
.unwrap()
.releases
.into_keys()
.collect::<Vec<Version>>();
let rand_version = versions.choose(&mut rand::thread_rng()).unwrap();
assert!(install(rand_version).await.is_ok());
}
#[tokio::test]
async fn test_version() {
let version = "0.8.10".parse().unwrap();
install(&version).await.unwrap();
let solc_path =
version_path(version.to_string().as_str()).join(&format!("solc-{}", version));
let output = Command::new(&solc_path)
.arg("--version")
.stdin(Stdio::piped())
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.output()
.unwrap();
assert!(String::from_utf8_lossy(&output.stdout)
.as_ref()
.contains("0.8.10"));
}
}