Skip to content

Commit

Permalink
einmal gott spielen und die richtigen ideen haben
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidma committed Jan 22, 2025
1 parent cd9c60c commit 97929b9
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 362 deletions.
48 changes: 21 additions & 27 deletions crates/repository/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use core::fmt;
use std::{
env::current_dir,
ffi::{OsStr, OsString},
fmt::{Display, Formatter},
path::Path,
};

use color_eyre::{
eyre::{bail, Context, ContextCompat},
eyre::{bail, Context},
Result,
};
use pathdiff::diff_paths;
use tokio::process::Command;

use crate::{data_home::get_data_home, sdk::download_and_install, Repository};
use crate::{sdk::download_and_install, Repository};

#[derive(Debug, Clone)]
pub enum Environment {
Expand Down Expand Up @@ -64,14 +62,18 @@ impl Cargo {
if let Environment::Sdk { version } = &self.environment {
match self.host {
Host::Local => {
let data_home = get_data_home().wrap_err("failed to get data home")?;
let data_home = repository
.resolve_data_home()
.await
.wrap_err("failed to resolve data home")?;

download_and_install(version, data_home)
.await
.wrap_err("failed to download and install SDK")?;
}
Host::Remote => {
let mut command = Command::new(repository.root.join("scripts/remoteWorkspace"));
let mut command =
Command::new(repository.root.join("scripts/remote_workspace"));

let status = command
.arg("pepsi")
Expand Down Expand Up @@ -110,11 +112,7 @@ impl Cargo {
) -> Result<Command> {
let arguments = self.arguments.join(OsStr::new(" "));

let relative_pwd = diff_paths(
current_dir().wrap_err("failed to get current directory")?,
&repository.root,
)
.wrap_err("failed to express current directory relative to repository root")?;
let data_home_script = repository.data_home_script()?;

let command_string = match self.environment {
Environment::Native => {
Expand All @@ -123,26 +121,22 @@ impl Cargo {
command
}
Environment::Sdk { version } => {
let data_home = get_data_home().wrap_err("failed to get data home")?;
let environment_file = &data_home.join(format!(
"sdk/{version}/environment-setup-corei7-64-aldebaran-linux"
));
let sdk_environment_setup = environment_file
.to_str()
.wrap_err("failed to convert sdk environment setup path to string")?;
let mut command = OsString::from(format!(". {sdk_environment_setup} && cargo "));
let environment_file = format!(
"$({data_home_script})/sdk/{version}/environment-setup-corei7-64-aldebaran-linux",
);
let mut command = OsString::from(format!(". {environment_file} && cargo "));
command.push(arguments);
command
}
Environment::Docker { image } => {
let data_home = get_data_home().wrap_err("failed to get data home")?;
let cargo_home = data_home.join("container-cargo-home/");
let cargo_home = format!("$({data_home_script})/container-cargo-home/");
// TODO: Make image generic over SDK/native by modifying entry point; source SDK not here
let pwd = Path::new("/hulk").join(&relative_pwd);
let pwd = Path::new("/hulk").join(&repository.root_to_current_dir()?);
let root = repository.current_dir_to_root()?;
let mut command = OsString::from(format!("\
mkdir -p {cargo_home} && \
docker run \
--volume={repository_root}:/hulk:z \
--volume={root}:/hulk:z \
--volume={cargo_home}:/naosdk/sysroots/corei7-64-aldebaran-linux/home/cargo:z \
--rm \
--interactive \
Expand All @@ -153,8 +147,7 @@ impl Cargo {
echo $PATH && \
cargo \
",
repository_root=repository.root.display(),
cargo_home=cargo_home.display(),
root=root.display(),
pwd=pwd.display(),
));
command.push(arguments);
Expand All @@ -170,12 +163,13 @@ impl Cargo {
command
}
Host::Remote => {
let mut command = Command::new(repository.root.join("scripts/remoteWorkspace"));
let mut command = Command::new(repository.root.join("scripts/remote_workspace"));

for path in compiler_artifacts {
command.arg("--return-file").arg(path.as_ref());
}
command.arg("--cd").arg(Path::new("./").join(relative_pwd));
let current_dir = repository.root_to_current_dir()?;
command.arg("--cd").arg(Path::new("./").join(current_dir));
command
}
};
Expand Down
45 changes: 33 additions & 12 deletions crates/repository/src/data_home.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
use std::{env, path::PathBuf};
use std::path::PathBuf;

use color_eyre::Result;
use color_eyre::{
eyre::{bail, Context},
Result,
};
use tokio::process::Command;

/// Get the data home directory.
///
/// This function returns the directory where hulk stores its data. The directory is determined by
/// the `HULK_DATA_HOME` environment variable. If the environment variable is not set, the
/// user-specific data directory (set by `XDG_DATA_HOME`) is used.
pub fn get_data_home() -> Result<PathBuf> {
if let Ok(home) = env::var("HULK_DATA_HOME") {
return Ok(PathBuf::from(home));
use crate::Repository;

impl Repository {
/// Get the data home directory.
///
/// This function returns the directory where hulk stores its data. The directory is determined by
/// the `HULK_DATA_HOME` environment variable. If the environment variable is not set, the
/// user-specific data directory (set by `XDG_DATA_HOME`) is used.
pub async fn resolve_data_home(&self) -> Result<PathBuf> {
let output = Command::new(self.root.join("scripts/resolve_data_home"))
.output()
.await
.wrap_err("failed to spawn resolve_data_home script")?;

if !output.status.success() {
bail!("failed to resolve data home");
}

let data_home = String::from_utf8(output.stdout).wrap_err("failed to parse data home")?;
Ok(PathBuf::from(data_home))
}

let base_directories = xdg::BaseDirectories::with_prefix("hulk")?;
Ok(base_directories.get_data_home())
pub fn data_home_script(&self) -> Result<String> {
let root = self.current_dir_to_root()?;
Ok(format!(
"{root}/scripts/resolve_data_home",
root = root.display(),
))
}
}
1 change: 1 addition & 0 deletions crates/repository/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod image;
pub mod inspect_version;
pub mod location;
pub mod modify_json;
pub mod paths;
pub mod player_number;
pub mod recording;
pub mod sdk;
Expand Down
25 changes: 25 additions & 0 deletions crates/repository/src/paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::{
env::current_dir,
path::{Path, PathBuf},
};

use color_eyre::eyre::{Context, ContextCompat, Result};
use pathdiff::diff_paths;

use crate::Repository;

impl Repository {
pub fn root_to_current_dir(&self) -> Result<PathBuf> {
let current_dir = current_dir().wrap_err("failed to get current directory")?;
let path = diff_paths(&current_dir, &self.root)
.wrap_err("failed to express current directory relative to repository root")?;
Ok(Path::new("./").join(path))
}

pub fn current_dir_to_root(&self) -> Result<PathBuf> {
let current_dir = current_dir().wrap_err("failed to get current directory")?;
let path = diff_paths(&self.root, &current_dir)
.wrap_err("failed to express repository root relative to current directory")?;
Ok(Path::new("./").join(path))
}
}
Loading

0 comments on commit 97929b9

Please sign in to comment.