///<summary>constructor</summary> 
 public CompilationUnit(string assemblyName, string language, string defaultNamespace, FileUnit[] fileList)
 {
     _assemblyName = assemblyName;
     _language = language;
     _fileList = fileList;
     _defaultNamespace = defaultNamespace;
 }
        // <summary>
        // Constructor
        // </summary>
        internal SourceFileInfo(FileUnit file)
        {
            _filePath = file.Path;
            _fileLinkAlias = file.LinkAlias;
            _fileLogicalName = file.LogicalName;
            _sourcePath = null;
            _relativeSourceFilePath = null;

            _stream = null;

            _isXamlFile = false;

            if (!string.IsNullOrEmpty(file.Path) && file.Path.ToUpperInvariant().EndsWith(XAML, StringComparison.Ordinal))
            {
                _isXamlFile = true;
            }
        }
        //
        // Return FileInfo for the given source file.
        //
        private SourceFileInfo OnSourceFileResolve(FileUnit file)
        {
            SourceFileInfo sourceFileInfo;

            if (SourceFileResolve != null)
            {
                //
                // If SourceFileResolve event handler is registered,  the handler
                // is responsible for generating the SourceFileInfo.
                // This is for MSBUILD tasks.
                //
                SourceFileResolveEventArgs scea = new SourceFileResolveEventArgs(file);

                SourceFileResolve(this, scea);

                sourceFileInfo = scea.SourceFileInfo;
            }
            else
            {
                // If SourceFileResolve event handler is not registered,  generate
                // the default SourceFileInfo for this file.
                //
                sourceFileInfo = new SourceFileInfo(file);

                sourceFileInfo.SourcePath = _compilationUnitSourcePath;

                if (sourceFileInfo.IsXamlFile)
                {
                    int fileExtIndex = file.Path.LastIndexOf(DOT, StringComparison.Ordinal);

                    sourceFileInfo.RelativeSourceFilePath = file.Path.Substring(0, fileExtIndex);
                }
            }

            return sourceFileInfo;
        }
        //
        // Generate the SourceFileInfo for the source file.
        // Do the appropriate initiallization work and file checking.
        //
        private void Initialize(FileUnit sourceFile)
        {
            try
            {
                // Keep the SourceFileInfo for the passed source file.
                SourceFileInfo = OnSourceFileResolve(sourceFile);

                // Process the input file
                if (sourceFile.Path == null || !SourceFileInfo.IsXamlFile)
                {
                    ThrowCompilerException(SRID.InvalidMarkupFile);
                }

                if (!TaskFileService.Exists(sourceFile.Path))
                {
                    ThrowCompilerException(SRID.FileNotFound, sourceFile.Path);
                }

                // Prime the output directory
                if (TargetPath.Length > 0)
                {
                    // check for ending '\'
                    if (!TargetPath.EndsWith(ESCAPED_BACKSLASH, StringComparison.Ordinal))
                    {
                        TargetPath += ESCAPED_BACKSLASH;
                    }
                }

                int pathEndIndex = SourceFileInfo.RelativeSourceFilePath.LastIndexOf(ESCAPED_BACKSLASH, StringComparison.Ordinal);
                string targetPath = TargetPath + SourceFileInfo.RelativeSourceFilePath.Substring(0, pathEndIndex + 1);

                // Create if not already exists
                if (targetPath.Length > 0 && !Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }
            }
            // All exceptions including NullRef & SEH need to be caught by the markupcompiler
            // since it is an app and not a component.
            #pragma warning suppress 6500
            catch (Exception e)
            {
                OnError(e);
            }
        }
        // <summary>
        // Start the compilation.
        // </summary>
        // <param name="assemblyName"></param>
        // <param name="language"></param>
        // <param name="rootNamespace"></param>
        // <param name="fileList"></param>
        // <param name="isSecondPass"></param>
        // <returns></returns>
        internal bool DoCompilation(string assemblyName, string language, string rootNamespace, FileUnit[] fileList, bool isSecondPass)
        {
            bool ret = true;

            CompilationUnit compUnit = new CompilationUnit(assemblyName, language, rootNamespace, fileList);
            compUnit.Pass2 = isSecondPass;

            // Set some properties required by the CompilationUnit
            compUnit.ApplicationFile = _applicationMarkup;
            compUnit.SourcePath = _sourceDir;

            //Set the properties required by MarkupCompiler

            _mc.SourceFileResolve += new SourceFileResolveEventHandler(OnSourceFileResolve);
            _mc.Error += new MarkupErrorEventHandler(OnCompilerError);

            LocalizationDirectivesToLocFile localizeFlag = (LocalizationDirectivesToLocFile)_localizationDirectivesToLocFile;


            //
            // Localization file should not be generated for Intellisense build. Thus
            // checking IsRealBuild.
            //
            if ((localizeFlag == MS.Internal.LocalizationDirectivesToLocFile.All
                 || localizeFlag == MS.Internal.LocalizationDirectivesToLocFile.CommentsOnly)
                && (TaskFileService.IsRealBuild))
            {
                _mc.ParserHooks = new LocalizationParserHooks(_mc, localizeFlag, isSecondPass);
            }

            if (isSecondPass)
            {
                for (int i = 0; i < _mc.ReferenceAssemblyList.Count; i++)
                {
                    ReferenceAssembly asmReference = _mc.ReferenceAssemblyList[i] as ReferenceAssembly;

                    if (asmReference != null)
                    {
                        if (String.Compare(asmReference.AssemblyName, assemblyName, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            // Set the local assembly file to markupCompiler
                            _mc.LocalAssemblyFile = asmReference;
                        }
                    }
                }
            }

            // finally compile the app
            _mc.Compile(compUnit);

            return ret;
        }
        //
        // Call MarkupCompiler to do the real compilation work.
        //
        private void DoLocalReferenceMarkupCompilation(FileUnit localApplicationFile, FileUnit[] localXamlPageFileList, ArrayList referenceList)
        {
            // When code goes here, the MarkupCompilation is really required, so don't need
            // to do more further validation inside this private method.

            Log.LogMessageFromResources(MessageImportance.Low, SRID.DoCompilation);

            AppDomain appDomain = null;
            CompilerWrapper compilerWrapper = null;

            try
            {
                compilerWrapper = TaskHelper.CreateCompilerWrapper(AlwaysCompileMarkupFilesInSeparateDomain, ref appDomain);

                if (compilerWrapper != null)
                {

                    compilerWrapper.OutputPath = OutputPath;

                    compilerWrapper.TaskLogger = Log;
                    compilerWrapper.UnknownErrorID = UnknownErrorID;
                    compilerWrapper.XamlDebuggingInformation = XamlDebuggingInformation;

                    compilerWrapper.TaskFileService = _taskFileService;

                    if (OutputType.Equals(SharedStrings.Exe) || OutputType.Equals(SharedStrings.WinExe))
                    {
                        compilerWrapper.ApplicationMarkup = localApplicationFile;
                    }

                    compilerWrapper.References = referenceList;

                    compilerWrapper.LocalizationDirectivesToLocFile = (int)_localizationDirectives;

                    // This is for Pass2 compilation
                    compilerWrapper.DoCompilation(AssemblyName, Language, RootNamespace, localXamlPageFileList, true);

                    //
                    // If no any xaml file with local-types wants to reference an internal type from
                    // current assembly and friend assembly, and InternalTypeHelperFile is set in the
                    // cache file, now it is the time to remove the content of InternalTypeHelper File.
                    //
                    // We still keep the empty file to make other parts of the build system happy.
                    //
                    if (!String.IsNullOrEmpty(_internalTypeHelperFile) && !compilerWrapper.HasInternals)
                    {
                        if (TaskFileService.Exists(_internalTypeHelperFile))
                        {
                            // Make empty content for this file.

                            MemoryStream memStream = new MemoryStream();

                            using (StreamWriter writer = new StreamWriter(memStream, new UTF8Encoding(false)))
                            {
                                writer.WriteLine(String.Empty);
                                writer.Flush();
                                TaskFileService.WriteFile(memStream.ToArray(), _internalTypeHelperFile);
                            }

                            Log.LogMessageFromResources(MessageImportance.Low, SRID.InternalTypeHelperNotRequired, _internalTypeHelperFile);
                        }
                    }
                }
            }
            finally
            {
                if (compilerWrapper != null && compilerWrapper.ErrorTimes > 0)
                {
                    _nErrors += compilerWrapper.ErrorTimes;
                }

                if (appDomain != null)
                {
                    Log.MarkAsInactive();       // see Dev11 354473
                    AppDomain.Unload(appDomain);
                    compilerWrapper = null;
                }
            }

        }
        //
        // Generate the necessary file lists and other information required by MarkupCompiler.
        //
        // Output ArrayLists:  localApplicationFile,
        //                     localXamlPageFileList
        //                     referenceList
        //
        private void PrepareForMarkupCompilation(out FileUnit localApplicationFile, out FileUnit[] localXamlPageFileList, out ArrayList referenceList)
        {
            Log.LogMessageFromResources(MessageImportance.Low, SRID.PreparingCompile);
            Log.LogMessageFromResources(MessageImportance.Low, SRID.OutputType, OutputType);

            // Initialize the output parameters
            localXamlPageFileList = new FileUnit[0];
            localApplicationFile = FileUnit.Empty;
            referenceList = new ArrayList();

            if (_localApplicationFile != null)
            {
                // We don't want to support multiple application definition file per project.
                localApplicationFile = new FileUnit(_localApplicationFile.FilePath, _localApplicationFile.LinkAlias, _localApplicationFile.LogicalName);

                Log.LogMessageFromResources(MessageImportance.Low, SRID.LocalRefAppDefFile, localApplicationFile);

            }

            // Generate the Xaml Markup file list
            if (_localMarkupPages != null && _localMarkupPages.Length > 0)
            {
                int localFileNum = _localMarkupPages.Length;
                localXamlPageFileList = new FileUnit[localFileNum];

                for (int i = 0; i < localFileNum; i++)
                {
                    FileUnit localPageFile = new FileUnit(_localMarkupPages[i].FilePath, _localMarkupPages[i].LinkAlias, _localMarkupPages[i].LogicalName);

                    localXamlPageFileList[i] = localPageFile;
                    Log.LogMessageFromResources(MessageImportance.Low, SRID.LocalRefMarkupPage, localPageFile);
                }
            }

            //
            // Generate the asmmebly reference list.
            // The temporay target assembly should have been added into Reference list from target file.
            //
            if (References != null && References.Length > 0)
            {
                ReferenceAssembly asmReference;
                string refpath, asmname;

                for (int i = 0; i < References.Length; i++)
                {
                    refpath = References[i].ItemSpec;
                    refpath = TaskHelper.CreateFullFilePath(refpath, SourceDir);

                    asmname = Path.GetFileNameWithoutExtension(refpath);

                    asmReference = new ReferenceAssembly(refpath, asmname);
                    referenceList.Add(asmReference);

                    //
                    // If always run the compilation in second appdomain, there is no need to specially
                    // handle the referenced assemblies.
                    // Unload the appdomain can unload all the referenced assemblies.
                    //
                    if (AlwaysCompileMarkupFilesInSeparateDomain == false)
                    {
                        bool bCouldbeChanged = TaskHelper.CouldReferenceAssemblyBeChanged(refpath, KnownReferencePaths, AssembliesGeneratedDuringBuild);

                        if (bCouldbeChanged)
                        {
                            MarkupCompiler.InitializeAssemblyState(asmname);
                        }
                    }
                }
            }

        }
Example #8
0
 /// <summary>
 /// constructor
 /// </summary>
 internal SourceFileResolveEventArgs(FileUnit file)
 {
     _sourceFileInfo = new SourceFileInfo(file);
 }
 /// <summary>
 /// constructor
 /// </summary>
 internal SourceFileResolveEventArgs(FileUnit file)
 {
     _sourceFileInfo = new SourceFileInfo(file);
 }