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.

102 lines
4.3 KiB

1 year ago
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using Microsoft.Win32;
  10. using TrustedUninstaller.Shared.Actions;
  11. using TrustedUninstaller.Shared.Tasks;
  12. using YamlDotNet.Serialization;
  13. using YamlDotNet.Serialization.NamingConventions;
  14. using YamlDotNet.Serialization.TypeResolvers;
  15. namespace TrustedUninstaller.Shared.Parser
  16. {
  17. public class TaskComparer : IComparer<UninstallTask> {
  18. public int Compare(UninstallTask x, UninstallTask y)
  19. {
  20. return ReferenceEquals(x, y) ? 0 : x.Priority.CompareTo(y.Priority);
  21. }
  22. }
  23. public class ConfigParser
  24. {
  25. public List<UninstallTask> Tasks { get; set; }
  26. private IDeserializer Deserializer { get; }
  27. private ISerializer Serializer { get; }
  28. public ConfigParser()
  29. {
  30. Tasks = new List<UninstallTask>();
  31. Deserializer = new DeserializerBuilder()
  32. .WithNamingConvention(CamelCaseNamingConvention.Instance)
  33. .WithTagMapping("!file", typeof(FileAction))
  34. .WithTagMapping("!service", typeof(ServiceAction))
  35. .WithTagMapping("!registryKey", typeof(RegistryKeyAction))
  36. .WithTagMapping("!registryValue", typeof(RegistryValueAction))
  37. .WithTagMapping("!appx", typeof(AppxAction))
  38. .WithTagMapping("!systemPackage", typeof(SystemPackageAction))
  39. .WithTagMapping("!lineInFile", typeof(LineInFileAction))
  40. .WithTagMapping("!scheduledTask", typeof(ScheduledTaskAction))
  41. .WithTagMapping("!user", typeof(UserAction))
  42. .WithTagMapping("!run", typeof(RunAction))
  43. .WithTagMapping("!powerShell", typeof(PowerShellAction))
  44. .WithTagMapping("!shortcut", typeof(ShortcutAction))
  45. .WithTagMapping("!cmd", typeof(CmdAction))
  46. .WithTagMapping("!uninstallTask", typeof(UninstallTask))
  47. .WithTagMapping("!taskKill", typeof(TaskKillAction))
  48. .WithTagMapping("!update", typeof(UpdateAction))
  49. .WithTagMapping("!writeStatus", typeof(WriteStatusAction))
  50. .WithNodeTypeResolver(new TaskActionResolver())
  51. .Build();
  52. Serializer = new SerializerBuilder()
  53. .WithNamingConvention(CamelCaseNamingConvention.Instance)
  54. .WithTagMapping("!file", typeof(FileAction))
  55. .WithTagMapping("!service", typeof(ServiceAction))
  56. .WithTagMapping("!registryKey", typeof(RegistryKeyAction))
  57. .WithTagMapping("!registryValue", typeof(RegistryValueAction))
  58. .WithTagMapping("!appx", typeof(AppxAction))
  59. .WithTagMapping("!systemPackage", typeof(SystemPackageAction))
  60. .WithTagMapping("!lineInFile", typeof(LineInFileAction))
  61. .WithTagMapping("!scheduledTask", typeof(ScheduledTaskAction))
  62. .WithTagMapping("!user", typeof(UserAction))
  63. .WithTagMapping("!run", typeof(RunAction))
  64. .WithTagMapping("!powerShell", typeof(PowerShellAction))
  65. .WithTagMapping("!shortcut", typeof(ShortcutAction))
  66. .WithTagMapping("!cmd", typeof(CmdAction))
  67. .WithTagMapping("!uninstallTask", typeof(UninstallTask))
  68. .WithTagMapping("!taskKill", typeof(TaskKillAction))
  69. .WithTagMapping("!update", typeof(UpdateAction))
  70. .WithTagMapping("!writeStatus", typeof(WriteStatusAction))
  71. .WithTypeResolver(new DynamicTypeResolver())
  72. .EnsureRoundtrip()
  73. .Build();
  74. }
  75. public void SerializeItem(TextWriter tw, object item)
  76. {
  77. Serializer.Serialize(tw, item);
  78. }
  79. public bool Add(string filename)
  80. {
  81. var configData = File.ReadAllText(filename);
  82. var taskData = Deserializer.Deserialize<UninstallTask>(configData);
  83. if (taskData.SupportedBuilds != null && !taskData.SupportedBuilds.Contains(Globals.WinVer.ToString()))
  84. {
  85. return false;
  86. }
  87. taskData.Update();
  88. Tasks.Add(taskData);
  89. return true;
  90. }
  91. }
  92. }