Skip to content

Commit

Permalink
Merge pull request #177 from J-ZhengLi/rim_dev_plus
Browse files Browse the repository at this point in the history
allowing `rim-dev` to generate mocked server config files
  • Loading branch information
J-ZhengLi authored Dec 12, 2024
2 parents 0b3bc2d + ca8590f commit b09d0d0
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 164 deletions.
20 changes: 0 additions & 20 deletions resources/mock/dist/distribution-manifest.toml

This file was deleted.

24 changes: 0 additions & 24 deletions resources/mock/dist/stable-1.81.0.toml

This file was deleted.

24 changes: 0 additions & 24 deletions resources/mock/dist/stable-1.82.0.toml

This file was deleted.

81 changes: 14 additions & 67 deletions rim_dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ extern crate rust_i18n;

mod common;
mod dist;
mod mocked_manager;
mod mocked;
mod vendor;

use anyhow::{Context, Result};
use anyhow::Result;
use dist::{DistMode, DIST_HELP};
use mocked::{installation, manager, server};
use std::env;
use std::io::{stdout, Write};
use std::path::PathBuf;
use std::process::{Command, ExitCode};
use std::{env, fs};
use std::process::ExitCode;
use vendor::VENDOR_HELP;

i18n!("../locales", fallback = "en");
Expand Down Expand Up @@ -51,79 +52,25 @@ impl DevCmd {
match self {
Self::Dist { mode, binary_only } => dist::dist(*mode, *binary_only)?,
Self::RunManager { no_gui, args } => {
let mut cargo_args = if *no_gui {
["run", "--"].to_vec()
} else {
["tauri", "dev", "--"].to_vec()
};
cargo_args.extend(args.iter().map(|s| s.as_str()));

gen_mocked_files()?;
// a mocked server is needed to run most of function in manager
server::generate()?;

// generate a fake manager binary with higher version so we
// can test the self update.
if args.iter().any(|arg| arg == "update") {
mocked_manager::generate()?;
manager::generate()?;
}

let mut mock_dir =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).with_file_name("resources");
mock_dir.push("mock");
let mocked_server = url::Url::from_directory_path(&mock_dir).unwrap_or_else(|_| {
panic!("path {} cannot be converted to URL", mock_dir.display())
});
let status = Command::new("cargo")
.args(cargo_args)
.env("MODE", "manager")
.env("RIM_DIST_SERVER", mocked_server.as_str())
.status()?;
println!(
"\nmanager exited with status code: {}",
status.code().unwrap_or(-1)
);
installation::generate_and_run_manager(*no_gui, args)?;
}
Self::Vendor => vendor::vendor()?,
}
Ok(())
}
}

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

