private byte[] GenerateFromHost()
        {
            var projectDirectory    = Path.GetDirectoryName(GetProject().FullName);
            var projectRelativePath = InputFilePath.Substring(projectDirectory.Length);

            using (var hostManager = new HostManager(projectDirectory))
            {
                var host = hostManager.CreateHost(InputFilePath, projectRelativePath, GetCodeProvider());
                host.DefaultNamespace = FileNameSpace;
                host.Error           += (o, eventArgs) =>
                {
                    GeneratorError(0, eventArgs.ErrorMessage, eventArgs.LineNumber, eventArgs.ColumnNumber);
                };
                host.Progress += (o, eventArgs) =>
                {
                    if (CodeGeneratorProgress != null)
                    {
                        CodeGeneratorProgress.Progress(eventArgs.Completed, eventArgs.Total);
                    }
                };

                var content = host.GenerateCode();
                return(ConvertToBytes(content));
            }
        }
Exemple #2
0
        /// <summary>Performs code generation</summary>
        /// <param name="inputFileName">Name of file to convert. Note: May be invalid or inactual. <paramref name="inputFileContent"/> is more important.</param>
        /// <param name="inputFileContent">Content of file to convert</param>
        /// <returns>Converted content</returns>
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            CodeCompileUnit codeCompileUnit;

            if (string.IsNullOrEmpty(inputFileContent) || IsLocalizedFile(inputFileName))
            {
                if (CodeGeneratorProgress != null)
                {
                    NativeMethods.ThrowOnFailure(CodeGeneratorProgress.Progress(100, 100));
                }

                return(null);
            }

            StreamWriter streamWriter           = new StreamWriter(new MemoryStream(), Encoding.UTF8);
            IDictionary  nodeDictionary         = new Hashtable(StringComparer.OrdinalIgnoreCase);
            IDictionary  nodePositionDictionary = new Hashtable(StringComparer.OrdinalIgnoreCase);

            using (IResourceReader resourceReader = ResXResourceReader.FromFileContents(inputFileContent)) {
                ResXResourceReader resXResourceReader = resourceReader as ResXResourceReader;
                if (resXResourceReader != null)
                {
                    resXResourceReader.UseResXDataNodes = true;
                    string inputDirectoryName = Path.GetDirectoryName(inputFileName);
                    resXResourceReader.BasePath = Path.GetFullPath(inputDirectoryName);
                }
                foreach (DictionaryEntry resourceEntry in resourceReader)
                {
                    ResXDataNode resXNode = (ResXDataNode)resourceEntry.Value;
                    nodeDictionary.Add(resourceEntry.Key, resXNode);
                    nodePositionDictionary.Add(resourceEntry.Key, resXNode.GetNodePosition());
                }
            }

            string resourceNamespace             = this.GetResourcesNamespace(false);
            string logicalName                   = this.GetResourcesNamespace(true);//Logical name added by Ðonny
            string inputFileNameWithoutExtension = Path.GetFileNameWithoutExtension(inputFileName);
            string className = inputFileNameWithoutExtension;

            //className from logical name added by Ðonny:
            GetClassNameFromLogicalName(ref className);


            List <ResourceErrorData> unmatchableResources = new List <ResourceErrorData>();

            if (resourceNamespace != null)
            {
                codeCompileUnit = StronglyTypedResourceBuilderEx.Create(this.GetType(),
                                                                        nodeDictionary, className, FileNamespace, resourceNamespace,                  //className added by Ðonny
                                                                        this.CodeProvider, GenerateInternalClass, unmatchableResources, logicalName); //Logical name added by Ðonny
            }
            else
            {
                codeCompileUnit = StronglyTypedResourceBuilderEx.Create(this.GetType(),
                                                                        nodeDictionary, className, base.FileNamespace, this.CodeProvider, //className added by Ðonny
                                                                        GenerateInternalClass, unmatchableResources, logicalName);        //Logical name added by Ðonny
            }
            if (base.CodeGeneratorProgress != null)
            {
                foreach (var resourceErrorData in unmatchableResources)
                {
                    Point nodePosition = (Point)nodePositionDictionary[resourceErrorData.ResourceKey];
                    base.CodeGeneratorProgress.GeneratorError(1, 1, resourceErrorData.ErrorString,
                                                              (uint)nodePosition.Y, (uint)nodePosition.X);
                }

                base.CodeGeneratorProgress.Progress(70, 100);
            }

            this.HandleCodeCompileUnit(codeCompileUnit);

            if (base.CodeGeneratorProgress != null)
            {
                base.CodeGeneratorProgress.Progress(0x4b, 100);
            }

            ICodeGenerator codeGenerator = this.CodeProvider.CreateGenerator();

            if (BeforeGenerateText != null)
            {
                BeforeGenerateText(codeGenerator);
            }
            codeGenerator.GenerateCodeFromCompileUnit(codeCompileUnit, streamWriter, null);
            if (base.CodeGeneratorProgress != null)
            {
                NativeMethods.ThrowOnFailure(base.CodeGeneratorProgress.Progress(100, 100));
            }

            streamWriter.Flush();

            return(StreamToBytes(streamWriter.BaseStream));
        }