使用dotnet pack使用外部.exe文件创建NuGet软件包

编程入门 行业动态 更新时间:2024-10-25 16:24:46
本文介绍了使用dotnet pack使用外部.exe文件创建NuGet软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

编辑:我要解决的问题:

使用以下nuspec创建了新的NuGet包:

< package> <元数据> < id> VIPSNuget< / id> < version> 1.0.0.0< / version> < title> $ title $< / title> < authors> Devedse< / authors> < owners> Devedse< / owners> < licenseUrl> https://github/jcupitt/libvips/blob/master/COPYING< / licenseUrl> < projectUrl> https://github/jcupitt/libvips< / projectUrl> < requireLicenseAcceptance> false< / requireLicenseAcceptance> < description>只是VIPS的EXE文件< / description> < releaseNotes>第一个版本< / releaseNotes> < copyright>版权2017< / copyright> < tags> VIPS exe文件< / tags> < / metadata> < files> < file src = VIPS\ *。* target = tools /> < / files> < / package>

将所有.exe文件放在该文件夹中。

现在从我的ImageOptimizer中使用以下代码:

var userProfileDir = System.Environment.GetEnvironmentVariable( USERPROFILE ); string pathVips = Path.Combine(userProfileDir,Constants.VipsDir, vips.exe);

某些背景

