bool GetAppDomainAndExecute()
        {
            AppDomain appDomain = null;

            try
            {
                appDomain = CreateNewAppDomain();
                bool ret = ExecuteInternal(appDomain);
                return(ret);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                XamlBuildTaskServices.LogException(this.Log, e.Message);
                return(false);
            }
            finally
            {
                if (appDomain != null)
                {
                    AppDomain.Unload(appDomain);
                }
            }
        }
Пример #2
0
        public override bool Execute()
        {
            AppDomain appDomain = null;

            try
            {
                ValidateRequiredDev11Properties();

                appDomain = XamlBuildTaskServices.CreateAppDomain("CompilationPass2AppDomain_" + Guid.NewGuid(), BuildTaskPath);

                CompilationPass2TaskInternal wrapper = (CompilationPass2TaskInternal)appDomain.CreateInstanceAndUnwrap(
                    Assembly.GetExecutingAssembly().FullName,
                    typeof(CompilationPass2TaskInternal).FullName);

                PopulateBuildArtifacts(wrapper);

                bool ret = wrapper.Execute();

                ExtractBuiltArtifacts(wrapper);

                if (!ret)
                {
                    foreach (LogData logData in wrapper.LogData)
                    {
                        XamlBuildTaskServices.LogException(
                            this.Log,
                            logData.Message,
                            logData.FileName,
                            logData.LineNumber,
                            logData.LinePosition);
                    }
                }

                return(ret);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                XamlBuildTaskServices.LogException(this.Log, e.Message);
                return(false);
            }
            finally
            {
                if (appDomain != null)
                {
                    AppDomain.Unload(appDomain);
                }
            }
        }
        bool ReuseAppDomainAndExecute()
        {
            AppDomain appDomain           = null;
            bool      createdNewAppDomain = false;

            try
            {
                try
                {
                    appDomain = GetInProcessAppDomain(out createdNewAppDomain);
                    bool ret = ExecuteInternal(appDomain);
                    return(ret);
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    if (createdNewAppDomain)
                    {
                        XamlBuildTaskServices.LogException(this.Log, e.Message);
                        return(false);
                    }
                    else
                    {
                        if (inProcessAppDomain != null)
                        {
                            AppDomain.Unload(inProcessAppDomain);
                            inProcessAppDomain = null;
                        }
                        return(GetAppDomainAndExecute());
                    }
                }
            }
            finally
            {
                if (Log != null)
                {
                    Log.MarkAsInactive();
                }
            }
        }
Пример #4
0
        XamlNodeList ReadXamlNodes(string xamlFileName)
        {
            XamlNodeList nodeList = new XamlNodeList(this.SchemaContext);

            try
            {
                XamlXmlReaderSettings settings = new XamlXmlReaderSettings
                {
                    AllowProtectedMembersOnRoot = true,
                    ProvideLineInfo             = true
                };

                using (StreamReader streamReader = new StreamReader(xamlFileName))
                {
                    XamlReader reader = new XamlXmlReader(XmlReader.Create(streamReader, new XmlReaderSettings {
                        XmlResolver = null
                    }), this.SchemaContext, settings);
                    XamlServices.Transform(reader, nodeList.Writer);
                }
            }
            catch (XmlException e)
            {
                XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, xamlFileName, e.LineNumber, e.LinePosition);
                return(null);
            }
            catch (XamlException e)
            {
                XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, xamlFileName, e.LineNumber, e.LinePosition);
                return(null);
            }

            if (nodeList.Count > 0)
            {
                return(nodeList);
            }
            else
            {
                return(null);
            }
        }
        public bool Execute()
        {
            try
            {
                if ((!this.SupportExtensions) && ((this.ApplicationMarkup == null) || this.ApplicationMarkup.Count == 0))
                {
                    return(true);
                }
                else if (this.ApplicationMarkupWithTypeName == null || this.ApplicationMarkupWithTypeName.Count == 0)
                {
                    return(true);
                }

                IList <Assembly> loadedAssemblyList = null;
                if (this.References != null)
                {
                    loadedAssemblyList = XamlBuildTaskServices.Load(this.References, false);
                }

                Assembly localAssembly = null;
                if (LocalAssemblyReference != null)
                {
                    try
                    {
                        localAssembly = XamlBuildTaskServices.Load(LocalAssemblyReference);
                        loadedAssemblyList.Add(localAssembly);
                    }
                    catch (FileNotFoundException e)
                    {
                        XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, e.FileName, 0, 0);
                        return(false);
                    }
                }

                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(XamlBuildTaskServices.ReflectionOnlyAssemblyResolve);
                XamlNsReplacingContext wxsc = new XamlNsReplacingContext(loadedAssemblyList, localAssembly.GetName().Name, this.AssemblyName);

                bool foundValidationErrors = false;
                if (!this.SupportExtensions)
                {
                    foreach (string app in ApplicationMarkup)
                    {
                        try
                        {
                            if (!ProcessMarkupItem(app, wxsc, localAssembly))
                            {
                                foundValidationErrors = true;
                            }
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e))
                            {
                                throw;
                            }
                            XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, app, 0, 0);
                            return(false);
                        }
                    }
                }
                else
                {
                    foreach (ITaskItem app in this.ApplicationMarkupWithTypeName.Values)
                    {
                        string inputMarkupFile = app.ItemSpec;
                        try
                        {
                            if (!ProcessMarkupItem(inputMarkupFile, wxsc, localAssembly))
                            {
                                foundValidationErrors = true;
                            }
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e))
                            {
                                throw;
                            }
                            XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, inputMarkupFile, 0, 0);
                            return(false);
                        }
                    }
                    if (!foundValidationErrors)
                    {
                        foundValidationErrors = !ExecuteExtensions();
                        if (!foundValidationErrors)
                        {
                            foundValidationErrors = this.BuildLogger.HasLoggedErrors;
                        }
                    }
                }
                return(!foundValidationErrors);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                // Log unknown errors that do not originate from the task.
                // Assumes that all known errors are logged when the exception is thrown.
                XamlBuildTaskServices.LogException(this.BuildLogger, e.Message);
                return(false);
            }
        }
