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
use std::path::PathBuf;
pub async fn obtain_ferron() -> std::io::Result<PathBuf> {
let ferron_dl_dir = dirs::data_local_dir()
.ok_or(std::io::Error::new(
std::io::ErrorKind::NotFound,
"data local directory not found",
))?
.join(".ferron-language-server-dl");
if tokio::fs::try_exists(&ferron_dl_dir).await.unwrap_or(false)
|| tokio::fs::try_exists(ferron_dl_dir.join("ferron.exe"))
.await
.unwrap_or(false)
{
// Don't download again if already present
return Ok(ferron_dl_dir);
}
std::fs::create_dir_all(&ferron_dl_dir)?;
// 1. Obtain the latest version info for Ferron 3 from https://dl.ferron.sh/latest3.ferron
let latest_ferron_version = reqwest::get("https://dl.ferron.sh/latest3.ferron")
.await
.map_err(std::io::Error::other)?
.error_for_status()
.map_err(std::io::Error::other)?
.text()
.await
.map_err(std::io::Error::other)?;
let latest_ferron_version = latest_ferron_version.trim();
// 2. Obtain the latest version of Ferron 3 from:
// - Windows: https://dl.ferron.sh/<version>/ferron-<version>-<targettriple>.zip
// - Others: https://dl.ferron.sh/<version>/ferron-<version>-<targettriple>.tar.gz
let ferron_url = if cfg!(windows) {
format!(
"https://dl.ferron.sh/{}/ferron-{}-{}.zip",
latest_ferron_version,
latest_ferron_version,
crate::build::BUILD_TARGET
)
} else {
format!(
"https://dl.ferron.sh/{}/ferron-{}-{}.tar.gz",
latest_ferron_version,
latest_ferron_version,
crate::build::BUILD_TARGET
)
};
let ferron_archive = reqwest::get(&ferron_url)
.await
.map_err(std::io::Error::other)?
.error_for_status()
.map_err(std::io::Error::other)?
.bytes()
.await
.map_err(std::io::Error::other)?;
// 3. Extract the archive to the download directory
#[cfg(not(windows))]
{
let mut gunzipped = async_compression::tokio::bufread::GzipDecoder::new(
std::io::Cursor::new(ferron_archive),
);
tokio_tar::Archive::new(&mut gunzipped)
.unpack(&ferron_dl_dir)
.await?;
}
#[cfg(windows)]
{
use tokio_util::compat::TokioAsyncWriteCompatExt;
let mut reader = async_zip::tokio::read::seek::ZipFileReader::with_tokio(
std::io::Cursor::new(ferron_archive),
)
.await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
// Taken from https://github.com/Majored/rs-async-zip/blob/main/examples/file_extraction.rs
for index in 0..reader.file().entries().len() {
let Some(entry) = reader.file().entries().get(index) else {
continue;
};
let path = ferron_dl_dir.join(
entry
.filename()
.as_str()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?
.replace('\\', "/")
.split('/')
.map(sanitize_filename::sanitize)
.collect::<PathBuf>(),
);
// If the filename of the entry ends with '/', it is treated as a directory.
// This is implemented by previous versions of this crate and the Python Standard Library.
// https://docs.rs/async_zip/0.0.8/src/async_zip/read/mod.rs.html#63-65
// https://github.com/python/cpython/blob/820ef62833bd2d84a141adedd9a05998595d6b6d/Lib/zipfile.py#L528
let entry_is_dir = entry
.dir()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
let mut entry_reader = reader
.reader_without_entry(index)
.await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
if entry_is_dir {
// The directory may have been created if iteration is out of order.
if !path.exists() {
tokio::fs::create_dir_all(&path).await.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to create extracted directory: {}", e),
)
})?;
}
} else {
// Creates parent directories. They may not exist if iteration is out of order
// or the archive does not contain directory entries.
let parent = path.parent().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"A file entry should have parent directories: {}",
path.display()
),
)
})?;
if !parent.is_dir() {
tokio::fs::create_dir_all(parent).await.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to create parent directories: {}", e),
)
})?;
}
let writer = tokio::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
.await
.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to create extracted file: {}", e),
)
})?;
futures_util::io::copy(&mut entry_reader, &mut writer.compat_write())
.await
.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to copy to extracted file: {}", e),
)
})?;
// Closes the file and manipulates its metadata here if you wish to preserve its metadata from the archive.
}
}
}
Ok(ferron_dl_dir)
}