1+ using System . Diagnostics ;
2+ using DiffEngine ;
3+ using Shrinker . Parser ;
4+ using TextCopy ;
5+
6+ namespace Shrinker . Cmd
7+ {
8+ internal static class Program
9+ {
10+ private static void Main ( string [ ] args )
11+ {
12+ var glsl = ClipboardService . GetText ( ) ;
13+ if ( string . IsNullOrEmpty ( glsl ) )
14+ {
15+ Console . WriteLine ( "Error - Clipboard is empty." ) ;
16+ return ;
17+ }
18+
19+ var lexer = new Lexer . Lexer ( ) ;
20+ if ( ! lexer . Load ( glsl ) )
21+ {
22+ Console . WriteLine ( "Error - Unable to process the GLSL." ) ;
23+ return ;
24+ }
25+
26+ var parser = new Parser . Parser ( lexer ) ;
27+ try
28+ {
29+ Console . WriteLine ( "Parsing..." ) ;
30+ var rootNode = parser . Parse ( ) ;
31+
32+ Console . WriteLine ( "Simplifying..." ) ;
33+ var options = CustomOptions . All ( ) ;
34+ options . CombineConsecutiveAssignments = false ;
35+ options . RemoveComments = false ;
36+ rootNode . Simplify ( options ) ;
37+
38+ Console . WriteLine ( "Creating GLSL..." ) ;
39+ var newGlsl = rootNode . ToCode ( ) ;
40+ ClipboardService . SetText ( newGlsl ) ;
41+
42+ Console . WriteLine ( "Success." ) ;
43+
44+ var oldFile = CreateTempFileFromString ( glsl , ".glsl" ) ;
45+ var newFile = CreateTempFileFromString ( newGlsl , ".glsl" ) ;
46+ try
47+ {
48+ Console . WriteLine ( "Launching diff process..." ) ;
49+ var diffTool = new [ ] { DiffTool . DiffMerge , DiffTool . VisualStudioCode } . FirstOrDefault ( o => DiffRunner . Launch ( o , oldFile , newFile ) != LaunchResult . NoDiffToolFound ) ;
50+ if ( diffTool != default )
51+ {
52+ if ( ! OperatingSystem . IsWindows ( ) )
53+ {
54+ if ( DiffTools . TryFindByName ( diffTool , out var resolved ) )
55+ {
56+ var processName = Path . GetFileNameWithoutExtension ( resolved . ExePath ) ;
57+
58+ var attempts = 50 ;
59+ while ( ! Process . GetProcesses ( ) . Any ( o => o . ProcessName . StartsWith ( processName , StringComparison . OrdinalIgnoreCase ) ) && -- attempts > 0 )
60+ Thread . Sleep ( 100 ) ;
61+
62+ if ( attempts > 0 )
63+ {
64+ Console . WriteLine ( " Waiting for process end..." ) ;
65+ while ( Process . GetProcesses ( ) . Any ( o => o . ProcessName . StartsWith ( processName , StringComparison . OrdinalIgnoreCase ) ) )
66+ Thread . Sleep ( 1000 ) ;
67+ }
68+ }
69+ else
70+ {
71+ Console . WriteLine ( " Unable to determine diff tool process name." ) ;
72+ return ;
73+ }
74+ }
75+
76+ Console . WriteLine ( " Complete." ) ;
77+ }
78+ else
79+ {
80+ Console . WriteLine ( " Failed to find a suitable tool." ) ;
81+ }
82+ }
83+ finally
84+ {
85+ File . Delete ( oldFile ) ;
86+ File . Delete ( newFile ) ;
87+ }
88+ }
89+ catch ( Exception ex )
90+ {
91+ Console . WriteLine ( $ " Error - { ex . Message } ") ;
92+ }
93+ }
94+
95+ private static string CreateTempFileFromString ( string s , string fileExtension )
96+ {
97+ var fileName = Path . Combine ( Path . GetTempPath ( ) , $ "{ Path . GetRandomFileName ( ) } { fileExtension } ") ;
98+ File . WriteAllText ( fileName , s ) ;
99+ return fileName ;
100+ }
101+ }
102+ }
0 commit comments