Skip to content

Commit 84d04a1

Browse files
committed
修复框架兼容和在单个框架下不判断框架兼容
1 parent d83482a commit 84d04a1

File tree

5 files changed

+87
-41
lines changed

5 files changed

+87
-41
lines changed

Code/ToolPackager/Build/dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</PropertyGroup>
3131

3232
<Exec
33-
Command="dotnet $(UsingMSBuildCopyOutputFileToFastDebugTaskFile) -- CopyOutputFile $(MainProjectExecutablePathCommandArgs) -CleanFilePath $(CleanUsingMSBuildCopyOutputFileToFastDebugFile) -OutputFileToCopyList $(OutputFileToCopyList) -TargetFramework $(TargetFramework)" />
33+
Command="dotnet $(UsingMSBuildCopyOutputFileToFastDebugTaskFile) -- CopyOutputFile $(MainProjectExecutablePathCommandArgs) -CleanFilePath $(CleanUsingMSBuildCopyOutputFileToFastDebugFile) -OutputFileToCopyList $(OutputFileToCopyList) -TargetFramework $(TargetFramework) -TargetFrameworks $(TargetFrameworks)" />
3434
</Target>
3535

3636
<Target Name="BanCopyOutputLibToFastDebug" AfterTargets="AfterBuild"

Code/ToolPackager/ToolPackager.csproj

+38-37
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<!--让多个不同的框架都能使用此工具-->
5-
<TargetFrameworks>net5.0;net45;netstandard1.0</TargetFrameworks>
6-
</PropertyGroup>
7-
8-
<PropertyGroup>
9-
<!-- 程序集名不是将打包的 -->
10-
<PackageId>dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug</PackageId>
11-
12-
<!-- 没有包含输出到 NuGet 的 Lib 文件夹的内容,不要警告 -->
13-
<NoPackageAnalysis>true</NoPackageAnalysis>
14-
<!-- 不包含任何构建的输出文件,这只是一个打包项目而已 -->
15-
<IncludeBuildOutput>false</IncludeBuildOutput>
16-
17-
<!-- 这是一个开发辅助的库 -->
18-
<DevelopmentDependency>true</DevelopmentDependency>
19-
20-
</PropertyGroup>
21-
22-
<ItemGroup>
23-
<!-- 仅仅只是设置设置项目构建顺序而已 -->
24-
<ProjectReference Include="..\UsingMSBuildCopyOutputFileToFastDebug\UsingMSBuildCopyOutputFileToFastDebug.csproj" Condition="'$(TargetFramework)' == 'net5.0'" ReferenceOutputAssembly="false" />
25-
</ItemGroup>
26-
27-
<PropertyGroup>
28-
<!-- 将实际执行的项目的输出作为工具 -->
29-
<UsingMSBuildCopyOutputFileToFastDebugOutputPath Condition="'$(Configuration)'=='Debug'">..\UsingMSBuildCopyOutputFileToFastDebug\bin\Debug\net5.0\</UsingMSBuildCopyOutputFileToFastDebugOutputPath>
30-
31-
<UsingMSBuildCopyOutputFileToFastDebugOutputPath Condition="'$(Configuration)'!='Debug'">..\UsingMSBuildCopyOutputFileToFastDebug\bin\Release\net5.0\</UsingMSBuildCopyOutputFileToFastDebugOutputPath>
32-
</PropertyGroup>
33-
34-
<Target Name="UsingMSBuildCopyOutputFileToFastDebugPackage" BeforeTargets="_GetPackageFiles">
35-
<ItemGroup>
36-
<None Include="build\dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets" Pack="True" PackagePath="\build\$(PackageId).targets" />
37-
<None Include="buildMultiTargeting\dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets" Pack="True" PackagePath="\buildMultiTargeting" />
38-
<None Include="$(UsingMSBuildCopyOutputFileToFastDebugOutputPath)**" Pack="true" PackagePath="\tools"/>
3+
<PropertyGroup>
4+
<!--让多个不同的框架都能使用此工具-->
5+
<!--多打一排排,让不明真相的人感觉到开森。事实上,只需一个net5.0,甚至netstandard1.0即可。其他框架只是在凑数而已-->
6+
<TargetFrameworks>net5.0;net45;netstandard1.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
7+
</PropertyGroup>
8+
9+
<PropertyGroup>
10+
<!-- 程序集名不是将打包的 -->
11+
<PackageId>dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug</PackageId>
12+
13+
<!-- 没有包含输出到 NuGet 的 Lib 文件夹的内容,不要警告 -->
14+
<NoPackageAnalysis>true</NoPackageAnalysis>
15+
<!-- 不包含任何构建的输出文件,这只是一个打包项目而已 -->
16+
<IncludeBuildOutput>false</IncludeBuildOutput>
17+
18+
<!-- 这是一个开发辅助的库 -->
19+
<DevelopmentDependency>true</DevelopmentDependency>
20+
21+
</PropertyGroup>
22+
23+
<ItemGroup>
24+
<!-- 仅仅只是设置设置项目构建顺序而已 -->
25+
<ProjectReference Include="..\UsingMSBuildCopyOutputFileToFastDebug\UsingMSBuildCopyOutputFileToFastDebug.csproj" Condition="'$(TargetFramework)' == 'net5.0'" ReferenceOutputAssembly="false" />
26+
</ItemGroup>
27+
28+
<PropertyGroup>
29+
<!-- 将实际执行的项目的输出作为工具 -->
30+
<UsingMSBuildCopyOutputFileToFastDebugOutputPath Condition="'$(Configuration)'=='Debug'">..\UsingMSBuildCopyOutputFileToFastDebug\bin\Debug\net5.0\</UsingMSBuildCopyOutputFileToFastDebugOutputPath>
31+
32+
<UsingMSBuildCopyOutputFileToFastDebugOutputPath Condition="'$(Configuration)'!='Debug'">..\UsingMSBuildCopyOutputFileToFastDebug\bin\Release\net5.0\</UsingMSBuildCopyOutputFileToFastDebugOutputPath>
33+
</PropertyGroup>
34+
35+
<Target Name="UsingMSBuildCopyOutputFileToFastDebugPackage" BeforeTargets="_GetPackageFiles">
36+
<ItemGroup>
37+
<None Include="build\dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets" Pack="True" PackagePath="\build\$(PackageId).targets" />
38+
<None Include="buildMultiTargeting\dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets" Pack="True" PackagePath="\buildMultiTargeting" />
39+
<None Include="$(UsingMSBuildCopyOutputFileToFastDebugOutputPath)**" Pack="true" PackagePath="\tools"/>
3940
</ItemGroup>
40-
</Target>
41+
</Target>
4142

