|
2 | 2 | using System; |
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.IO; |
| 5 | +using System.Reflection; |
5 | 6 | // ReSharper disable once RedundantUsingDirective |
6 | 7 | using System.Linq; |
7 | 8 | using System.Threading; |
@@ -61,6 +62,11 @@ public static void Main(string[] args) |
61 | 62 | { |
62 | 63 | StartingProject = args[0]; |
63 | 64 | } |
| 65 | + |
| 66 | + if (OperatingSystem.IsLinux()) |
| 67 | + { |
| 68 | + _ = SetupLinuxDesktopFile(); |
| 69 | + } |
64 | 70 |
|
65 | 71 | MainHost = Host.CreateDefaultBuilder(args) |
66 | 72 | .UseSerilog() |
@@ -161,6 +167,109 @@ await Dispatcher.UIThread.Invoke(async () => |
161 | 167 | }); |
162 | 168 | } |
163 | 169 |
|
| 170 | + private static async Task SetupLinuxDesktopFile() |
| 171 | + { |
| 172 | + await Task.Run(() => |
| 173 | + { |
| 174 | + var desktopPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
| 175 | + "applications"); |
| 176 | + if (!Directory.Exists(desktopPath)) |
| 177 | + { |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + var iconFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
| 182 | + "icons"); |
| 183 | + if (!Directory.Exists(iconFolderPath)) |
| 184 | + { |
| 185 | + Directory.CreateDirectory(iconFolderPath); |
| 186 | + } |
| 187 | + |
| 188 | + var iconPath = Path.Combine(Path.Combine(iconFolderPath, "MSUScripter.svg")); |
| 189 | + |
| 190 | + var appImagePath = Directory.EnumerateFiles(Environment.CurrentDirectory, "MSUScripter*.AppImage") |
| 191 | + .FirstOrDefault(); |
| 192 | + Log.Logger.Information("appImagePath: {Path}", appImagePath); |
| 193 | + if (string.IsNullOrEmpty(appImagePath) || !File.Exists(appImagePath)) |
| 194 | + { |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + var desktopFilePath = Path.Combine(desktopPath, "MSUScripter.desktop"); |
| 199 | + var uninstallFilePath = Path.Combine(Directories.BaseFolder, "uninstall.sh"); |
| 200 | + |
| 201 | + var assembly = Assembly.GetExecutingAssembly(); |
| 202 | + CopyIconFile(assembly, iconPath); |
| 203 | + CopyUninstallFile(assembly, appImagePath, iconPath, desktopFilePath, uninstallFilePath); |
| 204 | + CopyDesktopFile(assembly, appImagePath, iconPath, uninstallFilePath, desktopFilePath); |
| 205 | + }); |
| 206 | + } |
| 207 | + |
| 208 | + private static void CopyDesktopFile(Assembly assembly, string appImagePath, string iconPath, string uninstallPath, string targetPath) |
| 209 | + { |
| 210 | + const string resourceName = "MSUScripter.Assets.org.mattequalscoder.msuscripter.desktop"; |
| 211 | + using var stream = assembly.GetManifestResourceStream(resourceName); |
| 212 | + if (stream == null) |
| 213 | + { |
| 214 | + return; |
| 215 | + } |
| 216 | + using var reader = new StreamReader(stream); |
| 217 | + var desktopText = reader.ReadToEnd(); |
| 218 | + |
| 219 | + var workingDirectory = Path.GetDirectoryName(appImagePath); |
| 220 | + var fileName = Path.GetFileName(appImagePath); |
| 221 | + desktopText = desktopText.Replace("%FolderPath%", workingDirectory); |
| 222 | + desktopText = desktopText.Replace("%FileName%", fileName); |
| 223 | + desktopText = desktopText.Replace("%IconPath%", iconPath); |
| 224 | + desktopText = desktopText.Replace("%DesktopFilePath%", targetPath); |
| 225 | + desktopText = desktopText.Replace("%UninstallPath%", uninstallPath); |
| 226 | + File.WriteAllText(targetPath, desktopText); |
| 227 | + } |
| 228 | + |
| 229 | + private static void CopyUninstallFile(Assembly assembly, string appImagePath, string iconPath, string desktopFilePath, string targetPath) |
| 230 | + { |
| 231 | + if (!OperatingSystem.IsLinux()) |
| 232 | + { |
| 233 | + return; |
| 234 | + } |
| 235 | + |
| 236 | + const string resourceName = "MSUScripter.Assets.uninstall.sh"; |
| 237 | + using var stream = assembly.GetManifestResourceStream(resourceName); |
| 238 | + if (stream == null) |
| 239 | + { |
| 240 | + return; |
| 241 | + } |
| 242 | + using var reader = new StreamReader(stream); |
| 243 | + var fileText = reader.ReadToEnd(); |
| 244 | + |
| 245 | + var workingDirectory = Path.GetDirectoryName(appImagePath); |
| 246 | + var fileName = Path.GetFileName(appImagePath); |
| 247 | + fileText = fileText.Replace("%FolderPath%", workingDirectory); |
| 248 | + fileText = fileText.Replace("%FileName%", fileName); |
| 249 | + fileText = fileText.Replace("%IconPath%", iconPath); |
| 250 | + fileText = fileText.Replace("%DesktopFilePath%", desktopFilePath); |
| 251 | + fileText = fileText.Replace("%LocalDataPath%", Directories.BaseFolder); |
| 252 | + File.WriteAllText(targetPath, fileText); |
| 253 | + File.SetUnixFileMode(targetPath, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute | UnixFileMode.GroupExecute); |
| 254 | + } |
| 255 | + |
| 256 | + private static void CopyIconFile(Assembly assembly, string targetPath) |
| 257 | + { |
| 258 | + const string resourceName = "MSUScripter.Assets.icon.svg"; |
| 259 | + using var stream = assembly.GetManifestResourceStream(resourceName); |
| 260 | + if (stream == null) |
| 261 | + { |
| 262 | + return; |
| 263 | + } |
| 264 | + |
| 265 | + using var fileStream = new FileStream(Path.Combine(targetPath), FileMode.Create); |
| 266 | + for (var i = 0; i < stream.Length; i++) |
| 267 | + { |
| 268 | + fileStream.WriteByte((byte)stream.ReadByte()); |
| 269 | + } |
| 270 | + fileStream.Close(); |
| 271 | + } |
| 272 | + |
164 | 273 | #if DEBUG |
165 | 274 | private static string LogPath => Path.Combine(Directories.LogFolder, "msu-scripter-debug_.log"); |
166 | 275 | #else |
|
0 commit comments