Skip to content

Commit

Permalink
Merge pull request #311 from atc-net/feature/DotnetCsProjFileHelper-T…
Browse files Browse the repository at this point in the history
…ests

Add UT's for DotnetCsProjFileHelperTests -> FindAllInPathAndPredictProjectTypes
  • Loading branch information
perkops authored Apr 4, 2024
2 parents 416612c + 6c7c109 commit cdf00c1
Show file tree
Hide file tree
Showing 34 changed files with 746 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/CodeDoc/Atc.Console.Spectre/Atc.Console.Spectre.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
>```csharp
>string ArgumentShortHelp
>```
#### ArgumentShortVerbose
#### ArgumentShortVersion
>```csharp
>string ArgumentShortVerbose
>string ArgumentShortVersion
>```
#### NameOptionsFile
>```csharp
Expand Down
2 changes: 1 addition & 1 deletion docs/CodeDoc/Atc.Console.Spectre/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- string ArgumentLongVerbose
- string ArgumentLongVersion
- string ArgumentShortHelp
- string ArgumentShortVerbose
- string ArgumentShortVersion
- string NameOptionsFile
- string NameOptionsFileCreate
- string NameOptionsFileValidate
Expand Down
9 changes: 9 additions & 0 deletions docs/CodeDoc/Atc/Atc.Helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ The AssemblyHelper module contains procedures used to preform assembly operation
><b>Summary:</b> Gets the assembly informations by system.
>
><b>Returns:</b> The array of `Atc.Data.Models.AssemblyInformation`.
#### GetProjectRootDirectory
>```csharp
>DirectoryInfo GetProjectRootDirectory()
>```
><b>Summary:</b> Retrieves the project root directory by searching upwards from the current assembly's base directory. The method looks for a directory containing a .csproj or .sln file, which is commonly found in the root of a C# project.
>
><b>Returns:</b> A `System.IO.DirectoryInfo` object representing the project root directory, or the assembly's base directory if the project root cannot be determined.
>
><b>Remarks:</b> This method starts at the current assembly's base directory and moves up the directory tree until it finds a directory containing at least one .csproj or .sln file. This directory is considered the project root. If no such directory is found, the method defaults to returning the base directory of the application domain. This approach allows the method to be more adaptable to various project structures without relying on a fixed directory depth. However, it assumes that the project root will contain at least one .csproj or .sln file.
#### GetSystemLocation
>```csharp
>string GetSystemLocation()
Expand Down
1 change: 1 addition & 0 deletions docs/CodeDoc/Atc/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -4409,6 +4409,7 @@
- GetAssemblyInformations()
- GetAssemblyInformationsByStartsWith(string value)
- GetAssemblyInformationsBySystem()
- GetProjectRootDirectory()
- GetSystemLocation()
- GetSystemLocationPath()
- GetSystemName()
Expand Down
29 changes: 29 additions & 0 deletions src/Atc/Helpers/AssemblyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@ namespace Atc.Helpers;
/// </summary>
public static class AssemblyHelper
{
/// <summary>
/// Retrieves the project root directory by searching upwards from the current assembly's base directory.
/// The method looks for a directory containing a .csproj or .sln file, which is commonly found in the root of a C# project.
/// </summary>
/// <remarks>
/// This method starts at the current assembly's base directory and moves up the directory tree until it finds a directory
/// containing at least one .csproj or .sln file. This directory is considered the project root. If no such directory is found,
/// the method defaults to returning the base directory of the application domain.
/// This approach allows the method to be more adaptable to various project structures without relying on a fixed directory depth.
/// However, it assumes that the project root will contain at least one .csproj or .sln file.
/// </remarks>
/// <returns>
/// A <see cref="DirectoryInfo"/> object representing the project root directory,
/// or the assembly's base directory if the project root cannot be determined.
/// </returns>
public static DirectoryInfo GetProjectRootDirectory()
{
var directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);

while (directory is not null &&
directory.GetFiles("*.csproj").Length == 0 &&
directory.GetFiles("*.sln").Length == 0)
{
directory = directory.Parent;
}

return directory ?? new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
}