/// Generate mocked `.fingerprint`, and `toolset-manifest` files when running with `run-manager`
fn gen_mocked_files() -> 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");
// don't write if the path already exists
if !fingerprint_path.exists() {
fs::write(
fingerprint_path,
format!(
"
name = 'Custom Rust Distribution'
version = 'stable-1.80.1'
root = '{0}'
[rust]
version = '1.80.1'
components = [\"llvm-tools\", \"rustc-dev\"]
[tools.mingw64]
kind = 'dir-with-bin'
version = '13.0.0'
paths = ['{0}/tools/mingw64']",
debug_dir.display()
),
)?;
}

let manifest = include_str!("../../resources/toolset_manifest.toml");
let manifest_path = debug_dir.join("toolset-manifest.toml");
fs::write(manifest_path, manifest)?;

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

fn main() -> Result<ExitCode> {
Expand Down
107 changes: 107 additions & 0 deletions rim_dev/src/mocked/installation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//! Module to create a fake installation root, useful to test the `manager` utilities.
use super::TOOLKIT_NAME;
use anyhow::{bail, Result};
use std::{env::consts::EXE_SUFFIX, fs, path::PathBuf, process::Command};

struct FakeInstallation {
manager_bin: Option<PathBuf>,
}

impl FakeInstallation {
fn new() -> Self {
Self { manager_bin: None }
}

fn fingerprint_content(&self, ver: &str) -> String {
format!(
"
name = '{TOOLKIT_NAME}'
version = 'stable-{ver}'
root = '{0}'
[rust]
version = '{ver}'
components = [\"llvm-tools\", \"rustc-dev\"]
[tools.mingw64]
kind = 'dir-with-bin'
version = '13.0.0'
paths = ['{0}/tools/mingw64']
",
super::install_dir().display()
)
}

fn generate_manager_bin(&mut self, no_gui: bool, args: &[String]) -> Result<()> {
let mut cargo_args = if no_gui {
["build", "--"].to_vec()
} else {
["tauri", "build", "--debug", "-b", "none", "--"].to_vec()
};
cargo_args.extend(args.iter().map(|s| s.as_str()));

// build rim
let build_status = Command::new("cargo").args(cargo_args).status()?;
if !build_status.success() {
bail!("failed to build manager binary");
}

// make a copy of rim as manager binary to the fake installation root
let (src_bin, dest_bin) = if no_gui {
(
format!("rim-cli{EXE_SUFFIX}"),
format!("manager-cli{EXE_SUFFIX}"),
)
} else {
(
format!("rim-gui{EXE_SUFFIX}"),
format!("manager{EXE_SUFFIX}"),
)
};
let build_artifact = super::debug_dir().join(src_bin);
let dest_path = super::install_dir().join(dest_bin);
fs::copy(build_artifact, &dest_path)?;

self.manager_bin = Some(dest_path);

Ok(())
}

fn generate_meta_files(&self) -> Result<()> {
let fingerprint_path = super::install_dir().join(".fingerprint.toml");
let manifest_path = super::install_dir().join("toolset-manifest.toml");

// don't write if the path already exists
if !fingerprint_path.exists() {
fs::write(fingerprint_path, self.fingerprint_content("1.0.0"))?;
}
let manifest = include_str!("../../../resources/toolset_manifest.toml");
if !manifest_path.exists() {
fs::write(manifest_path, manifest)?;
}

Ok(())
}
}

pub(crate) fn generate_and_run_manager(no_gui: bool, args: &[String]) -> Result<()> {
let mut fake = FakeInstallation::new();
fake.generate_meta_files()?;
fake.generate_manager_bin(no_gui, args)?;

// `fake.manager_bin` cannot be none if the previous `generate_manager_bin`
// succeeded, so it's safe to unwrap
let manager = &fake.manager_bin.unwrap();

let mocked_dist_server = super::server_dir_url();
// run the manager copy
let status = Command::new(manager)
.env("RIM_DIST_SERVER", mocked_dist_server.as_str())
.status()?;
println!(
"\nmanager exited with status code: {}",
status.code().unwrap_or(-1)
);
Ok(())
}
23 changes: 3 additions & 20 deletions rim_dev/src/mocked_manager.rs → rim_dev/src/mocked/manager.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
use anyhow::Result;
use std::{
env::consts::EXE_SUFFIX,
fs,
path::{Path, PathBuf},
process::Command,
sync::OnceLock,
};

static MANAGER_DIR: OnceLock<PathBuf> = OnceLock::new();
use std::{env::consts::EXE_SUFFIX, fs, path::PathBuf, process::Command};

struct FakeRim {
main_rs: String,
Expand Down Expand Up @@ -63,7 +55,7 @@ edition = \"2021\"
let mut binary_path = temp_dir.join("target");
binary_path.push("release");
binary_path.push(format!("rim{}", std::env::consts::EXE_SUFFIX));
let mut dest_dir = manager_dir().join("archive");
let mut dest_dir = super::manager_dir().join("archive");
dest_dir.push(&self.version);
dest_dir.push(env!("TARGET"));
fs::create_dir_all(&dest_dir)?;
Expand All @@ -77,18 +69,9 @@ edition = \"2021\"
}
}

fn manager_dir() -> &'static Path {
MANAGER_DIR.get_or_init(|| {
let mut m_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).with_file_name("resources");
m_dir.push("mock");
m_dir.push("manager");
m_dir
})
}

/// Generate a `release.toml` for self update, that the version will always be newer.
fn gen_release_toml(version: &str) -> Result<()> {
let release_toml = manager_dir().join("release.toml");
let release_toml = super::manager_dir().join("release.toml");

let desired_content = format!("version = '{version}'");
fs::write(release_toml, desired_content)?;
Expand Down
Loading

0 comments on commit b09d0d0

Please sign in to comment.