Snippet released by nuget

Source: Internet
Author: User
Like java Maven,. NET Has nuget. The convenience of nuget is not described here. The nuget release process is not as complicated as you think. Therefore, we hope that the developers in this article can contribute more useful packages.

1. Register a user
Official address for nuget: http://nuget.org/

2. Use nuget Packager
You can refer to the nuget documentation to create your package step by step. Reading documents is still a little troublesome.
The good news is that nuget has a dedicated package (which has a few interfaces). It creates a packager project for you, which simplifies many steps and is recommended.

OK. As a sample, I will create an mvvmlight snippet package and submit it to 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. Create an nuget packager Project

First, modify the package information of the following file.

(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 is placed in the tools directory, and then you need to install snippet Using Install. PS1 (Power shell script): Copy snippet

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

Install. PS1

Note: $ snippetfolder

# Install script for code snippets with NuGet# Required call to get environment variablesparam($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"}}}

Oh, of course, you still need an uninstall
Uninstall. PS1

# Uninstall script for code snippets with NuGet# Required call to get environment variablesparam($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"        }            }}

Note that if the content directory does not have any files, install. PS1 is not automatically run. (You can also use init. PS1 ),
But here I added a ReadMe file to run install. PS1.

4. Build package & upload
Run buildpublishpackage. CMD in the packager project.

The following page is displayed:

Finally, create a WPF project to try this snippet. Enter install-package mvvmlight. snippet in the "program management control point" of the WPF Project (Case Insensitive)

Enter "lightprop" in the Code and press the tab:

OK. You can try it too!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.