Skip to content

Commit

Permalink
Windows compile script
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKaszeba19 committed Jun 23, 2021
1 parent e900423 commit ef9dd14
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 12 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ Author: Paul Lipkowski
- Required: FreePascal Compiler `fpc` (version 3.0.4 or newer)
- So far works on Linux amd64 and Windows x64 only.
- Not designed for 32-bit computers.
- Compile using `compile.sh` on Linux (or `compile.bat` on Windows)
- Install to `$PATH` using `installBash.sh`
- If using Linux, then just compile by executing `compile.sh`
* Then you can install fpclock to `$PATH` using `installBash.sh`
- If using Windows, then compile by executing `compile.bat`
* The default version of FPC is 3.0.4. If you use the another version of it, then edit the `compile.bat` script and change the setting containing version of FPC (variable `ver`) in order to match your FPC version.

## Usage
- Syntax: `fpclock 'process' [flags]`
Expand All @@ -23,6 +25,8 @@ Author: Paul Lipkowski
* `-p N`, `--prec=N` – Set precision to N digits (default N=4)
* `-P`, `--prompt` – Prompt for a command from standard input
* `-u U`, `--units=U` – Set measurement unit to U' (see more Us below)
* `-w` , `--wait` – Pause after measuring time (Windows only)
* `-w N`, `--wait=N` – Wait N milliseconds after measuring time (Windows only, default N=0)
- Available units with their flag values (`U`):
* days – `d` or `days`
* minutes – `m`, `mins` or `minutes`
Expand Down
29 changes: 28 additions & 1 deletion compile.bat
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
C:\lazarus\fpc\3.0.4\bin\x86_64-win64\fpc.exe main.pas -o"fpclock.exe"
@echo off
echo Starting...

set ver=3.0.4

echo Attempting to install from Lazarus FPC executable...
call C:\lazarus\fpc\%ver%\bin\x86_64-win64\fpc.exe main.pas -o"fpclock.exe"
if %ERRORLEVEL% == 0 goto :next

echo Failed. Attempting to install from a non-Lazarus FPC executable...
call C:\fpc\%ver%\bin\x86_64-win64\fpc.exe main.pas -o"fpclock.exe"
if %ERRORLEVEL% == 0 goto :next

echo Failed. Maybe there is FPC in Windows PATH...
fpc.exe main.pas -o"fpclock.exe"
if %ERRORLEVEL% == 0 goto :next
echo Failed. Could not find a FPC in the %ver% version.
goto :end

:next
echo.
echo.
echo ================================================================
echo Done.
echo ================================================================

:end
pause
75 changes: 66 additions & 9 deletions main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ procedure printClocked(input : Extended; Precision : ShortInt);
if (s < 10.0)
then write('0', s:2:(Precision))
else write(s:2:(Precision));

end;

type
Expand All @@ -68,6 +67,10 @@ FPClock = class(TCustomApplication)
Units : Extended;
CString : Boolean;
Clocked : Boolean;
{$IFDEF MSWINDOWS}
Wait : Integer;
function GetLatestPowershell : String;
{$ENDIF}
protected
procedure DoRun; override;
public
Expand All @@ -76,6 +79,29 @@ FPClock = class(TCustomApplication)
procedure WriteHelp; virtual;
end;

{$IFDEF MSWINDOWS}
function FPClock.GetLatestPowershell : String;
var
a, b : ShortInt;
Found : Boolean = False;
begin
Result := 'C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe';
for a := 8 downto 1 do
begin
for b := 1 downto 0 do
begin
if FileExists('C:\Windows\System32\WindowsPowershell\v'+IntToStr(a)+'.'+IntToStr(b)+'\powershell.exe') then
begin
Result := 'C:\Windows\System32\WindowsPowershell\v'+IntToStr(a)+'.'+IntToStr(b)+'\powershell.exe';
Found := True;
end;
if Found then Break;
end;
if Found then Break;
end;
end;
{$ENDIF}

