The reason is that I wrote the source for my build system, where we've standardized on 
log4net for all our projects.  What I ended up doing is rewriting the task, but forgot 
to resubmit the changes.  The revised task is attached.  Usage is like so: 
<asminfo> 
<attributes> 
<option name="System.Reflection.AssemblyVersion" value="1.0.0.0" /> 
<option name="System.Reflection.AssemblyTitle" value="My fun assembly" /> 
<option name="System.Reflection.AssemblyDescription" value="More fun than a barrel of 
monkeys" /> 
<option name="System.Reflection.AssemblyCopyright" value="Copyright (c) 2002, 
Monkeyboy, Inc." /> 
</attributes> 
</asminfo> 
  
This allows you to specify arbitrary assembly level attributes. 
  
-----Original Message----- 
  
Hi all, 
  
Just trying out the <asminfo> task: 
  
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/nantcontrib/NAntContrib/src/Tasks/AssemblyInfoTask.cs?rev=1.2&content-type=text/vnd.viewcvs-markup
 
  
Is there a reason it always generates a log4net declaration? I don't use 
log4net so I get a compile error whenever I try to use it. Would it not 
be more sensible to only set a log4net line if an appropriate attribute 
is set? Maybe it should be set 'true' by default for backwards 
compatibility but if that's not a concern I'd default it to false since 
I think a lot of people aren't using log4net. 
  
E.g. instead of 
  
att = new CodeAttributeDeclaration("log4net.Config.Domain"); 
att.Arguments.Add(new CodeAttributeArgument("UseDefaultDomain",new 
CodePrimitiveExpression(true))); 
ccu.AssemblyCustomAttributes.Add(att); 
  
have 
  
if ( UseLog4netDomain ) 
{ 
att = new CodeAttributeDeclaration("log4net.Config.Domain"); 
att.Arguments.Add(new CodeAttributeArgument("UseDefaultDomain",new 
CodePrimitiveExpression(true))); 
ccu.AssemblyCustomAttributes.Add(att); 
} 
  
where 'UseLog4netDomain' is a new property on the task. 
  
Mike 
  
  
------------------------------------------------------- 
This SF.Net email sponsored by: Free pre-built ASP.NET sites including 
Data Reports, E-commerce, Portals, and Forums are available now. 
Download today and enter to win an XBOX or Visual Studio .NET. 
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 
_______________________________________________ 
NAntContrib-Developer mailing list 
[EMAIL PROTECTED] 
https://lists.sourceforge.net/lists/listinfo/nantcontrib-developer 
// Copyright (C) 2002 Gordon Weakliem
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// Gordon Weakliem ([EMAIL PROTECTED])
using System;
using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using SourceForge.NAnt;
using SourceForge.NAnt.Tasks;
using SourceForge.NAnt.Attributes;


namespace NAnt.Contrib.Tasks 
{
    /// <summary>
    /// Generates an AssemblyInfo.cs file using the attributes given.
    /// </summary>
    [TaskName("asminfo")]
    public class AssemblyInfoTask : Task 
    {
        
        private string _asmVer = "1.0.*.*";
        private string _projectName = null ;
        private string _description = null;
        private string _config = null;
        private string _companyName = null;
        private string _productName = null;
        private string _copyright = null;
        private string _trademark = null;
        private string _culture = null;
        private bool _delaySign = false;
        private string _keyFile = null;
        private string _keyName = null;
        private string _path = 
System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(),"AssemblyInfo.cs");

        /// <summary>
        /// Options for the assembly info file.  These are specified as 
attribute/initializer pairs.
        /// </summary>
        /// <example>
        /// &lt;asminfo&gt;
        ///     &lt;attributes&gt;
        ///         &lt;option name="System.Reflection.AssemblyVersion" 
value="1.0.0.0" /&gt;
        ///         &lt;option name="System.Reflection.AssemblyTitle" value="My fun 
assembly" /&gt;
        ///         &lt;option name="System.Reflection.AssemblyDescription" 
value="More fun than a barrel of monkeys" /&gt;
        ///         &lt;option name="System.Reflection.AssemblyCopyright" 
value="Copyright (c) 2002, Monkeyboy, Inc." /&gt;
        ///     &lt;/attributes&gt;
        /// &lt;/asminfo&gt;
        /// </example>
        OptionSet _attributes = new OptionSet();
        [OptionSet("attributes")]
        public OptionSet AssemblyAttributes { 
            set { _attributes = value; }
            get { return _attributes; } }

        /// <summary>
        /// Path where the generated assemblyinfo.cs gets stored.
        /// </summary>
        [TaskAttribute("path")]
        public string Path 
        {
            get { return _path; }
            set { _path = value; }
        }

        [TaskAttribute("version")]
        public string AssemblyVersion 
        {
            get { return _asmVer; }
            set { _asmVer = value; }
        }

        [TaskAttribute("name")]
        public string ProjectName  
        {
            get { return _projectName; }
            set { _projectName= value; }
        }

        [TaskAttribute("description")]
        public string ProjectDescription 
        {
            get { return _description; }
            set { _description = value; }
        }

        [TaskAttribute("config")]
        public string AssemblyConfiguration 
        {
            get { return _config; }
            set { _config = value; }
        }

