Skip to content

Commit

Permalink
make linux compile as well
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Nov 10, 2024
1 parent 6ce3e6d commit ce30ab0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 58 deletions.
1 change: 1 addition & 0 deletions crates/rattler_menuinst/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ shlex = { workspace = true }
serde_json = { workspace = true }
tracing = { workspace = true }
rattler_conda_types = { path = "../rattler_conda_types", default-features = false }
rattler_shell = { path = "../rattler_shell", default-features = false }
thiserror = { workspace = true }
unicode-normalization = { workspace = true }
regex = { workspace = true }
Expand Down
9 changes: 6 additions & 3 deletions crates/rattler_menuinst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::path::Path;

use rattler_conda_types::Platform;

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
Expand Down Expand Up @@ -53,10 +52,14 @@ pub fn install_menuitems(

for item in menu_inst.menu_items {
if platform.is_linux() {
#[cfg(target_os = "linux")]
if let Some(linux_item) = item.platforms.linux {
let command = item.command.merge(linux_item.base);
linux::install_menu_item(linux_item.specific, command, MenuMode::System)?;
linux::install_menu_item(
linux_item.specific,
command,
&placeholders,
MenuMode::System,
)?;
}
} else if platform.is_osx() {
#[cfg(target_os = "macos")]
Expand Down
78 changes: 34 additions & 44 deletions crates/rattler_menuinst/src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::{fs::File, path::PathBuf};

use rattler_conda_types::Platform;

use crate::render::{BaseMenuItemPlaceholders, MenuItemPlaceholders};
use crate::{
schema::{Linux, MenuItemCommand},
MenuInstError, MenuMode,
Expand All @@ -29,6 +30,7 @@ pub struct LinuxMenu {
item: Linux,

Check failure on line 30 in crates/rattler_menuinst/src/linux.rs

View workflow job for this annotation

GitHub Actions / Format and Lint

field `item` is never read

Check failure on line 30 in crates/rattler_menuinst/src/linux.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

field `item` is never read

Check failure on line 30 in crates/rattler_menuinst/src/linux.rs

View workflow job for this annotation

GitHub Actions / Check intra-doc links

field `item` is never read
command: MenuItemCommand,
directories: Directories,
placeholders: MenuItemPlaceholders,
}

pub struct Directories {
Expand Down Expand Up @@ -71,15 +73,25 @@ impl Directories {
}

impl LinuxMenu {
fn new(item: Linux, command: MenuItemCommand, mode: MenuMode) -> Self {
fn new(
item: Linux,
command: MenuItemCommand,
placeholders: &BaseMenuItemPlaceholders,
mode: MenuMode,
) -> Self {
let directories = Directories::new(mode);
// TODO unsure if this is the right value for MENU_ITEM_LOCATION
let refined_placeholders = placeholders.refine(&directories.system_menu_config_location);

LinuxMenu {
name: command
.name
.resolve(crate::schema::Environment::Base)
.resolve(crate::schema::Environment::Base, &placeholders)

Check failure on line 89 in crates/rattler_menuinst/src/linux.rs

View workflow job for this annotation

GitHub Actions / Format and Lint

this expression creates a reference which is immediately dereferenced by the compiler
.to_string(),
item,
command,
directories: Directories::new(mode),
directories,
placeholders: refined_placeholders,
}
}

Expand Down Expand Up @@ -160,22 +172,34 @@ impl LinuxMenu {
writeln!(writer, "Type=Application")?;
writeln!(writer, "Encoding=UTF-8")?;
writeln!(writer, "Name={:?}", self.command.name)?;
writeln!(writer, "Exec={}", self.command.command.join(" "))?;
writeln!(
writer,
"Exec={}",
self.command
.command
.iter()
.map(|s| s.resolve(&self.placeholders))
.collect::<Vec<_>>()
.join(" ")
)?;
writeln!(
writer,
"Terminal={}",
self.command.terminal.unwrap_or(false)
)?;

if let Some(icon) = &self.command.icon {
let icon = icon.resolve(&self.placeholders);
writeln!(writer, "Icon={icon}")?;
}

if !self.command.description.is_empty() {
writeln!(writer, "Comment={}", self.command.description)?;
let description = self.command.description.resolve(&self.placeholders);
if !description.is_empty() {
writeln!(writer, "Comment={}", description)?;
}

if let Some(working_dir) = &self.command.working_dir {
let working_dir = working_dir.resolve(&self.placeholders);
writeln!(writer, "Path={working_dir}")?;
}

Expand Down Expand Up @@ -203,7 +227,7 @@ impl LinuxMenu {

fn install(&self) -> Result<(), MenuInstError> {
self.pre_create()?;

self.create_desktop_entry()?;
Ok(())
}

Expand All @@ -215,9 +239,10 @@ impl LinuxMenu {
pub fn install_menu_item(
item: Linux,
command: MenuItemCommand,
placeholders: &BaseMenuItemPlaceholders,
menu_mode: MenuMode,
) -> Result<(), MenuInstError> {
let menu = LinuxMenu::new(item, command, menu_mode);
let menu = LinuxMenu::new(item, command, placeholders, menu_mode);
menu.install()?;
println!("{:?}", menu.location());
println!("{:?}", menu.directories.config_directory);
Expand Down Expand Up @@ -334,42 +359,7 @@ pub fn install_menu_item(
// let doc =
// Element::parse(fs::read_to_string(&self.menu_config_location).unwrap().
// as_bytes()).unwrap(); doc.children.iter().any(|child| {
// if let XMLNode::Element(element) = child {
// if element.name == "Menu" {
// if let Some(name_element) = element.get_child("Name") {
// return name_element.get_text() ==
// Some(Self::render(&self.name, false)); }
// }
// }
// false
// })
// }

// fn add_this_menu(&self) {
// debug!("Editing {:?} to add {} config", self.menu_config_location,
// Self::render(&self.name, false)); let mut doc =
// Element::parse(fs::read_to_string(&self.menu_config_location).unwrap().
// as_bytes()).unwrap(); let mut menu_element = Element::new("Menu");
// add_xml_child(&mut menu_element, "Name", Self::render(&self.name,
// false)); add_xml_child(&mut menu_element, "Directory",
// format!("{}.directory", Self::render(&self.name, true))); let mut
// inc_element = Element::new("Include"); add_xml_child(&mut
// inc_element, "Category", Self::render(&self.name, false));
// menu_element.children.push(XMLNode::Element(inc_element));
// doc.children.push(XMLNode::Element(menu_element));
// self.write_menu_file(&doc);
// }

// fn is_valid_menu_file(&self) -> bool {
// if let Ok(content) = fs::read_to_string(&self.menu_config_location) {
// if let Ok(doc) = Element::parse(content.as_bytes()) {
// return doc.name == "Menu";
// }
// }
// false
// }

// fn write_menu_file(&self, doc: &Element) {
// if let XMLNode::Element(element) {
// debug!("Writing {:?}", self.menu_config_location);
// indent_xml_tree(doc);
// let mut file = File::create(&self.menu_config_location).unwrap();
Expand Down
29 changes: 18 additions & 11 deletions crates/rattler_menuinst/src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ use std::{
use fs_err as fs;
use fs_err::File;
use plist::Value;
use rattler_conda_types::Platform;
use rattler_shell::{
activation::{ActivationVariables, Activator},
shell,
};

use crate::{
render::{resolve, BaseMenuItemPlaceholders, MenuItemPlaceholders},
Expand Down Expand Up @@ -435,17 +440,19 @@ impl MacOSMenu {
lines.push(precommand.resolve(&self.placeholders));
}

// if self.command.activate {
// // Assuming these fields exist in your MacOS struct
// let conda_exe = &self.item.conda_exe;
// let prefix = &self.item.prefix;
// let activate = if self.is_micromamba(conda_exe) {
// "shell activate"
// } else {
// "shell.bash activate"
// };
// lines.push(format!(r#"eval "$("{}" {} "{}")""#, conda_exe, activate,
// prefix)); }
// Run a cached activation
if self.command.activate.unwrap_or(false) {
// create a bash activation script and emit it into the script
let activator =
Activator::from_path(&self.prefix, shell::Bash, Platform::current()).unwrap();
let activation_env = activator
.run_activation(ActivationVariables::default(), None)
.unwrap();

for (k, v) in activation_env {
lines.push(format!(r#"export {k}="{v}""#, k = k, v = v));
}
}

let command = self
.command
Expand Down

0 comments on commit ce30ab0

Please sign in to comment.