3

I create my nuget packages in gitlab with the following command line.

nuget pack -Prop Configuration=Release -OutputDirectory nuget %REPONAME%\%APPNAME%\%APPNAME%.csproj

If I declare Build Action to Content and Copy To Output Directory to Always, it is not applied in the nuget package or rather when I install it.

I'm a little bit confused about the answers here: Set content files to "copy local : always" in a nuget package

I don't have a *.nuspec file. It is automatically generated by the command above.


  1. I have declared a Install.ps1 file in my solution in tools/*
  2. I want to automatically include the tools folder with the Install.ps1 script into the nuget package, that this script is invoked on install

Install.ps1 script

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

$configItem = $project.ProjectItems.Item("snap7.dll")

# set 'Copy To Output Directory' to 'Always'
#    0 = Never
#    1 = Always
#    2 = PreserveNewestFile
$copyToOutput = $configItem.Properties.Item("CopyToOutputDirectory")
$copyToOutput.Value = 1

# set 'Build Action' to 'Content'
#    0 = None
#    1 = Compile
#    2 = Content
#    3 = EmbeddedResource
$buildAction = $configItem.Properties.Item("BuildAction")
$buildAction.Value = 2
Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
  • How about this issue? Could you get useful information from Wendy answer? I have created a package with `install.ps1` in the Tools folder, it works fine when I install it:https://1drv.ms/u/s!Ai1sp_yvodHfe8hmU7Duo9pCh5U – Leo Liu Nov 06 '17 at 08:27
  • This is not the solution I want to have. I'm currently developing a little application to add this file automatically in gitlab. – Dominic Jonas Nov 06 '17 at 08:42
  • Thanks for your reply. Since you want to add `install.ps1` automatically, which was not included in your previous question (If I declare Build Action to Content and Copy To Output Directory to Always, it is not applied in the nuget package or rather when I install it.). So you can update you question and share your solution here, so it could help other community members who get the same issues. Thanks. – Leo Liu Nov 06 '17 at 08:55

3 Answers3

1

You need to add your install.ps1 file into your nuget package.

After use nuget pack command that you provided in your original post, it will generate a package in nuget folder.

Then please open this package with NuGet Package Explorer and add a tools folder through CONTENT -> Add -> Tools Folder in NuGet Package Explorer menu. And then add your install.ps1 file into the tools folder.

Now when the package install into a project, it will call the install.ps1 file to set the file's Copy To Output Directory property as Copy Always.

Weiwei
  • 3,674
  • 1
  • 10
  • 13
0

To automatically add files/folders to a nuget package, I have written a little tool called NuGetLib.

The global variables are automatically set in the installer.

In my .gitlab-ci.yml only these 4 lines are needed to build the nuget package with all dependencies and the tools folder:

# create output directory
- mkdir nuget
# create nuget package
- nuget pack -Prop Configuration=Release -OutputDirectory nuget %REPONAME%\%APPNAME%\%APPNAME%.csproj
# Add tools folder
- nugetlib add -t nuget -f .\%REPONAME%\%APPNAME%\tools
# deploy package to local repo
- xcopy /s /y /i nuget %NUGET_PATH%

You can download it here: https://github.com/dojo90/nugetLib/releases

Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
0

From the OP:

I don't have a *.nuspec file. It is automatically generated by the command above.

That shouldn't prevent you from using a *.nuspec file. It should be named with the same name as your project (e.g. My.Consoto.Library.csproj ==> My.Consoto.Library.nuspec) - and you can still use the same .csproj command as shown in the OP.

Then, in the *.nuspec you can have code like this:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata minClientVersion="2.5">
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>Consoto LLC</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <copyright>$copyright$</copyright>    
    <projectUrl>https://dev.azure.com/Consoto/</projectUrl> 
    <iconUrl>http://www.consoto.com/images/favicon.ico</iconUrl> 
    <releaseNotes>My custom release notes</releaseNotes> 
    <language>en-US</language> 
    <tags>MyCustomTag1</tags>
    <dependencies>
      <dependency id="My.Other.Library" version="1.0.0" />
    </dependencies>
  </metadata>  
  <files>
        <file src="nuget_tools/install.ps1" target="tools/" />
   </files>
</package>

The tokens in the .nuspec will receive the values of the .csproj's AssemblyInfo.cs.

I realize my example above has a hard-coded dependency, I haven't tried it yet without that part.. so I'll check back in and update my answer to see if a more genericized *nuspec will work seamlessly with the command demonstrated in the OP.

bkwdesign
  • 1,953
  • 2
  • 28
  • 50
  • Actually I do not use this project format anymore. With the new format, everything is done automatically if you use `dotnet pack -c Release` – Dominic Jonas Sep 18 '19 at 06:20