Skip to content

Commit

Permalink
修复框架兼容和在单个框架下不判断框架兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Dec 6, 2024
1 parent d83482a commit 84d04a1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</PropertyGroup>

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

<Target Name="BanCopyOutputLibToFastDebug" AfterTargets="AfterBuild"
Expand Down
75 changes: 38 additions & 37 deletions Code/ToolPackager/ToolPackager.csproj
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!--让多个不同的框架都能使用此工具-->
<TargetFrameworks>net5.0;net45;netstandard1.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<!-- 程序集名不是将打包的 -->
<PackageId>dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug</PackageId>

<!-- 没有包含输出到 NuGet 的 Lib 文件夹的内容,不要警告 -->
<NoPackageAnalysis>true</NoPackageAnalysis>
<!-- 不包含任何构建的输出文件,这只是一个打包项目而已 -->
<IncludeBuildOutput>false</IncludeBuildOutput>

<!-- 这是一个开发辅助的库 -->
<DevelopmentDependency>true</DevelopmentDependency>

</PropertyGroup>

<ItemGroup>
<!-- 仅仅只是设置设置项目构建顺序而已 -->
<ProjectReference Include="..\UsingMSBuildCopyOutputFileToFastDebug\UsingMSBuildCopyOutputFileToFastDebug.csproj" Condition="'$(TargetFramework)' == 'net5.0'" ReferenceOutputAssembly="false" />
</ItemGroup>

<PropertyGroup>
<!-- 将实际执行的项目的输出作为工具 -->
<UsingMSBuildCopyOutputFileToFastDebugOutputPath Condition="'$(Configuration)'=='Debug'">..\UsingMSBuildCopyOutputFileToFastDebug\bin\Debug\net5.0\</UsingMSBuildCopyOutputFileToFastDebugOutputPath>

<UsingMSBuildCopyOutputFileToFastDebugOutputPath Condition="'$(Configuration)'!='Debug'">..\UsingMSBuildCopyOutputFileToFastDebug\bin\Release\net5.0\</UsingMSBuildCopyOutputFileToFastDebugOutputPath>
</PropertyGroup>

<Target Name="UsingMSBuildCopyOutputFileToFastDebugPackage" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<None Include="build\dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets" Pack="True" PackagePath="\build\$(PackageId).targets" />
<None Include="buildMultiTargeting\dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets" Pack="True" PackagePath="\buildMultiTargeting" />
<None Include="$(UsingMSBuildCopyOutputFileToFastDebugOutputPath)**" Pack="true" PackagePath="\tools"/>
<PropertyGroup>
<!--让多个不同的框架都能使用此工具-->
<!--多打一排排,让不明真相的人感觉到开森。事实上,只需一个net5.0,甚至netstandard1.0即可。其他框架只是在凑数而已-->
<TargetFrameworks>net5.0;net45;netstandard1.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<!-- 程序集名不是将打包的 -->
<PackageId>dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug</PackageId>

<!-- 没有包含输出到 NuGet 的 Lib 文件夹的内容,不要警告 -->
<NoPackageAnalysis>true</NoPackageAnalysis>
<!-- 不包含任何构建的输出文件,这只是一个打包项目而已 -->
<IncludeBuildOutput>false</IncludeBuildOutput>

<!-- 这是一个开发辅助的库 -->
<DevelopmentDependency>true</DevelopmentDependency>

</PropertyGroup>

<ItemGroup>
<!-- 仅仅只是设置设置项目构建顺序而已 -->
<ProjectReference Include="..\UsingMSBuildCopyOutputFileToFastDebug\UsingMSBuildCopyOutputFileToFastDebug.csproj" Condition="'$(TargetFramework)' == 'net5.0'" ReferenceOutputAssembly="false" />
</ItemGroup>

<PropertyGroup>
<!-- 将实际执行的项目的输出作为工具 -->
<UsingMSBuildCopyOutputFileToFastDebugOutputPath Condition="'$(Configuration)'=='Debug'">..\UsingMSBuildCopyOutputFileToFastDebug\bin\Debug\net5.0\</UsingMSBuildCopyOutputFileToFastDebugOutputPath>

<UsingMSBuildCopyOutputFileToFastDebugOutputPath Condition="'$(Configuration)'!='Debug'">..\UsingMSBuildCopyOutputFileToFastDebug\bin\Release\net5.0\</UsingMSBuildCopyOutputFileToFastDebugOutputPath>
</PropertyGroup>

