|
| 1 | + |
| 2 | +use std::error::Error; |
| 3 | +#[cfg(target_os = "windows")] |
| 4 | +use std::io::Write; |
| 5 | +#[cfg(target_os = "windows")] |
| 6 | +use std::fs::File; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +#[cfg(any(target_os = "macos", target_os = "linux"))] |
| 10 | +use std::os::unix::fs::symlink; |
| 11 | + |
| 12 | +use clap::ArgMatches; |
| 13 | +#[cfg(any(target_os = "macos", target_os = "linux"))] |
| 14 | +use simple_error::*; |
| 15 | +use simplelog::*; |
| 16 | + |
| 17 | +#[cfg(target_os = "macos")] |
| 18 | +use crate::macos::*; |
| 19 | + |
| 20 | +#[cfg(target_os = "windows")] |
| 21 | +use crate::windows::*; |
| 22 | + |
| 23 | +#[cfg(target_os = "linux")] |
| 24 | +use crate::linux::*; |
| 25 | + |
| 26 | +use crate::escalate::*; |
| 27 | + |
| 28 | +#[cfg(target_os = "macos")] |
| 29 | +pub fn get_alias(args: &ArgMatches) -> Option<String> { |
| 30 | + match args.value_of("str") { |
| 31 | + None => None, |
| 32 | + Some(str) => { |
| 33 | + match str { |
| 34 | + "oldrel" | "oldrel/1" => Some("oldrel".to_string()), |
| 35 | + "release" | "devel" | "next" => Some(str.to_string()), |
| 36 | + _ => None |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(target_os = "linux")] |
| 43 | +pub fn get_alias(args: &ArgMatches) -> Option<String> { |
| 44 | + match args.value_of("str") { |
| 45 | + None => None, |
| 46 | + Some(str) => { |
| 47 | + match str { |
| 48 | + "oldrel" | "oldrel/1" => Some("oldrel".to_string()), |
| 49 | + "release" => Some(str.to_string()), |
| 50 | + _ => None |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg(target_os = "windows")] |
| 57 | +pub fn get_alias(args: &ArgMatches) -> Option<String> { |
| 58 | + match args.value_of("str") { |
| 59 | + None => None, |
| 60 | + Some(str) => { |
| 61 | + match str { |
| 62 | + "oldrel" | "oldrel/1" => Some("oldrel".to_string()), |
| 63 | + "release" | "next" => Some(str.to_string()), |
| 64 | + _ => None |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(target_os = "macos")] |
| 71 | +pub fn add_alias(ver: &str, alias: &str) -> Result<(), Box<dyn Error>> { |
| 72 | + let msg = "Adding R-".to_string() + alias + " alias"; |
| 73 | + escalate(&msg)?; |
| 74 | + |
| 75 | + info!("Adding R-{} alias to R {}", alias, ver); |
| 76 | + |
| 77 | + let base = Path::new(R_ROOT); |
| 78 | + let target = base.join(ver).join("Resources/bin/R"); |
| 79 | + let linkfile = Path::new("/usr/local/bin/").join("R-".to_string() + alias); |
| 80 | + |
| 81 | + // If it exists then we check that it points to the right place |
| 82 | + // Cannot use .exists(), because it follows symlinks |
| 83 | + let meta = std::fs::symlink_metadata(&linkfile); |
| 84 | + if meta.is_ok() { |
| 85 | + match std::fs::read_link(&linkfile) { |
| 86 | + Err(_) => bail!("{} is not a symlink, aborting", linkfile.display()), |
| 87 | + Ok(xtarget) => { |
| 88 | + if xtarget == target { |
| 89 | + return Ok(()) |
| 90 | + } else { |
| 91 | + debug!("{} is wrong, updating", linkfile.display()); |
| 92 | + match std::fs::remove_file(&linkfile) { |
| 93 | + Err(err) => { |
| 94 | + bail!( |
| 95 | + "Failed to delete {}, cannot update alias: {}", |
| 96 | + linkfile.display(), |
| 97 | + err.to_string() |
| 98 | + ); |
| 99 | + }, |
| 100 | + _ => {} |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // If we are still here, then we need to create the link |
| 108 | + debug!("Adding {} -> {}", linkfile.display(), target.display()); |
| 109 | + match symlink(&target, &linkfile) { |
| 110 | + Err(err) => bail!( |
| 111 | + "Cannot create alias {}: {}", |
| 112 | + linkfile.display(), |
| 113 | + err.to_string() |
| 114 | + ), |
| 115 | + _ => {} |
| 116 | + }; |
| 117 | + |
| 118 | + Ok(()) |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(target_os = "windows")] |
| 122 | +pub fn add_alias(ver: &str, alias: &str) -> Result<(), Box<dyn Error>> { |
| 123 | + let msg = "Adding R-".to_string() + alias + " alias"; |
| 124 | + escalate(&msg)?; |
| 125 | + let base = Path::new(R_ROOT); |
| 126 | + let bin = base.join("bin"); |
| 127 | + |
| 128 | + // should exist at this point, but make sure |
| 129 | + std::fs::create_dir_all(&bin)?; |
| 130 | + |
| 131 | + let filename = "R-".to_string() + alias + ".bat"; |
| 132 | + let linkfile = bin.join(&filename); |
| 133 | + |
| 134 | + let cnt = "@\"C:\\Program Files\\R\\R-".to_string() + &ver + "\\bin\\R\" %*\n"; |
| 135 | + let op; |
| 136 | + if linkfile.exists() { |
| 137 | + op = "Updating"; |
| 138 | + let orig = std::fs::read_to_string(&linkfile)?; |
| 139 | + if orig == cnt { |
| 140 | + return Ok(()); |
| 141 | + } |
| 142 | + } else { |
| 143 | + op = "Adding"; |
| 144 | + }; |
| 145 | + info!("{} R-{} -> {} alias", op, alias, ver); |
| 146 | + let mut file = File::create(&linkfile)?; |
| 147 | + file.write_all(cnt.as_bytes())?; |
| 148 | + |
| 149 | + Ok(()) |
| 150 | +} |
| 151 | + |
| 152 | +#[cfg(target_os = "linux")] |
| 153 | +pub fn add_alias(ver: &str, alias: &str) -> Result<(), Box<dyn Error>> { |
| 154 | + let msg = "Adding R-".to_string() + alias + " alias"; |
| 155 | + escalate(&msg)?; |
| 156 | + |
| 157 | + info!("Adding R-{} alias to R {}", alias, ver); |
| 158 | + |
| 159 | + let base = Path::new(R_ROOT); |
| 160 | + let target = base.join(ver).join("bin/R"); |
| 161 | + let linkfile = Path::new("/usr/local/bin/").join("R-".to_string() + alias); |
| 162 | + |
| 163 | + // If it exists then we check that it points to the right place |
| 164 | + // Cannot use .exists(), because it follows symlinks |
| 165 | + let meta = std::fs::symlink_metadata(&linkfile); |
| 166 | + if meta.is_ok() { |
| 167 | + match std::fs::read_link(&linkfile) { |
| 168 | + Err(_) => bail!("{} is not a symlink, aborting", linkfile.display()), |
| 169 | + Ok(xtarget) => { |
| 170 | + if xtarget == target { |
| 171 | + return Ok(()) |
| 172 | + } else { |
| 173 | + debug!("{} is wrong, updating", linkfile.display()); |
| 174 | + match std::fs::remove_file(&linkfile) { |
| 175 | + Err(err) => { |
| 176 | + bail!( |
| 177 | + "Failed to delete {}, cannot update alias: {}", |
| 178 | + linkfile.display(), |
| 179 | + err.to_string() |
| 180 | + ); |
| 181 | + }, |
| 182 | + _ => {} |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // If we are still here, then we need to create the link |
| 190 | + debug!("Adding {} -> {}", linkfile.display(), target.display()); |
| 191 | + match symlink(&target, &linkfile) { |
| 192 | + Err(err) => bail!( |
| 193 | + "Cannot create alias {}: {}", |
| 194 | + linkfile.display(), |
| 195 | + err.to_string() |
| 196 | + ), |
| 197 | + _ => {} |
| 198 | + }; |
| 199 | + |
| 200 | + Ok(()) |
| 201 | +} |
0 commit comments