public static string Generate(AssemblyInfoOptions metadata, IEnumerable <string> sourceFiles)
        {
            var projectAttributes = GetProjectAttributes(metadata);

            var existingAttributes = new List <Type>();

            foreach (var sourceFile in sourceFiles)
            {
                var tree = CSharpSyntaxTree.ParseText(File.ReadAllText(sourceFile.Trim('"')));
                var root = tree.GetRoot();

                // assembly attributes can be only on first level
                foreach (var attributeListSyntax in root.ChildNodes().OfType <AttributeListSyntax>())
                {
                    if (attributeListSyntax.Target.Identifier.Kind() == SyntaxKind.AssemblyKeyword)
                    {
                        foreach (var attributeSyntax in attributeListSyntax.Attributes)
                        {
                            var projectAttribute = projectAttributes.FirstOrDefault(attribute => IsSameAttribute(attribute.Key, attributeSyntax));
                            if (projectAttribute.Key != null)
                            {
                                existingAttributes.Add(projectAttribute.Key);
                            }
                        }
                    }
                }
            }

            return(string.Join(Environment.NewLine, projectAttributes
                               .Where(projectAttribute => projectAttribute.Value != null && !existingAttributes.Contains(projectAttribute.Key))
                               .Select(projectAttribute => $"[assembly:{projectAttribute.Key.FullName}(\"{projectAttribute.Value}\")]")));
        }
        public static string Generate(AssemblyInfoOptions metadata, IEnumerable<string> sourceFiles)
        {
            var projectAttributes = GetProjectAttributes(metadata);

            var existingAttributes = new List<Type>();
            foreach (var sourceFile in sourceFiles)
            {
                var tree = CSharpSyntaxTree.ParseText(File.ReadAllText(sourceFile));
                var root = tree.GetRoot();

                // assembly attributes can be only on first level
                foreach (var attributeListSyntax in root.ChildNodes().OfType<AttributeListSyntax>())
                {
                    if (attributeListSyntax.Target.Identifier.Kind() == SyntaxKind.AssemblyKeyword)
                    {
                        foreach (var attributeSyntax in attributeListSyntax.Attributes)
                        {
                            var projectAttribute = projectAttributes.FirstOrDefault(attribute => IsSameAttribute(attribute.Key, attributeSyntax));
                            if (projectAttribute.Key != null)
                            {
                                existingAttributes.Add(projectAttribute.Key);
                            }
                        }
                    }
                }
            }

            return string.Join(Environment.NewLine, projectAttributes
                .Where(projectAttribute => projectAttribute.Value != null && !existingAttributes.Contains(projectAttribute.Key))
                .Select(projectAttribute => $"[assembly:{projectAttribute.Key.FullName}(\"{projectAttribute.Value}\")]"));
        }
        public static void Generate(ResourceSource[] sourceFiles, Stream outputStream, AssemblyInfoOptions metadata, string assemblyName, string[] references)
        {
            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
                {
                    throw new InvalidOperationException("Generation of resource assemblies from dll not supported");
                }
            }

            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.Generate(metadata, Enumerable.Empty<string>()))
            });

            var result = compilation.Emit(outputStream, manifestResources: resourceDescriptions);
            if (!result.Success)
            {
                foreach (var diagnostic in result.Diagnostics)
                {
                    Reporter.Error.WriteLine(diagnostic.ToString());
                }
                throw new InvalidOperationException("Error occured while emiting assembly");
            }
        }
        public static string GenerateFSharp(AssemblyInfoOptions metadata)
        {
            var projectAttributes = GetProjectAttributes(metadata);

            return string.Join(Environment.NewLine,
                new[] { "namespace System", Environment.NewLine, Environment.NewLine } 
                .Concat(projectAttributes.Select(projectAttribute => $"[<assembly:{projectAttribute.Key.FullName}(\"{projectAttribute.Value}\")>]"))
                .Concat(new[] { "do ()", Environment.NewLine }));
        }
        public static string GenerateFSharp(AssemblyInfoOptions metadata)
        {
            var projectAttributes = GetProjectAttributes(metadata);

            return(string.Join(Environment.NewLine,
                               new[] { "namespace System", Environment.NewLine, Environment.NewLine }
                               .Concat(projectAttributes.Select(projectAttribute => $"[<assembly:{projectAttribute.Key.FullName}(\"{projectAttribute.Value}\")>]"))
                               .Concat(new[] { "do ()", Environment.NewLine })));
        }
 private static Dictionary<Type, string> GetProjectAttributes(AssemblyInfoOptions metadata)
 {
     return new Dictionary<Type, string>()
     {
         [typeof(AssemblyTitleAttribute)] = EscapeCharacters(metadata.Title),
         [typeof(AssemblyDescriptionAttribute)] = EscapeCharacters(metadata.Description),
         [typeof(AssemblyCopyrightAttribute)] = EscapeCharacters(metadata.Copyright),
         [typeof(AssemblyFileVersionAttribute)] = EscapeCharacters(metadata.AssemblyFileVersion?.ToString()),
         [typeof(AssemblyVersionAttribute)] = EscapeCharacters(metadata.AssemblyVersion?.ToString()),
         [typeof(AssemblyInformationalVersionAttribute)] = EscapeCharacters(metadata.InformationalVersion),
         [typeof(AssemblyCultureAttribute)] = EscapeCharacters(metadata.Culture),
         [typeof(NeutralResourcesLanguageAttribute)] = EscapeCharacters(metadata.NeutralLanguage),
         [typeof(TargetFrameworkAttribute)] = EscapeCharacters(metadata.TargetFramework)
     };
 }
 private static Dictionary <Type, string> GetProjectAttributes(AssemblyInfoOptions metadata)
 {
     return(new Dictionary <Type, string>()
     {
         [typeof(AssemblyTitleAttribute)] = EscapeCharacters(metadata.Title),
         [typeof(AssemblyDescriptionAttribute)] = EscapeCharacters(metadata.Description),
         [typeof(AssemblyCopyrightAttribute)] = EscapeCharacters(metadata.Copyright),
         [typeof(AssemblyFileVersionAttribute)] = EscapeCharacters(metadata.AssemblyFileVersion?.ToString()),
         [typeof(AssemblyVersionAttribute)] = EscapeCharacters(metadata.AssemblyVersion?.ToString()),
         [typeof(AssemblyInformationalVersionAttribute)] = EscapeCharacters(metadata.InformationalVersion),
         [typeof(AssemblyCultureAttribute)] = EscapeCharacters(metadata.Culture),
         [typeof(NeutralResourcesLanguageAttribute)] = EscapeCharacters(metadata.NeutralLanguage),
         [typeof(TargetFrameworkAttribute)] = EscapeCharacters(metadata.TargetFramework)
     });
 }
        private static bool SupportsTargetFrameworkAttribute(AssemblyInfoOptions metadata)
        {
            if (string.IsNullOrEmpty(metadata.TargetFramework))
            {
                // target framework is unknown. to be on the safe side, return false.
                return false;
            }

            var targetFramework = NuGetFramework.Parse(metadata.TargetFramework);
            if (!targetFramework.IsDesktop())
            {
                // assuming .NET Core, which should support .NET 4.0 attributes
                return true;
            }

            return targetFramework.Version >= new Version(4, 0);
        }
