Beispiel #1
0
        public static bool GenerateNonCultureResources(Project project, List<CompilerUtility.NonCultureResgenIO> resgenFiles, IList<string> diagnostics)
        {
            foreach (var resgenFile in resgenFiles)
            {
                if (ResourceUtility.IsResxFile(resgenFile.InputFile))
                {
                    var outputResourceFile = ResourceFile.Create(resgenFile.OutputFile);

                    if (outputResourceFile.Type != ResourceFileType.Resources)
                    {
                        diagnostics.Add("Resource output type not supported");
                        return false;
                    }

                    using (var outputStream = outputResourceFile.File.Create())
                    {
                        var resourceSource = new ResourceSource(ResourceFile.Create(resgenFile.InputFile), resgenFile.InputFile);
                        ResourceFileGenerator.Generate(resourceSource.Resource, outputStream);
                    }
                }
            }

            return true;
        }
Beispiel #2
0
        public static bool GenerateNonCultureResources(Project project, List <CompilerUtility.NonCultureResgenIO> resgenFiles, IList <string> diagnostics)
        {
            foreach (var resgenFile in resgenFiles)
            {
                if (ResourceUtility.IsResxFile(resgenFile.InputFile))
                {
                    var outputResourceFile = ResourceFile.Create(resgenFile.OutputFile);

                    if (outputResourceFile.Type != ResourceFileType.Resources)
                    {
                        diagnostics.Add("Resource output type not supported");
                        return(false);
                    }

                    using (var outputStream = outputResourceFile.File.Create())
                    {
                        var resourceSource = new ResourceSource(ResourceFile.Create(resgenFile.InputFile), resgenFile.InputFile);
                        ResourceFileGenerator.Generate(resourceSource.Resource, outputStream);
                    }
                }
            }

            return(true);
        }
        public static bool Generate(ResourceSource[] sourceFiles, Stream outputStream, AssemblyInfoOptions metadata, string assemblyName, string[] references, IList<string> diagnostics)
        {
            if (sourceFiles == null)
            {
                throw new ArgumentNullException(nameof(sourceFiles));
            }

            if (outputStream == null)
            {
                throw new ArgumentNullException(nameof(outputStream));
            }

            if (!sourceFiles.Any())
            {
                throw new InvalidOperationException("No source files specified");
            }

            var resourceDescriptions = new List<ResourceDescription>();
            foreach (var resourceInputFile in sourceFiles)
            {
                if (resourceInputFile.Resource.Type == ResourceFileType.Resx)
                {
                    resourceDescriptions.Add(new ResourceDescription(
                        resourceInputFile.MetadataName,
                        () => GenerateResources(resourceInputFile.Resource), true));

                }
                else if (resourceInputFile.Resource.Type == ResourceFileType.Resources)
                {
                    resourceDescriptions.Add(new ResourceDescription(resourceInputFile.Resource.File.Name, () => resourceInputFile.Resource.File.OpenRead(), true));
                }
                else
                {
                    diagnostics.Add("Generation of resource assemblies from dll not supported");
                    return false;
                }
            }

            var compilationOptions = new CSharpCompilationOptions(outputKind: OutputKind.DynamicallyLinkedLibrary);

            var compilation = CSharpCompilation.Create(assemblyName,
                references: references.Select(reference => MetadataReference.CreateFromFile(reference)),
                options: compilationOptions);

            compilation = compilation.AddSyntaxTrees(new[]
            {
                CSharpSyntaxTree.ParseText(AssemblyInfoFileGenerator.GenerateCSharp(metadata, Enumerable.Empty<string>()))
            });

            var result = compilation.Emit(outputStream, manifestResources: resourceDescriptions);

            if (!result.Success)
            {
                diagnostics.Add("Error occured while emiting resource assembly");

                foreach (var diagnostic in result.Diagnostics)
                {
                    diagnostics.Add(diagnostic.ToString());
                }
            }

            return result.Success;
        }