Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,7 @@ L10N.Integrations/phantom*
tools/

# VS Code settings
.vscode/mcp.json
.vscode/mcp.json

# Auto-generated package version file for playground
src/ReferenceCop.Package/package-version.txt
3 changes: 3 additions & 0 deletions playground/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Standard .NET ignores for the test projects
**/bin/
**/obj/
43 changes: 43 additions & 0 deletions playground/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ReferenceCop Playground

This is a playground environment for testing ReferenceCop changes end-to-end.

## Structure

- `TestProject/` - Sample project that uses ReferenceCop for validation
- `SampleApp/` - Sample application project
- `SampleLibrary/` - Sample library project
- `nuget.config` - Configured to use ReferenceCop package from build output
- `Directory.Build.props` - Centralized ReferenceCop package reference with auto-version detection

## Workflow

1. **Make changes** to ReferenceCop source code
2. **Build and pack** the ReferenceCop package:
```powershell
cd ..\src\ReferenceCop.Package
dotnet pack -c Debug
```
This automatically writes the package version to `package-version.txt`
3. **Restore and build** the test project:
```powershell
cd playground\TestProject\SampleApp
dotnet restore
dotnet build
```
4. **Introduce test violations** or modify the `ReferenceCop.config` to test your changes

**Note**:
- The `nuget.config` uses the package directly from the build output directory (`../../src/ReferenceCop.Package/bin/Debug`)
- The package version is automatically read from `package-version.txt` via `Directory.Build.props`
- No manual version updates needed!

## Testing Different Rule Types

The `ReferenceCop.config` includes examples of all three rule types:

1. **AssemblyName** - Blocks references to assemblies matching a pattern
2. **ProjectTag** - Blocks references between projects with specific tags
3. **ProjectPath** - Blocks references based on project folder paths

Modify the rules or project references to test violations.
33 changes: 33 additions & 0 deletions playground/TestProject/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project>

<PropertyGroup>
<!-- ReferenceCopRepositoryRoot as required by ReferenceCop -->
<ReferenceCopRepositoryRoot>$(MSBuildThisFileDirectory)</ReferenceCopRepositoryRoot>

<!--
Read the package version from the file written during pack
Falls back to a default version if file doesn't exist
-->
<PlaygroundVersionFile>$(MSBuildThisFileDirectory)..\..\src\ReferenceCop.Package\package-version.txt</PlaygroundVersionFile>
<ReferenceCopVersion Condition="Exists('$(PlaygroundVersionFile)')">$([System.IO.File]::ReadAllText('$(PlaygroundVersionFile)').Trim())</ReferenceCopVersion>
<ReferenceCopVersion Condition="'$(ReferenceCopVersion)' == ''">20241220.000000</ReferenceCopVersion>
</PropertyGroup>

<ItemGroup>
<!--
ReferenceCop package reference applied to all projects in the playground
Version is automatically read from package-version.txt after pack
The version format for Debug builds is: yyyyMMdd.hhmmss
-->
<PackageReference Include="ReferenceCop.Dev" Version="$(ReferenceCopVersion)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<!-- ReferenceCop.config as AdditionalFile with required Label -->
<AdditionalFiles Include="$(MSBuildThisFileDirectory)ReferenceCop.config">
<Label>ReferenceCopConfig</Label>
</AdditionalFiles>
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions playground/TestProject/ReferenceCop.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<ReferenceCopConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/mfogliatto/ReferenceCop/main/ReferenceCopConfig.xsd">
<Rules>
<!-- Example 1: Block references to assemblies matching a pattern -->
<AssemblyName>
<Name>NoSystemWebReferences</Name>
<Description>System.Web assemblies should not be referenced</Description>
<Severity>Error</Severity>
<Pattern>System.Web*</Pattern>
</AssemblyName>

<!-- Example 2: Block references between projects with specific tags -->
<ProjectTag>
<Name>AppCannotReferenceTools</Name>
<Description>Application projects should not reference tool projects</Description>
<Severity>Warning</Severity>
<FromProjectTag>App</FromProjectTag>
<ToProjectTag>Tools</ToProjectTag>
</ProjectTag>

<!-- Example 3: Block references based on project paths -->
<ProjectPath>
<Name>AppCannotReferenceInternal</Name>
<Description>Applications should not reference internal implementation libraries</Description>
<Severity>Error</Severity>
<FromPath>**/SampleApp/**</FromPath>
<ToPath>**/Internal/**</ToPath>
</ProjectPath>

<!-- Example 4: Another AssemblyName rule for testing -->
<AssemblyName>
<Name>NoNewtonsoftJson</Name>
<Description>Use System.Text.Json instead of Newtonsoft.Json</Description>
<Severity>Warning</Severity>
<Pattern>Newtonsoft.Json*</Pattern>
</AssemblyName>
</Rules>
</ReferenceCopConfig>
29 changes: 29 additions & 0 deletions playground/TestProject/SampleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using SampleLibrary;

namespace SampleApp;

/// <summary>
/// Sample application to test ReferenceCop.
/// </summary>
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("ReferenceCop Playground Test Application");
Console.WriteLine("=========================================");

var calculator = new Calculator();

Console.WriteLine($"5 + 3 = {calculator.Add(5, 3)}");
Console.WriteLine($"10 - 4 = {calculator.Subtract(10, 4)}");
Console.WriteLine($"6 * 7 = {calculator.Multiply(6, 7)}");
Console.WriteLine($"20 / 5 = {calculator.Divide(20, 5)}");

Console.WriteLine("\nTest completed successfully!");

// Uncomment to test violation detection:
// 1. Add a PackageReference to Newtonsoft.Json in the .csproj
// 2. Add: using Newtonsoft.Json;
// 3. Build to see the warning
}
}
15 changes: 15 additions & 0 deletions playground/TestProject/SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ProjectTag>App</ProjectTag>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SampleLibrary\SampleLibrary.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions playground/TestProject/SampleLibrary/Calculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace SampleLibrary;

/// <summary>
/// A simple calculator class for demonstration.
/// </summary>
public class Calculator
{
public int Add(int a, int b) => a + b;

public int Subtract(int a, int b) => a - b;

public int Multiply(int a, int b) => a * b;

public int Divide(int a, int b)
{
if (b == 0)
{
throw new DivideByZeroException("Cannot divide by zero");
}

return a / b;
}
}
15 changes: 15 additions & 0 deletions playground/TestProject/SampleLibrary/SampleLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- Tag this project as a library -->
<ProjectTag>Library</ProjectTag>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions playground/TestProject/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<!-- Direct reference to ReferenceCop package build output -->
<add key="local-build" value="../../src/ReferenceCop.Package/bin/Debug" />
<!-- NuGet.org for other dependencies -->
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
9 changes: 9 additions & 0 deletions src/ReferenceCop.Package/ReferenceCop.Package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@
</ItemGroup>
</Target>

<!-- Write package version to file for playground offline dev feed -->
<Target Name="_WritePackageVersionForPlaygroundOfflineDevFeed" AfterTargets="Pack" Condition="'$(IsDevEnvironment)' == 'true'">
<PropertyGroup>
<VersionFile>$(MSBuildThisFileDirectory)package-version.txt</VersionFile>
</PropertyGroup>
<WriteLinesToFile File="$(VersionFile)" Lines="$(PackageVersion)" Overwrite="true" />
<Message Text="Wrote package version $(PackageVersion) to $(VersionFile)" Importance="high" />
</Target>

</Project>
Loading