4243
</Project>

Code/UsingMSBuildCopyOutputFileToFastDebug/Program.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
9898

9999
Logger.Message($"LaunchMainProjectExecutablePath={launchMainProjectExecutableFile}");
100100
var destinationFolder = launchMainProjectExecutableFile.Directory;
101-
if (TargetFrameworkChecker.CheckCanCopy(launchMainProjectExecutableFile, copyOutputFileOptions) is false)
101+
102+
var isSingleFramework = IsSingleFramework(copyOutputFileOptions);
103+
104+
// 非单个框架,即多个框架的情况下,需要检查是否可以拷贝
105+
if (!isSingleFramework && TargetFrameworkChecker.CheckCanCopy(launchMainProjectExecutableFile, copyOutputFileOptions) is false)
102106
{
103107
#if DEBUG
104108
Logger.Message($"当前框架{copyOutputFileOptions.TargetFramework}{launchMainProjectExecutableFile.FullName}不兼容");
@@ -117,6 +121,21 @@ private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
117121
safeOutputFileCopyTask.Execute();
118122
}
119123

124+
private static bool IsSingleFramework(CopyOutputFileOptions copyOutputFileOptions)
125+
{
126+
var targetFrameworks = copyOutputFileOptions.TargetFrameworks;
127+
if (string.IsNullOrEmpty(targetFrameworks))
128+
{
129+
// 没有记录 TargetFrameworks 属性,则证明是单个框架
130+
// 如 <TargetFramework>net9.0</TargetFramework>
131+
return true;
132+
}
133+
134+
// 虽然写的是 TargetFrameworks 属性,但是实际上是单个框架
135+
// 如 <TargetFrameworks>net9.0</TargetFrameworks>
136+
return targetFrameworks == copyOutputFileOptions.TargetFramework;
137+
}
138+
120139
/// <summary>
121140
/// 获取准备运行的 Exe 的路径
122141
/// </summary>
@@ -189,6 +208,9 @@ public class CopyOutputFileOptions
189208
[Option("TargetFramework")]
190209
public string TargetFramework { set; get; } = null!;
191210

211+
[Option("TargetFrameworks")]
212+
public string? TargetFrameworks { set; get; }
213+
192214
public List<FileInfo> GetOutputFileList()
193215
{
194216
var fileList = new List<FileInfo>();

Code/UsingMSBuildCopyOutputFileToFastDebug/TargetFrameworkChecker.cs

+24-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,27 @@ private static DotNetType GetTargetFrameworkDotNetType(string targetFramework)
155155
return DotNetType.Net6;
156156
}
157157

158-
throw new ArgumentException($"Unknown TargetFrame {targetFramework}");
158+
if (targetFramework.Contains("net7."))
159+
{
160+
return DotNetType.Net7;
161+
}
162+
163+
if (targetFramework.Contains("net8."))
164+
{
165+
return DotNetType.Net8;
166+
}
167+
168+
if (targetFramework.Contains("net9."))
169+
{
170+
return DotNetType.Net9;
171+
}
172+
173+
if (Regex.IsMatch(targetFramework, @"net\d"))
174+
{
175+
return DotNetType.NetCore;
176+
}
177+
178+
throw new ArgumentException($"Unknown TargetFramework {targetFramework}");
159179
}
160180
}
161181

@@ -183,5 +203,8 @@ public enum DotNetType
183203
Net5 = 1 << 15 | NetCore,
184204
Net6 = 1 << 16 | NetCore,
185205

206+
Net7 = 1 << 17 | NetCore,
207+
Net8 = 1 << 18 | NetCore,
208+
Net9 = 1 << 19 | NetCore,
186209
}
187210
}

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageProjectUrl>https://github.com/dotnet-campus/UsingMSBuildCopyOutputFileToFastDebug</PackageProjectUrl>
99
<RepositoryUrl>https://github.com/dotnet-campus/UsingMSBuildCopyOutputFileToFastDebug</RepositoryUrl>
1010
<Description>Using MSBuild Copy Output File To Fast Debug. 通过复制输出文件让 VisualStudio 外部启动快速调试底层库</Description>
11-
<Copyright>Copyright (c) 2019-2021 dotnet-campus</Copyright>
11+
<Copyright>Copyright (c) 2019-$([System.DateTime]::Now.ToString(`yyyy`)) dotnet-campus</Copyright>
1212
<PackageTags>msbuild debug</PackageTags>
1313
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1414
<BuildOutputTargetFolder>tools</BuildOutputTargetFolder>

0 commit comments

Comments
 (0)