Пример #1
0
        /// <summary>
        /// Runs the compiler with specified options.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        /// <returns>Whether the compilation was successful.</returns>
        public bool Compile(List <string> /*!*/ args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            TextErrorSink errorSink = null;

            // processes arguments:
            try
            {
                try
                {
                    commandLineParser.Parse(args);
                }
                finally
                {
                    if (commandLineParser.Quiet)
                    {
                        output = errors = TextWriter.Null;
                    }
                    else if (commandLineParser.RedirectErrors)
                    {
                        output = errors = Console.Out;
                    }
                    else
                    {
                        output = Console.Out;
                        errors = Console.Error;
                    }

                    errorSink = new TextErrorSink(errors);

                    ShowLogo();

                    if (commandLineParser.ShowHelp)
                    {
                        ShowHelp();
                    }
                    else
                    {
                        DumpArguments(args);
                    }
                }
            }
            catch (InvalidCommandLineArgumentException e)
            {
                e.Report(errorSink);
                return(false);
            }

            if (commandLineParser.ShowHelp)
            {
                return(true);
            }

            // allow loading of all assemblies in /Bin directory by their FullName
            HandleAssemblies(Path.Combine(commandLineParser.Parameters.SourceRoot, "Bin"));

            //
            ApplicationContext.DefineDefaultContext(false, true, false);
            ApplicationContext app_context = ApplicationContext.Default;

            CompilerConfiguration compiler_config;

            // loads entire configuration:
            try
            {
                if (commandLineParser.Parameters.ConfigPaths.Count == 0)
                {
                    // Add config files for known targets
                    switch (commandLineParser.Parameters.Target)
                    {
                    case ApplicationCompiler.Targets.Web:
                        if (File.Exists("web.config"))
                        {
                            commandLineParser.Parameters.ConfigPaths.Add(new FullPath("web.config"));
                        }
                        break;

                    case ApplicationCompiler.Targets.WinApp:
                        if (File.Exists("app.config"))
                        {
                            commandLineParser.Parameters.ConfigPaths.Add(new FullPath("app.config"));
                        }
                        break;
                    }
                }
                compiler_config = ApplicationCompiler.LoadConfiguration(app_context,
                                                                        commandLineParser.Parameters.ConfigPaths, output);

                commandLineParser.Parameters.ApplyToConfiguration(compiler_config);
            }
            catch (ConfigurationErrorsException e)
            {
                if (commandLineParser.Verbose)
                {
                    output.WriteLine(CoreResources.GetString("reading_configuration") + ":");
                    output.WriteLine();

                    if (!String.IsNullOrEmpty(e.Filename))                     // Mono puts here null
                    {
                        output.WriteLine(FileSystemUtils.ReadFileLine(e.Filename, e.Line).Trim());
                        output.WriteLine();
                    }
                }

                errorSink.AddConfigurationError(e);
                return(false);
            }

            // load referenced assemblies:
            try
            {
                try
                {
                    app_context.AssemblyLoader.Load(commandLineParser.Parameters.References);
                }
                finally
                {
                    if (commandLineParser.Verbose)
                    {
                        DumpLoadedLibraries();
                    }
                }
            }
            catch (ConfigurationErrorsException e)
            {
                errorSink.AddConfigurationError(e);
                return(false);
            }

            output.WriteLine(CoreResources.GetString("performing_compilation") + " ...");

            try
            {
                CommandLineParser p = commandLineParser;
                Statistics.DrawGraph = p.DrawInclusionGraph;

                errorSink.DisabledGroups        = compiler_config.Compiler.DisabledWarnings;
                errorSink.DisabledWarnings      = compiler_config.Compiler.DisabledWarningNumbers;
                errorSink.TreatWarningsAsErrors = compiler_config.Compiler.TreatWarningsAsErrors;

                // initializes log:
                DebugUtils.ConsoleInitialize(Path.GetDirectoryName(p.Parameters.OutPath));

                new ApplicationCompiler().Compile(app_context, compiler_config, errorSink, p.Parameters);
            }
            catch (InvalidSourceException e)
            {
                e.Report(errorSink);
                return(false);
            }
            catch (Exception e)
            {
                errorSink.AddInternalError(e);
                return(false);
            }

            var errorscount  = errorSink.ErrorCount + errorSink.FatalErrorCount;
            var warningcount = errorSink.WarningCount + errorSink.WarningAsErrorCount;

            output.WriteLine();
            output.WriteLine("Build complete -- {0} error{1}, {2} warning{3}.",
                             errorscount, (errorscount == 1) ? "" : "s",
                             warningcount, (warningcount == 1) ? "" : "s");

            return(!errorSink.AnyError);
        }