@@ -6,57 +6,131 @@ public static async Task<int> Handle(ParseResult parseResult, CancellationToken
66    { 
77        var  runArgs  =  parseResult . GetRequiredValue ( Model . RunArgs ) ; 
88        var  projectOption  =  parseResult . GetValue ( Model . ProjectOption ) ; 
9+         var  fileOption  =  parseResult . GetValue ( Model . FileOption ) ; 
10+ 
11+         if  ( fileOption  is  {  Length :  >  0  } ) 
12+             return  await  RunFile ( fileOption ,  runArgs ,  cancellationToken ) ; 
913
1014        var  currentDirectory  =  new  DirectoryInfo ( Directory . GetCurrentDirectory ( ) ) ; 
1115
1216        while  ( currentDirectory ? . Exists  is  true ) 
1317        { 
1418            if  ( Directory . Exists ( Path . Combine ( currentDirectory . FullName ,  "_atom" ) ) ) 
19+                 return  await  RunProject ( currentDirectory ,  projectOption ,  runArgs ,  cancellationToken ) ; 
20+ 
21+             currentDirectory  =  currentDirectory . Parent ; 
22+         } 
23+ 
24+         Console . WriteLine ( "No Atom project found." ) ; 
25+ 
26+         return  1 ; 
27+     } 
28+ 
29+     private  static async  Task < int >  RunProject ( 
30+         DirectoryInfo  currentDirectory , 
31+         string ?  projectOption , 
32+         string [ ]  runArgs , 
33+         CancellationToken  cancellationToken ) 
34+     { 
35+         // Sanitize arguments 
36+         var  escapedArgs  =  runArgs . Select ( arg => 
37+         { 
38+             arg  =  arg 
39+                 . Replace ( "\n " ,  string . Empty ) 
40+                 . Replace ( "\r " ,  string . Empty ) ; 
41+ 
42+             return  arg . Contains ( ';' )  ||  arg . Contains ( '&' )  ||  arg . Contains ( '|' )  ||  arg . Contains ( ' ' ) 
43+                 ?  $ "\" { arg } \" "
44+                 :  arg ; 
45+         } ) ; 
46+ 
47+         var  atomProjectName  =  "_atom" ; 
48+ 
49+         for  ( var  i  =  0 ;  i  <  runArgs . Length ;  i ++ ) 
50+             if  ( projectOption  is  {  Length :  >  0  } ) 
1551            { 
16-                 // Sanitize arguments 
17-                 var  escapedArgs  =  runArgs . Select ( arg => 
52+                 atomProjectName  =  projectOption 
53+                     . Replace ( "\n " ,  string . Empty ) 
54+                     . Replace ( "\r " ,  string . Empty ) ; 
55+ 
56+                 if  ( atomProjectName . Contains ( ';' )  || 
57+                     atomProjectName . Contains ( '&' )  || 
58+                     atomProjectName . Contains ( '|' )  || 
59+                     atomProjectName . Contains ( ' ' ) ) 
60+                     atomProjectName  =  $ "\" { atomProjectName } \" "; 
61+ 
62+                 break ; 
63+             } 
64+ 
65+         var  atomProjectPath  =  Path . Combine ( currentDirectory . FullName ,  atomProjectName ,  $ "{ atomProjectName } .csproj") ; 
66+         var  allArgs  =  new [ ]  {  "run" ,  "--project" ,  atomProjectPath ,  "--"  } . Concat ( escapedArgs ) ; 
67+ 
68+         var  atomProcess  =  Process . Start ( "dotnet" ,  allArgs ) ; 
69+         await  atomProcess . WaitForExitAsync ( cancellationToken ) ; 
70+ 
71+         return  atomProcess . ExitCode ; 
72+     } 
73+ 
74+     private  static async  Task < int >  RunFile ( string ?  fileOption ,  string [ ]  runArgs ,  CancellationToken  cancellationToken ) 
75+     { 
76+         // Sanitize arguments 
77+         var  escapedArgs  =  runArgs . Select ( arg => 
78+         { 
79+             arg  =  arg 
80+                 . Replace ( "\n " ,  string . Empty ) 
81+                 . Replace ( "\r " ,  string . Empty ) ; 
82+ 
83+             return  arg . Contains ( ';' )  ||  arg . Contains ( '&' )  ||  arg . Contains ( '|' )  ||  arg . Contains ( ' ' ) 
84+                 ?  $ "\" { arg } \" "
85+                 :  arg ; 
86+         } ) ; 
87+ 
88+         string [ ]  possibleAtomFileNames  =  [ "Atom.cs" ,  "_atom.cs" ,  "Program.cs" ,  "App.cs" ] ; 
89+         string ?  atomFilePath  =  null ; 
90+         var  currentDirectory  =  new  DirectoryInfo ( Directory . GetCurrentDirectory ( ) ) ; 
91+ 
92+         foreach  ( var  possibleAtomFileName  in  possibleAtomFileNames ) 
93+         { 
94+             var  atomFileName  =  possibleAtomFileName ; 
95+ 
96+             for  ( var  i  =  0 ;  i  <  runArgs . Length ;  i ++ ) 
97+                 if  ( fileOption  is  {  Length :  >  0  } ) 
1898                { 
19-                     arg  =  arg 
99+                     atomFileName  =  fileOption 
20100                        . Replace ( "\n " ,  string . Empty ) 
21101                        . Replace ( "\r " ,  string . Empty ) ; 
22102
23-                     return  arg . Contains ( ';' )  ||  arg . Contains ( '&' )  ||  arg . Contains ( '|' )  ||  arg . Contains ( ' ' ) 
24-                         ?  $ "\" { arg } \" "
25-                         :  arg ; 
26-                 } ) ; 
27- 
28-                 var  atomProjectName  =  "_atom" ; 
29- 
30-                 for  ( var  i  =  0 ;  i  <  runArgs . Length ;  i ++ ) 
31-                     if  ( projectOption  is  {  Length :  >  0  } ) 
32-                     { 
33-                         atomProjectName  =  projectOption 
34-                             . Replace ( "\n " ,  string . Empty ) 
35-                             . Replace ( "\r " ,  string . Empty ) ; 
103+                     if  ( atomFileName . Contains ( ';' )  || 
104+                         atomFileName . Contains ( '&' )  || 
105+                         atomFileName . Contains ( '|' )  || 
106+                         atomFileName . Contains ( ' ' ) ) 
107+                         atomFileName  =  $ "\" { atomFileName } \" "; 
36108
37-                         if  ( atomProjectName . Contains ( ';' )  || 
38-                             atomProjectName . Contains ( '&' )  || 
39-                             atomProjectName . Contains ( '|' )  || 
40-                             atomProjectName . Contains ( ' ' ) ) 
41-                             atomProjectName  =  $ "\" { atomProjectName } \" "; 
109+                     break ; 
110+                 } 
42111
43-                         break ; 
44-                     } 
112+             atomFilePath  =  Path . IsPathRooted ( atomFileName ) 
113+                 ?  atomFileName 
114+                 :  Path . Combine ( currentDirectory . FullName ,  atomFileName ) ; 
45115
46-                  var   atomProjectPath   =   Path . Combine ( currentDirectory . FullName ,   atomProjectName ,   $ " { atomProjectName } .csproj" ) ; 
47-                 var   allArgs   =   new [ ]   {   "run" ,   "--project" ,   atomProjectPath ,   "--"   } . Concat ( escapedArgs ) ; 
116+             if   ( File . Exists ( atomFilePath ) ) 
117+                 break ; 
48118
49-                  var   atomProcess   =   Process . Start ( "dotnet" ,   allArgs ) ; 
50-                  await   atomProcess . WaitForExitAsync ( cancellationToken ) ; 
119+             atomFilePath   =   null ; 
120+         } 
51121
52-                 return  atomProcess . ExitCode ; 
53-             } 
122+         if  ( atomFilePath  is  null ) 
123+         { 
124+             Console . WriteLine ( "No Atom file found." ) ; 
54125
55-             currentDirectory   =   currentDirectory . Parent ; 
126+             return   1 ; 
56127        } 
57128
58-         Console . WriteLine ( "No Atom project found." ) ; 
129+         var   allArgs   =   new [ ]   {   "run" ,   atomFilePath ,   "--"   } . Concat ( escapedArgs ) ; 
59130
60-         return  1 ; 
131+         var  atomProcess  =  Process . Start ( "dotnet" ,  allArgs ) ; 
132+         await  atomProcess . WaitForExitAsync ( cancellationToken ) ; 
133+ 
134+         return  atomProcess . ExitCode ; 
61135    } 
62136} 
0 commit comments