Skip to content

Commit e461e64

Browse files
authored
Merge pull request #11 from dotnet-campus/t/lindexi
支持清理文件
2 parents 7d69bf4 + 5c5b5aa commit e461e64

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

Code/UsingMSBuildCopyOutputFileToFastDebug/Build/dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
<EnableUsingMSBuildCopyOutputFileToFastDebug Condition="'$(Configuration)' != 'Debug'">false</EnableUsingMSBuildCopyOutputFileToFastDebug>
1313
</PropertyGroup>
1414

15-
<UsingTask TaskName="UsingMSBuildCopyOutputFileToFastDebug.SafeOutputFileCopyTask"
15+
<!-- 清理文件 -->
16+
<PropertyGroup>
17+
<CleanUsingMSBuildCopyOutputFileToFastDebugFile>$(IntermediateOutputPath)CleanUsingMSBuildCopyOutputFileToFastDebugFile.txt</CleanUsingMSBuildCopyOutputFileToFastDebugFile>
18+
</PropertyGroup>
19+
20+
<UsingTask TaskName="UsingMSBuildCopyOutputFileToFastDebug.SafeOutputFileCopyTask"
1621
AssemblyFile="$(NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder)\UsingMSBuildCopyOutputFileToFastDebug.dll" />
1722
<UsingTask TaskName="UsingMSBuildCopyOutputFileToFastDebug.LaunchSettingsParser"
1823
AssemblyFile="$(NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder)\UsingMSBuildCopyOutputFileToFastDebug.dll" />
@@ -37,10 +42,29 @@
3742
<OutputFileToCopy Include="$(OutputPath)$(AssemblyName).dll"></OutputFileToCopy>
3843
<OutputFileToCopy Include="$(OutputPath)$(AssemblyName).pdb"></OutputFileToCopy>
3944
</ItemGroup>
40-
<SafeOutputFileCopyTask SourceFiles="@(OutputFileToCopy)" DestinationFolder="$(MainProjectPath)"
45+
<SafeOutputFileCopyTask SourceFiles="@(OutputFileToCopy)"
46+
DestinationFolder="$(MainProjectPath)"
47+
CleanFile="$(CleanUsingMSBuildCopyOutputFileToFastDebugFile)"
4148
Condition="$(MainProjectPath)!=''"></SafeOutputFileCopyTask>
4249

43-
<SafeOutputFileCopyTask SourceFiles="@(OutputFileToCopy)" DestinationFolder="$(LaunchMainProjectPath)"
50+
<SafeOutputFileCopyTask SourceFiles="@(OutputFileToCopy)"
51+
DestinationFolder="$(LaunchMainProjectPath)"
52+
CleanFile="$(CleanUsingMSBuildCopyOutputFileToFastDebugFile)"
4453
Condition="$(LaunchMainProjectPath)!='' and $(MainProjectPath)==''"></SafeOutputFileCopyTask>
4554
</Target>
55+
56+
<Target Name="UsingMSBuildCopyOutputFileToFastDebugClean" AfterTargets="Clean">
57+
<ReadLinesFromFile
58+
File="$(CleanUsingMSBuildCopyOutputFileToFastDebugFile)" >
59+
<Output
60+
TaskParameter="Lines"
61+
ItemName="UsingMSBuildCopyOutputFileToFastDebugCleanFile"/>
62+
</ReadLinesFromFile>
63+
64+
<Delete Files="@(UsingMSBuildCopyOutputFileToFastDebugCleanFile)" >
65+
<Output TaskParameter="DeletedFiles" ItemName="UsingMSBuildCopyOutputFileToFastDebugCleanFileDeletedList"/>
66+
</Delete>
67+
68+
<Message Text="Deleted files: '@(UsingMSBuildCopyOutputFileToFastDebugCleanFileDeletedList)'"/>
69+
</Target>
4670
</Project>

Code/UsingMSBuildCopyOutputFileToFastDebug/SafeOutputFileCopyTask.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6+
using System.Threading.Tasks;
67

