Recent

Author Topic: [SOLVED] Dart sass gui  (Read 313 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 597
[SOLVED] Dart sass gui
« on: June 19, 2025, 11:34:01 am »
Hello, I wanted to write a gui for dart-sass, but I encountered a problem in the dart-sass folder there is a sass.snapshot file and a dart.exe file there is also a sass.bat file with contents
Code: Pascal  [Select][+][-]
  1. @echo off
  2. REM This script drives the standalone dart-sass package, which bundles together a
  3. REM Dart executable and a snapshot of dart-sass.
  4.  
  5. set SCRIPTPATH=%~dp0
  6. set arguments=%*
  7. "%SCRIPTPATH%\src\dart.exe" "%SCRIPTPATH%\src\sass.snapshot" %arguments%
  8.  

my code not working
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons, Process, StrUtils, Math;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Edit1: TEdit;
  18.     Edit2: TEdit;
  19.     Edit3: TEdit;
  20.     Memo1: TMemo;
  21.     OpenDialog1: TOpenDialog;
  22.     SpeedButton1: TSpeedButton;
  23.     SpeedButton2: TSpeedButton;
  24.     procedure Button1Click(Sender: TObject);
  25.     procedure Button2Click(Sender: TObject);
  26.     procedure SpeedButton1Click(Sender: TObject);
  27.     procedure SpeedButton2Click(Sender: TObject);
  28.   private
  29.      SassProcess: TProcess;
  30.     procedure KillProcess;
  31.     function GetRelativePath(const BasePath, FullPath: string): string;
  32.  
  33.   public
  34.  
  35.   end;
  36.  
  37. var
  38.   Form1: TForm1;
  39.  
  40. implementation
  41.  
  42. {$R *.lfm}
  43.  
  44. { TForm1 }
  45.  
  46. function TForm1.GetRelativePath(const BasePath, FullPath: string): string;
  47. var
  48.   BaseList, FileList: TStringArray;
  49.   i, CommonIndex, UpLevels: Integer;
  50.   RelativePart, NormalizedBase, NormalizedFull: String;
  51. begin
  52.   // Normalizacja ścieżek
  53.   NormalizedBase := ExcludeTrailingPathDelimiter(ExpandFileName(BasePath));
  54.   NormalizedFull := ExpandFileName(FullPath);
  55.  
  56.   // Jeśli ścieżki są na różnych dyskach → pełna ścieżka
  57.   if AnsiCompareText(ExtractFileDrive(NormalizedBase), ExtractFileDrive(NormalizedFull)) <> 0 then
  58.     Exit(NormalizedFull);
  59.  
  60.   // Jeśli BasePath jest początkiem FullPath → bezpośrednia relatywizacja
  61.   if AnsiStartsText(NormalizedBase + PathDelim, NormalizedFull + PathDelim) then
  62.     Exit(Copy(NormalizedFull, Length(NormalizedBase) + 2, MaxInt));
  63.  
  64.   // Podział ścieżek na segmenty
  65.   BaseList := SplitString(NormalizedBase, PathDelim);
  66.   FileList := SplitString(NormalizedFull, PathDelim);
  67.  
  68.   // Znalezienie wspólnego katalogu
  69.   CommonIndex := -1;
  70.   for i := 0 to Min(High(BaseList), High(FileList)) do
  71.   begin
  72.     if AnsiCompareText(BaseList[i], FileList[i]) <> 0 then Break;
  73.     CommonIndex := i;
  74.   end;
  75.  
  76.   // Cofanie się do wspólnego katalogu
  77.   UpLevels := Length(BaseList) - CommonIndex - 1;
  78.   RelativePart := DupeString('..' + PathDelim, UpLevels);
  79.  
  80.   // Dodanie reszty ścieżki
  81.   for i := CommonIndex + 1 to High(FileList) do
  82.   begin
  83.     RelativePart := RelativePart + FileList[i];
  84.     if i < High(FileList) then
  85.       RelativePart := RelativePart + PathDelim;
  86.   end;
  87.  
  88.   Result := RelativePart;
  89. end;
  90.  
  91. procedure TForm1.Button1Click(Sender: TObject);
  92. var
  93.   DartPath, SnapshotPath, InputPath, OutputPath: string;
  94. begin
  95.  DartPath := ExpandFileName(Edit3.Text + 'src' + PathDelim + 'dart.exe');
  96.   SnapshotPath := ExpandFileName(Edit3.Text + 'src' + PathDelim + 'sass.snapshot');
  97.   SassProcess := TProcess.Create(nil);
  98.   SassProcess.Options := [poUsePipes, poStderrToOutPut, poNoConsole];
  99.   SassProcess.CurrentDirectory := Form1.Edit3.Text;
  100.  
  101.   SassProcess.Executable := DartPath;
  102.   SassProcess.Parameters.Add(SnapshotPath);
  103. SassProcess.Parameters.Add('sass');
  104. SassProcess.Parameters.Add(Edit1.Text);
  105. SassProcess.Parameters.Add(Edit2.Text);
  106.  
  107.   SassProcess.Execute;
  108. end;
  109.  
  110. procedure TForm1.Button2Click(Sender: TObject);
  111. begin
  112.   KillProcess;
  113. end;
  114.  
  115. procedure TForm1.SpeedButton1Click(Sender: TObject);
  116. var
  117.   FullPath, ProjectPath: string;
  118. begin
  119.   if OpenDialog1.Execute then
  120.   begin
  121.     FullPath := ExtractFilePath(OpenDialog1.FileName);
  122.     ProjectPath := ExtractFileDir(ExcludeTrailingPathDelimiter(FullPath));
  123.     Edit3.Text := IncludeTrailingPathDelimiter(ProjectPath);
  124.     Edit1.Text := GetRelativePath(Edit3.Text, OpenDialog1.FileName);
  125.   end;
  126. end;
  127.  
  128. procedure TForm1.SpeedButton2Click(Sender: TObject);
  129. begin
  130.  OpenDialog1.InitialDir := Edit3.Text;
  131.  
  132.   if OpenDialog1.Execute then
  133.   begin
  134.     Edit2.Text := GetRelativePath(Edit3.Text, OpenDialog1.FileName);
  135.   end;
  136. end;
  137.  
  138. procedure TForm1.KillProcess;
  139. begin
  140.    if Assigned(SassProcess) then
  141.   begin
  142.     if SassProcess.Running then
  143.       SassProcess.Terminate(0);
  144.     SassProcess.Free;
  145.     SassProcess := nil;
  146.   end;
  147.  
  148. end;
  149.  
  150. end.
  151.  

Is it possible to improve the code to make it work?
« Last Edit: June 19, 2025, 12:08:47 pm by Pe3s »

 

TinyPortal © 2005-2018