11using Google . Protobuf ;
2+ using SqlcGenCsharp . Drivers ;
23using System ;
4+ using System . Linq ;
35using File = Plugin . File ;
46
57
68namespace SqlcGenCsharp . Generators ;
79
8- internal class CsprojGen ( string outputDirectory , string projectName , string namespaceName , Options options )
10+ internal class CsprojGen ( DbDriver dbDriver , string outputDirectory , string projectName , string namespaceName )
911{
10- // TODO this logic needs to be moved to the Drivers project
11- private const string DefaultDapperVersion = "2.1.66" ;
12- private const string DefaultNpgsqlVersion = "8.0.6" ;
13- private const string DefaultMysqlConnectorVersion = "2.4.0" ;
14- private const string DefaultSqliteVersion = "9.0.0" ;
15- private const string DefaultCsvHelperVersion = "33.0.1" ;
16- private const string DefaultSystemTextJsonVersion = "9.0.6" ;
17-
1812 public File GenerateFile ( )
1913 {
2014 var csprojContents = GetFileContents ( ) ;
@@ -27,7 +21,11 @@ public File GenerateFile()
2721
2822 private string GetFileContents ( )
2923 {
30- var optionalNullableProperty = options . DotnetFramework . IsDotnetCore ( ) ? Environment . NewLine + " <Nullable>enable</Nullable>" : "" ;
24+ var optionalNullableProperty = dbDriver . Options . DotnetFramework . IsDotnetCore ( ) ? Environment . NewLine + " <Nullable>enable</Nullable>" : "" ;
25+ var referenceItems = dbDriver . GetPackageReferences ( )
26+ . Select ( p => $ """ <PackageReference Include="{ p . Key } " Version="{ p . Value } "/>""" )
27+ . JoinByNewLine ( ) ;
28+
3129 return $ """
3230 <!--{ Consts . AutoGeneratedComment } -->
3331 <!--Run the following to add the project to the solution:
@@ -36,60 +34,16 @@ dotnet sln add {outputDirectory}/{projectName}.csproj
3634 <Project Sdk="Microsoft.NET.Sdk">
3735
3836 <PropertyGroup>
39- <TargetFramework>{ options . DotnetFramework . ToName ( ) } </TargetFramework>
37+ <TargetFramework>{ dbDriver . Options . DotnetFramework . ToName ( ) } </TargetFramework>
4038 <RootNamespace>{ namespaceName } </RootNamespace>
4139 <OutputType>Library</OutputType>{ optionalNullableProperty }
4240 </PropertyGroup>
4341
44- { GetPackageReferences ( ) }
42+ <ItemGroup>
43+ { referenceItems }
44+ </ItemGroup>
4545
4646 </Project>
4747 """ ;
48-
49- string GetPackageReferences ( )
50- {
51- var optionalDapperPackageReference = options . UseDapper
52- ? Environment . NewLine + $ """ <PackageReference Include="Dapper" Version="{ GetDapperVersion ( options ) } "/>"""
53- : string . Empty ;
54- var optionalCsvHelper = options . DriverName is DriverName . MySqlConnector
55- ? Environment . NewLine + $ """ <PackageReference Include="CsvHelper" Version="{ DefaultCsvHelperVersion } "/>"""
56- : string . Empty ;
57- var optionalSystemTextJson = IsSystemTextJsonNeeded ( )
58- ? Environment . NewLine + $ """ <PackageReference Include="System.Text.Json" Version="{ DefaultSystemTextJsonVersion } "/>"""
59- : string . Empty ;
60- return $ """
61- <ItemGroup>
62- <PackageReference Include="{ options . DriverName . ToName ( ) } " Version="{ GetDriverVersion ( options ) } "/>{ optionalDapperPackageReference } { optionalCsvHelper } { optionalSystemTextJson }
63- <PackageReference Include="NodaTime" Version="3.2.0"/>
64- </ItemGroup>
65- """ ;
66- }
67- }
68-
69- private bool IsSystemTextJsonNeeded ( )
70- {
71- if ( options . DotnetFramework . IsDotnetCore ( ) )
72- return false ;
73- return options . DriverName is DriverName . MySqlConnector or DriverName . Npgsql ;
74- }
75-
76- private static string GetDriverVersion ( Options options )
77- {
78- if ( string . IsNullOrEmpty ( options . OverrideDriverVersion ) )
79- return options . DriverName switch
80- {
81- DriverName . Npgsql => DefaultNpgsqlVersion ,
82- DriverName . MySqlConnector => DefaultMysqlConnectorVersion ,
83- DriverName . Sqlite => DefaultSqliteVersion ,
84- _ => throw new NotSupportedException ( $ "unsupported driver: { options . DriverName } ")
85- } ;
86- return options . OverrideDriverVersion ;
87- }
88-
89- private static string GetDapperVersion ( Options options )
90- {
91- return string . IsNullOrEmpty ( options . OverrideDapperVersion )
92- ? DefaultDapperVersion
93- : options . OverrideDapperVersion ;
9448 }
9549}
0 commit comments