상속: System.Exception
예제 #1
0
        public void Execute()
        {
            try
            {
                SafeExecute();
            }
            catch (Exception e)
            {
                if (e is ContentFileException)
                {
                    ContentFileException contentEx = (ContentFileException)e;

                    Output.Error(contentEx.FileName, contentEx.LineNumber, 0, e.Message);

                    while ((e = e.InnerException) != null)
                    {
                        string message = e.Message;

                        if (e is XmlException)
                        {
                            int n = message.IndexOf("file://");

                            if (n != -1)
                            {
                                message = message.Substring(0, n);
                            }
                        }

                        Output.Error(contentEx.FileName, contentEx.LineNumber, 0, message);

#if DEBUG
                        Console.WriteLine(e.StackTrace);
#endif
                    }
                }
                else
                {
                    Output.Error(e.Message);
                }
            }
        }
예제 #2
0
        private void SafeExecute()
        {
            if (!NoLogo)
            {
                Console.WriteLine(Parser.LogoBanner);
            }

            if (!runningFromCommandLine)
            {
                Parser.GetTargetArguments(this);
                Output.Message(MessageImportance.Normal, Parser.CommandName + Parser.Arguments);
            }

            if (ShowHelp)
            {
                Console.WriteLine(Parser.Usage);
                return;
            }

            if (String.IsNullOrEmpty(ContentPath))
            {
                Output.Error("A .content file must be specified");
                return;
            }

            this.ContentPath = this.ContentPath.MakeFullPath();

            if (!File.Exists(this.ContentPath))
            {
                Output.Error("Content file '{0}' does not exist", this.ContentPath);
                return;
            }

            // Initialize properties from the environment and command line
            PropertyGroup globalProps = new PropertyGroup();

            globalProps.AddFromEnvironment();
            globalProps.AddWellKnownProperties(
                new ParsedPath(Assembly.GetExecutingAssembly().Location, PathType.File).VolumeAndDirectory,
                ContentPath.VolumeAndDirectory);
            globalProps.AddFromPropertyString(this.Properties);

            BuildContext buildContext = new BuildContext(this.Output, this.ContentPath);

            ContentFileV2 contentFile = null;

            try
            {
                contentFile = ContentFileReaderV2.ReadFile(this.ContentPath);
            }
            catch (Exception e)
            {
                throw new ContentFileException(this.ContentPath, (int)e.Data["LineNumber"], "Problem reading content file", e);
            }

            Output.Message(MessageImportance.Low, "Read content file '{0}'", this.ContentPath);

            ItemGroup globalItems = new ItemGroup();

            globalItems.ExpandAndAddFromList(contentFile.Items, globalProps);

            List <CompilerClass> compilerClasses = LoadCompilerClasses(globalItems, globalProps);

            this.NewestAssemblyWriteTime = FindNewestAssemblyWriteTime(compilerClasses);
            this.ContentPathWriteTime    = File.GetLastWriteTime(this.ContentPath);

            List <BuildTarget> BuildTargets = PrepareBuildTargets(contentFile.Targets, globalItems, globalProps);

            foreach (var buildTarget in BuildTargets)
            {
                bool compilerFound = false;

                foreach (var compilerClass in compilerClasses)
                {
                    if (buildTarget.InputExtensions.SequenceEqual(compilerClass.InputExtensions) &&
                        buildTarget.OutputExtensions.SequenceEqual(compilerClass.OutputExtensions))
                    {
                        compilerFound = true;

                        string msg = String.Format("Building target '{0}' with '{1}' compiler", buildTarget.Name, compilerClass.Name);

                        foreach (var input in buildTarget.InputFiles)
                        {
                            msg += Environment.NewLine + "\t" + input;
                        }

                        msg += Environment.NewLine + "\t->";

                        foreach (var output in buildTarget.OutputFiles)
                        {
                            msg += Environment.NewLine + "\t" + output;
                        }

                        Output.Message(MessageImportance.Normal, msg);

                        if (TestMode)
                        {
                            continue;
                        }

                        compilerClass.ContextProperty.SetValue(compilerClass.Instance, buildContext, null);
                        compilerClass.TargetProperty.SetValue(compilerClass.Instance, buildTarget, null);

                        try
                        {
                            compilerClass.CompileMethod.Invoke(compilerClass.Instance, null);
                        }
                        catch (TargetInvocationException e)
                        {
                            ContentFileException contentEx = e.InnerException as ContentFileException;

                            if (contentEx != null)
                            {
                                contentEx.EnsureFileNameAndLineNumber(buildContext.ContentFile, buildTarget.LineNumber);
                                throw contentEx;
                            }
                            else
                            {
                                throw new ContentFileException(
                                          this.ContentPath, buildTarget.LineNumber, "Unable to compile target '{0}'".CultureFormat(buildTarget.Name), e.InnerException);
                            }
                        }

                        // Ensure that the output files were generated
                        foreach (var outputFile in buildTarget.OutputFiles)
                        {
                            if (!File.Exists(outputFile))
                            {
                                throw new ContentFileException(this.ContentPath, buildTarget.LineNumber,
                                                               "Output file '{0}' was not generated".CultureFormat(outputFile));
                            }
                        }
                    }
                }

                if (!compilerFound)
                {
                    Output.Warning("No compiler found for target '{0}' for extensions '{1}' -> '{2}'",
                                   buildTarget.Name,
                                   String.Join(Path.PathSeparator.ToString(), buildTarget.InputExtensions),
                                   String.Join(Path.PathSeparator.ToString(), buildTarget.OutputExtensions));
                }
            }

            Output.Message(MessageImportance.Normal, "Done");
        }