/// <summary>
/// Load the specified assembly file, with a reference to memory and not the specified file.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions test/Atc.DotNet.Tests/Atc.DotNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="XUnitTestDataProjectSampleTypes\**" />
<EmbeddedResource Remove="XUnitTestDataProjectSampleTypes\**" />
<None Remove="XUnitTestDataProjectSampleTypes\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
Expand Down
37 changes: 37 additions & 0 deletions test/Atc.DotNet.Tests/DotnetCsProjFileHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,43 @@ public void GetProjectType_FileContent(
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(DotnetProjectType.AzureFunctionApp, "AzureFunctionApp1")]
[InlineData(DotnetProjectType.BlazorServerApp, "BlazorServerApp1")]
[InlineData(DotnetProjectType.BlazorWAsmApp, "BlazorWebAssemblyApp1")]
[InlineData(DotnetProjectType.BlazorWAsmApp, "BlazorWebAssemblyStandaloneApp1")]
[InlineData(DotnetProjectType.Library, "ClassLibrary1")]
[InlineData(DotnetProjectType.CliApp, "CliApp1")]
[InlineData(DotnetProjectType.ConsoleApp, "ConsoleApp1")]
[InlineData(DotnetProjectType.MauiApp, "MauiApp1")]
[InlineData(DotnetProjectType.MsTest, "MsTestProject1")]
[InlineData(DotnetProjectType.WebApp, "WebApplication1")]
[InlineData(DotnetProjectType.WebApp, "WebApplication2")]
[InlineData(DotnetProjectType.WebApi, "WebApi1")]
[InlineData(DotnetProjectType.Library, "WindowsFormsControlLibrary1")]
[InlineData(DotnetProjectType.WinFormApp, "WinFormsApp1")]
[InlineData(DotnetProjectType.WpfApp, "WpfApp1")]
public void FindAllInPathAndPredictProjectTypes(
DotnetProjectType expected,
string projectName)
{
// Arrange
var sampleTypesDirectory = new DirectoryInfo(
Path.Combine(
AssemblyHelper.GetProjectRootDirectory().FullName,
"XUnitTestDataProjectSampleTypes"));

// Act
var actual = DotnetCsProjFileHelper.FindAllInPathAndPredictProjectTypes(sampleTypesDirectory);

// Assert
Assert.NotNull(actual);

var (actualCsProjFile, actualProjectType) = actual.FirstOrDefault(x => x.CsProjFile.Directory!.Name == projectName);
Assert.Equal(expected, actualProjectType);
Assert.Equal($"{projectName}.csproj", actualCsProjFile.Name);
}

private static Task CreateCsProjFile(
DirectoryInfo workingDirectory,
string fileName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using BlazorServerApp1.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BlazorWebAssemblyApp1.Client\BlazorWebAssemblyApp1.Client.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BlazorWebAssemblyApp1.Client.Pages;
using BlazorWebAssemblyApp1.Components;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveWebAssemblyComponents();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(BlazorWebAssemblyApp1.Client._Imports).Assembly);

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.3" PrivateAssets="all" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using BlazorWebAssemblyStandaloneApp1;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PackageId>atc-rest-api-generator</PackageId>
<PackageTags>rest;api;netstandard;generator</PackageTags>
<Description>A .NET Core Web API C# code generator using a OpenApi 3.0.x specification YAML file.</Description>
<Title>Atc API Generator CLI</Title>
<OutputType>Exe</OutputType>
<AssemblyName>atc-rest-api-generator</AssemblyName>
<PackAsTool>true</PackAsTool>
<DebugType>pdbonly</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.360" />
<PackageReference Include="Atc.Console.Spectre" Version="2.0.360" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Atc.Rest.ApiGenerator.CodingRules\Atc.Rest.ApiGenerator.CodingRules.csproj" />
<ProjectReference Include="..\Atc.Rest.ApiGenerator\Atc.Rest.ApiGenerator.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading

0 comments on commit cdf00c1

Please sign in to comment.