Create Nuget Pacakge with configuration of cloud c

2019-08-04 09:39发布

I would like to build Nuget package for my add-on which will be used by end user to install as startup task and after that they will upload their applications on window azure platform.

Let's take one simple web application and one cloud project now using Nuget end user will added add-on package it will add 2 files(exe & config) in web application project & add startup task to ServiceDefinition.csdef of cloud project as per shown in figure

enter image description here

How can i created this type of Nuget Pacakge?

Thanks in advance.

Update:

I have tried according with NICK's answer however i am getting problem with 2 webrole in cloud project according to shown in below fig. Cloud project with 2 webrole and error i am getting as per below

error to add startup task with powershell script

Also i have one question that If i am installing that Nuget package with command line then how i can consider all webrole projects to add exe and config file in solution??

1条回答
Juvenile、少年°
2楼-- · 2019-08-04 10:07

Nuget packages work based on a convention: http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#From_a_convention_based_working_directory

As far as the exe and config you can do the following:

  1. In your package directory make the following directories
    • mkdir lib (for the exe)
    • mkdir content (for the config)

All you have to do for the exe is drop the file in the lib directory and modify you .nuspec file just under the metadata node. There should be a "files" node (if not you can add one). Add something like this inside the files node:

<file src="content\my.exe" target="content\my.exe" />

The config is a little different. Just add a file named myname.config.transform to the content directory and add an entry into the .nuspec file:

A couple of things to note:

  1. If the config file does not exist in your app it will add one for you.
  2. You only have to add the nodes you want transformed if there is a file that already exists
  3. The transform file will do a complete match on your node so if the following existed in

your config file:

<add key="test" value="myval"/>

And in your transform you had:

<add key="test" value="myval2"/>

The resulting file would look like:

<add key="test" value="myval"/>
<add key="test" value="myval2"/>

As far as adding the startup task, that's been a little more tricky for me (there might be a much better way). I use powershell in the install.ps1 (just like the files above but you create a "tools" directory for it):

param($installPath, $toolsPath, $package, $project)

#Modify the service config - adding a new Startup task
$svcConfigFile = $DTE.Solution.Projects|Select-Object -Expand ProjectItems|Where-Object{$_.Name -eq 'ServiceDefinition.csdef'}
$ServiceDefinitionConfig = $svcConfigFile.Properties.Item("FullPath").Value
[xml] $xml = gc $ServiceDefinitionConfig

#Create startup and task nodes

# So that you dont get the blank ns in your node
$startupNode = $xml.CreateElement('Startup','http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition')
$taskNode = $xml.CreateElement('Task','http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition')
$taskNode.SetAttribute('commandLine','my.exe')
$taskNode.SetAttribute('executionContext','elevated')
$taskNode.SetAttribute('taskType','simple')
$startupNode.AppendChild($taskNode)

#Check to see if the startup node exists
$modified = $xml.ServiceDefinition.WebRole.StartUp
if($modified -eq $null){
    $modified = $xml.ServiceDefinition.WebRole
    $modified.PrependChild($startupNode)
}
else{
    $nodeExists = $false
    foreach ($i in $xml.ServiceDefinition.WebRole.Startup.Task){
        if ($i.commandLine -eq 'my.exe'){
            $nodeExists = $true
        }
    }
    if($taskNode -eq $null -and !$nodeExists){
        $modified.AppendChild($taskNode)
    }
}
$xml.Save($ServiceDefinitionConfig);

I hope this helps out.

--Nick

查看更多
登录 后发表回答