Пример #6
0
        public bool Execute()
        {
            try
            {
                if (this.ApplicationMarkup == null || this.ApplicationMarkup.Count == 0)
                {
                    return(true);
                }
                if (!CodeDomProvider.IsDefinedLanguage(this.Language))
                {
                    throw FxTrace.Exception.Argument("Language", SR.UnrecognizedLanguage(this.Language));
                }

                if (this.SupportExtensions)
                {
                    this.xamlBuildTypeGenerationExtensions = XamlBuildTaskServices.GetXamlBuildTaskExtensions <IXamlBuildTypeGenerationExtension>(
                        this.XamlBuildTaskTypeGenerationExtensionNames,
                        this.BuildLogger,
                        this.MSBuildProjectDirectory);
                }

                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(XamlBuildTaskServices.ReflectionOnlyAssemblyResolve);
                bool retVal = true;
                // We load the assemblies for the real builds
                // For intellisense builds, we load them the first time only
                if (!IsInProcessXamlMarkupCompile || this.LoadedAssemblyList == null)
                {
                    if (this.References != null)
                    {
                        try
                        {
                            this.LoadedAssemblyList = XamlBuildTaskServices.Load(this.References, IsInProcessXamlMarkupCompile);
                        }
                        catch (FileNotFoundException e)
                        {
                            XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, e.FileName, 0, 0);
                            retVal = false;
                        }
                    }
                }

                CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider(this.Language);

                ProcessHelperClassGeneration(codeDomProvider);
                foreach (ITaskItem app in ApplicationMarkup)
                {
                    string inputMarkupFile = app.ItemSpec;
                    try
                    {
                        retVal &= ProcessMarkupItem(app, codeDomProvider);
                    }
                    catch (LoggableException e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, e.Source, e.LineNumber, e.LinePosition);
                        retVal = false;
                    }
                    catch (FileLoadException e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }

                        XamlBuildTaskServices.LogException(this.BuildLogger, SR.AssemblyCannotBeResolved(XamlBuildTaskServices.FileNotLoaded), inputMarkupFile, 0, 0);
                        retVal = false;
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, inputMarkupFile, 0, 0);
                        retVal = false;
                    }
                }

                // Add the files generated from extensions
                if (this.SupportExtensions)
                {
                    if (retVal)
                    {
                        foreach (string fileName in this.BuildContextForExtensions.GeneratedFiles)
                        {
                            this.GeneratedCodeFiles.Add(fileName);
                        }

                        foreach (string fileName in this.BuildContextForExtensions.GeneratedResourceFiles)
                        {
                            this.GeneratedResources.Add(fileName);
                        }
                    }
                }

                return(retVal);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                // Log unknown errors that do not originate from the task.
                // Assumes that all known errors are logged when the exception is thrown.
                if (!(e is LoggableException))
                {
                    XamlBuildTaskServices.LogException(this.BuildLogger, e.Message);
                }
                return(false);
            }
        }
        public override bool Execute()
        {
            bool retVal;

            try
            {
                XDocument projectDocument = XDocument.Load(this.CurrentProject);
                if (projectDocument != null)
                {
                    XElement projectElement = projectDocument.Element(XName.Get("Project", MSBuildNamespace));
                    if (projectElement != null)
                    {
                        RemoveItemsByName(projectElement, this.ApplicationMarkupTypeName);
                        RemoveItemsByName(projectElement, "Reference");
                        RemoveItemsByName(projectElement, "ProjectReference");
                        if (this.supportExtensions)
                        {
                            AddNewResourceItems(projectElement, this.GeneratedResourcesFiles);
                        }
                        AddNewItems(projectElement, "Compile", this.SourceCodeFiles);
                        AddNewItems(projectElement, "ReferencePath", this.ReferencePaths);

                        RemovePropertyByName(projectElement, "OutputType");
                        RemovePropertyByName(projectElement, "AssemblyName");
                        AddNewProperties(projectElement,
                                         new ProjectProperty[] {
                            new ProjectProperty()
                            {
                                Name = "OutputType", Value = "Library"
                            },
                            new ProjectProperty()
                            {
                                Name = "AssemblyName", Value = this.AssemblyName
                            },
                            new ProjectProperty()
                            {
                                Name = "Utf8Output", Value = "true", Condition = "'$(Utf8Output)' == ''"
                            }
                        });
                    }
                }

                string randomName = Path.GetRandomFileName();
                randomName = Path.ChangeExtension(randomName, "");
                string filename = Path.ChangeExtension(randomName, ".tmp_proj");
                projectDocument.Save(filename);
                Hashtable globalProperties = new Hashtable();
                globalProperties["IntermediateOutputPath"] = this.OutputPath;
                globalProperties["AssemblyName"]           = this.AssemblyName;
                globalProperties["OutputType"]             = "Library";
                retVal = base.BuildEngine.BuildProjectFile(filename, new string[] { this.CompileTargetName }, globalProperties, null);
                File.Delete(filename);
                return(retVal);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                // Log unknown errors that do not originate from the task.
                // Assumes that all known errors are logged when the exception is thrown.
                XamlBuildTaskServices.LogException(this.Log, e.Message);
                retVal = false;
            }
            return(retVal);
        }