我目前正在处理一个图像处理库,该库需要使用名为VIPS的外部工具将图像从JPEG转换为PNG。 (

但是VIPS目录中的所有文件都不包含在我创建的nupkg中:

(原因是我选择了工具是因为在dotnet框架/ NuGet的早期版本中,我使用了ILRepack之类的工具,这些工具会自动安装在工具目录中,然后可以从已部署的.exe文件的相对路径中调用该工具)。

(例如,另请参见

(现在所有VIPS二进制文件都放在其中:DeveImageOptimizer.1.0.0.nupkg\c ontentFiles\any\netstandard1.5\并再次显示内容)VIPS)

这并不是真正想要的,因为其中的DLL的实际大小应该

这种方法的另一个问题

我实际上不确定是否有一个好的/更好的解决方案,但是目前,当NuGet软件包还原时,它将被提取到%userprofile%.nuget\packages\vipsnuget ...

因此,每当我要对VIPS.exe运行命令时,都需要将Process.Start参数指向%userprofile%...。有一个更好的方法吗? (例如,找到在代码中还原NuGet软件包的位置吗?)

我最想要的

基本上我最想做的是创建一个NuGet包,如果包含该包,则将某个目录复制到项目的生成输出中。

类似这样: DeveImageOptimizer\bin\debug\VIPS ...

如果不存在

如果没有办法,我想听听将某些工具放入NuGet包并调用的正常方法是什么。

解决方案

在调用 dotnet pack 时,任何参数,它将使用当前文件夹中的 .csproj 文件创建nuget包,并忽略任何 .nuspec 文件。而且,如果查看 dotnet包参数,您将找不到如何直接指定 .nuspec 的方法。

但是, dotnet-pack 文档说,我们可以为dotnet pack命令提供MSBuild属性以进行打包。来自 NuGet元数据属性:

NuspecFile

用于打包的.nuspec文件的相对或绝对路径。 注意

如果指定了.nuspec文件,则该文件仅用于打包信息,而不会使用项目中的任何信息。

NuspecBasePath

.nuspec文件的基本路径。

因此,通过添加以下内容来修改您的 .csproj

< PropertyGroup> < NuspecFile>位置到您的nuspec文件< / NuspecFile> < / PropertyGroup>

注意:您总是可以使用 nuget pack 直接命令。

Edit: What I did to solve this:

Created a new NuGet package with the following nuspec:

<package> <metadata> <id>VIPSNuget</id> <version>1.0.0.0</version> <title>$title$</title> <authors>Devedse</authors> <owners>Devedse</owners> <licenseUrl>github/jcupitt/libvips/blob/master/COPYING</licenseUrl> <projectUrl>github/jcupitt/libvips</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Just the EXE file for VIPS</description> <releaseNotes>First release</releaseNotes> <copyright>Copyright 2017</copyright> <tags>VIPS exe file</tags> </metadata> <files> <file src="VIPS\*.*" target="tools" /> </files> </package>

Placed all the .exe files in that folder.

Now from my ImageOptimizer I use the following code:

var userProfileDir = System.Environment.GetEnvironmentVariable("USERPROFILE"); string pathVips = Path.Combine(userProfileDir, Constants.VipsDir, "vips.exe");

Some Background

I'm currently working on an Image Processing library that needs to convert an Image from JPEG to PNG using the external tool named VIPS. (github/jcupitt/libvips).

Since I didn't want to include the binaries from VIPS in my own project I thought, let's make a NuGet package of it where the .exe file can be called from my code.

Steps I took

I create a new dotnet core solution using the 'dotnet new console' command. After that I created a directory named "VIPS" and pasted all binaries in there.

I then created a nuspec file containing the following XML:

<?xml version="1.0"?> <package> <metadata> <id>$id$</id> <version>$version$</version> <title>$title$</title> <authors>Devedse</authors> <owners>Devedse</owners> <licenseUrl>github/jcupitt/libvips/blob/master/COPYING</licenseUrl> <projectUrl>github/jcupitt/libvips</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Just the EXE file for VIPS</description> <releaseNotes>First release</releaseNotes> <copyright>Copyright 2017</copyright> <tags>VIPS exe file</tags> </metadata> <files> <file src="VIPS\*.*" target="tools" /> </files> </package>

I edited my .csproj file so that it only contained this:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard1.0</TargetFramework> <Version>1.0.4.0</Version> </PropertyGroup> </Project>

Problem

Whenever I run the 'dotnet pack' command now, everything seems to go fine:

But all the files from the VIPS directory are not included in my created nupkg:

(Reason why I chose tools is because in earlier versions of the dotnet framework / NuGet, I used tools like ILRepack that would be automatically installed in a tools directory which could then be called from a relative path of your deployed .exe file).

(Also see for example the nuspec code in github/gluck/il-repack/blob/master/build.gradle at line 92 where they also include the created .exe file in the 'tools' target)

Further investigation

What I then tried was to add all files from the VIPS directory into my csproj file and set the build action to Content.

All binary files now get included in the following 2 directories (so all duplicates which is also strange): vipsnuget.1.0.0.nupkg\content\VIPS\ vipsnuget.1.0.0.nupkg\contentFiles\any\netstandard1.0\VIPS\

However when I now include the NuGet package inside another project, and then pack that project it seems to also include all binaries in that package again too:

(All VIPS binaries are now placed inside: DeveImageOptimizer.1.0.0.nupkg\contentFiles\any\netstandard1.5\ and again in content\VIPS)

This isn't really desireable since the actual size of the DLL's in there should only be a few Kb's.

Another issue with this approach

I'm not actually sure if there's a good/better solution for this, but currently, when the NuGet package is restored, it will be extracted in %userprofile%.nuget\packages\vipsnuget...

So whenever I want to run a command against the VIPS.exe I need to point the Process.Start arguments to %userprofile%... . Is there a better way to do this? (E.g. find the place where NuGet packages are restored in code?)

What I would like best

Basically what I would like best, is to create a NuGet package where, if you include it, a certain directory will be copied to the build output of the project.

So something like: DeveImageOptimizer\bin\debug\VIPS...

If that doesn't exist

If there's no way to do that, I would like to hear what the normal approach would be for putting certain 'tools' inside a NuGet package and calling them from inside C# code.

解决方案

When you call dotnet pack without any arguments, it uses the .csproj file in the current folder to create a nuget package and ignore any .nuspec files. And if look into dotnet pack parameters you will not find how to specify .nuspec directly.

But, dotnet-pack docs says we can provide MSBuild properties to the dotnet pack command for the packing process. From NuGet metadata properties:

NuspecFile

Relative or absolute path to the .nuspec file being used for packing. Note

If the .nuspec file is specified, it's used exclusively for packaging information and any information in the projects is not used.

NuspecBasePath

Base path for the .nuspec file.

So modify your .csproj by adding the following

<PropertyGroup> <NuspecFile>location to your nuspec file</NuspecFile> </PropertyGroup>

Note: and you always could use nuget pack command directly.

更多推荐

使用dotnet pack使用外部.exe文件创建NuGet软件包

本文发布于:2023-11-10 09:27:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:软件包   文件   pack   dotnet   NuGet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!