        [TaskAttribute("companyname")]
        public string CompanyName 
        {
            get { return _companyName; }
            set { _companyName= value; }
        }

        [TaskAttribute("productname")]
        public string ProductName 
        {
            get { return _productName; }
            set { _productName = value; }
        }

        [TaskAttribute("copyright")]
        public string Copyright 
        {
            get { return _copyright; }
            set { _copyright = value; }
        }

        [TaskAttribute("trademark")]
        public string Trademark 
        {
            get { return _trademark; }
            set { _trademark = value; }
        }

        [TaskAttribute("culture")]
        public string Culture 
        {
            get { return _culture; }
            set { _culture = value; }
        }
        
        [TaskAttribute("delaysign")]
        public bool DelaySign 
        {
            get { return _delaySign; }
            set { _delaySign = value; }
        }

        [TaskAttribute("keyfile")]
        public string KeyFile 
        {
            get { return _keyFile; }
            set { _keyFile= value; }
        }

        [TaskAttribute("keyname")]
        public string KeyName 
        {
            get { return _keyName; }
            set { _keyName= value; }
        }

        protected override void ExecuteTask()  
        {
            try
            {
                                Log.WriteLine(LogPrefix + " Creating assembly info at 
{0}",Path);
                CodeCompileUnit ccu = new CodeCompileUnit();

                foreach (OptionElement ov in AssemblyAttributes)
                {
                    ccu.AssemblyCustomAttributes.Add(
                                                CreateAttributeDecl(ov.OptionName,
                                                        
Project.ExpandProperties(ov.Value,this.Location)));
                }

                
AddAssemblyAttribute("System.Reflection.AssemblyVersion",AssemblyVersion,ccu);
                
AddAssemblyAttribute("System.Reflection.AssemblyTitle",ProjectName,ccu);
                
AddAssemblyAttribute("System.Reflection.AssemblyDescription",ProjectDescription,ccu);
                
AddAssemblyAttribute("System.Reflection.AssemblyConfiguration",AssemblyConfiguration,ccu);
                
AddAssemblyAttribute("System.Reflection.AssemblyCompany",CompanyName,ccu);
                
AddAssemblyAttribute("System.Reflection.AssemblyProduct",ProductName,ccu);

                if (Copyright == null)
                {
                    Copyright = String.Format("Copyright (c) {1} {0}.  All Rights 
Reserved.",DateTime.Now.Year,CompanyName);
                }
                
AddAssemblyAttribute("System.Reflection.AssemblyCopyright",Copyright,ccu);
                
AddAssemblyAttribute("System.Reflection.AssemblyTrademark",Trademark,ccu);
                AddAssemblyAttribute("System.Reflection.AssemblyCulture",Culture,ccu);
                
AddAssemblyAttribute("System.Reflection.AssemblyDelaySign",DelaySign,ccu);
                AddAssemblyAttribute("System.Reflection.AssemblyKeyFile",KeyFile,ccu);
                AddAssemblyAttribute("System.Reflection.AssemblyKeyName",KeyName,ccu);

                CSharpCodeProvider ccp = new CSharpCodeProvider();
                ICodeGenerator gen = ccp.CreateGenerator();

                System.IO.StreamWriter w = new 
System.IO.StreamWriter(Path,false,System.Text.Encoding.Default);
                gen.GenerateCodeFromCompileUnit(ccu,w,new CodeGeneratorOptions());

                w.Close();
                                Log.WriteLine(LogPrefix + " Created assembly info at 
{0}",Path);
                        }
            catch (Exception e)
            {
                                Log.WriteLine(LogPrefix + " Failed to create assembly 
info at {0}: {1}",Path,e.Message);
                throw new SourceForge.NAnt.BuildException(e.Message,e);
            }
        }

        private CodeAttributeDeclaration CreateAttributeDecl(String attName, String 
attValue)
        {
            CodeAttributeDeclaration result = new CodeAttributeDeclaration(attName);
            if (attValue != "")
            {
                                if (Verbose)
                                {
                                        Log.WriteLine(LogPrefix + " Adding Attribute 
{0}: {1}",attName,attValue);
                                }
                result.Arguments.Add(new CodeAttributeArgument(new 
CodePrimitiveExpression(attValue)));
            }
            return result;
        }

        private bool HasAttribute(String type, CodeCompileUnit ccu)
        {
            for (int i = 0; i < ccu.AssemblyCustomAttributes.Count; i++)
            {
                if (type.Equals(ccu.AssemblyCustomAttributes[i].Name))
                {
                    return true;
                }
            }
            return false;
        }

        private void AddAssemblyAttribute(String attrType, object attrValue, 
CodeCompileUnit ccu)
        {
            if (attrValue == null) return;
                        if (!HasAttribute(attrType,ccu))
                        {
                                if (Verbose)
                                {
                                        Log.WriteLine(LogPrefix + " Adding Attribute 
{0}: {1}",attrType,attrValue);
                                }
                                ccu.AssemblyCustomAttributes.Add(new 
CodeAttributeDeclaration(attrType,
                                        new CodeAttributeArgument(new 
CodePrimitiveExpression(attrValue))));
                        }
                        else if (Verbose)
                        {
                                Log.WriteLine(LogPrefix + " Ignoring duplicate 
attribute {0}: {1}",attrType,attrValue);
                        }
        }
    }
}

Reply via email to