/// <summary>
		/// Patches the file and injects the revision data.
		/// </summary>
		/// <param name="fallbackFormat">The fallback format if none is defined in the file.</param>
		/// <param name="data">The revision data to use for resolving formats.</param>
		/// <param name="simpleAttributes">Indicates whether simple version attributes are processed.</param>
		/// <param name="informationalAttribute">Indicates whether the AssemblyInformationalVersion attribute is processed.</param>
		/// <param name="revOnly">Indicates whether only the last number is replaced by the revision number.</param>
		/// <param name="copyrightAttribute">Indicates whether the copyright year is replaced.</param>
		public void PatchFile(string fallbackFormat, RevisionData data, bool simpleAttributes, bool informationalAttribute, bool revOnly, bool copyrightAttribute)
		{
			Program.ShowDebugMessage("Patching file \"" + fileName + "\"…");
			string backupFileName = CreateBackup();

			// Read the backup file. If the backup was created earlier, it still contains the source
			// file while the regular file may have been resolved but not restored before. By
			// reading the former source file, we get the correct result and can heal the situation
			// with the next restore run.
			ReadFileLines(backupFileName);

			// Find the revision format for this file
			revisionFormat = FindRevisionFormat();
			if (revisionFormat == null)
			{
				// Nothing defined in this file. Use whatever was specified on the command line or
				// found in any of the projects in the solution.
				revisionFormat = fallbackFormat;
			}
			else
			{
				Program.ShowDebugMessage("The file defines a revision format: " + revisionFormat);
			}
			if (revisionFormat == null)
			{
				// If we don't have a revision format, there's nothing to replace in this file.
				return;
			}
			var rf = new RevisionFormat();
			rf.RevisionData = data;

			// Process all lines in the file
			ResolveAllLines(rf, simpleAttributes, informationalAttribute, revOnly, copyrightAttribute);

			// Write back all lines to the file
			WriteFileLines();
		}
Example #2
0
        /// <summary>
        /// Processes the specified directory with a suitable VCS provider.
        /// </summary>
        /// <param name="path">The directory to process.</param>
        /// <param name="scanRoot">true if the working directory root shall be scanned instead of <paramref name="path"/>.</param>
        /// <param name="requiredVcs">The required VCS name, or null if any VCS is acceptable.</param>
        /// <returns>Data about the revision. If no provider was able to process the directory,
        ///   dummy data is returned.</returns>
        private static RevisionData ProcessDirectory(string path, bool scanRoot, string requiredVcs)
        {
            RevisionData data = null;

            // Try to process the directory with all available VCS providers
            ShowDebugMessage("Processing directory…");
            foreach (IVcsProvider provider in GetVcsProviders())
            {
                ShowDebugMessage("Found VCS provider: " + provider);

                if (!string.IsNullOrEmpty(requiredVcs) &&
                    !provider.Name.Equals(requiredVcs, StringComparison.OrdinalIgnoreCase))
                {
                    ShowDebugMessage("Provider is not what is required, skipping.");
                    continue;
                }

                if (provider.CheckEnvironment())
                {
                    ShowDebugMessage("Provider can be executed in this environment.", 1);
                    string rootPath;
                    if (provider.CheckDirectory(path, out rootPath))
                    {
                        ShowDebugMessage("Provider can process this directory.", 1);
                        if (scanRoot)
                        {
                            ShowDebugMessage("Root directory will be scanned.", 0);
                            path = rootPath;
                        }
                        data = provider.ProcessDirectory(path);
                        break;
                    }
                }
            }

            if (data == null)
            {
                // No provider could process the directory, return dummy data
                ShowDebugMessage("No provider used, returning dummy data.", 2);
                data = new RevisionData
                {
                    CommitHash = "0000000000000000000000000000000000000000",
                    CommitTime = DateTimeOffset.Now,
                    IsModified = false,
                    RevisionNumber = 0
                };
            }

            data.DumpData();
            return data;
        }