I thought about building some small framework. The plan would be to build some libs with some helper classes. For example a lib for my camera classes which I can then use especially in my prototype projects. The idea of having the libs available as nugets is nice since I could do some prototyping in less time and also do it on machines where I don‘t have my code base directly available.
Since many here in the community are already using nugets for doing similar things and code distribution I thought I ask here for some guideline / workflow / best practise / advice.
I will describe where I am at the moment:
As a test I created a camera library / nuget with the project name Kwyrky.MyFramework.Camera
with a simple Camera2D
class as a starting point. The base namespace is be the same as the project name, so Kwyrky.MyFramework.Camera
in this case. Is the naming of the project and namespace good here or should I consider to name it different? MonoGame.Kwyrky.MyFramework.Camera
?
I read there are SDK-style
projects and non-SDK-style
projects. I think at this point MonoGame in the current version uses / provides the older non-SDK-style
project templates? I guess this will be changed in future updates of MonoGame? Will this be an issue if I want to update my libs / nugets to newer MonoGame versions in the future? Just asking because updating library projects manually is something I want to avoid if possible. Anyway I think in both SDK-style
projects and non-SDK-style
projects it is possible to configure the nuget metadata in different places:
- Visual Studio - project properties - package
- In a
*.nuspec
file - As parameters when using the
dotnet pack
command
I tried using nuget CLI
first and I could create a nuspec
file using the nuget spec
command.
NUSPEC FILE:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Kwyrky.MyFramework.Camera</id>
<version>1.0.0</version>
<title>Kwyrky.MyFramework.Camera</title>
<authors>Kwyrky</authors>
<description>Cameras for MonoGame.</description>
<tags>MonoGame Kwyrky Camera</tags>
<dependencies>
<group targetFramework="net6.0" />
</dependencies>
</metadata>
</package>
If on Windows and Visual Studio is available everything is also configurable in the project properties
in the package
section.
I tried to do the packaging also using the nuget CLI tool
but it did only work using the dotnet CLI tool
. Iirc it was because nuget can not handle non-SDK-style
projects?
It is possible to provide all metadata also with the dotnet pack
command directly which will also overwrite the settings made in a nuspec file if one is present. I think I will use method 3. in the future since I can then also create packages on linux where there is no Visual Studio available and also avoid creating a nuspec file.
DOTNET PACK AND PUSH COMMANDS:
dotnet pack -c Release /p:PackageId="Kwyrky.MyFramework.Camera" /p:Version="1.0.0" /p:Title="Kwyrky MyFramework Camera" /p:Authors="Kwyrky" /p:Description="Cameras for MonoGame." /p:PackageTags="MonoGame Kwyrky Camera" /p:PackageIcon="icon.png" /p:PackageOutputPath="H:\Dev\0001\LocalNugetFeed"
The icon file icon.png
(128x128 as size is recommended and format can be either png or jpg with transparency) in this case has to be placed in the project root directory (where the csproj file is). The parameter PackageIcon
specifies the path to the icon file inside the finished nuget package. To get the icon into the package it needs to be added by adding an ItemGroup entry to the csproj file like this:
<ItemGroup>
<None Update="icon.png" Pack="true" PackagePath="" />
</ItemGroup>
dotnet pack -c Release
dotnet pack -c Release /p:PackageOutputPath="H:\Dev\0001\LocalNugetFeed"
NOTE: The PackageOutputPath parameter will put the nuget directly into my local nuget feed. I save running a second command this way which would be something like:
dotnet nuget push ".\bin\Release\Kwyrky.MyFramework.Camera.1.0.0.nupkg" -s "H:/Dev/0001/LocalNugetFeed"
dotnet nuget push ".\bin\Release\Kwyrky.MyFramework.Camera.1.0.0.nupkg" --source "H:/Dev/0001/LocalNugetFeed"
When pushing to nuget.org instead of a local feed later I think I will need an API Key for authentication which I can provide like this:
dotnet nuget push ".\bin\Release\Kwyrky.MyFramework.Camera.1.0.0.nupkg" -s "nuget.org" -k "<your-api-key>"
dotnet nuget push ".\bin\Release\Kwyrky.MyFramework.Camera.1.0.0.nupkg" -k "<your-api-key>"
LOCAL NUGET FEED COMMANDS:
For testing I am using a local nuget feed which in my case is just a simple folder and using the folder name as the feed name:
dotnet nuget add source "H:\Dev\0001\LocalNugetFeed" -n LocalNugetFeed
dotnet nuget add source "H:\Dev\0001\LocalNugetFeed" --name LocalNugetFeed
After adding a local nuget feed it should show up when running this command which lists all available nuget sources:
dotnet nuget list source
Registered Sources:
1. nuget.org [Enabled]
https://api.nuget.org/v3/index.json
2. LocalNugetFeed [Enabled]
H:\Dev\0001\LocalNugetFeed
3. Microsoft Visual Studio Offline Packages [Enabled]
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
To later add a nuget to a new project where you want to use it:
dotnet add package "Kwyrky.MyFramework.Camera" --version "1.0.0" --source "H:\Dev\0001\LocalNugetFeed"