Example #1
0
        private IEnumerable <ILogger> GetLoggers(ConsoleForwardingLogger consoleLogger)
        {
            if (consoleLogger != null)
            {
                yield return(consoleLogger);
            }

            if (_arguments.FileLoggerParameters.HasValue)
            {
                yield return(new FileLogger
                {
                    Parameters = _arguments.FileLoggerParameters.Arguments.IsNullOrWhiteSpace() ? "LogFile=slngen.log;Verbosity=Detailed" : $"LogFile=slngen.log;{_arguments.FileLoggerParameters.Arguments}",
                });
            }

            if (_arguments.BinaryLogger.HasValue)
            {
                foreach (ILogger logger in ForwardingLogger.ParseBinaryLoggerParameters(_arguments.BinaryLogger.Arguments.IsNullOrWhiteSpace() ? "slngen.binlog" : _arguments.BinaryLogger.Arguments))
                {
                    yield return(logger);
                }
            }

            foreach (ILogger logger in ForwardingLogger.ParseLoggerParameters(_arguments.Loggers))
            {
                yield return(logger);
            }
        }
Example #2
0
        /// <summary>
        /// Executes the program.
        /// </summary>
        /// <returns>The exit code of the program.</returns>
        public int Execute()
        {
            LoggerVerbosity verbosity = ForwardingLogger.ParseLoggerVerbosity(_arguments.Verbosity?.LastOrDefault());

            ConsoleForwardingLogger consoleLogger = new ConsoleForwardingLogger(_console)
            {
                NoWarn     = _arguments.NoWarn,
                Parameters = _arguments.ConsoleLoggerParameters.Arguments.IsNullOrWhiteSpace() ? "ForceNoAlign=true;Summary" : _arguments.ConsoleLoggerParameters.Arguments,
                Verbosity  = verbosity,
            };

            ForwardingLogger forwardingLogger = new ForwardingLogger(GetLoggers(consoleLogger), _arguments.NoWarn)
            {
                Verbosity = verbosity,
            };

            using (ProjectCollection projectCollection = new ProjectCollection(
                       globalProperties: null,
                       loggers: new ILogger[]
            {
                forwardingLogger,
            },
                       remoteLoggers: null,
                       toolsetDefinitionLocations: ToolsetDefinitionLocations.Default,
                       maxNodeCount: 1,
#if NET46
                       onlyLogCriticalEvents: false))
#else
                       onlyLogCriticalEvents : false,
                       loadProjectsReadOnly : true))
#endif
            {
                try
                {
                    forwardingLogger.LogMessageLow("Command Line Arguments: {0}", Environment.CommandLine);

                    forwardingLogger.LogMessageLow("Using MSBuild from \"{0}\"", _msbuildExePath);

                    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().Where(i => i.FullName.StartsWith("Microsoft.Build")))
                    {
                        forwardingLogger.LogMessageLow("Loaded assembly: \"{0}\" from \"{1}\"", assembly.FullName, assembly.Location);
                    }

                    (TimeSpan evaluationTime, int evaluationCount) = LoadProjects(projectCollection, forwardingLogger);

                    if (forwardingLogger.HasLoggedErrors)
                    {
                        return(1);
                    }

                    (string solutionFileFullPath, int customProjectTypeGuidCount, int solutionItemCount) = GenerateSolutionFile(projectCollection.LoadedProjects.Where(i => !i.GlobalProperties.ContainsKey("TargetFramework")), forwardingLogger);

                    if (_arguments.ShouldLaunchVisualStudio())
                    {
                        bool loadProjectsInVisualStudio = _arguments.ShouldLoadProjectsInVisualStudio();
                        bool enableShellExecute         = _arguments.EnableShellExecute();

                        string devEnvFullPath = _arguments.DevEnvFullPath?.LastOrDefault();

                        if (!enableShellExecute || !loadProjectsInVisualStudio || IsCorext)
                        {
                            if (_instance == null)
                            {
                                forwardingLogger.LogError("Cannot launch Visual Studio.");

                                return(1);
                            }

                            if (_instance.IsBuildTools)
                            {
                                forwardingLogger.LogError("Cannot use a BuildTools instance of Visual Studio.");

                                return(1);
                            }

                            devEnvFullPath = Path.Combine(_instance.InstallationPath, "Common7", "IDE", "devenv.exe");
                        }

                        VisualStudioLauncher.Launch(solutionFileFullPath, loadProjectsInVisualStudio, devEnvFullPath, forwardingLogger);
                    }

                    try
                    {
                        LogTelemetry(evaluationTime, evaluationCount, customProjectTypeGuidCount, solutionItemCount);
                    }
                    catch (Exception)
                    {
                    }
                }
                catch (Exception e)
                {
                    forwardingLogger.LogError($"Unhandled exception: {e}");
                    throw;
                }
            }

            return(0);
        }