-
Notifications
You must be signed in to change notification settings - Fork 6
/
WakaTimeSendHeartbeatThread.pas
182 lines (162 loc) · 4.09 KB
/
WakaTimeSendHeartbeatThread.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
unit WakaTimeSendHeartbeatThread;
interface
uses
Classes, SysUtils, ToolsAPI;
type
TSendHeartbeatThread = class(TThread)
private
FCLIPath: string;
FAPIKey: string;
FFileName: string;
FProjectName: string;
FIsFile: Boolean;
FTotalLines: Integer;
FIsWrite: Boolean;
protected
procedure Execute; override;
public
constructor Create(CLIPath, APIKey, FileName, ProjectName: string;
IsFile: Boolean; TotalLines: Integer; IsWrite: Boolean);
end;
implementation
{$I DelphiVersions.inc}
uses
Windows,
ShellAPI,
WakaTimeLogger,
StrUtils;
const
PluginVersion = '1.73.1'; // Replace with actual plugin version
{$IFDEF DELPHI_11_3_ALEXANDRIA}
DelphiVersion = '11.3';
{$ENDIF}
{$IFDEF DELPHI_10_4_SYDNEY}
DelphiVersion = '10.4';
{$ENDIF}
{$IFDEF DELPHI_10_3_RIO}
DelphiVersion = '10.3';
{$ENDIF}
{$IFDEF DELPHI_10_2_TOKYO}
DelphiVersion = '10.2';
{$ENDIF}
{$IFDEF DELPHI_10_1_BERLIN}
DelphiVersion = '10.1';
{$ENDIF}
{$IFDEF DELPHI_10_SEATTLE}
DelphiVersion = '10.0';
{$ENDIF}
{$IFDEF DELPHI_XE8}
DelphiVersion = 'XE8';
{$ENDIF}
{$IFDEF DELPHI_XE7}
DelphiVersion = 'XE7';
{$ENDIF}
{$IFDEF DELPHI_XE6}
DelphiVersion = 'XE6';
{$ENDIF}
{$IFDEF DELPHI_XE5}
DelphiVersion = 'XE5';
{$ENDIF}
{$IFDEF DELPHI_XE4}
DelphiVersion = 'XE4';
{$ENDIF}
{$IFDEF DELPHI_XE3}
DelphiVersion = 'XE3';
{$ENDIF}
{$IFDEF DELPHI_XE2}
DelphiVersion = 'XE2';
{$ENDIF}
{$IFDEF DELPHI_XE}
DelphiVersion = 'XE';
{$ENDIF}
{$IFDEF DELPHI_2010}
DelphiVersion = '2010';
{$ENDIF}
{$IFDEF DELPHI_2009}
DelphiVersion = '2009';
{$ENDIF}
{$IFDEF DELPHI_2007_FOR_NET}
DelphiVersion = '2007.NET';
{$ENDIF}
{$IFDEF DELPHI_2007}
DelphiVersion = '2007';
{$ENDIF}
{$IFDEF DELPHI_2006}
DelphiVersion = '2006';
{$ENDIF}
{$IFDEF DELPHI_2005}
DelphiVersion = '2005';
{$ENDIF}
{$IFDEF DELPHI_8_FOR_NET}
DelphiVersion = '8.NET';
{$ENDIF}
{$IFDEF DELPHI_7}
DelphiVersion = '7.0';
{$ENDIF}
{$IFDEF DELPHI_6}
DelphiVersion = '6.0';
{$ENDIF}
UserAgent = 'delphi/' + DelphiVersion + ' delphi-wakatime/' + PluginVersion;
constructor TSendHeartbeatThread.Create(CLIPath, APIKey, FileName,
ProjectName: string; IsFile: Boolean; TotalLines: Integer; IsWrite: Boolean);
begin
inherited Create(False);
FreeOnTerminate := True;
FCLIPath := CLIPath;
FAPIKey := APIKey;
FFileName := FileName;
FProjectName := ChangeFileExt(ProjectName, '');
FIsFile := IsFile;
FTotalLines := TotalLines;
FIsWrite := IsWrite;
end;
procedure TSendHeartbeatThread.Execute;
var
Commands: TStringList;
CommandLine, CLIFileName: string;
Operation: PChar;
FileName: PChar;
Parameters: PChar;
Directory: PChar;
ShowCommand: Integer;
begin
if (FAPIKey = '') then
begin
TWakaTimeLogger.Log('ApiKey not found =(');
exit;
end;
CLIFileName := Format('"%swakatime-cli.exe"', [FCLIPath]);
// Prepare the command line
Commands := TStringList.Create;
try
Commands.Add('--entity "' + FFileName + '"');
Commands.Add('--lines-in-file ' + IntToStr(FTotalLines));
Commands.Add('--alternate-project "' + FProjectName + '"');
Commands.Add('--plugin "' + UserAgent + '"');
CommandLine := ReplaceStr(Commands.Text, '=', '');
CommandLine := ReplaceStr(CommandLine, #13#10, ' ');
if FIsWrite then
CommandLine := CommandLine + ' --write';
// Set the parameters for ShellExecute
Operation := 'open';
FileName := PChar(CLIFileName);
Parameters := PChar(CommandLine);
Directory := nil;
ShowCommand := SW_HIDE; // Use SW_SHOW to show the command prompt window
TWakaTimeLogger.Log('Running: ' + CLIFileName);
TWakaTimeLogger.Log('With commands: ' + CommandLine);
try
// Execute the command
ShellExecute(0, Operation, FileName, Parameters, Directory, ShowCommand);
TWakaTimeLogger.Log('Command executed.');
except
on E: Exception do
TWakaTimeLogger.Log('Error sending command to wakatime-cli => ' +
E.Message);
end;
TWakaTimeLogger.Log('Completed without errors');
finally
Commands.Free;
end;
end;
end.