在Visual Studio 2012中的所有源代码文件中插入版权声明/横幅

Len*_*rri 43 c# banner visual-studio-2012 copyright-display

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

// <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>
Run Code Online (Sandbox Code Playgroud)

当我尝试使用Tools -> Macros菜单选项时,VS 2012中已经不存在了.这是证明:Visual Studio 11开发人员预览版中的宏.他们刚刚放弃了这个功能.:(

所以,我只是想知道我可以使用哪个选项将版权信息添加到我的解决方案中使用Visual Studio 2012的所有现有源代码文件.有没有任何标准的方法来执行此操作,使用模板文件(与之相关的内容) T4 templates)或PowerShell脚本?我可以编写一些代码来迭代带有.cs扩展名的文件并添加版权信息,但这不是我所追求的.我想知道一些自动化这个过程的工具.

coo*_*ine 35

您可以创建一个新的代码段,只需键入cp + double选项卡即可将通知插入所需的位置(不用说您可以将关键字更改为您想要的任何内容).

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

这是一个关于代码片段外观的示例.下面的代码片段将自动获取类名,并在您输入"copyright"和双重标签的位置插入版权声明.

方法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>
Run Code Online (Sandbox Code Playgroud)

方法2

此外,这里有一个程序示例,您可以为此执行此操作.

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);
}
Run Code Online (Sandbox Code Playgroud)

当然需要一些错误捕获.但是这应该给你一般的想法,如何围绕它构建一个UI,添加你想要处理的文件.

方法3

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

C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ItemTemplates\CSharp\1033\
Run Code Online (Sandbox Code Playgroud)

有时,编辑ItemTemplatesCache也是显示结果所必需的.

这是一个基于您的问题的示例模板:

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$
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 遗憾的是,我不知道是否有一种方法可以在不打开它们的情况下修改.cs文件(虽然看起来非常容易制作一个为你做这个的程序).如果你能找到一个,我会感兴趣,因为我自己正在寻找更快的解决方案.至于自己制作一个,这个方法看起来很简单.在数组中添加所需的文件,遍历每个文件,然后将注释行插入所需的位置. (2认同)

Len*_*rri 21

我将在这里添加一个我在这篇文章中找到的PowerShell脚本:Powershell - 版权标题生成器脚本.它在发布问题之前抓住了我的想法......

param($target = "C:\MyProject", $companyname = "My Company")

$header = "//-----------------------------------------------------------------------

// <copyright file=""{0}"" company=""{1}"">

// Copyright (c) {1}. All rights reserved.

// </copyright>

//-----------------------------------------------------------------------`r`n"

function Write-Header ($file)
{
    $content = Get-Content $file

    $filename = Split-Path -Leaf $file

    $fileheader = $header -f $filename,$companyname

    Set-Content $file $fileheader

    Add-Content $file $content
}

Get-ChildItem $target -Recurse | ? { $_.Extension -like ".cs" } | % `
{
    Write-Header $_.PSPath.Split(":", 3)[2]
}
Run Code Online (Sandbox Code Playgroud)

我通过微小的修改写了它,以适应我的需求:

使用PowerShell在所有源代码文件中插入版权声明/标题/标题

  • 虽然我很欣赏coolmine的努力,但你的答案是任何谷歌搜索者都会想到的.也许继续接受你自己的答案.:) (4认同)
  • 如果该功能有点'更聪明'并且替换任何现有的标题,那么你可以多次运行它而不会发送垃圾邮件标题会很好 (2认同)

Tha*_*den 7

如果这可能仍然有趣,那么有一个License头插件可以在创建时为任何文件添加一个完整的可自定义头.目前,这适用于VS2013,但不适用于VS 2015.