public void Patch([Description("version control type ")] [DefaultValue(VersionControl.Automatic)] [Aliases("vc")] VersionControl versionControl, [Description(@"version control path: ($/Barak/Dev/Main)")] [Required] string vcPath, [Description(@"Url for the version control server")] [Required] string vcUrl, [Description(@"revision id, default will be latest")] [DefaultValue(null)] string revision, [Description(@"file system checkout root directory")] [Required] string fsPath, [Description("user name to use with git")] [DefaultValue(null)] string username, [Description("password to use with git")] [DefaultValue(null)] string password, [Description("comment to check-in")] [DefaultValue("VersionPathcer auto increment")] string comment, [Description("recursivly change projects depends on updated projects")] [DefaultValue(true)] bool recursive, [Description("commit changes when done")] [DefaultValue(true)] bool commit, [Description("what part of the version to incress")] [DefaultValue(VersionPart.Build)] VersionPart versionPart, [Description("path to the project(.csproj) file, leave empty to search the folder for all projects")] [DefaultValue(null)] string[] projectFiles) { PatchInfo = new PatchInfo() { ForceVersionControlType = versionControl == VersionControl.Automatic ? null : versionControl.ToString(), Comment = comment, FileSystemPath = fsPath, Revision = revision, ProjectFiles = projectFiles, SourceControlUrl = new Uri(vcUrl), VersionControlPath = vcPath, Commit = commit, Recursive = recursive, VersionPart = versionPart, Username = username, Passowrd = password, }; }
public void Patch(PatchInfo patchInfo) { using (ISourceControl sourceControl = GetSourceControl(patchInfo)) { sourceControl.Connect(patchInfo.SourceControlUrl); var maxRevision = sourceControl.GetRevisionById(patchInfo.Revision); var minRevision = sourceControl.GetRevisionOfItem(maxRevision, System.IO.Path.Combine(patchInfo.FileSystemPath, VersionPathcerConsts.VersionControlFile)); IEnumerable<IRevision> revisions = sourceControl.GetRevisions(minRevision, maxRevision); var changeFiles = revisions.Select(p => new ChangeFiles() { FullPath = System.IO.Path.Combine(patchInfo.FileSystemPath, p.Item), }).ToDictionary(p => p.FullPath,new StringIgnoreCaseEqualityComarer()); if (revisions.Count() != 0) { var allProjects = GetAllProjects(patchInfo, changeFiles); var changeProjects = GetDirectlyChangedProjects(changeFiles); if (patchInfo.Recursive == true) { AddIndirectChangedProjects(allProjects, changeProjects); } var fileToModify = GetAssemblyInfoFilesToUpdage(changeProjects); if (fileToModify.Any()) { UpgradeAssemblyFiles(fileToModify, sourceControl, patchInfo); if (patchInfo.Commit) { sourceControl.Commit(patchInfo.Comment); } else { sourceControl.Complete(); } } } } }
private void UpgradeAssemblyFiles(IEnumerable<string> fileToModify,ISourceControl sourceControl,PatchInfo patchInfo) { Regex regex = null; Regex regexFileVersion = null; if (fileToModify.Any()) { regex = new Regex(@"assembly: AssemblyVersion\(""(\d+)\.(\d+)\.(\d+)\.(\d+)""\)",RegexOptions.Compiled); regexFileVersion = new Regex(@"assembly: AssemblyFileVersion\(""(\d+)\.(\d+)\.(\d+)\.(\d+)""\)", RegexOptions.Compiled); } List<string> filesModified = new List<string>(); foreach (var fileAssemblySetting in fileToModify) { var fileText = System.IO.File.ReadAllText(fileAssemblySetting); string newFile = fileText; var match = regex.Match(newFile); bool isCheckout = false; newFile = UpdateAssemblyFile(sourceControl, patchInfo.VersionPart, match, fileAssemblySetting, newFile, ref isCheckout); match = regexFileVersion.Match(newFile); newFile = UpdateAssemblyFile(sourceControl, patchInfo.VersionPart, match, fileAssemblySetting, newFile, ref isCheckout); if (isCheckout) { filesModified.Add(fileAssemblySetting); System.IO.File.WriteAllText(fileAssemblySetting, newFile); } } var versionControlFile = Path.Combine(patchInfo.FileSystemPath, VersionPathcerConsts.VersionControlFile); if (System.IO.File.Exists(versionControlFile)) { sourceControl.Checkout(versionControlFile); System.IO.File.WriteAllText(versionControlFile, string.Format("{0}{1}{2}", DateTime.Now, Environment.NewLine, string.Join(Environment.NewLine, filesModified))); } else { System.IO.File.WriteAllText(versionControlFile, string.Format("{0}{1}{2}", DateTime.Now, Environment.NewLine, string.Join(Environment.NewLine, filesModified))); sourceControl.Checkout(versionControlFile); } }
private ISourceControl GetSourceControl(PatchInfo patchInfo) { var sourceControlProvider = m_sourceControlProvider; ISourceControl sourceControl = null; if (patchInfo.ForceVersionControlType != null) { sourceControl = sourceControlProvider.GetSourceControlByType(patchInfo.ForceVersionControlType, patchInfo.VersionControlPath); } else { sourceControl = sourceControlProvider.GetSourceControlByPath(patchInfo.VersionControlPath); } if (sourceControl == null) { throw new Exception("Source Control not found"); } return sourceControl; }
private List<CSProject> GetAllProjects(PatchInfo patchInfo, Dictionary<string, ChangeFiles> changeFiles) { string[] projectPaths = null; if (patchInfo.ProjectFiles != null && patchInfo.ProjectFiles.Length != 0) { projectPaths = patchInfo.ProjectFiles; } else { projectPaths = System.IO.Directory.GetFiles(patchInfo.FileSystemPath, "*.csproj", SearchOption.AllDirectories); } projectPaths = projectPaths.Where(p => System.IO.File.Exists(p)).ToArray(); List<CSProject> allProjects = new List<CSProject>(); foreach (var projectPath in projectPaths) { CSProject csProject = null; string projectDir = System.IO.Path.GetDirectoryName(projectPath); using (var projectStream = System.IO.File.OpenRead(projectPath)) { csProject = ProjectFileTypeToCSProject.Convert(System.IO.Path.GetFileName(projectPath), ProjectFileParser.ParseFile(projectStream)); csProject.FullPath = projectPath; // Add self if it has been changed. ChangeFiles changeProject; if (changeFiles.TryGetValue(csProject.FullPath, out changeProject)) { changeProject.Projects.Add(csProject); } foreach (File currFile in csProject.Files) { try { var fullpath = Path.GetFullPath(Path.Combine(projectDir, currFile.Path)); ChangeFiles changeFile; if (changeFiles.TryGetValue(fullpath,out changeFile)) { changeFile.Projects.Add(csProject); } } catch (Exception) { // Some times the path generation is failing it is ok, it is just unsupported format } } // Q: What happends When the referance is full and not relative? // A: Hope it wont crash. foreach (Referance currReferance in csProject.Referances) { if (!string.IsNullOrEmpty(currReferance.Path)) { ChangeFiles changeFile; if (changeFiles.TryGetValue(Path.GetFullPath(Path.Combine(projectDir, currReferance.Path)), out changeFile)) { changeFile.Projects.Add(csProject); } } } } allProjects.Add(csProject); } return allProjects; }