Beispiel #9
0
        private static bool SupportsTargetFrameworkAttribute(AssemblyInfoOptions metadata)
        {
            if (string.IsNullOrEmpty(metadata.TargetFramework))
            {
                // target framework is unknown. to be on the safe side, return false.
                return(false);
            }

            var targetFramework = NuGetFramework.Parse(metadata.TargetFramework);

            if (!targetFramework.IsDesktop())
            {
                // assuming .NET Core, which should support .NET 4.0 attributes
                return(true);
            }

            return(targetFramework.Version >= new Version(4, 0));
        }
        public static IEnumerable <string> SerializeToArgs(AssemblyInfoOptions assemblyInfoOptions)
        {
            var options = new List <string>();

            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.Title))
            {
                options.Add(FormatOption(TitleOptionName, assemblyInfoOptions.Title));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.Description))
            {
                options.Add(FormatOption(DescriptionOptionName, assemblyInfoOptions.Description));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.Copyright))
            {
                options.Add(FormatOption(CopyrightOptionName, assemblyInfoOptions.Copyright));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.AssemblyFileVersion))
            {
                options.Add(FormatOption(AssemblyFileVersionOptionName, assemblyInfoOptions.AssemblyFileVersion));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.AssemblyVersion))
            {
                options.Add(FormatOption(AssemblyVersionOptionName, assemblyInfoOptions.AssemblyVersion));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.InformationalVersion))
            {
                options.Add(FormatOption(InformationalVersionOptionName, assemblyInfoOptions.InformationalVersion));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.Culture))
            {
                options.Add(FormatOption(CultureOptionName, assemblyInfoOptions.Culture));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.NeutralLanguage))
            {
                options.Add(FormatOption(NeutralCultureOptionName, assemblyInfoOptions.NeutralLanguage));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.TargetFramework))
            {
                options.Add(FormatOption(TargetFrameworkOptionName, assemblyInfoOptions.TargetFramework));
            }

            return(options);
        }
        public static IEnumerable<string> SerializeToArgs(AssemblyInfoOptions assemblyInfoOptions)
        {
            var options = new List<string>();

            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.Title))
            {
                options.Add(FormatOption(TitleOptionName, assemblyInfoOptions.Title));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.Description))
            {
                options.Add(FormatOption(DescriptionOptionName, assemblyInfoOptions.Description));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.Copyright))
            {
                options.Add(FormatOption(CopyrightOptionName, assemblyInfoOptions.Copyright));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.AssemblyFileVersion))
            {
                options.Add(FormatOption(AssemblyFileVersionOptionName, assemblyInfoOptions.AssemblyFileVersion));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.AssemblyVersion))
            {
                options.Add(FormatOption(AssemblyVersionOptionName, assemblyInfoOptions.AssemblyVersion));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.InformationalVersion))
            {
                options.Add(FormatOption(InformationalVersionOptionName, assemblyInfoOptions.InformationalVersion));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.Culture))
            {
                options.Add(FormatOption(CultureOptionName, assemblyInfoOptions.Culture));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.NeutralLanguage))
            {
                options.Add(FormatOption(NeutralCultureOptionName, assemblyInfoOptions.NeutralLanguage));
            }
            if (!string.IsNullOrWhiteSpace(assemblyInfoOptions.TargetFramework))
            {
                options.Add(FormatOption(TargetFrameworkOptionName, assemblyInfoOptions.TargetFramework));
            }

            return options;
        }
