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.

64 lines
2.2 KiB

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