Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
<EnableUsingMSBuildCopyOutputFileToFastDebug Condition="'$(Configuration)' != 'Debug'">false</EnableUsingMSBuildCopyOutputFileToFastDebug>
</PropertyGroup>

<UsingTask TaskName="UsingMSBuildCopyOutputFileToFastDebug.SafeOutputFileCopyTask"
<!-- 清理文件 -->
<PropertyGroup>
<CleanUsingMSBuildCopyOutputFileToFastDebugFile>$(IntermediateOutputPath)CleanUsingMSBuildCopyOutputFileToFastDebugFile.txt</CleanUsingMSBuildCopyOutputFileToFastDebugFile>
</PropertyGroup>

<UsingTask TaskName="UsingMSBuildCopyOutputFileToFastDebug.SafeOutputFileCopyTask"
AssemblyFile="$(NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder)\UsingMSBuildCopyOutputFileToFastDebug.dll" />
<UsingTask TaskName="UsingMSBuildCopyOutputFileToFastDebug.LaunchSettingsParser"
AssemblyFile="$(NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder)\UsingMSBuildCopyOutputFileToFastDebug.dll" />
Expand All @@ -37,10 +42,29 @@
<OutputFileToCopy Include="$(OutputPath)$(AssemblyName).dll"></OutputFileToCopy>
<OutputFileToCopy Include="$(OutputPath)$(AssemblyName).pdb"></OutputFileToCopy>
</ItemGroup>
<SafeOutputFileCopyTask SourceFiles="@(OutputFileToCopy)" DestinationFolder="$(MainProjectPath)"
<SafeOutputFileCopyTask SourceFiles="@(OutputFileToCopy)"
DestinationFolder="$(MainProjectPath)"
CleanFile="$(CleanUsingMSBuildCopyOutputFileToFastDebugFile)"
Condition="$(MainProjectPath)!=''"></SafeOutputFileCopyTask>

<SafeOutputFileCopyTask SourceFiles="@(OutputFileToCopy)" DestinationFolder="$(LaunchMainProjectPath)"
<SafeOutputFileCopyTask SourceFiles="@(OutputFileToCopy)"
DestinationFolder="$(LaunchMainProjectPath)"
CleanFile="$(CleanUsingMSBuildCopyOutputFileToFastDebugFile)"
Condition="$(LaunchMainProjectPath)!='' and $(MainProjectPath)==''"></SafeOutputFileCopyTask>
</Target>

<Target Name="UsingMSBuildCopyOutputFileToFastDebugClean" AfterTargets="Clean">
<ReadLinesFromFile
File="$(CleanUsingMSBuildCopyOutputFileToFastDebugFile)" >
<Output
TaskParameter="Lines"
ItemName="UsingMSBuildCopyOutputFileToFastDebugCleanFile"/>
</ReadLinesFromFile>

<Delete Files="@(UsingMSBuildCopyOutputFileToFastDebugCleanFile)" >
<Output TaskParameter="DeletedFiles" ItemName="UsingMSBuildCopyOutputFileToFastDebugCleanFileDeletedList"/>
</Delete>

<Message Text="Deleted files: '@(UsingMSBuildCopyOutputFileToFastDebugCleanFileDeletedList)'"/>
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

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

/// <summary>
/// 用于清理文件
/// </summary>
public string CleanFile { set; get; }

public override bool Execute()
{
if (SourceFiles == null || !SourceFiles.Any())
Expand Down Expand Up @@ -54,6 +60,7 @@ public override bool Execute()
{
File.Move(destinationFile, newFileName);
Console.WriteLine($"移动文件完成,将{destinationFile}移动到{newFileName}");
AddToClean(newFileName);
break;
}
}
Expand All @@ -72,5 +79,27 @@ public override bool Execute()
//Tracer = str.ToString();
return true;
}

private async void AddToClean(string newFileName)
{
// 加入到清理文件

for (int i = 0; i < 10; i++)
{
try
{
newFileName = Path.GetFullPath(newFileName);
File.AppendAllLines(CleanFile, new[] { newFileName });

return;
}
catch (Exception)
{
// 忽略
}

await Task.Delay(TimeSpan.FromMilliseconds(200));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<description>Using MSBuild Copy Output File To Fast Debug
通过复制输出文件让 VisualStudio 外部启动快速调试底层库</description>
<releaseNotes></releaseNotes>
<copyright>Copyright 2019</copyright>
<copyright>Copyright 2020</copyright>
<tags>msbuild debug</tags>
<dependencies>
<group targetFramework=".NETFramework4.0" />
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@

## 原理

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

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

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

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

关于二进制兼容请看 [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 )

原有的 dll 和 pdb 文件将被加入清理列表文件,将会在执行清理的时候进行清理

## 细节

默认仅有在 Debug 下开启此功能,如需在 Release 也开启,请通过设置 EnableUsingMSBuildCopyOutputFileToFastDebug 属性为 true 开启
Expand Down
2 changes: 1 addition & 1 deletion build/Version.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.3.67</Version>
<Version>1.3.201</Version>
</PropertyGroup>
</Project>