/Code/Foundation/Build/VersionControl/Vault/VaultVersionControlProvider.cs

https://github.com/DavidMoore/Foundation · C# · 175 lines · 132 code · 27 blank · 16 comment · 17 complexity · a8bb4ce7a3c5730888e16d851c4d09cb MD5 · raw file

  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. using Foundation.ExtensionMethods;
  6. using Foundation.Services;
  7. using Foundation.Windows;
  8. namespace Foundation.Build.VersionControl.Vault
  9. {
  10. public class VaultVersionControlProvider : BaseCommandLineVersionControlProvider
  11. {
  12. public VaultVersionControlProvider(string fileName) : base(fileName) {}
  13. public override IServiceResult ParseResult(VersionControlArguments args, IProcessResult processResult)
  14. {
  15. var serializer = new VaultResultSerializer();
  16. var vaultResult = serializer.Deserialize(processResult.StandardOutput);
  17. if (!vaultResult.Success)
  18. {
  19. return new ServiceResult(ServiceResultCode.Error);
  20. }
  21. var result = new ServiceResult(ServiceResultCode.Success);
  22. switch (args.Operation)
  23. {
  24. case VersionControlOperation.GetLocalVersion:
  25. // Find the file we were trying to get the version for
  26. foreach (var file in vaultResult.Folder.Files)
  27. {
  28. if (args.SourcePath.EndsWith(file.Name))
  29. {
  30. result.ResultValue = file.Version;
  31. }
  32. }
  33. break;
  34. default:
  35. throw new ArgumentOutOfRangeException();
  36. }
  37. return result;
  38. }
  39. /// <summary>
  40. /// Builds the command line arguments.
  41. /// </summary>
  42. /// <param name="arguments">The arguments.</param>
  43. /// <returns></returns>
  44. public override string BuildCommandLineArguments(VersionControlArguments arguments)
  45. {
  46. var sb = new StringBuilder();
  47. // What command are we doing?
  48. sb.Append(OperationToVaultCommand(arguments));
  49. sb.Append(" -host \"").Append(arguments.Server).Append('"');
  50. // Add the security credentials if specified
  51. if (arguments.Credentials != null)
  52. {
  53. sb.Append(" -user \"").Append(arguments.Credentials.UserName).Append('"');
  54. sb.Append(" -password \"").Append(arguments.Credentials.Password).Append('"');
  55. }
  56. // The Project argument acts as the repository name
  57. sb.Append(" -repository \"").Append(arguments.Project).Append('"');
  58. // If we're getting a label and need a destination path, add that as an argument.
  59. if (arguments.Operation == VersionControlOperation.Get
  60. && !arguments.Label.IsNullOrEmpty()
  61. && !arguments.DestinationPath.IsNullOrEmpty())
  62. {
  63. sb.Append(" -destpath \"");
  64. // The destination needs to be a folder, so if this is a filename, get the folder instead.
  65. if( arguments.DestinationPath.Contains("/") || arguments.DestinationPath.Contains("\\") )
  66. {
  67. sb.Append(Path.GetDirectoryName(arguments.DestinationPath));
  68. }
  69. else
  70. {
  71. sb.Append(arguments.DestinationPath);
  72. }
  73. sb.Append('"');
  74. }
  75. // If the argument is the version, then add that to the arguments.
  76. if (!arguments.Version.IsNullOrEmpty()) sb.Append(" ").Append(arguments.Version);
  77. // The source path / file
  78. if (!arguments.SourcePath.IsNullOrEmpty())
  79. {
  80. sb.Append(" \"$");
  81. // Remove the repository root prefix ($) and / as we've already added it
  82. var sourcePath = arguments.SourcePath.TrimStart('$', '/', '\\').Replace('\\', '/');
  83. // If this is a GetLocalVersion operation, we can only list versions of all
  84. // files in a folder and not a specific file version.
  85. switch(arguments.Operation)
  86. {
  87. case VersionControlOperation.GetLocalVersion:
  88. sb.Append(Path.GetDirectoryName(sourcePath));
  89. break;
  90. default:
  91. sb.Append(sourcePath);
  92. break;
  93. }
  94. sb.Append('"');
  95. }
  96. // Destination
  97. if( !arguments.DestinationPath.IsNullOrEmpty())
  98. {
  99. // If we're getting a label, the destination will have already been specified.
  100. if (arguments.Operation != VersionControlOperation.Get || arguments.Label.IsNullOrEmpty())
  101. {
  102. sb.Append(" \"").Append(arguments.DestinationPath).Append('"');
  103. }
  104. }
  105. // The label
  106. if (!arguments.Label.IsNullOrEmpty()) sb.Append(" \"").Append(arguments.Label).Append('"');
  107. return sb.ToString();
  108. }
  109. public override string MapVersionControlSourcePath(string localPath, VersionControlArguments args)
  110. {
  111. if( args.DestinationPath.IsNullOrEmpty()) throw new ArgumentException("The DestinationPath is empty, so we cannot map a version control file if we have no working path.", "args");
  112. if (!localPath.StartsWith(args.DestinationPath,StringComparison.CurrentCultureIgnoreCase)) return null;
  113. var relativePath = localPath.Substring(args.DestinationPath.Length);
  114. // Ensure the path starts with a leading forward slash, removing
  115. // other slashes and the repository root character ($)
  116. relativePath = relativePath.TrimStart('$', '\\');
  117. if(!relativePath.StartsWith("/")) relativePath = "/" + relativePath;
  118. // Return a folder path, not a file path
  119. var result = Path.GetDirectoryName(relativePath);
  120. // Convert all back slashes to forward slashes
  121. return result.Replace(@"\", "/");
  122. }
  123. internal static string OperationToVaultCommand(VersionControlArguments args)
  124. {
  125. switch (args.Operation)
  126. {
  127. case VersionControlOperation.None:
  128. throw new ArgumentException("None is not a valid Vault version control operation", "operation");
  129. case VersionControlOperation.Get:
  130. // Are we getting a label or a version?
  131. return !args.Label.IsNullOrEmpty() ? "getlabel" : "getversion";
  132. case VersionControlOperation.GetLocalVersion:
  133. return "listfolder";
  134. default:
  135. throw new ArgumentOutOfRangeException("operation");
  136. }
  137. }
  138. }
  139. }