Beispiel #12
0
        public int Execute()
        {
            var inputResourceFiles = Args.Select(ParseInputFile).ToArray();
            var outputResourceFile = ResourceFile.Create(OutputFileName);

            switch (outputResourceFile.Type)
            {
                case ResourceFileType.Dll:
                    using (var outputStream = outputResourceFile.File.Create())
                    {
                        var metadata = new AssemblyInfoOptions
                        {
                            Culture = AssemblyCulture,
                            AssemblyVersion = AssemblyVersion,
                        };

                        ResourceAssemblyGenerator.Generate(inputResourceFiles,
                            outputStream,
                            metadata,
                            Path.GetFileNameWithoutExtension(outputResourceFile.File.Name),
                            CompilationReferences.ToArray()
                            );
                    }
                    break;
                case ResourceFileType.Resources:
                    using (var outputStream = outputResourceFile.File.Create())
                    {
                        if (inputResourceFiles.Length > 1)
                        {
                            Reporter.Error.WriteLine("Only one input file required when generating .resource output");
                            return 1;
                        }
                        ResourcesFileGenerator.Generate(inputResourceFiles.Single().Resource, outputStream);
                    }
                    break;
                default:
                    Reporter.Error.WriteLine("Resx output type not supported");
                    return 1;
            }

            return 0;
        }
        private static Dictionary<Type, string> GetProjectAttributes(AssemblyInfoOptions metadata)
        {
            var attributes = new Dictionary<Type, string>()
            {
                [typeof(AssemblyTitleAttribute)] = EscapeCharacters(metadata.Title),
                [typeof(AssemblyDescriptionAttribute)] = EscapeCharacters(metadata.Description),
                [typeof(AssemblyCopyrightAttribute)] = EscapeCharacters(metadata.Copyright),
                [typeof(AssemblyFileVersionAttribute)] = EscapeCharacters(metadata.AssemblyFileVersion?.ToString()),
                [typeof(AssemblyVersionAttribute)] = EscapeCharacters(metadata.AssemblyVersion?.ToString()),
                [typeof(AssemblyInformationalVersionAttribute)] = EscapeCharacters(metadata.InformationalVersion),
                [typeof(AssemblyCultureAttribute)] = EscapeCharacters(metadata.Culture),
                [typeof(NeutralResourcesLanguageAttribute)] = EscapeCharacters(metadata.NeutralLanguage)
            };

            if (SupportsTargetFrameworkAttribute(metadata))
            {
                // TargetFrameworkAttribute only exists since .NET 4.0
                attributes[typeof(TargetFrameworkAttribute)] = EscapeCharacters(metadata.TargetFramework);
            };

            return attributes;
        }
