1414using System . Threading . Tasks ;
1515
1616namespace fac {
17- class Program {
17+ public class Program {
18+ /// <summary>
19+ /// 获取文件夹下所有文件
20+ /// </summary>
21+ /// <param name="_path"></param>
22+ /// <returns></returns>
1823 private static List < string > _GetAllFiles ( string _path ) {
1924 var _files = new List < string > ( ) ;
2025 _files . AddRange ( Directory . GetFiles ( _path ) ) ;
@@ -25,8 +30,12 @@ private static List<string> _GetAllFiles (string _path) {
2530 return _files ;
2631 }
2732
28- // 调用进程
29- private static void _process_run ( string _cmd ) {
33+ /// <summary>
34+ /// 调用进程
35+ /// </summary>
36+ /// <param name="_cmd"></param>
37+ /// <returns></returns>
38+ private static async Task RunProcessAsync ( string _cmd ) {
3039 int _p = _cmd . IndexOf ( ' ' ) ;
3140 string _file = _p == - 1 ? _cmd : _cmd [ .._p ] ;
3241 string _arg = _p == - 1 ? "" : _cmd [ ( _p + 1 ) ..] ;
@@ -37,26 +46,23 @@ private static void _process_run (string _cmd) {
3746 _ps . StartInfo . Arguments = _arg ;
3847 _ps . StartInfo . CreateNoWindow = false ;
3948 _ps . Start ( ) ;
40- _ps . WaitForExit ( ) ;
49+ await _ps . WaitForExitAsync ( ) ;
4150 }
4251
43- // 从路径中提取有意义的名称做项目名
52+ /// <summary>
53+ /// 从路径中提取有意义的名称做项目名
54+ /// </summary>
55+ /// <param name="_path"></param>
56+ /// <returns></returns>
4457 private static string _GetLastName ( string _path ) {
45- var _sb = new StringBuilder ( ) ;
46- for ( int i = _path . Length - 1 ; i >= 0 ; -- i ) {
47- if ( i == '/' || i == '\\ ' || i == ':' || i == '.' ) {
48- if ( _sb . Length > 0 )
49- return string . Join ( "" , _sb . ToString ( ) . Reverse ( ) ) ;
50- } else {
51- _sb . Append ( _path [ i ] ) ;
52- }
53- }
54- if ( _sb . Length > 0 )
55- return string . Join ( "" , _sb . ToString ( ) . Reverse ( ) ) ;
56- return "Hello" ;
58+ return _path [ ( _path . LastIndexOfAny ( new char [ ] { '/' , '\\ ' } ) + 1 ) ..] ;
5759 }
5860
59- // 更新检查
61+ /// <summary>
62+ /// 更新检查
63+ /// </summary>
64+ /// <param name="version"></param>
65+ /// <returns></returns>
6066 private static async Task CheckUpdatesAsync ( Version version ) {
6167 try {
6268 using var client = new HttpClient { Timeout = TimeSpan . FromSeconds ( 2 ) } ;
@@ -83,38 +89,78 @@ private static async Task CheckUpdatesAsync (Version version) {
8389 } catch ( Exception ) {
8490 }
8591 }
86- static void Main ( string [ ] args ) {
92+
93+ /// <summary>
94+ /// 打印初始化信息
95+ /// </summary>
96+ private static async Task PrintInfoAsync ( ) {
8797 var _asm = Assembly . GetExecutingAssembly ( ) ;
8898 Console . WriteLine ( $ "fa语言编译器") ;
8999 Console . WriteLine ( $ " 版权:{ ( Attribute . GetCustomAttribute ( _asm , typeof ( AssemblyCopyrightAttribute ) ) as AssemblyCopyrightAttribute ) . Copyright } ") ;
90100 Console . WriteLine ( $ " 版本:{ _asm . GetName ( ) . Version } ") ;
91101 Console . WriteLine ( $ " 源码:https://github.com/fa-org/fa") ;
92102 if ( ! Debugger . IsAttached )
93- CheckUpdatesAsync ( _asm . GetName ( ) . Version ) . Wait ( ) ;
103+ await CheckUpdatesAsync ( _asm . GetName ( ) . Version ) ;
94104 Console . WriteLine ( ) ;
95- if ( args . Length == 0 || ( args . Length == 1 && ( args [ 0 ] == "--help" || args [ 0 ] == "-help" || args [ 0 ] == "/help" || args [ 0 ] == "-?" || args [ 0 ] == "/?" ) ) ) {
96- var _exename = Process . GetCurrentProcess ( ) . MainModule . ModuleName ;
97- if ( _exename == "dotnet" || _exename == "dotnet.exe" ) {
98- var _location = Assembly . GetEntryAssembly ( ) . Location ;
99- _location = _location [ ( _location . LastIndexOfAny ( new char [ ] { '/' , '\\ ' } ) + 1 ) ..] ;
100- _exename = $ "{ _exename } { _location } ";
101- }
102- Console . WriteLine ( $ "用法:") ;
103- Console . WriteLine ( $ "{ _exename } fa程序文件目录|fa程序文件.fa [-d] [-r]") ;
104- Console . WriteLine ( ) ;
105- Console . WriteLine ( $ " -d 调试模式,将在控制台打印C#后端输出") ;
106- Console . WriteLine ( $ " -r 发布模式,将编译为可发布版本") ;
107- Console . WriteLine ( ) ;
108- Console . WriteLine ( $ "备注:如果指定目录,则目录名不要以“.fa”结尾,另外fa程序扩展名必须小写") ;
109- Console . WriteLine ( $ "示例:") ;
110- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ) {
111- Console . WriteLine ( $ " { _exename } D:\\ FaProject -r") ;
112- Console . WriteLine ( $ " { _exename } D:\\ FaProject\\ Program.fa -d") ;
113- } else {
114- Console . WriteLine ( $ " { _exename } /FaProject -r") ;
115- Console . WriteLine ( $ " { _exename } /FaProject/Program.fa -d") ;
105+ }
106+
107+ /// <summary>
108+ /// 打印使用手册
109+ /// </summary>
110+ private static void PrintManual ( ) {
111+ var _exename = Process . GetCurrentProcess ( ) . MainModule . ModuleName ;
112+ if ( _exename == "dotnet" || _exename == "dotnet.exe" ) {
113+ var _location = Assembly . GetEntryAssembly ( ) . Location ;
114+ _location = _location [ ( _location . LastIndexOfAny ( new char [ ] { '/' , '\\ ' } ) + 1 ) ..] ;
115+ _exename = $ "{ _exename } { _location } ";
116+ }
117+ Console . WriteLine ( $ "用法:") ;
118+ Console . WriteLine ( $ "{ _exename } fa程序文件目录|fa程序文件.fa [-d] [-r]") ;
119+ Console . WriteLine ( ) ;
120+ Console . WriteLine ( $ " -d 调试模式,将在控制台打印C#后端输出") ;
121+ Console . WriteLine ( $ " -r 发布模式,将编译为可发布版本") ;
122+ Console . WriteLine ( ) ;
123+ Console . WriteLine ( $ "备注:如果指定目录,则目录名不要以“.fa”结尾,另外fa程序扩展名必须小写") ;
124+ Console . WriteLine ( $ "示例:") ;
125+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ) {
126+ Console . WriteLine ( $ " { _exename } D:\\ FaProject -r") ;
127+ Console . WriteLine ( $ " { _exename } D:\\ FaProject\\ Program.fa -d") ;
128+ } else {
129+ Console . WriteLine ( $ " { _exename } /FaProject -r") ;
130+ Console . WriteLine ( $ " { _exename } /FaProject/Program.fa -d") ;
131+ }
132+ Console . WriteLine ( ) ;
133+ }
134+
135+ /// <summary>
136+ /// 读取编译器自带标准库,比如Error等,用于后续part拼接,以及替换掉buildin实现
137+ /// </summary>
138+ public static async Task ReadBuildinSourceAsync ( ) {
139+ var _assembly = Assembly . GetExecutingAssembly ( ) ;
140+ var _fa_files = _assembly . GetManifestResourceNames ( ) ;
141+ _fa_files = ( from p in _fa_files where p [ ..7 ] == "fac.fa." select p ) . ToArray ( ) ;
142+ foreach ( var _fa_file in _fa_files ) {
143+ Info . CurrentFile = $ "res://fa/{ _fa_file [ 7 ..] } ";
144+ using var _stream = _assembly . GetManifestResourceStream ( _fa_file ) ;
145+ using var _reader = new StreamReader ( _stream , Encoding . UTF8 ) ;
146+ Info . CurrentSourceCode = await _reader . ReadToEndAsync ( ) ;
147+ Log . Mark ( LogMark . Parse ) ;
148+ var _program = Common . ParseCode < AstProgram > ( Info . CurrentSourceCode ) ;
149+ if ( _program == null ) {
150+ if ( Debugger . IsAttached ) {
151+ Console . WriteLine ( $ "按任意键退出。。。") ;
152+ Console . ReadKey ( ) ;
153+ }
154+ return ;
116155 }
117- Console . WriteLine ( ) ;
156+ Info . Programs . Add ( _program ) ;
157+ }
158+ }
159+
160+ private static async Task Main ( string [ ] args ) {
161+ await PrintInfoAsync ( ) ;
162+ if ( args . Length == 0 || ( args . Length == 1 && ( args [ 0 ] == "--help" || args [ 0 ] == "-help" || args [ 0 ] == "/help" || args [ 0 ] == "-?" || args [ 0 ] == "/?" ) ) ) {
163+ PrintManual ( ) ;
118164 if ( Debugger . IsAttached ) {
119165 Console . WriteLine ( $ "按任意键退出。。。") ;
120166 Console . ReadKey ( ) ;
@@ -131,9 +177,11 @@ static void Main (string[] args) {
131177 Console . WriteLine ( $ "按任意键退出。。。") ;
132178 Console . ReadKey ( ) ;
133179 }
180+ return ;
134181 }
135- Info . ProjectName = _GetLastName ( _path [ ..^ 3 ] ) ;
136- Info . SrcPath = Path . GetDirectoryName ( _path ) ;
182+ _path = _path [ .._path . LastIndexOfAny ( new char [ ] { '/' , '\\ ' } ) ] ;
183+ Info . ProjectName = _GetLastName ( _path ) ;
184+ Info . SrcPath = _path ;
137185 } else {
138186 // 文件夹
139187 if ( ! Directory . Exists ( _path ) ) {
@@ -142,6 +190,7 @@ static void Main (string[] args) {
142190 Console . WriteLine ( $ "按任意键退出。。。") ;
143191 Console . ReadKey ( ) ;
144192 }
193+ return ;
145194 }
146195 Info . ProjectName = _GetLastName ( _path ) ;
147196 Info . SrcPath = args [ 0 ] ;
@@ -164,31 +213,12 @@ static void Main (string[] args) {
164213 _src_files = ( from p in _src_files where p [ ^ 3 ..] . ToLower ( ) == ".fa" select p ) . ToList ( ) ;
165214 }
166215
167- // 读取编译器自带标准库,比如Error等,用于后续part拼接,以及替换掉buildin实现
168- var _assembly = Assembly . GetExecutingAssembly ( ) ;
169- var _fa_files = _assembly . GetManifestResourceNames ( ) ;
170- _fa_files = ( from p in _fa_files where p [ ..7 ] == "fac.fa." select p ) . ToArray ( ) ;
171- foreach ( var _fa_file in _fa_files ) {
172- Info . CurrentFile = $ "res://fa/{ _fa_file [ 7 ..] } ";
173- using var _stream = _assembly . GetManifestResourceStream ( _fa_file ) ;
174- using var _reader = new StreamReader ( _stream , Encoding . UTF8 ) ;
175- Info . CurrentSourceCode = _reader . ReadToEnd ( ) ;
176- Log . Mark ( LogMark . Parse ) ;
177- var _program = Common . ParseCode < AstProgram > ( Info . CurrentSourceCode ) ;
178- if ( _program == null ) {
179- if ( Debugger . IsAttached ) {
180- Console . WriteLine ( $ "按任意键退出。。。") ;
181- Console . ReadKey ( ) ;
182- }
183- return ;
184- }
185- Info . Programs . Add ( _program ) ;
186- }
216+ await ReadBuildinSourceAsync ( ) ;
187217
188218 // 读取源码
189219 foreach ( var _src_file in _src_files ) {
190220 Info . CurrentFile = _src_file ;
191- Info . CurrentSourceCode = File . ReadAllText ( _src_file , Encoding . UTF8 ) ;
221+ Info . CurrentSourceCode = await File . ReadAllTextAsync ( _src_file , Encoding . UTF8 ) ;
192222 Log . Mark ( LogMark . Parse ) ;
193223 var _program = Common . ParseCode < AstProgram > ( Info . CurrentSourceCode ) ;
194224 if ( _program == null ) {
@@ -239,20 +269,20 @@ static void Main (string[] args) {
239269 Directory . SetCurrentDirectory ( Info . DestPath ) ;
240270 string _dest_src = Info . GenerateCSharp ( ) ;
241271 if ( ! File . Exists ( Path . Combine ( Info . DestPath , $ "{ Info . ProjectName } .csproj") ) )
242- _process_run ( "dotnet new console --force" ) ;
272+ await RunProcessAsync ( "dotnet new console --force" ) ;
243273 if ( args . Contains ( "-d" ) ) {
244274 Console . WriteLine ( "-------------------- generate begin --------------------" ) ;
245275 Console . WriteLine ( _dest_src ) ;
246276 Console . WriteLine ( "-------------------- generate end --------------------" ) ;
247277 }
248278 string _dest_file = Path . Combine ( Info . DestPath , "Program.cs" ) ;
249- File . WriteAllText ( _dest_file , _dest_src , Encoding . UTF8 ) ;
279+ await File . WriteAllTextAsync ( _dest_file , _dest_src , Encoding . UTF8 ) ;
250280 if ( args . Contains ( "-r" ) ) {
251281 Console . WriteLine ( "正在构建。。。" ) ;
252- _process_run ( "dotnet publish" ) ;
282+ await RunProcessAsync ( "dotnet publish" ) ;
253283 } else {
254284 Console . WriteLine ( "正在构建并运行。。。" ) ;
255- _process_run ( "dotnet run" ) ;
285+ await RunProcessAsync ( "dotnet run" ) ;
256286 }
257287 //
258288 if ( Debugger . IsAttached ) {
0 commit comments