<commit_before>﻿unit FIToolkit.Main;

interface

uses
  System.SysUtils, System.Types;

  procedure RunApplication(const FullExePath : TFileName; const CmdLineOptions : TStringDynArray);

implementation

uses
  System.IOUtils,
  FIToolkit.Exceptions,
  FIToolkit.Base.Utils,
  FIToolkit.CommandLine.Options, FIToolkit.CommandLine.Consts,
  FIToolkit.Config.Manager;

type

  TFIToolkit = class sealed
    strict private
      FConfig : TConfigManager;
      FOptions : TCLIOptions;
    private
      procedure InitConfig;
      procedure InitOptions(const CmdLineOptions : TStringDynArray);
    public
      constructor Create(const FullExePath : TFileName; const CmdLineOptions : TStringDynArray);
      destructor Destroy; override;

      procedure Run;
  end;

{ Utils }

procedure RunApplication(const FullExePath : TFileName; const CmdLineOptions : TStringDynArray);
  var
    FIToolkit : TFIToolkit;
begin
  FIToolkit := TFIToolkit.Create(FullExePath, CmdLineOptions);
  try
    FIToolkit.Run;
  finally
    FIToolkit.Free;
  end;
end;

{ TFIToolkit }

constructor TFIToolkit.Create(const FullExePath : TFileName; const CmdLineOptions : TStringDynArray);
begin
  inherited Create;

  InitOptions(CmdLineOptions);
  InitConfig;
end;

destructor TFIToolkit.Destroy;
begin
  FreeAndNil(FConfig);
  FreeAndNil(FOptions);

  inherited Destroy;
end;

procedure TFIToolkit.InitConfig;
  var
    SetConfigOption : TCLIOption;
begin
  if FOptions.Find(STR_CLI_OPTION_SET_CONFIG, SetConfigOption) and
     TPath.IsApplicableFileName(SetConfigOption.Value)
  then
    FConfig := TConfigManager.Create(SetConfigOption.Value, FOptions.Contains(STR_CLI_OPTION_GENERATE_CONFIG), True)
  else
    raise ENoConfigSpecified.Create;
end;

procedure TFIToolkit.InitOptions(const CmdLineOptions : TStringDynArray);
  var
    S : String;
begin
  FOptions := TCLIOptions.Create;<commit_msg>[+] implemented behavior: fail if specified config file doesn't exist and generate-config option wasn't set<commit_after>﻿unit FIToolkit.Main;

interface

uses
  System.SysUtils, System.Types;

  procedure RunApplication(const FullExePath : TFileName; const CmdLineOptions : TStringDynArray);

implementation

uses
  System.IOUtils,
  FIToolkit.Exceptions,
  FIToolkit.Base.Utils,
  FIToolkit.CommandLine.Options, FIToolkit.CommandLine.Consts,
  FIToolkit.Config.Manager;

type

  TFIToolkit = class sealed
    strict private
      FConfig : TConfigManager;
      FOptions : TCLIOptions;
    private
      procedure InitConfig;
      procedure InitOptions(const CmdLineOptions : TStringDynArray);
    public
      constructor Create(const FullExePath : TFileName; const CmdLineOptions : TStringDynArray);
      destructor Destroy; override;

      procedure Run;
  end;

{ Utils }

procedure RunApplication(const FullExePath : TFileName; const CmdLineOptions : TStringDynArray);
  var
    FIToolkit : TFIToolkit;
begin
  FIToolkit := TFIToolkit.Create(FullExePath, CmdLineOptions);
  try
    FIToolkit.Run;
  finally
    FIToolkit.Free;
  end;
end;

{ TFIToolkit }

constructor TFIToolkit.Create(const FullExePath : TFileName; const CmdLineOptions : TStringDynArray);
begin
  inherited Create;

  InitOptions(CmdLineOptions);
  InitConfig;
end;

destructor TFIToolkit.Destroy;
begin
  FreeAndNil(FConfig);
  FreeAndNil(FOptions);

  inherited Destroy;
end;

procedure TFIToolkit.InitConfig;
  var
    bHasGenerateConfigOption : Boolean;
    SetConfigOption : TCLIOption;
begin
  bHasGenerateConfigOption := FOptions.Contains(STR_CLI_OPTION_GENERATE_CONFIG);

  if FOptions.Find(STR_CLI_OPTION_SET_CONFIG, SetConfigOption) and
     TPath.IsApplicableFileName(SetConfigOption.Value) and
     (TFile.Exists(SetConfigOption.Value) or bHasGenerateConfigOption)
  then
    FConfig := TConfigManager.Create(SetConfigOption.Value, bHasGenerateConfigOption, True)
  else
    raise ENoConfigSpecified.Create;
end;

procedure TFIToolkit.InitOptions(const CmdLineOptions : TStringDynArray);
  var
    S : String;
begin
  FOptions := TCLIOptions.Create;
{'fraction_non_alphanumeric': 0.07274539113104135, 'fraction_numerical': 0.0, 'mean_word_length': 4.719373219373219, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1275661', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1307, 'n_words': 271}