<commit_before>﻿unit FIToolkit.Runner.Tasks;

interface

uses
  System.SysUtils, System.Threading,
  FIToolkit.Config.FixInsight;

type

  TTaskRunner = class sealed
    strict private
      FExecutable : TFileName;
      FOptions : TFixInsightOptions;
      FOutputFileName : TFileName;
    private
      function GenerateOutputFileName : String;
    public
      constructor Create(const Executable : TFileName; Options : TFixInsightOptions);
      destructor Destroy; override;

      function Execute : ITask;

      property OutputFileName : TFileName read FOutputFileName;
  end;

implementation

uses
  System.IOUtils, System.Classes, Winapi.Windows,
  FIToolkit.Commons.Utils, FIToolkit.CommandLine.Types, FIToolkit.Runner.Exceptions, FIToolkit.Runner.Consts;

{ TTaskRunner }

constructor TTaskRunner.Create(const Executable : TFileName; Options : TFixInsightOptions);
begin
  inherited Create;

  FExecutable := Executable;
  FOptions := TFixInsightOptions.Create;
  FOptions.Assign(Options, False);
end;

destructor TTaskRunner.Destroy;
begin
  FreeAndNil(FOptions);

  inherited Destroy;
end;

function TTaskRunner.Execute : ITask;
begin
  Result := TTask.Run(
    procedure
    var
      sCmdLine : String;
      SI : TStartupInfo;
      PI : TProcessInformation;
    begin
      FOptions.Validate := True;
      FOptions.OutputFileName := GenerateOutputFileName;
      FOutputFileName := FOptions.OutputFileName;

      sCmdLine := Format('%s %s', [TPath.GetQuotedPath(FExecutable, TCLIOptionString.CHR_QUOTE), FOptions.ToString]);
      FillChar(SI, SizeOf(TStartupInfo), 0);
      SI.cb := SizeOf(TStartupInfo);
      SI.wShowWindow := SW_HIDE;

      if CreateProcess(PChar(FExecutable), PChar(sCmdLine), nil, nil, False, CREATE_NO_WINDOW, nil, nil, SI, PI) then
        try
          while WaitForSingleObject(PI.hProcess, INFINITE) <> WAIT_OBJECT_0 do
            TThread.SpinWait(INT_SPIN_WAIT_ITERATIONS);
        finally<commit_msg>[-] less exceptions in thread - better<commit_after>﻿unit FIToolkit.Runner.Tasks;

interface

uses
  System.SysUtils, System.Threading,
  FIToolkit.Config.FixInsight;

type

  TTaskRunner = class sealed
    strict private
      FExecutable : TFileName;
      FOptions : TFixInsightOptions;
      FOutputFileName : TFileName;
    private
      function GenerateOutputFileName : String;
    public
      constructor Create(const Executable : TFileName; Options : TFixInsightOptions);
      destructor Destroy; override;

      function Execute : ITask;

      property OutputFileName : TFileName read FOutputFileName;
  end;

implementation

uses
  System.IOUtils, System.Classes, Winapi.Windows,
  FIToolkit.Commons.Utils, FIToolkit.CommandLine.Types, FIToolkit.Runner.Exceptions, FIToolkit.Runner.Consts;

{ TTaskRunner }

constructor TTaskRunner.Create(const Executable : TFileName; Options : TFixInsightOptions);
begin
  inherited Create;

  FExecutable := Executable;
  FOptions := TFixInsightOptions.Create;
  FOptions.Assign(Options, False);
end;

destructor TTaskRunner.Destroy;
begin
  FreeAndNil(FOptions);

  inherited Destroy;
end;

function TTaskRunner.Execute : ITask;
begin
  Result := TTask.Run(
    procedure
    var
      sCmdLine : String;
      SI : TStartupInfo;
      PI : TProcessInformation;
    begin
      FOptions.OutputFileName := GenerateOutputFileName;
      FOutputFileName := FOptions.OutputFileName;

      sCmdLine := Format('%s %s', [TPath.GetQuotedPath(FExecutable, TCLIOptionString.CHR_QUOTE), FOptions.ToString]);
      FillChar(SI, SizeOf(TStartupInfo), 0);
      SI.cb := SizeOf(TStartupInfo);
      SI.wShowWindow := SW_HIDE;

      if CreateProcess(PChar(FExecutable), PChar(sCmdLine), nil, nil, False, CREATE_NO_WINDOW, nil, nil, SI, PI) then
        try
          while WaitForSingleObject(PI.hProcess, INFINITE) <> WAIT_OBJECT_0 do
            TThread.SpinWait(INT_SPIN_WAIT_ITERATIONS);
        finally
{'fraction_non_alphanumeric': 0.08717948717948718, 'fraction_numerical': 0.0010256410256410256, 'mean_word_length': 3.810110974106042, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2636126', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1203, 'n_words': 260}