Skip to content

Instantly share code, notes, and snippets.

@attilah
Last active April 8, 2025 20:22
Show Gist options
  • Select an option

  • Save attilah/fd3e71f03fd258c496179e0200c57b0b to your computer and use it in GitHub Desktop.

Select an option

Save attilah/fd3e71f03fd258c496179e0200c57b0b to your computer and use it in GitHub Desktop.
X.Y.Z.Sources nuget package
<Project>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<PropertyGroup>
<TargetFramework>netstandard1.0</TargetFramework>
<IsPackable>true</IsPackable>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>contentFiles</ContentTargetFolders>
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<NoWarn>CS8021</NoWarn>
<NoBuild>true</NoBuild>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
</PropertyGroup>
<ItemGroup>
<Compile Update="@(Compile)">
<Pack>true</Pack>
<PackagePath>$(ContentTargetFolders)\cs\netstandard1.0\$(PackageId)\%(RecursiveDir)\</PackagePath>
</Compile>
<EmbeddedResource Update="@(EmbeddedResource)">
<Pack>true</Pack>
<PackagePath>$(ContentTargetFolders)\any\any\$(PackageId)\%(RecursiveDir)\</PackagePath>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Remove="@(PackageReference)" />
</ItemGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
<Target Name="Compile" />
<Target Name="CopyFilesToOutputDirectory" />
</Project>
@markusschaber

markusschaber commented Aug 10, 2020

Copy link
Copy Markdown

It seems that project references do not work, and another problem seems to be that, while the ResX files are included within the NuGet Package, they're ignored (not processed) in the referencing project.

It seems to be hard to find documentation about this. :-(

I put a small code example on github: https://github.com/markusschaber/SourceOnlyNugetTest
The ConsoleApp1 fails with MissingManifestResource Exception.

@JakobFerdinand

Copy link
Copy Markdown

Hello,
Thank you for that great example and medium article.
I tried to use it but I experienced some issues when using it in an WPF environment.
As soon as an UserControl is added to the Project, the compailer cant find the nuget imported c# files.

I created that sample project to demonstrate the problem.
Can anyone help here? ;) Thank you!

@kzu

kzu commented Mar 28, 2021

Copy link
Copy Markdown

Added support in NuGetizer for the SuppressDependenciesWhenPacking property: devlooped/nugetizer#68

I learned about it thanks to this gist, so, thanks!

@gitfool

gitfool commented Mar 28, 2021

Copy link
Copy Markdown

@kzu can NuGetizer be used to define NuGet source packages more concisely?

@kzu

kzu commented Mar 30, 2021

Copy link
Copy Markdown

Oh, absolutely.

The above sample would look like the following:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.0</TargetFramework>
    <PackageId>X.Y.Z</PackageId>
    <PackBuildOutput>false</PackBuildOutput>
    <PackCompile>true</PackCompile>
    <PackEmbeddedResource>true</PackEmbeddedResource>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NuGetizer" Version="0.6.0" />
  </ItemGroup>

</Project>

And given a couple .cs and a .resx, you'd get a package with the following content after running dotnet restore followed by dotnet pack:

Package: X.Y.Z.1.0.0.nupkg
         C:\Delete\sourceonly\bin\X.Y.Z.nuspec
    Authors    : X.Y.Z.Sources
    Description: Package Description
    Version    : 1.0.0
  Contents:
    /contentFiles/
      any/
        netstandard1.0/
          Properties/
            Resources.resx (buildAction=EmbeddedResource)
      cs/
        netstandard1.0/
          Program.cs (buildAction=Compile)
          Shared/
            Helper.cs (buildAction=Compile)

(the output is from the dotnet-nugetize global tool, which can be run simply with nugetize on the project directory).

The actual console output looks way nicer though ;)

image

It makes it very easy to quickly iterate on the packaging structure without ever having to build or even pack/zip files.

If you wanted the .resx to also be provided only for the cs code language you just update those appropriately:

  <ItemGroup>
    <EmbeddedResource Update="@(EmbeddedResource)" CodeLanguage="cs" />
  </ItemGroup>
pwsh> nugetize

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.61
Package: X.Y.Z.1.0.0.nupkg
         C:\Delete\sourceonly\bin\X.Y.Z.nuspec
    Authors    : X.Y.Z.Sources
    Description: Package Description
    Version    : 1.0.0
  Contents:
    /contentFiles/
      cs/
        netstandard1.0/
          Program.cs (buildAction=Compile)
          Properties/
            Resources.resx (buildAction=EmbeddedResource)
          Shared/
            Helper.cs (buildAction=Compile)

@JakobFerdinand

Copy link
Copy Markdown

Hello,
Thank you for that great example and medium article.
I tried to use it but I experienced some issues when using it in an WPF environment.
As soon as an UserControl is added to the Project, the compailer cant find the nuget imported c# files.

I created that sample project to demonstrate the problem.
Can anyone help here? ;) Thank you!

I could find a fix for the problem.
You have to add

<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>

to your WPF project.

@gitfool

gitfool commented May 16, 2021

Copy link
Copy Markdown

@kzu I switched to NuGetizer in gitfool/Cake.Dungeon@9a0080b and all the csproj black magic disappeared. ❤️

image

@kzu

kzu commented May 17, 2021

Copy link
Copy Markdown

Awesome stuff @gitfool :D

@verquepasa

Copy link
Copy Markdown

@kzu I'm using nugetizer but I'm using a sqlproj non-standard sdk that requires the files to be buildAction=content. The files are included in the nupkg and in my test project but they default to buildAction=compile. Is there a way I can force them to be content either from the nupkg or from the csproj ?
image

@kzu

kzu commented Jun 16, 2021

Copy link
Copy Markdown

Absolutely! See https://github.com/devlooped/nugetizer/blob/main/src/NuGetizer.Tasks/NuGetizer.props#L89.

You can just add the following to change that default value for all Content items:

<ItemDefinitionGroup>
  <Content>
    <BuildAction>None</BuildAction/>
  </Content>
</ItemDefinitionGroup>

@hymccord

hymccord commented Jul 7, 2022

Copy link
Copy Markdown
<PackagePath>$(ContentTargetFolders)\cs\netstandard1.0\$(PackageId)\%(RecursiveDir)\</PackagePath>

This should not end with a \. Otherwise you'll get empty folders for the root content right under the project file.

image

@kzu

kzu commented Aug 3, 2022

Copy link
Copy Markdown

@inkahootz is that nugetizer or SDK pack?

@hymccord

hymccord commented Aug 4, 2022

Copy link
Copy Markdown

@inkahootz is that nugetizer or SDK pack?

SDK pack. The code from the gist is producing that.

@MisinformedDNA

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment