CLI tool for running Playbooks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.1 KiB

1 year ago
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using TrustedUninstaller.Shared.Tasks;
  5. using YamlDotNet.Serialization;
  6. using IWshRuntimeLibrary;
  7. using File = System.IO.File;
  8. namespace TrustedUninstaller.Shared.Actions
  9. {
  10. class ShortcutAction : ITaskAction
  11. {
  12. [YamlMember(typeof(string), Alias = "path")]
  13. public string RawPath { get; set; }
  14. [YamlMember(typeof(string), Alias = "name")]
  15. public string Name { get; set; }
  16. [YamlMember(typeof(string), Alias = "destination")]
  17. public string Destination { get; set; }
  18. [YamlMember(typeof(string), Alias = "description")]
  19. public string Description { get; set; }
  20. [YamlMember(typeof(string), Alias = "weight")]
  21. public int ProgressWeight { get; set; } = 1;
  22. public int GetProgressWeight() => ProgressWeight;
  23. private bool InProgress { get; set; }
  24. public void ResetProgress() => InProgress = false;
  25. public string ErrorString() => $"ShortcutAction failed to create shortcut to '{Destination}' from '{RawPath}' with name {Name}.";
  26. public UninstallTaskStatus GetStatus()
  27. {
  28. //If the shortcut already exists return Completed
  29. return File.Exists(Path.Combine(this.Destination, this.Name + ".lnk")) ?
  30. UninstallTaskStatus.Completed : UninstallTaskStatus.ToDo;
  31. }
  32. public async Task<bool> RunTask()
  33. {
  34. RawPath = Environment.ExpandEnvironmentVariables(RawPath);
  35. Console.WriteLine($"Creating shortcut from '{Destination}' to '{RawPath}'...");
  36. if (File.Exists(this.RawPath))
  37. {
  38. WshShell shell = new WshShell();
  39. var sc = (IWshShortcut)shell.CreateShortcut(Path.Combine(this.Destination, this.Name + ".lnk"));
  40. sc.Description = this.Description;
  41. sc.TargetPath = this.RawPath;
  42. sc.Save();
  43. }
  44. else
  45. {
  46. throw new FileNotFoundException($"File '{RawPath}' not found.");
  47. }
  48. return true;
  49. }
  50. }
  51. }