78
namespace UsingMSBuildCopyOutputFileToFastDebug
89
{
@@ -11,6 +12,11 @@ public class SafeOutputFileCopyTask : Microsoft.Build.Utilities.Task
1112
public string[] SourceFiles { set; get; }
1213
public string DestinationFolder { set; get; }
1314

15+
/// <summary>
16+
/// 用于清理文件
17+
/// </summary>
18+
public string CleanFile { set; get; }
19+
1420
public override bool Execute()
1521
{
1622
if (SourceFiles == null || !SourceFiles.Any())
@@ -54,6 +60,7 @@ public override bool Execute()
5460
{
5561
File.Move(destinationFile, newFileName);
5662
Console.WriteLine($"移动文件完成,将{destinationFile}移动到{newFileName}");
63+
AddToClean(newFileName);
5764
break;
5865
}
5966
}
@@ -72,5 +79,27 @@ public override bool Execute()
7279
//Tracer = str.ToString();
7380
return true;
7481
}
82+
83+
private async void AddToClean(string newFileName)
84+
{
85+
// 加入到清理文件
86+
87+
for (int i = 0; i < 10; i++)
88+
{
89+
try
90+
{
91+
newFileName = Path.GetFullPath(newFileName);
92+
File.AppendAllLines(CleanFile, new[] { newFileName });
93+
94+
return;
95+
}
96+
catch (Exception)
97+
{
98+
// 忽略
99+
}
100+
101+
await Task.Delay(TimeSpan.FromMilliseconds(200));
102+
}
103+
}
75104
}
76105
}

Code/UsingMSBuildCopyOutputFileToFastDebug/dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<description>Using MSBuild Copy Output File To Fast Debug
1313
通过复制输出文件让 VisualStudio 外部启动快速调试底层库</description>
1414
<releaseNotes></releaseNotes>
15-
<copyright>Copyright 2019</copyright>
15+
<copyright>Copyright 2020</copyright>
1616
<tags>msbuild debug</tags>
1717
<dependencies>
1818
<group targetFramework=".NETFramework4.0" />

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@
5050

5151
## 原理
5252

53-
在软件运行的时候依然可以移动 dll 或 exe 的路径,而此工具将底层库项目的输出 dll 和 pdb 文件拷贝到主项目的文件夹或 MainProjectPath 设置的文件夹,将原本的dll和pdb重命名,然后通过调试的可执行文件方式启动主项目
53+
在软件运行的时候依然可以移动 dll 或 exe 的路径,而此工具将底层库项目的输出 dll 和 pdb 文件拷贝到主项目的文件夹或 MainProjectPath 设置的文件夹,将原本的 dll 和 pdb 重命名,然后通过调试的可执行文件方式启动主项目
5454

55-
此时的主项目将会加载新的dll文件,同时因为存在 pdb 文件也能进去代码调试
55+
此时的主项目将会加载新的 dll 文件,同时因为存在 pdb 文件也能进去代码调试
5656

5757
通过将原本dll重命名的方式可以解决主项目执行的文件占用问题
5858

5959
此调试方式要求对底层库的更改满足二进制兼容
6060

6161
关于二进制兼容请看 [VisualStudio 通过外部调试方法快速调试库代码](https://blog.lindexi.com/post/visualstudio-%E9%80%9A%E8%BF%87%E5%A4%96%E9%83%A8%E8%B0%83%E8%AF%95%E6%96%B9%E6%B3%95%E5%BF%AB%E9%80%9F%E8%B0%83%E8%AF%95%E5%BA%93%E4%BB%A3%E7%A0%81 )
6262

63+
原有的 dll 和 pdb 文件将被加入清理列表文件,将会在执行清理的时候进行清理
64+
6365
## 细节
6466

6567
默认仅有在 Debug 下开启此功能,如需在 Release 也开启,请通过设置 EnableUsingMSBuildCopyOutputFileToFastDebug 属性为 true 开启

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

0 commit comments

Comments
 (0)