Skip to content

Commit aab2ba8

Browse files
committed
完成接入拷贝文件逻辑
1 parent 647045f commit aab2ba8

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

Code/UsingMSBuildCopyOutputFileToFastDebug/Program.cs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,13 @@ static int Main(string[] args)
3333

3434
try
3535
{
36-
return CommandLine.Parse(args).AddHandler<CleanOptions>(c =>
37-
{
38-
Logger.Message($"Enter CleanOptions");
39-
})
40-
.AddHandler<CopyOutputFileOptions>(c =>
41-
{
42-
Logger.Message($"Enter CopyOutputFileOptions");
43-
CopyOutputFile(c);
44-
})
45-
.Run();
36+
return CommandLine.Parse(args).AddHandler<CleanOptions>(c => { Logger.Message($"Enter CleanOptions"); })
37+
.AddHandler<CopyOutputFileOptions>(c =>
38+
{
39+
Logger.Message($"Enter CopyOutputFileOptions");
40+
CopyOutputFile(c);
41+
})
42+
.Run();
4643
}
4744
catch (Exception e)
4845
{
@@ -53,14 +50,21 @@ static int Main(string[] args)
5350

5451
private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions)
5552
{
56-
var targetFolder = GetTargetFolder(copyOutputFileOptions);
57-
if (TargetFrameworkChecker.CheckCanCopy(targetFolder, copyOutputFileOptions) is false)
53+
var destinationFolder = GetDestinationFolder(copyOutputFileOptions);
54+
if (TargetFrameworkChecker.CheckCanCopy(destinationFolder, copyOutputFileOptions) is false)
5855
{
5956
// 如果当前的框架是兼容的,那就进行拷贝,否则不做任何拷贝逻辑
6057
return;
6158
}
6259

6360
var outputFileList = GetOutputFileList(copyOutputFileOptions.OutputFileToCopyList);
61+
var safeOutputFileCopyTask = new SafeOutputFileCopyTask()
62+
{
63+
DestinationFolder = destinationFolder.FullName,
64+
CleanFile = copyOutputFileOptions.CleanFilePath,
65+
SourceFiles = outputFileList.Select(t => t.FullName).ToArray()
66+
};
67+
safeOutputFileCopyTask.Execute();
6468
}
6569

6670
private static List<FileInfo> GetOutputFileList(string outputFileToCopyList)
@@ -80,7 +84,7 @@ private static List<FileInfo> GetOutputFileList(string outputFileToCopyList)
8084
/// </summary>
8185
/// <param name="copyOutputFileOptions"></param>
8286
/// <returns></returns>
83-
private static DirectoryInfo GetTargetFolder(CopyOutputFileOptions copyOutputFileOptions)
87+
private static DirectoryInfo GetDestinationFolder(CopyOutputFileOptions copyOutputFileOptions)
8488
{
8589
var mainProjectPath = copyOutputFileOptions.MainProjectPath;
8690
// 如果用户有设置此文件夹,那就期望是输出到此文件夹
@@ -94,6 +98,7 @@ private static DirectoryInfo GetTargetFolder(CopyOutputFileOptions copyOutputFil
9498

9599
return new DirectoryInfo(mainProjectPath);
96100
}
101+
97102
// 尝试去读取 LaunchSettings 文件
98103
var launchSettingsParser = new LaunchSettingsParser();
99104
if (launchSettingsParser.Execute())
@@ -118,31 +123,25 @@ public static class TargetFrameworkChecker
118123
{
119124
public static bool CheckCanCopy(DirectoryInfo targetFolder, CopyOutputFileOptions copyOutputFileOptions)
120125
{
121-
122126
return true;
123127
}
124128
}
125129

126130
[Verb("CopyOutputFile")]
127131
public class CopyOutputFileOptions
128132
{
129-
[Option("MainProjectPath")]
130-
public string MainProjectPath { set; get; } = null!;
133+
[Option("MainProjectPath")] public string MainProjectPath { set; get; } = null!;
131134

132-
[Option("CleanFilePath")]
133-
public string CleanFilePath { set; get; }
135+
[Option("CleanFilePath")] public string CleanFilePath { set; get; }
134136

135-
[Option("OutputFileToCopyList")]
136-
public string OutputFileToCopyList { set; get; }
137+
[Option("OutputFileToCopyList")] public string OutputFileToCopyList { set; get; }
137138

138-
[Option("TargetFramework")]
139-
public string TargetFramework { set; get; }
139+
[Option("TargetFramework")] public string TargetFramework { set; get; }
140140
}
141141

142142
[Verb("Clean")]
143143
public class CleanOptions
144144
{
145-
[Option("CleanFilePath")]
146-
public string CleanFilePath { set; get; }
145+
[Option("CleanFilePath")] public string CleanFilePath { set; get; }
147146
}
148147
}

Code/UsingMSBuildCopyOutputFileToFastDebug/SafeOutputFileCopyTask.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Threading;
56
using System.Threading.Tasks;
67

78
namespace UsingMSBuildCopyOutputFileToFastDebug
@@ -79,7 +80,7 @@ public bool Execute()
7980
return true;
8081
}
8182

82-
private async void AddToClean(string newFileName)
83+
private void AddToClean(string newFileName)
8384
{
8485
// 加入到清理文件
8586

@@ -97,7 +98,8 @@ private async void AddToClean(string newFileName)
9798
// 忽略
9899
}
99100

100-
await Task.Delay(TimeSpan.FromMilliseconds(200));
101+
// 这是命令行工具,只有单个线程在跑,不适合用 Task 异步
102+
Thread.Sleep(TimeSpan.FromMilliseconds(200));
101103
}
102104
}
103105
}

0 commit comments

Comments
 (0)