在Visual Studio 2012中的所有源代码文件中插入版权声明/横幅 [英] Inserting copyright notice/banner in all source code files in Visual Studio 2012

查看:79
本文介绍了在Visual Studio 2012中的所有源代码文件中插入版权声明/横幅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一番谷歌搜索,我发现了这一点:使用Visual Studio宏将版权标头插入源文件中.看起来很有希望:

After some Googling I found this: Use a Visual Studio Macro to Insert Copyright Headers into Source Files. It looked promising:

// <copyright file="Sample.cs" company="My Company Name">
// Copyright (c) 2012 All Rights Reserved
// </copyright>
// <author>Leniel Macaferi</author>
// <date>08/30/2012 11:39:58 AM </date>
// <summary>Class representing a Sample entity</summary>

当我尝试 Tools->宏菜单选项在VS 2012中不再存在.这是证明:

When I tried Tools -> Macros menu option it wasn't there anymore in VS 2012. Here's the proof: Macros in Visual Studio 11 Developer Preview. They just dropped this functionality. :(

因此,我很想知道我可以使用哪个选项在使用Visual Studio 2012的解决方案中向所有现有源代码文件添加版权信息.是否有使用模板文件的标准方法(与 T4模板相关的东西)或 PowerShell 脚本?我可以编写一些代码来遍历扩展名为 .cs 的文件并添加版权信息,但这不是我要的.我想了解一些可自动执行此过程的工具.

So, I'm just curious to know which option I could use to add the copyright info to all existing source code files in my solution using Visual Studio 2012. Is there any standard way of doing this, using a template file (something related to T4 templates) or a PowerShell script? I could write some code to iterate over the files with .cs extension and add the copyright info but that is not what I'm after. I'd like to know about some tool to automate this process.

推荐答案

您可以创建一个新代码段,然后键入cp + double tab将通知插入您想要的位置(不必说您可以将关键字更改为您想要的任何内容).

You could create a new snippet and just type cp + double tab to insert the notice where you want (needless to say you can change the keyword to whatever you want).

它的唯一问题是,据我所知,摘要不支持时间功能,因此使用此技术似乎无法获取日期行的当前时间.一个不太好的解决方法是使时间字段可编辑(类似于mbox代码段的工作方式),然后手动插入时间.

The only problem with it is, from what I'm aware, snippets do not support time functions, so getting the current time for your date line seems impossible with this technique. A not so good workaround for this is to make the time fields editable (similar to how the mbox snippet works) and just insert the time manually.

这是一个有关代码段外观的示例.下面的代码段将自动获得类名称,并将版权声明插入您键入版权"并双击的位置.

Here's an example on how a snippet looks. The bellow snippet will get the class name automatically and insert the copyright notice in the place where you type 'copyright' and double tab.

方法1

 <?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Copyright</Title>
      <Shortcut>Copyright</Shortcut>
      <Description>Code snippet for Copyright notice</Description>
      <Author>author name</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>classname</ID>
          <Function>ClassName()</Function>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[// <copyright file="$classname$" company="My Company Name">
      // Copyright (c) 2012 All Rights Reserved
      // <author>Leniel Macaferi</author>
      // </copyright>
      ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

方法2

此外,这是您可以制作的一个程序示例.

Also, here's an example of a program you can make to do that for you.

List<string> files = new List<string>()
{
    "c:\\Form1.cs",
    "c:\\Form2.cs",
};

foreach (string file in files)
{
    string tempFile = Path.GetFullPath(file) + ".tmp";

    using (StreamReader reader = new StreamReader(file))
    {
        using (StreamWriter writer = new StreamWriter(tempFile))
        {
            writer.WriteLine(@"// <copyright file=" + Path.GetFileNameWithoutExtension(file) + @" company=My Company Name>
// Copyright (c) 2012 All Rights Reserved
// </copyright>
// <author>Leniel Macaferi</author>
// <date> " + DateTime.Now + @"</date>
// <summary>Class representing a Sample entity</summary>
");

            string line = string.Empty;
            while ((line = reader.ReadLine()) != null)
            {
                writer.WriteLine(line);
            }
        }
    }
    File.Delete(file);
    File.Move(tempFile, file);
}

当然需要某些错误捕获.但这应该为您提供了一个总体思路,即如何围绕该UI构造UI并添加要处理的文件.

Some error catching will be required of course. But this should give you the general idea how to construct an UI around it an add the files you will want to process.

方法3

还可以更改通常在以下位置找到的类的模板:

It's also possible to change the template for your classes that can be usually be found under:

C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ItemTemplates\CSharp\1033\

有时还需要编辑 ItemTemplatesCache 以显示结果.

Sometimes editing ItemTemplatesCache is also necessary to display the results.

以下是基于您的问题的示例模板:

Here's an example template based on your question:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

/* <copyright file=$safeitemrootname$ company="My Company Name">
   Copyright (c) 2012 All Rights Reserved
   </copyright>
   <author>Leniel Macaferi</author>
   <date>$time$</date>
   <summary>Class representing a Sample entity</summary>*/

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

这篇关于在Visual Studio 2012中的所有源代码文件中插入版权声明/横幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