Compare commits

...

3 Commits

Author SHA1 Message Date
Michael Pfaff bc44b249f4
Fix step logging 2022-06-10 13:30:48 -04:00
Michael Pfaff ceeaed0b8d
Don't download file if it already exists. 2022-06-10 13:23:07 -04:00
Michael Pfaff 12991439fa
Install Psiphon3 VPN 2022-06-10 13:16:30 -04:00
2 changed files with 103 additions and 60 deletions

View File

@ -72,76 +72,89 @@ impl<'a> Step<'a> {
pub async fn invoke(&self, ctx: &Context) -> Result<()> {
match self {
Self::DownloadFile { res, file } => {
println!(
"Downloading file {file} from {res:?}...",
file = file.to_str().unwrap_or("<NON UTF-8>")
);
const FETCH_FILE_ERROR_MSG: &'static str = "Fetching the remote resource failed.";
const WRITE_FILE_ERROR_MSG: &'static str =
"Writing the remote resource to disk failed.";
let url = match res {
RemoteResource::Url(url) => url::Url::parse(url)
.into_diagnostic()
.wrap_err("Invalid url for download step.")?,
RemoteResource::GitHubArtifact { repo, pattern } => {
let mut release = fetch_latest_release(&ctx.reqwest, repo).await?;
let pattern = ramhorns::Template::new(*pattern)
if file.exists() {
println!(
"File {file} already downloaded.",
file = file.to_str().unwrap_or("<NON UTF-8>")
);
} else {
println!(
"Downloading file {file} from {res:?}...",
file = file.to_str().unwrap_or("<NON UTF-8>")
);
const FETCH_FILE_ERROR_MSG: &'static str =
"Fetching the remote resource failed.";
const WRITE_FILE_ERROR_MSG: &'static str =
"Writing the remote resource to disk failed.";
let url = match res {
RemoteResource::Url(url) => url::Url::parse(url)
.into_diagnostic()
.wrap_err("Invalid pattern for artifact matching in download step.")?;
release.meta.tag_name_strip_prefix = release
.meta
.tag_name
.strip_prefix('v')
.unwrap_or(&release.meta.tag_name);
let asset_name = pattern.render(&release.meta);
let artifact = release.assets.into_iter().filter(move |asset| asset.name == asset_name).next().ok_or_else(|| miette!("No artifact of the latest release matched the pattern in download step."))?;
url::Url::parse(&artifact.browser_download_url)
.into_diagnostic()
.wrap_err(
"Invalid url returned by GitHub for latest release artifact.",
)?
}
};
let mut resp = ctx
.reqwest
.get(url)
.header("User-Agent", USER_AGENT_STR)
.send()
.await
.into_diagnostic()
.wrap_err(FETCH_FILE_ERROR_MSG)?;
let _content_length = resp.content_length();
mkdir_all(
file.parent().ok_or_else(|| {
miette!("Destination file for download step has no parent.")
})?,
)
.await?;
let mut writer = tokio::io::BufWriter::new(
tokio::fs::File::create(file.as_os_str())
.wrap_err("Invalid url for download step.")?,
RemoteResource::GitHubArtifact { repo, pattern } => {
let mut release = fetch_latest_release(&ctx.reqwest, repo).await?;
let pattern = ramhorns::Template::new(*pattern)
.into_diagnostic()
.wrap_err(
"Invalid pattern for artifact matching in download step.",
)?;
release.meta.tag_name_strip_prefix = release
.meta
.tag_name
.strip_prefix('v')
.unwrap_or(&release.meta.tag_name);
let asset_name = pattern.render(&release.meta);
let artifact = release.assets.into_iter().filter(move |asset| asset.name == asset_name).next().ok_or_else(|| miette!("No artifact of the latest release matched the pattern in download step."))?;
url::Url::parse(&artifact.browser_download_url)
.into_diagnostic()
.wrap_err(
"Invalid url returned by GitHub for latest release artifact.",
)?
}
};
let mut resp = ctx
.reqwest
.get(url)
.header("User-Agent", USER_AGENT_STR)
.send()
.await
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?,
);
while let Some(mut chunk) = resp
.chunk()
.await
.into_diagnostic()
.wrap_err(FETCH_FILE_ERROR_MSG)?
{
.wrap_err(FETCH_FILE_ERROR_MSG)?;
let _content_length = resp.content_length();
mkdir_all(file.parent().ok_or_else(|| {
miette!("Destination file for download step has no parent.")
})?)
.await?;
let mut writer = tokio::io::BufWriter::new(
tokio::fs::File::create(file.as_os_str())
.await
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?,
);
while let Some(mut chunk) = resp
.chunk()
.await
.into_diagnostic()
.wrap_err(FETCH_FILE_ERROR_MSG)?
{
writer
.write_all_buf(&mut chunk)
.await
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?;
}
writer
.write_all_buf(&mut chunk)
.flush()
.await
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?;
}
writer
.flush()
.await
.into_diagnostic()
.wrap_err(WRITE_FILE_ERROR_MSG)?;
}
Self::ExtractFile { file, dest } => {
println!(
"Extracting {file} to {dest}...",
file = file.to_str().unwrap_or("<NON UTF-8>"),
dest = dest.to_str().unwrap_or("<NON UTF-8>")
);
const EXTRACT_FILE_ERROR_MSG: &'static str = "Extracting file failed.";
mkdir_all(&dest).await.wrap_err(EXTRACT_FILE_ERROR_MSG)?;
let dest = tokio::fs::canonicalize(&dest)
@ -161,6 +174,11 @@ impl<'a> Step<'a> {
ensure!(status.success(), EXTRACT_FILE_ERROR_MSG);
}
Self::ExecuteCommand { file, args } => {
println!(
"Executing command `{file} {args}`...",
file = file.to_str().unwrap_or("<NON UTF-8>"),
args = args.into_iter().map(|s| *s).collect::<String>(),
);
const EXECUTE_COMMAND_ERROR_MSG: &'static str = "Executing command failed.";
let status = tokio::process::Command::new(file.as_os_str())
.args(*args)
@ -173,6 +191,10 @@ impl<'a> Step<'a> {
ensure!(status.success(), EXECUTE_COMMAND_ERROR_MSG);
}
Self::CreateShortcut { target, file } => {
println!(
"Creating shortcut to {target:?} at {file}...",
file = file.to_str().unwrap_or("<NON UTF-8>")
);
const CREATE_SHORTCUT_ERROR_MSG: &'static str = "Creating shortcut failed.";
mkdir_all(
file.parent().ok_or_else(|| {

View File

@ -46,6 +46,9 @@ async fn main() -> Result<()> {
let minecraft_dir = swtools_path().join("minecraft");
let psiphon_dir = swtools_path().join("psiphon");
let psiphon_bin = psiphon_dir.join("psiphon3.exe");
let utilities = [
Pipeline::new(
"Install Notepad++",
@ -94,6 +97,24 @@ async fn main() -> Result<()> {
},
],
),
Pipeline::new(
"Install Psiphon VPN",
vec![
Step::DownloadFile {
file: psiphon_bin.as_path().into(),
res: RemoteResource::Url(
"https://s3.amazonaws.com/f58p-mqce-k1yj/psiphon3.exe",
),
},
Step::CreateShortcut {
target: ShortcutTarget::Executable {
file: psiphon_bin.as_path().into(),
args: "",
},
file: desktop_path().join("Psiphon3.lnk").into(),
},
],
),
Pipeline::new(
"Install Minecraft (Java Edition)",
vec![