@@ -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}
0 commit comments