Skip to content

Commit 3ce98be

Browse files
committed
更改输入从文件夹修改为文件,方便进行输出
1 parent aab2ba8 commit 3ce98be

3 files changed

Lines changed: 49 additions & 31 deletions

File tree

Code/UsingMSBuildCopyOutputFileToFastDebug/LaunchSettingsParser.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public bool Execute()
4242
if (executablePath != null)
4343
{
4444
// executablePath = C:\lindexi\foo\foo.exe
45-
// 需要拿到的是文件夹
46-
LaunchMainProjectPath = Path.GetDirectoryName(executablePath.ToString());
45+
LaunchMainProjectExecutablePath = executablePath.ToString();
4746
return true;
4847
}
4948
}
@@ -52,6 +51,6 @@ public bool Execute()
5251
return false;
5352
}
5453

55-
public string LaunchMainProjectPath { set; get; }
54+
public string LaunchMainProjectExecutablePath { set; get; }
5655
}
5756
}

Code/UsingMSBuildCopyOutputFileToFastDebug/Program.cs

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ static int Main(string[] args)
5050

5151
private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
5252
{
53-
var destinationFolder = GetDestinationFolder(copyOutputFileOptions);
53+
var launchMainProjectExecutablePath = GetLaunchMainProjectExecutablePath(copyOutputFileOptions);
54+
Logger.Message($"LaunchMainProjectExecutablePath={launchMainProjectExecutablePath}");
55+
var destinationFolder = launchMainProjectExecutablePath.Directory;
5456
if (TargetFrameworkChecker.CheckCanCopy(destinationFolder, copyOutputFileOptions) is false)
5557
{
5658
// 如果当前的框架是兼容的,那就进行拷贝,否则不做任何拷贝逻辑
5759
return;
5860
}
5961

60-
var outputFileList = GetOutputFileList(copyOutputFileOptions.OutputFileToCopyList);
62+
var outputFileList = copyOutputFileOptions.GetOutputFileList();
6163
var safeOutputFileCopyTask = new SafeOutputFileCopyTask()
6264
{
6365
DestinationFolder = destinationFolder.FullName,
@@ -67,49 +69,49 @@ private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
6769
safeOutputFileCopyTask.Execute();
6870
}
6971

70-
private static List<FileInfo> GetOutputFileList(string outputFileToCopyList)
71-
{
72-
var fileList = new List<FileInfo>();
73-
foreach (var file in outputFileToCopyList.Split(System.IO.Path.PathSeparator))
74-
{
75-
// 不要优化 Linq 我需要调试这些文件,在这里加断点
76-
fileList.Add(new FileInfo(file));
77-
}
78-
79-
return fileList;
80-
}
72+
8173

8274
/// <summary>
83-
/// 获取准备输出的文件夹
75+
/// 获取准备运行的 Exe 的路径
8476
/// </summary>
8577
/// <param name="copyOutputFileOptions"></param>
8678
/// <returns></returns>
87-
private static DirectoryInfo GetDestinationFolder(CopyOutputFileOptions copyOutputFileOptions)
79+
private static FileInfo GetLaunchMainProjectExecutablePath(CopyOutputFileOptions copyOutputFileOptions)
8880
{
8981
var mainProjectPath = copyOutputFileOptions.MainProjectPath;
9082
// 如果用户有设置此文件夹,那就期望是输出到此文件夹
9183
if (!string.IsNullOrEmpty(mainProjectPath))
9284
{
93-
if (Directory.Exists(mainProjectPath) is false)
85+
if (File.Exists(mainProjectPath) is false)
9486
{
95-
throw new DirectoryNotFoundException(
87+
throw new FileNotFoundException(
9688
$"Can not find '{mainProjectPath}' FullPath={Path.GetFullPath(mainProjectPath)}");
9789
}
9890

99-
return new DirectoryInfo(mainProjectPath);
91+
return new FileInfo(mainProjectPath);
10092
}
10193

10294
// 尝试去读取 LaunchSettings 文件
10395
var launchSettingsParser = new LaunchSettingsParser();
10496
if (launchSettingsParser.Execute())
10597
{
106-
var launchMainProjectPath = launchSettingsParser.LaunchMainProjectPath;
107-
if (Directory.Exists(launchMainProjectPath) is false)
98+
string launchMainProjectExecutablePath = launchSettingsParser.LaunchMainProjectExecutablePath!;
99+
// 获取到的 launchMainProjectPath 如果是相对路径,那么相对的是当前的输出文件的路径,而不是 csproj 的路径
100+
if (Path.IsPathRooted(launchMainProjectExecutablePath) is false)
108101
{
109-
throw new DirectoryNotFoundException($"Can not find '{launchMainProjectPath}'");
102+
launchMainProjectExecutablePath =
103+
Path.Combine(copyOutputFileOptions.GetOutputFileList().First().Directory!.FullName,
104+
launchMainProjectExecutablePath!);
105+
106+
launchMainProjectExecutablePath = Path.GetFullPath(launchMainProjectExecutablePath);
110107
}
111108

112-
return new DirectoryInfo(launchMainProjectPath);
109+
if (File.Exists(launchMainProjectExecutablePath) is false)
110+
{
111+
throw new FileNotFoundException($"Can not find '{launchMainProjectExecutablePath}'");
112+
}
113+
114+
return new FileInfo(launchMainProjectExecutablePath!);
113115
}
114116

115117
throw new ArgumentException($"没有从 MainProjectPath 和 LaunchSettings 获取到输出的文件夹");
@@ -130,18 +132,35 @@ public static bool CheckCanCopy(DirectoryInfo targetFolder, CopyOutputFileOption
130132
[Verb("CopyOutputFile")]
131133
public class CopyOutputFileOptions
132134
{
133-
[Option("MainProjectPath")] public string MainProjectPath { set; get; } = null!;
135+
[Option("MainProjectPath")]
136+
public string MainProjectPath { set; get; } = null!;
137+
138+
[Option("CleanFilePath")]
139+
public string CleanFilePath { set; get; }
134140

135-
[Option("CleanFilePath")] public string CleanFilePath { set; get; }
141+
[Option("OutputFileToCopyList")]
142+
public string OutputFileToCopyList { set; get; }
136143

137-
[Option("OutputFileToCopyList")] public string OutputFileToCopyList { set; get; }
144+
[Option("TargetFramework")]
145+
public string TargetFramework { set; get; }
138146

139-
[Option("TargetFramework")] public string TargetFramework { set; get; }
147+
public List<FileInfo> GetOutputFileList()
148+
{
149+
var fileList = new List<FileInfo>();
150+
foreach (var file in OutputFileToCopyList.Split(System.IO.Path.PathSeparator))
151+
{
152+
// 不要优化 Linq 我需要调试这些文件,在这里加断点
153+
fileList.Add(new FileInfo(file));
154+
}
155+
156+
return fileList;
157+
}
140158
}
141159

142160
[Verb("Clean")]
143161
public class CleanOptions
144162
{
145-
[Option("CleanFilePath")] public string CleanFilePath { set; get; }
163+
[Option("CleanFilePath")]
164+
public string CleanFilePath { set; get; }
146165
}
147166
}

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.211</Version>
3+
<Version>1.3.214</Version>
44
</PropertyGroup>
55
</Project>

0 commit comments

Comments
 (0)