Skip to content

Commit 87c21c5

Browse files
authored
Merge pull request #10 from coderbusy/copilot/fix-dat-file-generation
Fix NuGet package automatic import: rename build files to match package ID
2 parents bc2fca4 + acd0d42 commit 87c21c5

File tree

9 files changed

+159
-2
lines changed

9 files changed

+159
-2
lines changed

LuYao.ResourcePacker.MSBuild/LuYao.ResourcePacker.MSBuild.csproj

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,23 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<ProjectReference Include="..\LuYao.ResourcePacker\LuYao.ResourcePacker.csproj" />
16+
<ProjectReference Include="..\LuYao.ResourcePacker\LuYao.ResourcePacker.csproj" PrivateAssets="all" />
1717
</ItemGroup>
1818

19+
<PropertyGroup>
20+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
21+
</PropertyGroup>
22+
23+
<Target Name="PackTaskDependencies" BeforeTargets="GenerateNuspec">
24+
<ItemGroup>
25+
<_PackageFiles Include="$(OutputPath)LuYao.ResourcePacker.dll">
26+
<PackagePath>tasks/$(TargetFramework)</PackagePath>
27+
<Visible>false</Visible>
28+
<BuildAction>Content</BuildAction>
29+
</_PackageFiles>
30+
</ItemGroup>
31+
</Target>
32+
1933
<ItemGroup>
2034
<Content Include="build\*.props" PackagePath="build\" />
2135
<Content Include="build\*.targets" PackagePath="build\" />

LuYao.ResourcePacker.MSBuild/build/LuYao.ResourcePacker.props renamed to LuYao.ResourcePacker.MSBuild/build/LuYao.ResourcePacker.MSBuild.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
<PropertyGroup>
44
<ResourcePackerEnabled Condition="'$(ResourcePackerEnabled)' == ''">true</ResourcePackerEnabled>
55
<ResourcePackerPattern Condition="'$(ResourcePackerPattern)' == ''">*.res.*</ResourcePackerPattern>
6-
<ResourcePackerOutputFileName Condition="'$(ResourcePackerOutputFileName)' == ''">$(AssemblyName).dat</ResourcePackerOutputFileName>
76
</PropertyGroup>
87
</Project>

LuYao.ResourcePacker.MSBuild/build/LuYao.ResourcePacker.targets renamed to LuYao.ResourcePacker.MSBuild/build/LuYao.ResourcePacker.MSBuild.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
AssemblyFile="$(MSBuildThisFileDirectory)..\tasks\netstandard2.0\LuYao.ResourcePacker.MSBuild.dll" />
55

66
<Target Name="PackResources" BeforeTargets="CopyFilesToOutputDirectory" Condition="'$(ResourcePackerEnabled)' == 'true'">
7+
<PropertyGroup>
8+
<ResourcePackerOutputFileName Condition="'$(ResourcePackerOutputFileName)' == ''">$(AssemblyName).dat</ResourcePackerOutputFileName>
9+
</PropertyGroup>
710
<ResourcePackerTask
811
ProjectDir="$(ProjectDir)"
912
OutputPath="$(OutputPath)"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nuget.config
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="LuYao.ResourcePacker" Version="1.0.0" />
13+
<PackageReference Include="LuYao.ResourcePacker.MSBuild" Version="1.0.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Include="Resources\**\*.res.*" CopyToOutputDirectory="Never" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Reflection;
2+
using LuYao.ResourcePacker;
3+
4+
// This example demonstrates using LuYao.ResourcePacker.MSBuild via NuGet package reference
5+
// The .dat file is automatically generated during build
6+
7+
var assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? "NuGetReferenceExample";
8+
var datFile = Path.Combine(AppContext.BaseDirectory, $"{assemblyName}.dat");
9+
10+
if (!File.Exists(datFile))
11+
{
12+
Console.WriteLine($"Error: DAT file not found at {datFile}");
13+
return 1;
14+
}
15+
16+
Console.WriteLine($"Reading resources from: {datFile}");
17+
Console.WriteLine();
18+
19+
using var reader = new ResourcePackageReader(datFile);
20+
21+
// List all resources
22+
Console.WriteLine("Available resources:");
23+
foreach (var key in reader.ResourceKeys)
24+
{
25+
Console.WriteLine($" - {key}");
26+
}
27+
Console.WriteLine();
28+
29+
// Read message resource
30+
var message = await reader.ReadResourceAsStringAsync("message");
31+
Console.WriteLine($"Message content:");
32+
Console.WriteLine(message);
33+
Console.WriteLine();
34+
35+
// Read config resource
36+
var config = await reader.ReadResourceAsStringAsync("config");
37+
Console.WriteLine($"Config content:");
38+
Console.WriteLine(config);
39+
40+
return 0;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# NuGet Reference Example
2+
3+
This example demonstrates how to use LuYao.ResourcePacker.MSBuild via NuGet package reference.
4+
5+
**Note**: This example is intended to show the expected project structure when using the NuGet package. To test it, you need to:
6+
1. Pack the NuGet package locally: `dotnet pack LuYao.ResourcePacker.MSBuild/LuYao.ResourcePacker.MSBuild.csproj -o /tmp/local-nuget`
7+
2. Create a `nuget.config` file pointing to your local package source
8+
3. Build this project
9+
10+
Alternatively, use the published NuGet package version once available.
11+
12+
## Project Structure
13+
14+
```
15+
NuGetReferenceExample/
16+
├── NuGetReferenceExample.csproj # Project file with NuGet package reference
17+
├── Program.cs # Application code
18+
├── Resources/
19+
│ ├── config.res.json # Resource file (JSON)
20+
│ └── message.res.txt # Resource file (text)
21+
└── README.md
22+
```
23+
24+
## Key Points
25+
26+
1. **NuGet Package References**: The project references both:
27+
- `LuYao.ResourcePacker`: Core library for reading packed resources at runtime
28+
- `LuYao.ResourcePacker.MSBuild`: Build-time MSBuild tasks for packing resources
29+
30+
2. **Automatic Build Integration**: The MSBuild targets and props files are automatically imported by NuGet, so you don't need to manually configure any build tasks.
31+
32+
3. **Resource Naming Convention**: Files matching the pattern `*.res.*` are automatically packed into a `.dat` file during build.
33+
34+
4. **Configuration**: You can customize the behavior using MSBuild properties:
35+
- `ResourcePackerEnabled`: Enable/disable resource packing (default: `true`)
36+
- `ResourcePackerPattern`: File pattern for resources (default: `*.res.*`)
37+
- `ResourcePackerOutputFileName`: Output filename (default: `$(AssemblyName).dat`)
38+
39+
## Building (with local NuGet package)
40+
41+
1. Create a `nuget.config` in this directory:
42+
```xml
43+
<?xml version="1.0" encoding="utf-8"?>
44+
<configuration>
45+
<packageSources>
46+
<clear />
47+
<add key="local" value="/tmp/local-nuget" />
48+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
49+
</packageSources>
50+
</configuration>
51+
```
52+
53+
2. Build:
54+
```bash
55+
dotnet build
56+
```
57+
58+
After building, the `NuGetReferenceExample.dat` file will be automatically generated in the output directory.
59+
60+
## Running
61+
62+
```bash
63+
dotnet run
64+
```
65+
66+
The application will read and display the contents of the packed resources.
67+
68+
## What Makes This Different from ExampleProject?
69+
70+
The main `ExampleProject` uses **project references** to LuYao.ResourcePacker.MSBuild, which is useful for development. This example uses **NuGet package reference**, which is how external users would consume the library.
71+
72+
When using NuGet packages, the MSBuild props and targets files must be named exactly as `<PackageId>.props` and `<PackageId>.targets` to be automatically imported. This example validates that the naming is correct.
73+
74+
For more information about NuGet MSBuild conventions, see the [official documentation on creating MSBuild props/targets packages](https://learn.microsoft.com/en-us/nuget/create-packages/creating-a-package#include-msbuild-props-and-targets-in-a-package).
75+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "Hello from resource file!",
3+
"timestamp": "2025-10-25"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a text resource file for testing NuGet package reference.

0 commit comments

Comments
 (0)