private static void CopyOutputFile(CopyOutputFileOptions copyOutputFileOptions) { var launchMainProjectExecutableFile = GetLaunchMainProjectExecutablePath(copyOutputFileOptions); if (launchMainProjectExecutableFile == null) { Logger.Message($"[UsingMSBuildCopyOutputFileToFastDebug] LaunchMainProjectExecutableFile is null. 没有啥需要做的"); return; } Logger.Message($"LaunchMainProjectExecutablePath={launchMainProjectExecutableFile}"); var destinationFolder = launchMainProjectExecutableFile.Directory; if (TargetFrameworkChecker.CheckCanCopy(launchMainProjectExecutableFile, copyOutputFileOptions) is false) { #if DEBUG Logger.Message($"当前框架{copyOutputFileOptions.TargetFramework}与{launchMainProjectExecutableFile.FullName}不兼容"); #endif // 如果当前的框架是兼容的,那就进行拷贝,否则不做任何拷贝逻辑 return; } var outputFileList = copyOutputFileOptions.GetOutputFileList(); var safeOutputFileCopyTask = new SafeOutputFileCopyTask() { DestinationFolder = destinationFolder !.FullName, CleanFile = copyOutputFileOptions.CleanFilePath, SourceFiles = outputFileList.Select(t => t.FullName).ToArray() }; safeOutputFileCopyTask.Execute(); }
public static bool CheckCanCopy(FileInfo targetExecutableFile, CopyOutputFileOptions copyOutputFileOptions) { DotNetType targetFramework = GetTargetFrameworkDotNetType(copyOutputFileOptions.TargetFramework); var exeDotNetType = GetExecutableFileDotNetType(targetExecutableFile); return(IsCompatible(exeDotNetType, targetFramework)); }
private static FileInfo?GetLaunchMainProjectExecutablePath(CopyOutputFileOptions copyOutputFileOptions) { var mainProjectPath = copyOutputFileOptions.MainProjectExecutablePath; // 如果用户有设置此文件夹,那就期望是输出到此文件夹 if (!string.IsNullOrEmpty(mainProjectPath)) { if (File.Exists(mainProjectPath) is false) { // 这里不能扔出异常,考虑这个项目还是第一次构建,此时啥输出都应该是不存在的 // 更好的做法是在找不到目标文件的时候,给一个警告而已,毕竟此工具也只推荐在调试下使用而已。加个警告没啥锅 //throw new FileNotFoundException( // $"Can not find '{mainProjectPath}' FullPath={Path.GetFullPath(mainProjectPath)}"); Logger.Warning($"[UsingMSBuildCopyOutputFileToFastDebug] 找不到 MainProjectExecutablePath 的定义 '{mainProjectPath}' FullPath={Path.GetFullPath(mainProjectPath)} 文件。如项目首次构建,可忽略"); return(null); } return(new FileInfo(mainProjectPath)); } // 尝试去读取 LaunchSettings 文件 var launchSettingsParser = new LaunchSettingsParser(); if (launchSettingsParser.Execute()) { string launchMainProjectExecutablePath = launchSettingsParser.LaunchMainProjectExecutablePath !; // 获取到的 launchMainProjectPath 如果是相对路径,那么相对的是当前的输出文件的路径,而不是 csproj 的路径 if (Path.IsPathRooted(launchMainProjectExecutablePath) is false) { launchMainProjectExecutablePath = Path.Combine(copyOutputFileOptions.GetOutputFileList().First().Directory !.FullName, launchMainProjectExecutablePath !); launchMainProjectExecutablePath = Path.GetFullPath(launchMainProjectExecutablePath); } if (File.Exists(launchMainProjectExecutablePath) is false) { // 这里不能扔出异常,考虑这个项目还是第一次构建,此时啥输出都应该是不存在的 //throw new FileNotFoundException($"Can not find '{launchMainProjectExecutablePath}'"); Logger.Warning($"[UsingMSBuildCopyOutputFileToFastDebug] 找不到 LaunchSettings 的定义 '{launchMainProjectExecutablePath}' 文件。如项目首次构建,可忽略"); return(null); } return(new FileInfo(launchMainProjectExecutablePath !)); } Logger.Warning($"[UsingMSBuildCopyOutputFileToFastDebug] 没有从 MainProjectExecutablePath 和 LaunchSettings 获取到输出的文件夹"); return(null); }