Visual Studio: A macro to set assembly properties with solution or directory root scope

670 Views Asked by At

I would like a macro for Visual Studio to set assembly properties. Is there a code template somewhere for this? Either within VS or on the interwebs? I'm looking to set Company name, trademark etc.

Update:

O.k. this is what I've just written/hacked in C#. I thought there might be a snippet tho.

  class Program
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        string rootDir = args[0];
        string qualifyingFileName = args[1];
        string disqualifyingPathNamePart = args[2];
        string assAttrib = args[3];
        string assAttribReplace = args[4];
        bool removeComments = bool.Parse(args[5]);
        bool removeEmptyLines = bool.Parse(args[6]);
        bool IsCSharp = false;

        IEnumerable<string> assFiles = Directory.EnumerateFiles(rootDir, qualifyingFileName, SearchOption.AllDirectories);
        foreach (string ass in assFiles)
        {
            try
            {
                if (ass.Contains(disqualifyingPathNamePart)) { continue; }
                IsCSharp = ass.EndsWith(".cs");
                List<string> assFileLines = new List<string>();
                assFileLines.AddRange(File.ReadAllLines(ass));

                for (int i = assFileLines.Count - 1; i >= 0; i--)
                {
                    if (assFileLines[i].Trim().StartsWith(IsCSharp ? "using" : "Imports")) { assFileLines.RemoveAt(i); continue; }
                    if (assFileLines[i].Trim().StartsWith("Option")) { assFileLines.RemoveAt(i); continue; }

                    if ((removeComments) && (assFileLines[i].Trim().StartsWith(IsCSharp ? "//" : "'"))) { assFileLines.RemoveAt(i); continue; }
                    if ((removeEmptyLines) && (assFileLines[i].Trim().Length == 0)) { 
                        assFileLines.RemoveAt(i); continue;
                    }
                    if (!assFileLines[i].Contains(assAttrib)) { continue; }
                    assFileLines[i] = assAttribReplace;
                }

                if (IsCSharp)
                {
                    assFileLines.Insert(0, "using System;");
                    assFileLines.Insert(1, "using System.Resources;");
                    assFileLines.Insert(2, "using System.Reflection;" );
                    assFileLines.Insert(3, "using System.Runtime.InteropServices;");
                    assFileLines.Insert(4, "");
                }
                else
                {
                    assFileLines.Insert(0, "Option Strict On");
                    assFileLines.Insert(1, "Option Explicit On");
                    assFileLines.Insert(2, "");
                    assFileLines.Insert(3, "Imports System");
                    assFileLines.Insert(4, "Imports System.Resources");
                    assFileLines.Insert(5, "Imports System.Reflection");
                    assFileLines.Insert(6, "Imports System.Runtime.InteropServices");
                    assFileLines.Insert(7, "");
                }
                File.WriteAllLines(ass, assFileLines);
            }
            catch { }
        }
        Console.WriteLine("Finito");
        Console.ReadLine();
    }
}

useage: A batch file full of statements like these:

> macros.exe D:\Devzone AssemblyInfo.cs 3PL AssemblyCompany
> "[assembly: AssemblyCompany(\"Your company name\")]" true true
> 
> macros.exe D:\Devzone AssemblyInfo.cs 3PL AssemblyCopyright
> "<Assembly: AssemblyCopyright(\"Copyright © 2003-2013. Your company
> name. All Rights Reserved.\")>" true true
> 
> macros.exe D:\Devzone AssemblyInfo.vb 3PL AssemblyCopyright
> "<Assembly: AssemblyCopyright(\"Copyright © 2003-2013. Your company
> name. All Rights Reserved.\")>" true true
2

There are 2 best solutions below

3
On BEST ANSWER

Any reason why you dont use an assemblyinfo file and define its attributes?

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.assemblyinfo.aspx

1
On

Here is an old post on creating your own templates.

http://devrants.blog.com/2006/09/19/visual-studio-template-beyond-the-basics/

Templates are zip files containing:

.vstemplate

one or more custom files.

By customising these you can save a lot of cut-and-paste time and effort.

This page of Template parameters is not complete.

$safeitemrootname$ = This appears to be the name that the user enters into the dialog.

If you combine this with multi-item templates as defined here. This allows you to use the supplied name as a root of a class. For example you could have a template that takes a name such as Entity.cs that creates the following files:

EntityModel.cs
IEntityModel.cs
IEntityView.cs
IEntityController.cs
EntityController.cs

$fileinputname$ = This also work in the template

$XmlConvert_itemname$ = what does this do? I think this is a wizard populated item.

It appears that the WizardExtension appears to work in item templates too despite the documentation.