Beispiel #14
0
        private static Dictionary <Type, string> GetProjectAttributes(AssemblyInfoOptions metadata)
        {
            var attributes = new Dictionary <Type, string>()
            {
                [typeof(AssemblyTitleAttribute)]                = EscapeCharacters(metadata.Title),
                [typeof(AssemblyDescriptionAttribute)]          = EscapeCharacters(metadata.Description),
                [typeof(AssemblyCopyrightAttribute)]            = EscapeCharacters(metadata.Copyright),
                [typeof(AssemblyFileVersionAttribute)]          = EscapeCharacters(metadata.AssemblyFileVersion?.ToString()),
                [typeof(AssemblyVersionAttribute)]              = EscapeCharacters(metadata.AssemblyVersion?.ToString()),
                [typeof(AssemblyInformationalVersionAttribute)] = EscapeCharacters(metadata.InformationalVersion),
                [typeof(AssemblyCultureAttribute)]              = EscapeCharacters(metadata.Culture),
                [typeof(NeutralResourcesLanguageAttribute)]     = EscapeCharacters(metadata.NeutralLanguage)
            };

            if (SupportsTargetFrameworkAttribute(metadata))
            {
                // TargetFrameworkAttribute only exists since .NET 4.0
                attributes[typeof(TargetFrameworkAttribute)] = EscapeCharacters(metadata.TargetFramework);
            }
            ;

            return(attributes);
        }
Beispiel #15
0
        public static int Main(string[] args)
        {
            DebugHelper.HandleDebugSwitch(ref args);

            var app = new CommandLineApplication(false);
            app.Name = "resgen";
            app.FullName = "Resource compiler";
            app.Description = "Microsoft (R) .NET Resource Generator";
            app.HelpOption("-h|--help");

            var ouputFile = app.Option("-o", "Output file name", CommandOptionType.SingleValue);
            var culture = app.Option("-c", "Ouput assembly culture", CommandOptionType.SingleValue);
            var version = app.Option("-v", "Ouput assembly version", CommandOptionType.SingleValue);
            var references = app.Option("-r", "Compilation references", CommandOptionType.MultipleValue);
            var inputFiles = app.Argument("<input>", "Input files", true);

            app.OnExecute(() =>
            {
                if (!inputFiles.Values.Any())
                {
                    Reporter.Error.WriteLine("No input files specified");
                    return 1;
                }

                var intputResourceFiles = inputFiles.Values.Select(ParseInputFile).ToArray();
                var outputResourceFile = ResourceFile.Create(ouputFile.Value());

                switch (outputResourceFile.Type)
                {
                    case ResourceFileType.Dll:
                        using (var outputStream = outputResourceFile.File.Create())
                        {
                            var metadata = new AssemblyInfoOptions();
                            metadata.Culture = culture.Value();
                            metadata.AssemblyVersion = version.Value();

                            ResourceAssemblyGenerator.Generate(intputResourceFiles,
                                outputStream,
                                metadata,
                                Path.GetFileNameWithoutExtension(outputResourceFile.File.Name),
                                references.Values.ToArray()
                                );
                        }
                        break;
                    case ResourceFileType.Resources:
                        using (var outputStream = outputResourceFile.File.Create())
                        {
                            if (intputResourceFiles.Length > 1)
                            {
                                Reporter.Error.WriteLine("Only one input file required when generating .resource output");
                                return 1;
                            }
                            ResourcesFileGenerator.Generate(intputResourceFiles.Single().Resource, outputStream);
                        }
                        break;
                    default:
                        Reporter.Error.WriteLine("Resx output type not supported");
                        return 1;
                }

                return 0;
            });

            try
            {
                return app.Execute(args);
            }
            catch (Exception ex)
            {
#if DEBUG
                Reporter.Error.WriteLine(ex.ToString());
#else
                Reporter.Error.WriteLine(ex.Message);
#endif
                return 1;
            }
        }
Beispiel #16
0
        public static bool GenerateCultureResourceAssemblies(Project project, List<CompilerUtility.CultureResgenIO> cultureResgenFiles, List<string> referencePaths, IList<string> diagnostics)
        {
            foreach (var resgenFile in cultureResgenFiles)
            {
                var resourceOutputPath = Path.GetDirectoryName(resgenFile.OutputFile);
                Directory.CreateDirectory(resourceOutputPath);

                var inputResourceFiles = resgenFile.InputFileToMetadata
                    .Select(fileToMetadata => new ResourceSource(ResourceFile.Create(fileToMetadata.Key), fileToMetadata.Value))
                    .ToArray();

                var outputResourceFile = ResourceFile.Create(resgenFile.OutputFile);

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

                using (var outputStream = outputResourceFile.File.Create())
                {
                    var metadata = new AssemblyInfoOptions
                    {
                        Culture = resgenFile.Culture,
                        AssemblyVersion = project.Version.Version.ToString(),
                    };

                    ResourceAssemblyGenerator.Generate(inputResourceFiles,
                        outputStream,
                        metadata,
                        Path.GetFileNameWithoutExtension(outputResourceFile.File.Name),
                        referencePaths.ToArray(),
                        diagnostics);
                }
            }

            return true;
        }