Skip to content

Commit bcc339e

Browse files
committed
判断版本兼容
1 parent affe167 commit bcc339e

3 files changed

Lines changed: 144 additions & 14 deletions

File tree

Code/UsingMSBuildCopyOutputFileToFastDebug/Program.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,22 @@ static int Main(string[] args)
5050

5151
private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
5252
{
53-
var launchMainProjectExecutablePath = GetLaunchMainProjectExecutablePath(copyOutputFileOptions);
54-
Logger.Message($"LaunchMainProjectExecutablePath={launchMainProjectExecutablePath}");
55-
var destinationFolder = launchMainProjectExecutablePath.Directory;
56-
if (TargetFrameworkChecker.CheckCanCopy(destinationFolder, copyOutputFileOptions) is false)
53+
var launchMainProjectExecutableFile = GetLaunchMainProjectExecutablePath(copyOutputFileOptions);
54+
Logger.Message($"LaunchMainProjectExecutablePath={launchMainProjectExecutableFile}");
55+
var destinationFolder = launchMainProjectExecutableFile.Directory;
56+
if (TargetFrameworkChecker.CheckCanCopy(launchMainProjectExecutableFile, copyOutputFileOptions) is false)
5757
{
58+
#if DEBUG
59+
Logger.Message($"当前框架{copyOutputFileOptions.TargetFramework}{launchMainProjectExecutableFile.FullName}不兼容");
60+
#endif
5861
// 如果当前的框架是兼容的,那就进行拷贝,否则不做任何拷贝逻辑
5962
return;
6063
}
6164

6265
var outputFileList = copyOutputFileOptions.GetOutputFileList();
6366
var safeOutputFileCopyTask = new SafeOutputFileCopyTask()
6467
{
65-
DestinationFolder = destinationFolder.FullName,
68+
DestinationFolder = destinationFolder!.FullName,
6669
CleanFile = copyOutputFileOptions.CleanFilePath,
6770
SourceFiles = outputFileList.Select(t => t.FullName).ToArray()
6871
};
@@ -121,14 +124,6 @@ private static FileInfo GetLaunchMainProjectExecutablePath(CopyOutputFileOptions
121124
}
122125

123126

124-
public static class TargetFrameworkChecker
125-
{
126-
public static bool CheckCanCopy(DirectoryInfo targetFolder, CopyOutputFileOptions copyOutputFileOptions)
127-
{
128-
return true;
129-
}
130-
}
131-
132127
[Verb("CopyOutputFile")]
133128
public class CopyOutputFileOptions
134129
{
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace UsingMSBuildCopyOutputFileToFastDebug
5+
{
6+
public static class TargetFrameworkChecker
7+
{
8+
public static bool CheckCanCopy(FileInfo targetExecutableFile, CopyOutputFileOptions copyOutputFileOptions)
9+
{
10+
DotNetType targetFramework = GetTargetFrameworkDotNetType(copyOutputFileOptions.TargetFramework);
11+
12+
var exeDotNetType = GetExecutableFileDotNetType(targetExecutableFile);
13+
14+
return IsCompatible(exeDotNetType, targetFramework);
15+
}
16+
17+
/// <summary>
18+
/// 判断 a 是否兼容 b 版本
19+
/// </summary>
20+
/// <param name="a"></param>
21+
/// <param name="b"></param>
22+
/// <returns></returns>
23+
private static bool IsCompatible(DotNetType a, DotNetType b)
24+
{
25+
if (a.HasFlag(DotNetType.NetCore) && b.HasFlag(DotNetType.NetCore))
26+
{
27+
return true;
28+
}
29+
30+
if (a.HasFlag(DotNetType.NetFramework) && b.HasFlag(DotNetType.NetFramework))
31+
{
32+
return true;
33+
}
34+
35+
return false;
36+
}
37+
38+
private static DotNetType GetExecutableFileDotNetType(FileInfo targetExecutableFile)
39+
{
40+
// 先兼容判断,当前业务逻辑没有那么复杂,毕竟写了周六日了怕两周都写不完
41+
var name = Path.GetFileNameWithoutExtension(targetExecutableFile.FullName);
42+
// 如果是存在 runtimeconfig.json 和 deps.json 文件,那就是 .NET Core 的
43+
var runtimeConfigFile = Path.Combine(targetExecutableFile.DirectoryName!, $"{name}.runtimeconfig.json");
44+
var depsFile = Path.Combine(targetExecutableFile.DirectoryName!, $"{name}.deps.json");
45+
46+
if (File.Exists(runtimeConfigFile) || File.Exists(depsFile))
47+
{
48+
return DotNetType.NetCore;
49+
}
50+
51+
return DotNetType.NetFramework;
52+
}
53+
54+
private static DotNetType GetTargetFrameworkDotNetType(string targetFramework)
55+
{
56+
if (targetFramework.Contains("net40"))
57+
{
58+
return DotNetType.NetFramework40;
59+
}
60+
61+
if (targetFramework.Contains("net45"))
62+
{
63+
return DotNetType.NetFramework45;
64+
}
65+
66+
if (targetFramework.Contains("net46"))
67+
{
68+
return DotNetType.NetFramework46;
69+
}
70+
71+
if (targetFramework.Contains("net47"))
72+
{
73+
return DotNetType.NetFramework47;
74+
}
75+
76+
if (targetFramework.Contains("net48"))
77+
{
78+
return DotNetType.NetFramework48;
79+
}
80+
81+
if (targetFramework.Contains("netcoreapp1"))
82+
{
83+
return DotNetType.NetCore1;
84+
}
85+
86+
if (targetFramework.Contains("netcoreapp2"))
87+
{
88+
return DotNetType.NetCore2;
89+
}
90+
91+
if (targetFramework.Contains("netcoreapp3"))
92+
{
93+
return DotNetType.NetCore3;
94+
}
95+
96+
if (targetFramework.Contains("net5."))
97+
{
98+
return DotNetType.Net5;
99+
}
100+
101+
if (targetFramework.Contains("net6."))
102+
{
103+
return DotNetType.Net6;
104+
}
105+
106+
throw new ArgumentException($"Unknown TargetFrame {targetFramework}");
107+
}
108+
}
109+
110+
[Flags]
111+
public enum DotNetType
112+
{
113+
NetFramework = 1 << 25,
114+
NetCore = 1 << 26,
115+
// 还没有需求,就不写了
116+
NetStandard = 1 << 27,
117+
118+
NetFramework40 = 1 << 0 | NetFramework,
119+
NetFramework45 = 1 << 1 | NetFramework,
120+
// 对于 4.5.1 等,都归为 45 好了
121+
//NetFramework451 = 1 << 2 | NetFramework,
122+
//NetFramework452 = 1 << 3 | NetFramework,
123+
NetFramework46 = 1 << 4 | NetFramework,
124+
NetFramework47 = 1 << 5 | NetFramework,
125+
NetFramework48 = 1 << 6 | NetFramework,
126+
127+
NetCore1 = 1 << 11 | NetCore,
128+
NetCore2 = 1 << 12 | NetCore,
129+
NetCore3 = 1 << 13 | NetCore,
130+
131+
Net5 = 1 << 15 | NetCore,
132+
Net6 = 1 << 16 | NetCore,
133+
134+
}
135+
}

build/Version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.3.214</Version>
3+
<Version>1.3.215</Version>
44
</PropertyGroup>
55
</Project>

0 commit comments

Comments
 (0)