Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Sep 20, 2024
1 parent cd53c35 commit dc99fcb
Show file tree
Hide file tree
Showing 25 changed files with 422 additions and 156 deletions.
7 changes: 3 additions & 4 deletions installer/src-tauri/src/installer_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use tauri::api::dialog::FileDialogBuilder;

use super::INSTALL_DIR;
use crate::error::Result;
use rim::manifest::{baked_in_manifest, ToolInfo};
use rim::toolset_manifest::{baked_in_manifest, ToolInfo};
use rim::utils::MultiThreadProgress;
use rim::{
get_component_list_from_manifest, try_it, utils, Component, EnvConfig, InstallConfiguration,
};
use rim::{try_it, utils, EnvConfig, InstallConfiguration};
use rim::components::{Component, get_component_list_from_manifest};

static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();

Expand Down
1 change: 1 addition & 0 deletions installer/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate rust_i18n;
mod error;
mod installer_mode;
mod manager_mode;
mod toolkit;

use std::env;
use std::path::PathBuf;
Expand Down
16 changes: 14 additions & 2 deletions installer/src-tauri/src/manager_mode.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::error::Result;
use crate::{error::Result, toolkit::Toolkit};
use anyhow::Context;
use rim::toolset_manifest::baked_in_manifest;

pub(super) fn main() -> Result<()> {
super::hide_console();

tauri::Builder::default()
.invoke_handler(tauri::generate_handler![])
.invoke_handler(tauri::generate_handler![get_installed_kit])
.setup(|app| {
let version = env!("CARGO_PKG_VERSION");
let _manager_window = tauri::WindowBuilder::new(
Expand All @@ -23,3 +24,14 @@ pub(super) fn main() -> Result<()> {
.context("unknown error occurs while running tauri application")?;
Ok(())
}

#[tauri::command]
fn get_installed_kit() -> Result<Option<Toolkit>> {
// FIXME: load manifest from a cached location
let manifest = baked_in_manifest()?;

let toolkit = Toolkit::from_installed(&manifest)?;
println!("installed: {:#?}", &toolkit);

Ok(toolkit)
}
54 changes: 54 additions & 0 deletions installer/src-tauri/src/toolkit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use rim::{fingerprint::InstallationRecord, toolset_manifest::ToolsetManifest, components};
use serde::Serialize;
use crate::Result;

#[derive(Debug, Serialize)]
pub struct Toolkit {
name: String,
version: String,
desc: Option<String>,
#[serde(alias = "notes")]
info: Option<String>,
#[serde(rename = "manifestURL")]
manifest_url: Option<String>,
components: Vec<components::Component>,
}

impl Toolkit {
/// Try getting the toolkit from installation record and the original manifest.
///
/// We need the manifest because it contains the details of the toolkit along with
/// what components it has.
pub fn from_installed(manifest: &ToolsetManifest) -> Result<Option<Self>> {
if !InstallationRecord::exists()? {
// No toolkit installed, return None
return Ok(None);
}

let mut tk = Self {
name: t!("unknown_toolkit").to_string(),
version: "N/A".to_string(),
desc: None,
info: None,
manifest_url: None,
components: vec![]
};
let fp = InstallationRecord::load_from_install_dir()?;

// The name and version should always be read from the install record,
// but the components should only be load from manifest if the name and version are matched
if let Some(name) = &fp.name {
tk.name = name.to_owned();
}
if let Some(ver) = &fp.version {
tk.version = ver.to_owned();
}
if matches!(&manifest.name, Some(name) if *name == tk.name)
&& matches!(&manifest.version, Some(ver) if *ver == tk.version)
{
tk.components = components::get_component_list_from_manifest(manifest)?;
}

Ok(Some(tk))
}
}
16 changes: 13 additions & 3 deletions installer/src/utils/managerConf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { KitItem } from './types/KitItem';
import { ManagerComponent } from './types/Component';
import { CheckGroup, CheckGroupItem } from './types/CheckBoxGroup';
import LabelComponent from '@/views/manager/components/Label.vue';
import { invokeCommand } from './invokeCommand';

const kits: KitItem[] = [
{
Expand Down Expand Up @@ -210,16 +211,25 @@ class ManagerConf {
...components
);
}
public loadConf(): any {
this.setCurrent(kits[0]);
this.setInstalled(kits[0]);
async loadConf() {
await this.loadInstalledKit();

this.setKits(kits);
this.setComponents(
this.getInstalled().value?.components.filter(
(i) => i.installed || i.required
) || []
);
}
async loadInstalledKit() {
const installedKit = (await invokeCommand(
'get_installed_kit'
)) as KitItem | undefined;
if (installedKit) {
this.setInstalled(installedKit);
this.setCurrent(installedKit);
}
}
}

export const managerConf = new ManagerConf();
4 changes: 3 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@
"uninstall_confirmation": "Are you sure you want to uninstall the following components: \n\n%{list}\n",
"uninstall_all_confirmation": "Are you sure you want to uninstall %{vendor}-manager and the following components: \n\n%{list}\n",
"uninstall_unknown_tool_warn": "warn: no suitable method for uninstalling tool '%{tool}', skipping",
"uninstall_tool_failed_warn": "warn: unable to uninstall tool '%{tool}', maybe it was already uninstalled, skipping"
"uninstall_tool_failed_warn": "warn: unable to uninstall tool '%{tool}', maybe it was already uninstalled, skipping",

"unknown_toolkit": "Unknown Toolkit"
}
36 changes: 36 additions & 0 deletions resources/dist_manifest_mocked.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[[packages]]
name = "XXX Community"
version = "1.80.1"
desc = "Description for community version"
info = '''
# Changelog
This is the initial build, includes:
- Rust 1.80.1 version
- blah blah blah
- ...
'''
manifest-url = "https://example.com/path/to/manifest-1.80.1"

[[packages]]
name = "XXX Community"
version = "1.81.0"
desc = "Description for community version"
info = '''
# Changelog
- add new tool support: hello_world
- ...
'''
manifest-url = "https://example.com/path/to/manifest-1.81.0"

[[packages]]
name = "XXX LTS"
version = "1.80.1"
desc = "Description for LTS version"
info = '''
# Changelog
This is the initial build, includes:
- Rust 1.80.1 version
- blah blah blah
- ...
'''
manifest-url = "https://example.com/path/to/manifest-1.80.1"
26 changes: 23 additions & 3 deletions rim_dev/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{bail, Context, Result};
use rust_i18n::{i18n, t};
use std::env;
use std::{env, fs};
use std::io::{stdout, Write};
use std::path::PathBuf;
use std::process::{Command, ExitCode};
Expand Down Expand Up @@ -115,8 +115,7 @@ impl DevCmd {
std::fs::create_dir_all(&dist_dir)?;

// Get the target dir
let dev_bin =
env::current_exe().context("failed to get the path of current binary")?;
let dev_bin = current_exe()?;
let release_dir = dev_bin.parent().unwrap().with_file_name("release");
for (args, orig_name, new_name) in x {
let status = Command::new("cargo").args(args).status()?;
Expand All @@ -138,6 +137,8 @@ impl DevCmd {
};
cargo_args.extend(args.iter().map(|s| s.as_str()));

gen_fingerprint()?;

let status = Command::new("cargo")
.args(cargo_args)
.env("MODE", "manager")
Expand All @@ -155,6 +156,25 @@ impl DevCmd {
}
}

fn current_exe() -> Result<PathBuf> {
env::current_exe().context("failed to get the path of current binary")
}

/// Generate a mocked `.fingerprint` file when running with `run-manager`
fn gen_fingerprint() -> Result<()> {
let cur_exe = current_exe()?;
// safe to unwrap, always have parent dir
let debug_dir = cur_exe.parent().unwrap();
// Note: there is a `.fingerprint` folder generated by cargo, don't touch it
let fingerprint_path = debug_dir.join(".fingerprint.toml");
let mut file = fs::OpenOptions::new()
.create(true)
.write(true)
.open(&fingerprint_path)?;
writeln!(&mut file, "root = '{}'", debug_dir.display())?;
Ok(())
}

fn main() -> Result<ExitCode> {
let mut args = std::env::args().skip(1);
let mut stdout = stdout();
Expand Down
7 changes: 4 additions & 3 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use crate::core::install::{
InstallConfiguration,
};
use crate::core::try_it;
use crate::manifest::{baked_in_manifest, ToolMap};
use crate::{default_install_dir, get_component_list_from_manifest, utils, Component};
use crate::toolset_manifest::{baked_in_manifest, ToolMap};
use crate::{default_install_dir, components, utils};
use crate::components::Component;

use super::{GlobalOpt, Installer, ManagerSubcommands};

Expand All @@ -34,7 +35,7 @@ pub(super) fn execute_installer(installer: &Installer) -> Result<()> {
let mut manifest = baked_in_manifest()?;
manifest.adjust_paths()?;

let component_list = get_component_list_from_manifest(&manifest)?;
let component_list = components::get_component_list_from_manifest(&manifest)?;
let user_opt = CustomInstallOpt::collect_from_user(
prefix.as_deref().unwrap_or(&default_install_dir()),
component_list,
Expand Down
2 changes: 1 addition & 1 deletion src/core/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU32, Ordering};

use crate::manifest::{ToolInfo, ToolsetManifest};
use crate::toolset_manifest::{ToolInfo, ToolsetManifest};

static COMPONENTS_COUNTER: AtomicU32 = AtomicU32::new(0);

Expand Down
3 changes: 2 additions & 1 deletion src/core/custom_instructions/vscode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! there's currently no suitable solution other than execute some commands to hack it.
use std::path::{Path, PathBuf};
use crate::core::directories::RimDir;
use crate::core::install::InstallConfiguration;
use crate::core::uninstall::UninstallConfiguration;
use crate::{core::os::add_to_path, utils};
Expand Down Expand Up @@ -119,7 +120,7 @@ Keywords=vscode;
use crate::core::os::remove_from_path;

// We've added a path for VSCode at `<InstallDir>/tools/vscode/bin`, try removing it from `PATH`.
let mut vscode_path = config.tools_dir();
let mut vscode_path = config.tools_dir().to_path_buf();
vscode_path.push(self.tool_name);
vscode_path.push("bin");
remove_from_path(&vscode_path)?;
Expand Down
36 changes: 36 additions & 0 deletions src/core/directories.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::path::Path;

/// Declare a statically allocated `OnceLock` path, and create that directory if it does not exists.
macro_rules! get_path_and_create {
($path_ident:ident, $init:expr) => {{
static $path_ident: std::sync::OnceLock<std::path::PathBuf> = std::sync::OnceLock::new();
let __path__ = $path_ident.get_or_init(|| $init);
$crate::utils::ensure_dir(__path__)
.expect("unable to create one of the directory under installation folder");
__path__
}};
}

pub(crate) trait RimDir {
fn install_dir(&self) -> &Path;

fn cargo_home(&self) -> &Path {
get_path_and_create!(CARGO_HOME_DIR, self.install_dir().join(".cargo"))
}

fn cargo_bin(&self) -> &Path {
get_path_and_create!(CARGO_BIN_DIR, self.cargo_home().join("bin"))
}

fn rustup_home(&self) -> &Path {
get_path_and_create!(RUSTUP_HOME_DIR, self.install_dir().join(".rustup"))
}

fn temp_root(&self) -> &Path {
get_path_and_create!(TEMP_DIR, self.install_dir().join("temp"))
}

fn tools_dir(&self) -> &Path {
get_path_and_create!(TOOLS_DIR, self.install_dir().join("tools"))
}
}
Loading

0 comments on commit dc99fcb

Please sign in to comment.