Geen omschrijving
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.

ParseArguments.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 WorkspacePath;
  16. internal readonly string Organization;
  17. internal readonly string Repository;
  18. internal readonly Operation OperationType;
  19. internal Command(
  20. string projectPath,
  21. string workspacePath,
  22. string organization,
  23. string repository,
  24. Operation operationType)
  25. {
  26. ProjectPath = projectPath;
  27. WorkspacePath = workspacePath;
  28. Organization = organization;
  29. Repository = repository;
  30. OperationType = operationType;
  31. }
  32. internal bool IsValid()
  33. {
  34. return !string.IsNullOrEmpty(ProjectPath)
  35. && !string.IsNullOrEmpty(Organization)
  36. && !string.IsNullOrEmpty(Repository)
  37. && OperationType != Operation.None;
  38. }
  39. internal bool HasWorkspacePath()
  40. {
  41. return !string.IsNullOrEmpty(WorkspacePath);
  42. }
  43. }
  44. internal static Command GetCommand(Dictionary<string, string> args)
  45. {
  46. return new Command(
  47. GetProjectPath(args),
  48. GetWorkspacePath(args),
  49. GetOrganization(args),
  50. GetRepository(args),
  51. GetOperation(args));
  52. }
  53. internal static string GetProjectPath(Dictionary<string, string> args)
  54. {
  55. string data;
  56. if (args.TryGetValue(CREATE_PROJECT, out data))
  57. return data;
  58. if (args.TryGetValue(OPEN_PROJECT, out data))
  59. return data;
  60. return null;
  61. }
  62. internal static string GetOrganization(Dictionary<string, string> args)
  63. {
  64. string data;
  65. if (args.TryGetValue(UVCSArguments.ORGANIZATION_ARG, out data))
  66. return data;
  67. // -cloudOrganization 151d73c7-38cb-4eec-b11e-34764e707226-{plastic-org-name}
  68. if (args.TryGetValue(CloudArguments.ORGANIZATION_ARG, out data))
  69. return GetOrganizationNameFromData(
  70. data, CloudArguments.PLASTIC_ORG_PREFIX_VALUE);
  71. return null;
  72. }
  73. internal static string GetRepository(Dictionary<string, string> args)
  74. {
  75. string data;
  76. if (args.TryGetValue(UVCSArguments.REPOSITORY_ARG, out data))
  77. return data;
  78. if (args.TryGetValue(CloudArguments.PROJECT_ARG, out data))
  79. return data;
  80. return null;
  81. }
  82. internal static string GetWorkspacePath(Dictionary<string, string> args)
  83. {
  84. string data;
  85. if (args.TryGetValue(UVCSArguments.WORKSPACE_PATH_ARG, out data))
  86. return data;
  87. return null;
  88. }
  89. static Command.Operation GetOperation(Dictionary<string, string> args)
  90. {
  91. if (IsCreateWorkspaceCommand(args))
  92. return Command.Operation.CreateWorkspace;
  93. if (IsDownloadRepositoryCommand(args))
  94. return Command.Operation.DownloadRepository;
  95. return Command.Operation.None;
  96. }
  97. static string GetOrganizationNameFromData(string data, string plasticOrgPrefix)
  98. {
  99. if (data == null)
  100. return null;
  101. if (!data.StartsWith(plasticOrgPrefix))
  102. return null;
  103. if (data.Length <= plasticOrgPrefix.Length)
  104. return null;
  105. return data.Substring(plasticOrgPrefix.Length);
  106. }
  107. static bool IsCreateWorkspaceCommand(Dictionary<string, string> args)
  108. {
  109. // Connect UVCS to new project or existing project commands:
  110. // -createProject/-projectPath {project_path}
  111. // -uvcsRepository {plastic_repo}
  112. // -uvcsOrganization {plastic_org}
  113. // -uvcsCreateWorkspace
  114. if (!args.ContainsKey(CREATE_PROJECT) &&
  115. !args.ContainsKey(OPEN_PROJECT))
  116. return false;
  117. return args.ContainsKey(UVCSArguments.ORGANIZATION_ARG)
  118. && args.ContainsKey(UVCSArguments.REPOSITORY_ARG)
  119. && args.ContainsKey(UVCSArguments.CREATE_WORKSPACE_FLAG);
  120. }
  121. static bool IsDownloadRepositoryCommand(Dictionary<string, string> args)
  122. {
  123. return IsDownloadRepositoryCommandUsingLegacyArgs(args)
  124. || IsDownloadRepositoryCommandUsingUVCSArgs(args);
  125. }
  126. static bool IsDownloadRepositoryCommandUsingLegacyArgs(Dictionary<string, string> args)
  127. {
  128. // Open remote project command using legacy args:
  129. // -createProject {project_path}
  130. // -cloudProject {plastic_repo}
  131. // -cloudOrganization 151d73c7-38cb-4eec-b11e-34764e707226-{plastic_org}
  132. if (!args.ContainsKey(CREATE_PROJECT) ||
  133. !args.ContainsKey(CloudArguments.PROJECT_ARG))
  134. return false;
  135. string data;
  136. if (!args.TryGetValue(CloudArguments.ORGANIZATION_ARG, out data))
  137. return false;
  138. return data != null && data.StartsWith(
  139. CloudArguments.PLASTIC_ORG_PREFIX_VALUE);
  140. }
  141. static bool IsDownloadRepositoryCommandUsingUVCSArgs(Dictionary<string, string> args)
  142. {
  143. // Open remote project command using UVCS args:
  144. // -createProject {project_path}
  145. // -uvcsRepository {plastic_repo}
  146. // -uvcsOrganization {plastic_org_name}
  147. if (!args.ContainsKey(CREATE_PROJECT))
  148. return false;
  149. return args.ContainsKey(UVCSArguments.ORGANIZATION_ARG)
  150. && args.ContainsKey(UVCSArguments.REPOSITORY_ARG);
  151. }
  152. static class UVCSArguments
  153. {
  154. internal const string ORGANIZATION_ARG = "-uvcsOrganization";
  155. internal const string REPOSITORY_ARG = "-uvcsRepository";
  156. internal const string WORKSPACE_PATH_ARG = "-uvcsWorkspacePath";
  157. internal const string CREATE_WORKSPACE_FLAG = "-uvcsCreateWorkspace";
  158. }
  159. static class CloudArguments
  160. {
  161. internal const string ORGANIZATION_ARG = "-cloudOrganization";
  162. internal const string PLASTIC_ORG_PREFIX_VALUE = "151d73c7-38cb-4eec-b11e-34764e707226-";
  163. internal const string PROJECT_ARG = "-cloudProject";
  164. }
  165. const string CREATE_PROJECT = "-createProject";
  166. const string OPEN_PROJECT = "-projectPath";
  167. }
  168. }