现在的位置: 首页 > 综合 > 正文

NuGet 发布 Snippet

2013年08月16日 ⁄ 综合 ⁄ 共 4095字 ⁄ 字号 评论关闭
就像 Java 的 Maven,.Net 有 NuGet。NuGet 的便利性,这里就不赘述了。而 NuGet 的发布过程也没有想象中的那么复杂,所以希望看到本文的开发者也能贡献更多好用的 Package。

1. 注册一个用户
NuGet 的官方地址: http://nuget.org/

2. 使用 NuGet Packager
    你完全可以参考 NuGet 的文档,一步步创建你的 Package。看文档还是有点麻烦的。
    好消息是 NuGet 上有个专门创建 Package 的 Package(有点拗口),它为你创建一个 Packager 工程, 简化不少步骤,推荐使用。
    

OK,作为Sample,我将创建一个 Mvvmlight Snippet Package 提交到 NuGet 上。

Mvvmlight.snippet

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <!--View Model Property-->
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Mvvmlight Property</Title>
      <Shortcut>lightprop</Shortcut>
      <Description>Property of View Model for WPF & Silverlight MVVM </Description>
      <Author>Felix Fang</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Property Type</ToolTip>
          <Default>string</Default>
        </Literal>
        <Literal>
          <ID>property</ID>
          <ToolTip>Property Name</ToolTip>
          <Default>MyProperty</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
        private $type$ _$property$;
        public $type$ $property$
        {
            get { return _$property$; }
            set 
            { 
               _$property$ = value;
               RaisePropertyChanged<$type$>(() => this.$property$);
            }
        }
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

3. 创建一个 NuGet Packager 工程

首先需要修改下面文件的 Package 信息

(1) Package.nuspec

<?xml version="1.0"?>
<package >
  <metadata>
    <id>Mvvmlight.Snippet</id>
    <version>1.0.0</version>
    <authors>Felix Fang</authors>
    <owners>Felix Fang</owners>
    <projectUrl>http://blog.csdn.net/fangxinggood</projectUrl>
    <iconUrl>https://www.codeplex.com/favicon.ico</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>
      Mvvmlight Code Snippet
    </description>
    <releaseNotes>
    </releaseNotes>
    <copyright>Copyright FelixFang 2012</copyright>
    <tags></tags>
    <dependencies>
      <dependency id="Mvvmlight" version="1.2.10" />
    </dependencies>
  </metadata>
  <files>
    <file src="lib\**" target="lib" />
    <file src="tools\**" target="tools" />
    <file src="content\**" target="content" />
  </files>
</package>

(2) BuildPublishPackage.cmd (AppKey, ProjectUrl)

Mvvmlight.snippet 放在 tools 目录下,然后你需要利用 install.ps1 (Power Shell 脚本) 来安装 snippet:将 snippet 拷贝到

$HOME\My Documents\Visual Studio $vsVersion\Code Snippets\Visual C#\My Code Snippets\

install.ps1

注意修改: $snippetFolder

# Install script for code snippets with NuGet

# Required call to get environment variables
param($installPath, $toolsPath, $package, $project)
 
# You only have to customize the $snippetFolder name below,
# don't forget to rename the $snippetFolder of the other file
# ("uninstall.ps1") as well
 
$snippetFolder = "MVVMLight"
 
# Actual script start
$source = "$toolsPath\*.snippet"
$vsVersions = @("2008", "2010", "2012")
 
Foreach ($vsVersion in $vsVersions)
{
	$myCodeSnippetsFolder = "$HOME\My Documents\Visual Studio $vsVersion\Code Snippets\Visual C#\My Code Snippets\"
	if (Test-Path $myCodeSnippetsFolder)
	{
		$destination = "$myCodeSnippetsFolder$snippetFolder"
		if (!($myCodeSnippetsFolder -eq $destination))
		{
			if (!(Test-Path $destination))
			{
				New-Item $destination -itemType "directory"
			}
		
			"Installing code snippets for Visual Studio $vsVersion"
			Copy-Item $source $destination
		}
		else
		{
			"Define a value for snippetFolder variable, cannot be empty"
		}
	}
}

哦,当然还需要一个 uninstall
uninstall.ps1

# Uninstall script for code snippets with NuGet

# Required call to get environment variables
param($installPath, $toolsPath, $package, $project)
 
# You only have to customize the $snippetFolder name below,
# don't forget to rename the $snippetFolder of the other file
# ("install.ps1") as well
 
$snippetFolder = "MVVMLight"
 
# Actual script start
$source = "$toolsPath\*.snippet"
$vsVersions = @("2008", "2010", "2012")
 
Foreach ($vsVersion in $vsVersions)
{
    $myCodeSnippetsFolder = "$HOME\My Documents\Visual Studio $vsVersion\Code Snippets\Visual C#\My Code Snippets\"
    if (Test-Path $myCodeSnippetsFolder)
    {
        $destination = "$myCodeSnippetsFolder$snippetFolder"
        if (!($myCodeSnippetsFolder -eq $destination))
        {        
            if (Test-Path $destination)
            {
                "Uninstalling code snippets for Visual Studio $vsVersion"
                Remove-Item $destination -recurse -force
            }
        }
        else
        {
            "Define a value for snippetFolder variable, cannot be empty"
        }        
    }
}

需要注意的是,如果 content 目录没有任何文件是不会自动运行 install.ps1 的。(当然你还可以利用 init.ps1),
但是这里我加了个 readme 文件,让它运行 install.ps1。

4. Build Package & Upload
运行 Packager 工程中的 BuildPublishPackage.cmd 

成功后就能看到下面的页面:

最后,创建一个 WPF 工程来试试这个 snippet ,在WPF工程的 "程序包管理控制点" 里输入:install-package mvvmlight.snippet (大小写不区分)

在代码中输入 "lightprop" 按下 tab:

OK,你也来试试吧!

抱歉!评论已关闭.