<Target Name="UsingMSBuildCopyOutputFileToFastDebugPackage" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<None Include="build\dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets" Pack="True" PackagePath="\build\$(PackageId).targets" />
<None Include="buildMultiTargeting\dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets" Pack="True" PackagePath="\buildMultiTargeting" />
<None Include="$(UsingMSBuildCopyOutputFileToFastDebugOutputPath)**" Pack="true" PackagePath="\tools"/>
</ItemGroup>
</Target>
</Target>

</Project>
24 changes: 23 additions & 1 deletion Code/UsingMSBuildCopyOutputFileToFastDebug/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)

Logger.Message($"LaunchMainProjectExecutablePath={launchMainProjectExecutableFile}");
var destinationFolder = launchMainProjectExecutableFile.Directory;
if (TargetFrameworkChecker.CheckCanCopy(launchMainProjectExecutableFile, copyOutputFileOptions) is false)

var isSingleFramework = IsSingleFramework(copyOutputFileOptions);

// 非单个框架,即多个框架的情况下,需要检查是否可以拷贝
if (!isSingleFramework && TargetFrameworkChecker.CheckCanCopy(launchMainProjectExecutableFile, copyOutputFileOptions) is false)
{
#if DEBUG
Logger.Message($"当前框架{copyOutputFileOptions.TargetFramework}{launchMainProjectExecutableFile.FullName}不兼容");
Expand All @@ -117,6 +121,21 @@ private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
safeOutputFileCopyTask.Execute();
}

private static bool IsSingleFramework(CopyOutputFileOptions copyOutputFileOptions)
{
var targetFrameworks = copyOutputFileOptions.TargetFrameworks;
if (string.IsNullOrEmpty(targetFrameworks))
{
// 没有记录 TargetFrameworks 属性,则证明是单个框架
// 如 <TargetFramework>net9.0</TargetFramework>
return true;
}

// 虽然写的是 TargetFrameworks 属性,但是实际上是单个框架
// 如 <TargetFrameworks>net9.0</TargetFrameworks>
return targetFrameworks == copyOutputFileOptions.TargetFramework;
}

/// <summary>
/// 获取准备运行的 Exe 的路径
/// </summary>
Expand Down Expand Up @@ -189,6 +208,9 @@ public class CopyOutputFileOptions
[Option("TargetFramework")]
public string TargetFramework { set; get; } = null!;

[Option("TargetFrameworks")]
public string? TargetFrameworks { set; get; }

public List<FileInfo> GetOutputFileList()
{
var fileList = new List<FileInfo>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,27 @@ private static DotNetType GetTargetFrameworkDotNetType(string targetFramework)
return DotNetType.Net6;
}

throw new ArgumentException($"Unknown TargetFrame {targetFramework}");
if (targetFramework.Contains("net7."))
{
return DotNetType.Net7;
}

if (targetFramework.Contains("net8."))
{
return DotNetType.Net8;
}

if (targetFramework.Contains("net9."))
{
return DotNetType.Net9;
}

if (Regex.IsMatch(targetFramework, @"net\d"))
{
return DotNetType.NetCore;
}

throw new ArgumentException($"Unknown TargetFramework {targetFramework}");
}
}

Expand Down Expand Up @@ -183,5 +203,8 @@ public enum DotNetType
Net5 = 1 << 15 | NetCore,
Net6 = 1 << 16 | NetCore,

Net7 = 1 << 17 | NetCore,
Net8 = 1 << 18 | NetCore,
Net9 = 1 << 19 | NetCore,
}
}
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/dotnet-campus/UsingMSBuildCopyOutputFileToFastDebug</PackageProjectUrl>
<RepositoryUrl>https://github.com/dotnet-campus/UsingMSBuildCopyOutputFileToFastDebug</RepositoryUrl>
<Description>Using MSBuild Copy Output File To Fast Debug. 通过复制输出文件让 VisualStudio 外部启动快速调试底层库</Description>
<Copyright>Copyright (c) 2019-2021 dotnet-campus</Copyright>
<Copyright>Copyright (c) 2019-$([System.DateTime]::Now.ToString(`yyyy`)) dotnet-campus</Copyright>
<PackageTags>msbuild debug</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<BuildOutputTargetFolder>tools</BuildOutputTargetFolder>
Expand Down

0 comments on commit 84d04a1

Please sign in to comment.