Skip to content

Commit c2d7208

Browse files
committed
Support error handling for command line mode
1 parent 73a7121 commit c2d7208

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/UserInputMacro/App.xaml.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
2+
using System.IO;
23
using System.Windows;
34
using System.Text.RegularExpressions;
4-
using System.Runtime.InteropServices;
5+
using Microsoft.CodeAnalysis.Scripting;
56

67
namespace UserInputMacro
78
{
@@ -10,16 +11,12 @@ namespace UserInputMacro
1011
/// </summary>
1112
public partial class App : Application
1213
{
13-
[DllImport( "Kernel32.dll" )]
14-
private static extern bool AttachConsole( int processId );
15-
16-
const int ATTACH_PARENT_PROCESS = -1;
17-
1814
private async void Application_StartupAsync( object sender, StartupEventArgs e )
1915
{
2016
try {
2117
// when no arguments are specified, execute main window
2218
if( e.Args.Length == 0 ) {
19+
AppEnvironment.GetInstance().IsConsoleMode = false;
2320
var window = new MainWindow();
2421
window.Show();
2522
return;
@@ -32,25 +29,33 @@ private async void Application_StartupAsync( object sender, StartupEventArgs e )
3229
Match argsChacker = scriptArgPattern.Match( e.Args[ 0 ] );
3330

3431
if( argsChacker.Success ) {
35-
await ScriptExecuter.ExecuteAsync( argsChacker.Groups[ "scriptPath" ].Value );
32+
string filePath = argsChacker.Groups[ "scriptPath" ].Value;
33+
if( File.Exists( filePath ) ){
34+
await ScriptExecuter.ExecuteAsync( filePath );
35+
}
36+
else {
37+
CommonUtil.WriteToConsole( "[File Error]" + Environment.NewLine + "'" + filePath + "' is not found." );
38+
}
3639
}
3740
else {
3841
Usage();
3942
}
4043
}
44+
catch( CompilationErrorException ex ) {
45+
CommonUtil.WriteToConsole( "[Compile Error]" + Environment.NewLine + ex.Message );
46+
}
4147
catch( Exception ex ) {
42-
Console.WriteLine( ex );
48+
CommonUtil.HandleException( ex );
4349
}
4450

4551
Current.Shutdown();
4652
}
4753

4854
private void Usage()
4955
{
50-
AttachConsole( ATTACH_PARENT_PROCESS );
51-
Console.WriteLine( "Usage: UserInputMacro <option>" );
52-
Console.WriteLine( "[option list]" );
53-
Console.WriteLine( "script=<scirpt path>: Command line mode and only execute script" );
56+
CommonUtil.WriteToConsole( "Usage: UserInputMacro <option>" + Environment.NewLine +
57+
"[option]" + Environment.NewLine +
58+
"-script=<scirpt path>: Command line mode and only execute script" );
5459
}
5560
}
5661
}

src/UserInputMacro/AppEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class AppEnvironment
1515
{
1616
private static AppEnvironment instance = new AppEnvironment();
1717
public ModeKind Mode { get; set; } = ModeKind.None;
18+
public bool IsConsoleMode { get; set; } = true;
1819

1920
private AppEnvironment()
2021
{

src/UserInputMacro/CommonUtil.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22
using System.Windows;
33
using System.Windows.Interop;
44
using System.Windows.Media;
5+
using System.Runtime.InteropServices;
56

67
namespace UserInputMacro
78
{
89
class CommonUtil
910
{
11+
const int ATTACH_PARENT_PROCESS = -1;
12+
13+
[DllImport( "Kernel32.dll" )]
14+
private static extern bool AttachConsole( int processId );
15+
16+
[DllImport( "Kernel32.dll" )]
17+
private static extern bool FreeConsole();
18+
1019
public static bool CheckMode( ModeKind mode )
1120
{
1221
var currentMode = AppEnvironment.GetInstance().Mode;
@@ -26,12 +35,25 @@ public static double GetDpiHeight()
2635
public static void HandleException( Exception ex )
2736
{
2837
Logger.WriteErrorLog( ex );
29-
MessageBox.Show( ex.ToString(), "Error" );
38+
39+
if( AppEnvironment.GetInstance().IsConsoleMode ) {
40+
WriteToConsole( ex.ToString() );
41+
}
42+
else {
43+
MessageBox.Show( ex.ToString(), "Error" );
44+
}
3045

3146
// for executing destructor of hook
3247
GC.Collect();
3348
}
3449

50+
public static void WriteToConsole( string str )
51+
{
52+
AttachConsole( ATTACH_PARENT_PROCESS );
53+
Console.WriteLine( str );
54+
FreeConsole();
55+
}
56+
3557
private static Matrix GetTransform()
3658
{
3759
Matrix transform;

0 commit comments

Comments
 (0)