Changeset 61 for trunk/Compiler/UProducer.pas
- Timestamp:
- Jul 17, 2012, 9:15:42 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Compiler/UProducer.pas
r59 r61 6 6 7 7 uses 8 USourceCode, Classes, SysUtils, StrUtils, SpecializedList; 8 USourceCode, Classes, SysUtils, StrUtils, SpecializedList, Process, 9 FileUtil, Forms; 9 10 10 11 type 11 12 12 13 TWriteTargetEvent = function (Name: string; const Code: string): Boolean of object; 14 TStringEvent = procedure (Value: string) of object; 13 15 14 16 { TProducer } … … 16 18 TProducer = class 17 19 private 20 FOnProcessOutput: TStringEvent; 18 21 FOnWriteTarget: TWriteTargetEvent; 19 22 public 23 Process: TProcess; 20 24 TextSource: TStringList; 21 25 IndentationLength: Integer; 22 26 Indentation: Integer; 23 27 CompilerPath: string; 28 CompilerParameters: string; 24 29 procedure Emit(AText: string); 25 30 procedure EmitLn(AText: string = ''); 26 31 procedure AssignToStringList(Target: TStringList); virtual; abstract; 27 32 procedure Produce(Module: TModule); virtual; abstract; 33 procedure ExternalExecute(CommandLine: string); 28 34 constructor Create; 29 35 destructor Destroy; override; 30 36 property OnWriteTarget: TWriteTargetEvent read FOnWriteTarget write FOnWriteTarget; 37 property OnProcessOutput: TStringEvent read FOnProcessOutput write FOnProcessOutput; 31 38 end; 32 39 … … 41 48 end; 42 49 50 procedure TProducer.ExternalExecute(CommandLine: string); 51 var 52 Buffer: string; 53 Count: Integer; 54 Text: string; 55 Line: string; 56 begin 57 if not FileExistsUTF8(CompilerPath) then Exit; 58 Text := ''; 59 try 60 Process := TProcess.Create(nil); 61 //if Path <> '' then 62 // Process.CurrentDirectory := Path; 63 //Path := ''; 64 //if Environment <> '' then 65 // Process.Environment.Text := Environment; 66 //Environment := ''; 67 Process.CommandLine := CommandLine; 68 Process.Options := [poUsePipes, poNoConsole]; 69 Process.Execute; 70 Application.ProcessMessages; 71 while Process.Running or (Process.Output.NumBytesAvailable > 0) or 72 (Process.Stderr.NumBytesAvailable > 0) do 73 begin 74 if Process.Output.NumBytesAvailable > 0 then begin 75 SetLength(Buffer, 1000); 76 Count := Process.Output.Read(Buffer[1], Length(Buffer)); 77 SetLength(Buffer, Count); 78 Text := Text + Buffer; 79 while Pos(LineEnding, Text) > 0 do begin 80 Line := Copy(Text, 1, Pos(LineEnding, Text) - 1); 81 Delete(Text, 1, Length(Line) + Length(LineEnding)); 82 if Assigned(FOnProcessOutput) then 83 FOnProcessOutput(Line); 84 end; 85 end; 86 87 if Process.Stderr.NumBytesAvailable > 0 then begin 88 SetLength(Buffer, 1000); 89 Count := Process.Stderr.Read(Buffer[1], Length(Buffer)); 90 SetLength(Buffer, Count); 91 Text := Text + Buffer; 92 while Pos(LineEnding, Text) > 0 do begin 93 Line := Copy(Text, 1, Pos(LineEnding, Text) - 1); 94 Delete(Text, 1, Length(Line) + Length(LineEnding)); 95 if Assigned(FOnProcessOutput) then 96 FOnProcessOutput(Line); 97 end; 98 end; 99 Sleep(10); 100 Application.ProcessMessages; 101 end; 102 finally 103 if Assigned(FOnProcessOutput) then 104 FOnProcessOutput(Text); 105 FreeAndNil(Process); 106 end; 107 end; 108 43 109 constructor TProducer.Create; 44 110 begin 45 111 TextSource := TStringList.Create; 46 112 IndentationLength := 2; 113 CompilerParameters := '%0:s'; 47 114 end; 48 115
Note:
See TracChangeset
for help on using the changeset viewer.