-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use std::collections::HashMap; | ||
use std::env::temp_dir; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use color_eyre::eyre::Result; | ||
|
||
use crate::cmd::CmdLineRunner; | ||
use crate::config::{Config, Settings}; | ||
use crate::duration::DAILY; | ||
|
||
use crate::file::display_path; | ||
use crate::git::Git; | ||
use crate::github::GithubRelease; | ||
use crate::install_context::InstallContext; | ||
use crate::lock_file::LockFile; | ||
use crate::plugins::core::CorePlugin; | ||
use crate::plugins::{Plugin, HTTP}; | ||
use crate::toolset::{ToolVersion, ToolVersionRequest, Toolset}; | ||
use crate::ui::progress_report::ProgressReport; | ||
use crate::{cmd, env, file, http}; | ||
|
||
#[derive(Debug)] | ||
pub struct ErlangPlugin { | ||
core: CorePlugin, | ||
} | ||
|
||
impl ErlangPlugin { | ||
pub fn new() -> Self { | ||
Self { | ||
core: CorePlugin::new("erlang"), | ||
} | ||
} | ||
|
||
fn kerl_path(&self) -> PathBuf { | ||
self.core.cache_path.join("kerl") | ||
} | ||
|
||
fn lock_build_tool(&self) -> Result<fslock::LockFile> { | ||
LockFile::new(&self.kerl_path()) | ||
.with_callback(|l| { | ||
trace!("install_or_update_kerl {}", l.display()); | ||
}) | ||
.lock() | ||
} | ||
|
||
fn update_kerl(&self) -> Result<()> { | ||
let _lock = self.lock_build_tool(); | ||
if self.kerl_path().exists() { | ||
return Ok(()); | ||
} | ||
debug!("Updating kerl in {}", display_path(&self.kerl_path())); | ||
file::remove_all(self.kerl_path())?; | ||
self.install_kerl()?; | ||
Ok(()) | ||
} | ||
|
||
fn install_kerl(&self) -> Result<()> { | ||
debug!("Installing kerl to {}", display_path(&self.kerl_path())); | ||
let kerl_version = "4.0.0"; | ||
HTTP.download_file( | ||
format!("https://raw.githubusercontent.com/kerl/kerl/{kerl_version}/kerl"), | ||
&self.kerl_path(), | ||
)?; | ||
file::make_executable(&self.kerl_path())?; | ||
Ok(()) | ||
} | ||
|
||
fn kerl_recently_updated(&self) -> Result<bool> { | ||
let updated_at = file::modified_duration(&self.kerl_path())?; | ||
Ok(updated_at < DAILY) | ||
} | ||
|
||
fn fetch_remote_versions(&self) -> Result<Vec<String>> { | ||
match self.core.fetch_remote_versions_from_rtx() { | ||
Ok(Some(versions)) => return Ok(versions), | ||
Ok(None) => {} | ||
Err(e) => warn!("failed to fetch remote versions: {}", e), | ||
} | ||
self.update_kerl()?; | ||
let versions = CorePlugin::run_fetch_task_with_timeout(move || { | ||
dbg!(self.kerl_path()); | ||
let output = cmd!(self.kerl_path(), "list", "releases", "all").read()?; | ||
let versions = output | ||
.split('\n') | ||
.filter(|s| regex!(r"^[0-9].+$").is_match(s)) | ||
.map(|s| s.to_string()) | ||
.collect(); | ||
Ok(versions) | ||
})?; | ||
Ok(versions) | ||
} | ||
} | ||
|
||
impl Plugin for ErlangPlugin { | ||
fn name(&self) -> &str { | ||
self.core.name | ||
} | ||
|
||
fn list_remote_versions(&self, _settings: &Settings) -> Result<Vec<String>> { | ||
self.core | ||
.remote_version_cache | ||
.get_or_try_init(|| self.fetch_remote_versions()) | ||
.cloned() | ||
} | ||
|
||
fn install_version_impl(&self, ctx: &InstallContext) -> Result<()> { | ||
self.update_kerl()?; | ||
// TODO: handle ref | ||
|
||
// self.install_cmd(ctx.config, &ctx.tv, &ctx.pr)?.execute()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters