Skip to content

Commit

Permalink
Allow running samples from main solution for debugging
Browse files Browse the repository at this point in the history
Projects can be added to the main solution and set as the target for roslyn debugging as needed.

The new directory targets simulate package referencing and remove the actual package reference that's otherwise used (using solution name to detect standalone vs debugging sources mode).
  • Loading branch information
kzu committed Dec 23, 2024
1 parent 8c93a92 commit afbf296
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Sample/Common/Product.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Sample;
using StructId;

namespace Sample;

public record Product(ProductId Id, string Name);

Expand Down
4 changes: 2 additions & 2 deletions src/Sample/ConsoleDb/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ConsoleDb;
using Dapper;
using Dapper;
using Microsoft.Data.Sqlite;
using StructId;

SQLitePCL.Batteries.Init();

Expand Down
9 changes: 9 additions & 0 deletions src/Sample/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>
<Import Project="..\Directory.Build.props" />

<PropertyGroup>
<FromSource>false</FromSource>
<FromSource Condition="$(SolutionName) == 'StructId'">true</FromSource>
</PropertyGroup>

</Project>
36 changes: 36 additions & 0 deletions src/Sample/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project>
<Import Project="..\Directory.Build.targets" />

<PropertyGroup>
<AfterMicrosoftNETSdkTargets>$(AfterMicrosoftNETSdkTargets);$(MSBuildThisFileDirectory)..\StructId.Package\StructId.targets</AfterMicrosoftNETSdkTargets>
</PropertyGroup>

<ItemGroup Condition="$(FromSource)">
<PackageReference Remove="StructId" />
<PackageReference Include="Scriban" Version="5.12.1" GeneratePathProperty="true" />

<ProjectReference Include="$(MSBuildThisFileDirectory)..\StructId.Analyzer\StructId.Analyzer.csproj" OutputItemType="Analyzer" />
<ProjectReference Include="$(MSBuildThisFileDirectory)..\StructId.CodeFix\StructId.CodeFix.csproj" OutputItemType="Analyzer" />
<ProjectReference Include="$(MSBuildThisFileDirectory)..\StructId.Package\StructId.Package.msbuildproj" ReferenceOutputAssembly="false" />

<Analyzer Include="$(PkgScriban)/lib/netstandard2.0/Scriban.dll" Condition="Exists('$(PkgScriban)/lib/netstandard2.0/Scriban.dll')" />
</ItemGroup>

<Target Name="FromSource" Condition="$(FromSource)" BeforeTargets="AddStructId">
<ItemGroup>
<!-- These would be added from the package-relative paths by the StructId.targets -->
<StructIdCompile Include="$(MSBuildThisFileDirectory)..\StructId\*.cs" />
<StructIdTemplates Include="$(MSBuildThisFileDirectory)..\StructId\Templates\*.cs" />
</ItemGroup>

<ItemGroup>
<!-- Templates should always be added by buildTransitive targets -->
<StructId Include="@(StructIdTemplates)" Link="StructId\Templates\%(StructIdTemplates.Filename)%(StructIdTemplates.Extension)" />
</ItemGroup>

<ItemGroup Condition="'$(HasStructIdReference)' != 'false'">
<StructId Include="@(StructIdCompile)" Link="StructId\%(StructIdCompile.Filename)%(StructIdCompile.Extension)" />
</ItemGroup>
</Target>

</Project>
1 change: 1 addition & 0 deletions src/Sample/MinimalApi/MinimalApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<RestoreSources>https://api.nuget.org/v3/index.json;$(PackageOutputPath)</RestoreSources>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<HasStructIdReference>false</HasStructIdReference>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Sample/MinimalApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sample;
using StructId;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
Expand All @@ -8,6 +9,6 @@

app.Run();

readonly partial record struct UserId : IStructId<int>;
readonly partial record struct UserId(int Value) : IStructId<int>;

record User(UserId id, string Alias);
3 changes: 2 additions & 1 deletion src/Sample/MvcWebApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Sample;
using StructId;

namespace MvcWebApplication.Controllers;

Expand All @@ -24,6 +25,6 @@ public IActionResult GetProduct(ProductId? id)
}
}

public readonly partial record struct UserId : IStructId<Guid>;
public readonly partial record struct UserId : IStructId<Ulid>;

public record User(UserId id, string Alias);
5 changes: 5 additions & 0 deletions src/Sample/MvcWebApp/MvcWebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
<ImplicitUsings>enable</ImplicitUsings>
<RestoreSources>https://api.nuget.org/v3/index.json;$(PackageOutputPath)</RestoreSources>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<HasStructIdReference>false</HasStructIdReference>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ulid" Version="1.3.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/StructId.Analyzer/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"Roslyn": {
"commandName": "DebugRoslynComponent",
"targetProject": "..\\StructId.FunctionalTests\\StructId.FunctionalTests.csproj"
},
"MinimalApi": {
"commandName": "DebugRoslynComponent",
"targetProject": "..\\Sample\\MinimalApi\\MinimalApi.csproj"
}
}
}

0 comments on commit afbf296

Please sign in to comment.