示例#1
0
        public void Create(FilePath outputPath, AssemblyInfoSettings settings)
        {
            if (outputPath == null)
            {
                throw new ArgumentNullException("outputPath");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            var data = new AssemblyInfoCreatorData(settings);

            outputPath = outputPath.MakeAbsolute(_environment);
            _log.Verbose("Creating assembly info file: {0}", outputPath);

            using (var stream = _fileSystem.GetFile(outputPath).OpenWrite())
                using (var writer = new StreamWriter(stream, System.Text.Encoding.UTF8))
                {
                    writer.WriteLine("//------------------------------------------------------------------------------");
                    writer.WriteLine("// <auto-generated>");
                    writer.WriteLine("//     This code was generated by Cake.");
                    writer.WriteLine("// </auto-generated>");
                    writer.WriteLine("//------------------------------------------------------------------------------");

                    if (data.Namespaces.Count > 0)
                    {
                        var namespaces = data.Namespaces.Select(n => string.Concat("using ", n, ";"));
                        foreach (var @namespace in namespaces)
                        {
                            writer.WriteLine(@namespace);
                        }
                        writer.WriteLine();
                    }

                    if (data.Attributes.Count > 0)
                    {
                        foreach (var attribute in data.Attributes)
                        {
                            writer.WriteLine(string.Concat("[assembly: ", attribute.Key, "(", attribute.Value, ")]"));
                        }
                        writer.WriteLine();
                    }

                    if (data.InternalVisibleTo.Count > 0)
                    {
                        foreach (var temp in data.InternalVisibleTo)
                        {
                            writer.WriteLine(string.Concat("[assembly: ", temp, "]"));
                        }
                        writer.WriteLine();
                    }
                }
        }
示例#2
0
        public void Create(FilePath outputPath, AssemblyInfoSettings settings,
                           string attributeFormat               = CSharpAttributeFormat,
                           string attributeWithValueFormat      = CSharpAttributeWithValueFormat,
                           string attributeWithKeyValueFormat   = CSharpAttributeWithKeyValueFormat,
                           string vbAttributeFormat             = VBAttributeFormat,
                           string vbAttributeWithValueFormat    = VBAttributeWithValueFormat,
                           string vbAttributeWithKeyValueFormat = VBAttributeWithKeyValueFormat)
        {
            if (outputPath == null)
            {
                throw new ArgumentNullException(nameof(outputPath));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            string comment     = CSharpComment;
            string usingFormat = CSharpUsingFormat;

            var isVisualBasicAssemblyInfoFile = false;

            if (outputPath.GetExtension() == ".vb")
            {
                isVisualBasicAssemblyInfoFile = true;
                comment                     = VBComment;
                usingFormat                 = VBUsingFormat;
                attributeFormat             = vbAttributeFormat;
                attributeWithValueFormat    = vbAttributeWithValueFormat;
                attributeWithKeyValueFormat = vbAttributeWithKeyValueFormat;
            }

            var data = new AssemblyInfoCreatorData(settings, isVisualBasicAssemblyInfoFile);

            outputPath = outputPath.MakeAbsolute(_environment);
            _log.Verbose("Creating assembly info file: {0}", outputPath);

            using (var stream = _fileSystem.GetFile(outputPath).OpenWrite())
                using (var writer = new StreamWriter(stream, System.Text.Encoding.UTF8))
                {
                    writer.WriteLine(comment + "------------------------------------------------------------------------------");
                    writer.WriteLine(comment + " <auto-generated>");
                    writer.WriteLine(comment + "     This code was generated by Cake.");
                    writer.WriteLine(comment + " </auto-generated>");
                    writer.WriteLine(comment + "------------------------------------------------------------------------------");

                    if (data.Namespaces.Count > 0)
                    {
                        var namespaces = data.Namespaces.Select(n => string.Format(usingFormat, n));
                        foreach (var @namespace in namespaces)
                        {
                            writer.WriteLine(@namespace);
                        }
                        writer.WriteLine();
                    }

                    if (data.Attributes.Count > 0)
                    {
                        foreach (var attribute in data.Attributes)
                        {
                            writer.WriteLine(string.Format(attributeWithValueFormat, attribute.Key, attribute.Value));
                        }
                        writer.WriteLine();
                    }

                    if (data.InternalVisibleTo.Count > 0)
                    {
                        foreach (var temp in data.InternalVisibleTo)
                        {
                            writer.WriteLine(string.Format(attributeFormat, temp));
                        }
                        writer.WriteLine();
                    }

                    if (data.CustomAttributes.Count > 0)
                    {
                        writer.WriteLine(comment + " Custom Attributes");
                        foreach (var attribute in data.CustomAttributes)
                        {
                            writer.WriteLine(string.Format(attributeWithValueFormat, attribute.Key, attribute.Value));
                        }
                    }

                    if (data.MetadataAttributes.Count > 0)
                    {
                        writer.WriteLine(comment + " Metadata Attributes");
                        var mdAttribute = new AssemblyInfoMetadataAttribute();
                        foreach (var attribute in data.MetadataAttributes)
                        {
                            writer.WriteLine(string.Format(attributeWithKeyValueFormat, mdAttribute.Name, attribute.Key, attribute.Value));
                        }
                    }
                }
        }