Skip to content

Commit 524dc9c

Browse files
committed
优化项目首次构建找不到输出路径
1 parent 6d1b5da commit 524dc9c

File tree

1 file changed

+23
-9
lines changed
  • Code/UsingMSBuildCopyOutputFileToFastDebug

1 file changed

+23
-9
lines changed

Code/UsingMSBuildCopyOutputFileToFastDebug/Program.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -89,6 +90,12 @@ private static void CleanFile(CleanOptions cleanOptions)
8990
private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
9091
{
9192
var launchMainProjectExecutableFile = GetLaunchMainProjectExecutablePath(copyOutputFileOptions);
93+
if (launchMainProjectExecutableFile == null)
94+
{
95+
Logger.Message($"[UsingMSBuildCopyOutputFileToFastDebug] LaunchMainProjectExecutableFile is null. 没有啥需要做的");
96+
return;
97+
}
98+
9299
Logger.Message($"LaunchMainProjectExecutablePath={launchMainProjectExecutableFile}");
93100
var destinationFolder = launchMainProjectExecutableFile.Directory;
94101
if (TargetFrameworkChecker.CheckCanCopy(launchMainProjectExecutableFile, copyOutputFileOptions) is false)
@@ -115,16 +122,20 @@ private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
115122
/// </summary>
116123
/// <param name="copyOutputFileOptions"></param>
117124
/// <returns></returns>
118-
private static FileInfo GetLaunchMainProjectExecutablePath(CopyOutputFileOptions copyOutputFileOptions)
125+
private static FileInfo? GetLaunchMainProjectExecutablePath(CopyOutputFileOptions copyOutputFileOptions)
119126
{
120127
var mainProjectPath = copyOutputFileOptions.MainProjectExecutablePath;
121128
// 如果用户有设置此文件夹,那就期望是输出到此文件夹
122129
if (!string.IsNullOrEmpty(mainProjectPath))
123130
{
124131
if (File.Exists(mainProjectPath) is false)
125132
{
126-
throw new FileNotFoundException(
127-
$"Can not find '{mainProjectPath}' FullPath={Path.GetFullPath(mainProjectPath)}");
133+
// 这里不能扔出异常,考虑这个项目还是第一次构建,此时啥输出都应该是不存在的
134+
// 更好的做法是在找不到目标文件的时候,给一个警告而已,毕竟此工具也只推荐在调试下使用而已。加个警告没啥锅
135+
//throw new FileNotFoundException(
136+
// $"Can not find '{mainProjectPath}' FullPath={Path.GetFullPath(mainProjectPath)}");
137+
Logger.Warning($"[UsingMSBuildCopyOutputFileToFastDebug] 找不到 MainProjectExecutablePath 的定义 '{mainProjectPath}' FullPath={Path.GetFullPath(mainProjectPath)} 文件。如项目首次构建,可忽略");
138+
return null;
128139
}
129140

130141
return new FileInfo(mainProjectPath);
@@ -147,7 +158,10 @@ private static FileInfo GetLaunchMainProjectExecutablePath(CopyOutputFileOptions
147158

148159
if (File.Exists(launchMainProjectExecutablePath) is false)
149160
{
150-
throw new FileNotFoundException($"Can not find '{launchMainProjectExecutablePath}'");
161+
// 这里不能扔出异常,考虑这个项目还是第一次构建,此时啥输出都应该是不存在的 //throw new FileNotFoundException($"Can not find '{launchMainProjectExecutablePath}'");
162+
Logger.Warning($"[UsingMSBuildCopyOutputFileToFastDebug] 找不到 LaunchSettings 的定义 '{launchMainProjectExecutablePath}' 文件。如项目首次构建,可忽略");
163+
164+
return null;
151165
}
152166

153167
return new FileInfo(launchMainProjectExecutablePath!);
@@ -167,13 +181,13 @@ public class CopyOutputFileOptions
167181
public string MainProjectExecutablePath { set; get; } = null!;
168182

169183
[Option("CleanFilePath")]
170-
public string CleanFilePath { set; get; }
184+
public string CleanFilePath { set; get; } = null!;
171185

172186
[Option("OutputFileToCopyList")]
173-
public string OutputFileToCopyList { set; get; }
187+
public string OutputFileToCopyList { set; get; } = null!;
174188

175189
[Option("TargetFramework")]
176-
public string TargetFramework { set; get; }
190+
public string TargetFramework { set; get; } = null!;
177191

178192
public List<FileInfo> GetOutputFileList()
179193
{
@@ -192,6 +206,6 @@ public List<FileInfo> GetOutputFileList()
192206
public class CleanOptions
193207
{
194208
[Option("CleanFilePath")]
195-
public string CleanFilePath { set; get; }
209+
public string CleanFilePath { set; get; } = null!;
196210
}
197211
}

0 commit comments

Comments
 (0)