procedure FPClock.DoRun;
var
input : String;
Expand All @@ -102,6 +128,17 @@ procedure FPClock.DoRun;
Halt(1);
end;
end;

{$IFDEF MSWINDOWS}
if HasOption('w', 'wait') then
begin
if not (TryStrToInt(getOptionValue('w', 'wait'), Wait)) or (Wait < 0)
then begin
Wait := -1;
end;
end;
{$ENDIF}

if HasOption('u', 'units') then
begin
case getOptionValue('u', 'units') of
Expand Down Expand Up @@ -151,18 +188,26 @@ procedure FPClock.DoRun;
begin
case getOptionValue('e', 'env') of
'cmd' : ExecPath := 'c:\windows\system32\cmd.exe';
'cmd.exe' : ExecPath := 'c:\windows\system32\cmd.exe';
'powershell.exe' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe';
'powershell' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe';
'powershell' : ExecPath := GetLatestPowershell();
'powershell1' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe';
'powershell2' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v2.0\powershell.exe';
'powershell3' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v3.0\powershell.exe';
'powershell4' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v4.0\powershell.exe';
'ps' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe';
'powershell5' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v5.0\powershell.exe';
'powershell5.1' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v5.1\powershell.exe';
'powershell6' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v6.0\powershell.exe';
'powershell7' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v7.0\powershell.exe';
'powershell8' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v8.0\powershell.exe';
'ps' : ExecPath := GetLatestPowershell();
'ps1' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe';
'ps2' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v2.0\powershell.exe';
'ps3' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v3.0\powershell.exe';
'ps4' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v4.0\powershell.exe';
'ps5' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v5.0\powershell.exe';
'ps5.1' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v5.1\powershell.exe';
'ps6' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v6.0\powershell.exe';
'ps7' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v7.0\powershell.exe';
'ps8' : ExecPath := 'C:\Windows\System32\WindowsPowershell\v8.0\powershell.exe';
else ExecPath := getOptionValue('e', 'env');
end;
end;
Expand All @@ -171,20 +216,24 @@ procedure FPClock.DoRun;
Clock := TStopwatch.Create;
Clock.Start();
{$IFDEF MSWINDOWS}
//RunCommand(ExecPath, ['/c', input], Result);
SysUtils.ExecuteProcess(utf8toansi(ExecPath + ' /c "' + input + '"'), '', []);
{$ENDIF}
{$IFDEF UNIX}
Status := fpSystem(input);
{$ENDIF}
Clock.Stop();
{$IFDEF MSWINDOWS}
//writeln(Result);
{$ENDIF}

if (Clocked)
then printClocked(Clock.ElapsedTicks / Units, Precision)
else write((Clock.ElapsedTicks / Units):2:(Precision));
if (FeedLine) then writeln();

{$IFDEF MSWINDOWS}
if (Wait > 0)
then Sleep(Wait)
else if (Wait = -1) then readln();
{$ENDIF}

Terminate;
end;

Expand All @@ -197,6 +246,9 @@ constructor FPClock.Create(TheOwner: TComponent);
Clocked := False;
Precision := 4;
Units := SEC;
{$IFDEF MSWINDOWS}
Wait := 0;
{$ENDIF}
end;

destructor FPClock.Destroy;
Expand All @@ -223,6 +275,11 @@ procedure FPClock.WriteHelp;
writeln(' -P , --prompt : Prompt for a command from standard input');
writeln(' -u U, --units=U : Set measurement unit to U');
writeln(' (U in [ticks, clock, ns, mus, μs, ms, s, m, h, d])');
{$IFDEF MSWINDOWS}
writeln(' -w , --wait : Pause after measuring time (Windows only)');
writeln(' -w N, --wait=N : Wait N milliseconds after measuring time ');
writeln(' (Windows only, default N=0)');
{$ENDIF}
writeln();
writeln('Examples');
writeln(' - fpclock ''ls -l''');
Expand Down

0 comments on commit ef9dd14

Please sign in to comment.