Exemplo n.º 1
0
        /// <summary>
        /// Wrapped main program, uses <see cref="ConsoleException"/> as return code in case of
        /// error and does not wait at the end.
        /// </summary>
        private static void MainWrapper()
        {
            CommandLineHelper cmdLine        = new CommandLineHelper();
            var showHelpOption               = cmdLine.RegisterOption("help").Alias("h", "?");
            var showVersionOption            = cmdLine.RegisterOption("version").Alias("ver");
            var debugOption                  = cmdLine.RegisterOption("debug");
            var patchAssemblyInfoOption      = cmdLine.RegisterOption("patch");
            var restorePatchedFilesOption    = cmdLine.RegisterOption("restore");
            var simpleAttributeOption        = cmdLine.RegisterOption("simple");
            var informationalAttributeOption = cmdLine.RegisterOption("info");
            var noCopyrightAttributeOption   = cmdLine.RegisterOption("nocopyright");
            var echoOption             = cmdLine.RegisterOption("echo");
            var formatOption           = cmdLine.RegisterOption("format", 1);
            var revisionOnlyOption     = cmdLine.RegisterOption("revonly");
            var requireVcsOption       = cmdLine.RegisterOption("require", 1);
            var rejectModifiedOption   = cmdLine.RegisterOption("rejectmod").Alias("rejectmodified");
            var rejectMixedOption      = cmdLine.RegisterOption("rejectmix").Alias("rejectmixed");
            var tagMatchOption         = cmdLine.RegisterOption("tagmatch", 1);
            var removeTagVOption       = cmdLine.RegisterOption("removetagv");
            var multiProjectOption     = cmdLine.RegisterOption("multi");
            var scanRootOption         = cmdLine.RegisterOption("root");
            var decodeRevisionOption   = cmdLine.RegisterOption("decode", 1);
            var predictRevisionsOption = cmdLine.RegisterOption("predict");

            try
            {
                //cmdLine.ReadArgs(Environment.CommandLine, true);   // Alternative split method, should have the same result
                cmdLine.Parse();
                showDebugOutput = debugOption.IsSet;
                if (showDebugOutput)
                {
                    ShowDebugMessage(
                        "Command line: " +
                        Environment.GetCommandLineArgs()
                        .Select(s => "[" + s + "]")
                        .Aggregate((a, b) => a + " " + b));
                }
            }
            catch (Exception ex)
            {
                throw new ConsoleException(ex.Message, ExitCodes.CmdLineError);
            }

            // Handle simple text output options
            if (showHelpOption.IsSet)
            {
                ShowHelp();
                return;
            }
            if (showVersionOption.IsSet)
            {
                ShowVersion();
                return;
            }

            // Check for environment variable from PowerShell build framework.
            // If psbuild has set this variable, it is using .NET Revision Tool itself in
            // multi-project mode and pre/postbuild actions in individual projects should not do
            // anything on their own.
            if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("SuppressNetRevisionTool")))
            {
                ShowDebugMessage("SuppressNetRevisionTool environment variable is set. Quitting…");
                return;
            }

            // Find all directories
            string path = GetWorkPath(cmdLine);

            string[] projectDirs = null;
            if (multiProjectOption.IsSet)
            {
                // Read solution file and collect all projects
                projectDirs = GetProjectsFromSolution(path);

                // From now on, work with the solution directory as default path to get the revision of
                if (Path.GetExtension(path).ToLowerInvariant() == ".sln")
                {
                    path = Path.GetDirectoryName(path);
                }
            }
            else
            {
                if (!Directory.Exists(path))
                {
                    throw new ConsoleException("The specified project directory does not exist.", ExitCodes.FileNotFound);
                }

                projectDirs = new[] { path };
            }

            // Restoring doesn't need more info, do it now
            if (restorePatchedFilesOption.IsSet)
            {
                // Restore AssemblyInfo file(s)
                foreach (string projectDir in projectDirs)
                {
                    var aih = new AssemblyInfoHelper(projectDir, true);
                    aih.RestoreFile();
                }
                return;
            }

            // Setup public data
            if (tagMatchOption.IsSet)
            {
                TagMatch = tagMatchOption.Value;
            }
            RemoveTagV = removeTagVOption.IsSet;

            // Analyse working directory
            RevisionData data = ProcessDirectory(path, scanRootOption.IsSet, requireVcsOption.Value);

            data.Normalize();

            // Check for required VCS
            if (requireVcsOption.IsSet)
            {
                if (data.VcsProvider == null ||
                    !data.VcsProvider.Name.Equals(requireVcsOption.Value, StringComparison.OrdinalIgnoreCase))
                {
                    throw new ConsoleException("Required VCS \"" + requireVcsOption.Value + "\" not present.", ExitCodes.RequiredVcs);
                }
            }

            // Check for reject modifications/mixed revisions
            if (rejectModifiedOption.IsSet && data.IsModified)
            {
                throw new ConsoleException("The working directory contains uncommitted modifications.", ExitCodes.RejectModified);
            }
            if (rejectMixedOption.IsSet && data.IsMixed)
            {
                throw new ConsoleException("The working directory contains mixed revisions.", ExitCodes.RejectMixed);
            }

            // Determine revision ID format, in case we need one here
            string format = null;

            if (formatOption.IsSet && !string.IsNullOrWhiteSpace(formatOption.Value))
            {
                // Take from command-line option
                format = formatOption.Value;
                ShowDebugMessage("Format specified: " + format);
            }
            else
            {
                // None or empty specified. Search in AssemblyInfo file(s) in the project(s)
                ShowDebugMessage("No format specified, searching AssemblyInfo source file.");
                AssemblyInfoHelper aih = null;
                foreach (string projectDir in projectDirs)
                {
                    aih = new AssemblyInfoHelper(projectDir, false);
                    if (aih.FileExists)
                    {
                        format = aih.GetRevisionFormat();
                        if (format != null)
                        {
                            if (projectDirs.Length > 1)
                            {
                                ShowDebugMessage("Found format in project \"" + projectDir + "\".");
                            }
                            break;
                        }
                    }
                    else
                    {
                        ShowDebugMessage("  AssemblyInfo source file not found.", 2);
                    }
                }
                if (format != null)
                {
                    ShowDebugMessage("Found format: " + format);
                }
            }

            if (format == null)
            {
                if (data.RevisionNumber > 0)
                {
                    ShowDebugMessage("No format available, using default format for revision number.");
                    format = "{revnum}";
                }
                else if (!string.IsNullOrEmpty(data.CommitHash) && !Regex.IsMatch(data.CommitHash, "^0+$"))
                {
                    ShowDebugMessage("No format available, using default format for commit hash.");
                    format = "{chash:8}";
                }
                else
                {
                    ShowDebugMessage("No format available, using empty format.");
                    format = "";
                }
            }

            if (decodeRevisionOption.IsSet)
            {
                // Decode specified revision ID
                RevisionFormat.ShowDecode(format, decodeRevisionOption.Value);
            }
            else if (predictRevisionsOption.IsSet)
            {
                // Predict next revision IDs
                RevisionFormat.PredictValue(format);
            }
            else if (patchAssemblyInfoOption.IsSet)
            {
                // Patch AssemblyInfo file(s)
                bool noAttrSet              = !simpleAttributeOption.IsSet && !informationalAttributeOption.IsSet;
                bool simpleAttributes       = simpleAttributeOption.IsSet || noAttrSet;
                bool informationalAttribute = informationalAttributeOption.IsSet || noAttrSet;

                foreach (string projectDir in projectDirs)
                {
                    var aih = new AssemblyInfoHelper(projectDir, true);
                    aih.PatchFile(format, data, simpleAttributes, informationalAttribute, revisionOnlyOption.IsSet, !noCopyrightAttributeOption.IsSet, echoOption.IsSet);
                }
            }
            else
            {
                // Just display revision ID
                var rf = new RevisionFormat();
                rf.RevisionData = data;
                Console.WriteLine(rf.Resolve(format));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Wrapped main program, uses <see cref="ConsoleException"/> as return code in case of
        /// error and does not wait at the end.
        /// </summary>
        private static void MainWrapper()
        {
            CommandLineHelper cmdLine = new CommandLineHelper();
            var showHelpOption = cmdLine.RegisterOption("help").Alias("h", "?");
            var showVersionOption = cmdLine.RegisterOption("version").Alias("ver");
            var debugOption = cmdLine.RegisterOption("debug");
            var patchAssemblyInfoOption = cmdLine.RegisterOption("patch");
            var restorePatchedFilesOption = cmdLine.RegisterOption("restore");
            var simpleAttributeOption = cmdLine.RegisterOption("simple");
            var informationalAttributeOption = cmdLine.RegisterOption("info");
            var allAttributesOption = cmdLine.RegisterOption("all");
            var formatOption = cmdLine.RegisterOption("format", 1);
            var revisionOnlyOption = cmdLine.RegisterOption("revonly");
            var requireVcsOption = cmdLine.RegisterOption("require", 1);
            var rejectModifiedOption = cmdLine.RegisterOption("rejectmod").Alias("rejectmodified");
            var rejectMixedOption = cmdLine.RegisterOption("rejectmix").Alias("rejectmixed");
            var multiProjectOption = cmdLine.RegisterOption("multi");
            var scanRootOption = cmdLine.RegisterOption("root");
            var decodeRevisionOption = cmdLine.RegisterOption("decode", 1);
            var predictRevisionsOption = cmdLine.RegisterOption("predict");

            try
            {
                //cmdLine.ReadArgs(Environment.CommandLine, true);   // Alternative split method, should have the same result
                cmdLine.Parse();
                showDebugOutput = debugOption.IsSet;
                if (showDebugOutput)
                {
                    ShowDebugMessage(
                        "Command line: " +
                            Environment.GetCommandLineArgs()
                                .Select(s => "[" + s + "]")
                                .Aggregate((a, b) => a + " " + b));
                }
            }
            catch (Exception ex)
            {
                throw new ConsoleException(ex.Message, ExitCodes.CmdLineError);
            }

            // Handle simple text output options
            if (showHelpOption.IsSet)
            {
                ShowHelp();
                return;
            }
            if (showVersionOption.IsSet)
            {
                ShowVersion();
                return;
            }

            // Check for environment variable from PowerShell build framework.
            // If psbuild has set this variable, it is using NetRevisionTool itself in multi-project
            // mode and pre/postbuild actions in individual projects should not do anything on their
            // own.
            if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("SuppressNetRevisionTool")))
            {
                ShowDebugMessage("SuppressNetRevisionTool environment variable is set. Quitting…");
                return;
            }

            // Find all directories
            string path = GetWorkPath(cmdLine);
            string[] projectDirs = null;
            if (multiProjectOption.IsSet)
            {
                // Read solution file and collect all projects
                projectDirs = GetProjectsFromSolution(path);

                // From now on, work with the solution directory as default path to get the revision of
                if (Path.GetExtension(path).ToLowerInvariant() == ".sln")
                {
                    path = Path.GetDirectoryName(path);
                }
            }
            else
            {
                if (!Directory.Exists(path))
                    throw new ConsoleException("The specified project directory does not exist.", ExitCodes.FileNotFound);

                projectDirs = new[] { path };
            }

            // Restoring doesn't need more info, do it now
            if (restorePatchedFilesOption.IsSet)
            {
                // Restore AssemblyInfo file(s)
                foreach (string projectDir in projectDirs)
                {
                    var aih = new AssemblyInfoHelper(projectDir, true);
                    aih.RestoreFile();
                }
                return;
            }

            // Analyse working directory
            RevisionData data = ProcessDirectory(path, scanRootOption.IsSet, requireVcsOption.Value);
            data.Normalize();

            // Check for required VCS
            if (requireVcsOption.IsSet)
            {
                if (data.VcsProvider == null ||
                    !data.VcsProvider.Name.Equals(requireVcsOption.Value, StringComparison.OrdinalIgnoreCase))
                {
                    throw new ConsoleException("Required VCS \"" + requireVcsOption.Value + "\" not present.", ExitCodes.RequiredVcs);
                }
            }

            // Check for reject modifications/mixed revisions
            if (rejectModifiedOption.IsSet && data.IsModified)
            {
                throw new ConsoleException("The working directory contains uncommitted modifications.", ExitCodes.RejectModified);
            }
            if (rejectMixedOption.IsSet && data.IsMixed)
            {
                throw new ConsoleException("The working directory contains mixed revisions.", ExitCodes.RejectMixed);
            }

            // Determine revision ID format, in case we need one here
            string format = null;
            if (formatOption.IsSet && !string.IsNullOrWhiteSpace(formatOption.Value))
            {
                // Take from command-line option
                format = formatOption.Value;
                ShowDebugMessage("Format specified: " + format);
            }
            else
            {
                // None or empty specified. Search in AssemblyInfo file(s) in the project(s)
                ShowDebugMessage("No format specified, searching AssemblyInfo source file.");
                AssemblyInfoHelper aih = null;
                foreach (string projectDir in projectDirs)
                {
                    aih = new AssemblyInfoHelper(projectDir, false);
                    if (aih.FileExists)
                    {
                        format = aih.GetRevisionFormat();
                        if (format != null)
                        {
                            if (projectDirs.Length > 1)
                            {
                                ShowDebugMessage("Found format in project \"" + projectDir + "\".");
                            }
                            break;
                        }
                    }
                    else
                    {
                        ShowDebugMessage("  AssemblyInfo source file not found.", 2);
                    }
                }
                if (format != null)
                {
                    ShowDebugMessage("Found format: " + format);
                }
            }

            if (format == null)
            {
                if (data.RevisionNumber > 0)
                {
                    ShowDebugMessage("No format available, using default format for revision number.");
                    format = "{revnum}";
                }
                else if (!string.IsNullOrEmpty(data.CommitHash) && !Regex.IsMatch(data.CommitHash, "^0+$"))
                {
                    ShowDebugMessage("No format available, using default format for commit hash.");
                    format = "{chash:8}";
                }
                else
                {
                    ShowDebugMessage("No format available, using empty format.");
                    format = "";
                }
            }

            if (decodeRevisionOption.IsSet)
            {
                // Decode specified revision ID
                RevisionFormat.ShowDecode(format, decodeRevisionOption.Value);
            }
            else if (predictRevisionsOption.IsSet)
            {
                // Predict next revision IDs
                RevisionFormat.PredictValue(format);
            }
            else if (patchAssemblyInfoOption.IsSet)
            {
                // Patch AssemblyInfo file(s)
                bool noAttrSet = !simpleAttributeOption.IsSet && !informationalAttributeOption.IsSet;
                bool simpleAttributes = simpleAttributeOption.IsSet || noAttrSet;
                bool informationalAttribute = informationalAttributeOption.IsSet || noAttrSet;

                foreach (string projectDir in projectDirs)
                {
                    var aih = new AssemblyInfoHelper(projectDir, true);
                    aih.PatchFile(format, data, simpleAttributes, informationalAttribute, revisionOnlyOption.IsSet);
                }
            }
            else
            {
                // Just display revision ID
                var rf = new RevisionFormat();
                rf.RevisionData = data;
                Console.WriteLine(rf.Resolve(format));
            }
        }