Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ParseArguments.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System.Collections.Generic;
  2. namespace Unity.PlasticSCM.Editor.Hub
  3. {
  4. internal static class ParseArguments
  5. {
  6. internal class Command
  7. {
  8. internal enum Operation
  9. {
  10. None,
  11. DownloadRepository,
  12. CreateWorkspace
  13. }
  14. internal readonly string ProjectPath;
  15. internal readonly string Organization;
  16. internal readonly string Repository;
  17. internal readonly Operation OperationType;
  18. internal Command(
  19. string projectPath,
  20. string organization,
  21. string repository,
  22. Operation operationType)
  23. {
  24. ProjectPath = projectPath;
  25. Organization = organization;
  26. Repository = repository;
  27. OperationType = operationType;
  28. }
  29. internal bool IsValid()
  30. {
  31. return !string.IsNullOrEmpty(ProjectPath)
  32. && !string.IsNullOrEmpty(Organization)
  33. && !string.IsNullOrEmpty(Repository)
  34. && OperationType != Operation.None;
  35. }
  36. }
  37. internal static Command GetCommand(Dictionary<string, string> args)
  38. {
  39. return new Command(
  40. GetProjectPath(args),
  41. GetOrganization(args),
  42. GetRepository(args),
  43. GetOperation(args));
  44. }
  45. internal static string GetProjectPath(Dictionary<string, string> args)
  46. {
  47. string data;
  48. if (args.TryGetValue(CREATE_PROJECT, out data))
  49. return data;
  50. if (args.TryGetValue(OPEN_PROJECT, out data))
  51. return data;
  52. return null;
  53. }
  54. internal static string GetOrganization(Dictionary<string, string> args)
  55. {
  56. string data;
  57. if (args.TryGetValue(UVCSArguments.ORGANIZATION_ARG, out data))
  58. return data;
  59. // -cloudOrganization 151d73c7-38cb-4eec-b11e-34764e707226-{plastic-org-name}
  60. if (args.TryGetValue(CloudArguments.ORGANIZATION_ARG, out data))
  61. return GetOrganizationNameFromData(
  62. data, CloudArguments.PLASTIC_ORG_PREFIX_VALUE);
  63. return null;
  64. }
  65. internal static string GetRepository(Dictionary<string, string> args)
  66. {
  67. string data;
  68. if (args.TryGetValue(UVCSArguments.REPOSITORY_ARG, out data))
  69. return data;
  70. if (args.TryGetValue(CloudArguments.PROJECT_ARG, out data))
  71. return data;
  72. return null;
  73. }
  74. static Command.Operation GetOperation(Dictionary<string, string> args)
  75. {
  76. if (IsCreateWorkspaceCommand(args))
  77. return Command.Operation.CreateWorkspace;
  78. if (IsDownloadRepositoryCommand(args))
  79. return Command.Operation.DownloadRepository;
  80. return Command.Operation.None;
  81. }
  82. static string GetOrganizationNameFromData(string data, string plasticOrgPrefix)
  83. {
  84. if (data == null)
  85. return null;
  86. if (!data.StartsWith(plasticOrgPrefix))
  87. return null;
  88. if (data.Length <= plasticOrgPrefix.Length)
  89. return null;
  90. return data.Substring(plasticOrgPrefix.Length);
  91. }
  92. static bool IsCreateWorkspaceCommand(Dictionary<string, string> args)
  93. {
  94. // Connect UVCS to new project or existing project commands:
  95. // -createProject/-projectPath {project_path}
  96. // -uvcsRepository {plastic_repo}
  97. // -uvcsOrganization {plastic_org}
  98. // -uvcsCreateWorkspace
  99. if (!args.ContainsKey(CREATE_PROJECT) &&
  100. !args.ContainsKey(OPEN_PROJECT))
  101. return false;
  102. return args.ContainsKey(UVCSArguments.ORGANIZATION_ARG)
  103. && args.ContainsKey(UVCSArguments.REPOSITORY_ARG)
  104. && args.ContainsKey(UVCSArguments.CREATE_WORKSPACE_FLAG);
  105. }
  106. static bool IsDownloadRepositoryCommand(Dictionary<string, string> args)
  107. {
  108. // Open remote project command:
  109. // -createProject {project_path}
  110. // -cloudProject {plastic_repo}
  111. // -cloudOrganization 151d73c7-38cb-4eec-b11e-34764e707226-{plastic_org}
  112. if (!args.ContainsKey(CREATE_PROJECT) ||
  113. !args.ContainsKey(CloudArguments.PROJECT_ARG))
  114. return false;
  115. string data;
  116. if (!args.TryGetValue(CloudArguments.ORGANIZATION_ARG, out data))
  117. return false;
  118. return data != null && data.StartsWith(
  119. CloudArguments.PLASTIC_ORG_PREFIX_VALUE);
  120. }
  121. static class UVCSArguments
  122. {
  123. internal const string ORGANIZATION_ARG = "-uvcsOrganization";
  124. internal const string REPOSITORY_ARG = "-uvcsRepository";
  125. internal const string CREATE_WORKSPACE_FLAG = "-uvcsCreateWorkspace";
  126. }
  127. static class CloudArguments
  128. {
  129. internal const string ORGANIZATION_ARG = "-cloudOrganization";
  130. internal const string PLASTIC_ORG_PREFIX_VALUE = "151d73c7-38cb-4eec-b11e-34764e707226-";
  131. internal const string PROJECT_ARG = "-cloudProject";
  132. }
  133. const string CREATE_PROJECT = "-createProject";
  134. const string OPEN_PROJECT = "-projectPath";
  135. }
  136. }