Check-in [15ba04d971]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added update command. Retrieved release notes for the latest version.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | updatecheck
Files: files | file ages | folders
SHA1: 15ba04d971ba1229b69fce52c35e09393e39a4dc
User & Date: tinus 2017-02-07 21:42:10
Context
2017-02-19
17:30
Merged in trunk, and incorporated some fixes for Win64. check-in: 23338c7949 user: tinus tags: updatecheck
2017-02-07
21:42
Added update command. Retrieved release notes for the latest version. check-in: 15ba04d971 user: tinus tags: updatecheck
2013-07-14
10:04
Fix botched-up merge. check-in: c8966d9ec7 user: Martijn tags: updatecheck
Changes

Changes to src/U_AutoUpdate.pas.

6
7
8
9
10
11
12


13
14
15
16
17
18
19
..
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
...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  SysUtils;

type
  TPluginUpdate = class
  private
    FURL: string;
//    FOnProgress: TProgressEvent; // TODO: see L_HttpClient



    function GetCurrentVersion: string;
    function GetLatestVersion: string;
  public
    constructor Create{(const AURL: string)};

    function IsUpdateAvailable(out NewVersion, Changes: string): Boolean;
................................................................................
  end;
end {TPluginUpdate.GetCurrentVersion};

{ ------------------------------------------------------------------------------------------------ }
function TPluginUpdate.GetLatestVersion: string;
var
  Http: THttpClient;

  RxVersion: TRegEx;

  Match: TMatch;
  i: Integer;
begin
  Result := '';


  // Download the current release notes
ODS('Create HttpClient');
  Http := THttpClient.Create('http://fossil.2of4.net/npp_preview/doc/publish/ReleaseNotes.txt');
  try
ODS('Http.Get');
    if Http.Get() = 200 then begin
ODS('%d %s', [Http.StatusCode, Http.StatusText]);
for i := 0 to Http.ResponseHeaders.Count - 1 do
  ODS(Http.ResponseHeaders[i]);

ODS('Response: "%s"', [Copy(StringReplace(StringReplace(Http.ResponseString, #10, '·', [rfReplaceAll]), #13, '·', [rfReplaceAll]), 1, 250)]);
      // get the response stream, and look for the latest version in there
      Match := TRegEx.Match(Http.ResponseString, 'v[0-9]+(\.[0-9]+){3}');



ODS('Match: Success=%s; Value="%s"', [BoolToStr(Match.Success, True), Match.Value]);
      if Match.Success then begin
        Result := Match.Value;






      end;


    end else begin
      // TODO: show message, open project's main page?
      raise EUpdateError.CreateFmt('%d %s', [Http.StatusCode, Http.StatusText]);
    end;
  finally
    Http.Free;
  end;


end {TPluginUpdate.GetLatestVersion};

{ ------------------------------------------------------------------------------------------------ }
function TPluginUpdate.IsUpdateAvailable(out NewVersion, Changes: string): Boolean;
begin
  NewVersion := LatestVersion;
  Result := CompareVersions(CurrentVersion, NewVersion) < 0;
  // TODO: Populate Changes from the text between the match of the first version number, and the next (or, if there is no next, the </pre> tag).


end {TPluginUpdate.IsUpdateAvailable};

{ ------------------------------------------------------------------------------------------------ }
function TPluginUpdate.DownloadUpdate: string;
begin
  // TODO: Download http://fossil.2of4.net/npp_preview/zip/Preview_Plugin.zip?uuid=publish&name=plugins to a temp dir,
  //  extract it to a custom temp folder, and return that folder's path
................................................................................
end {TPluginUpdate.DownloadUpdate};

{ ------------------------------------------------------------------------------------------------ }
function TPluginUpdate.ReplacePlugin(const PathExtracted: string): Boolean;
begin
  // TODO: Rename the current DLL to ChangeFileExt(DllName, '-' + OwnVersion + '.~dll')
  // HardlinkOrCopy all files in extract location to path relative to plugins folder. ./Config should
  //  be translated to PluginsConfigFolder. ReleaseNotes.txt get special treatment: it's in the
  //  root folder, but should be moved to ./Doc/PreviewHTML.
end {TPluginUpdate.ReplacePlugin};

{ ------------------------------------------------------------------------------------------------ }
class function TPluginUpdate.CompareVersions(const VersionA, VersionB: string): Integer;
var
  va, vb: string;
  ia, ib: Integer;
  na, nb: Integer;
  Code: Integer;
begin
  Result := 0;
  if (VersionA = '') or (VersionB = '') then
    Exit;

................................................................................
//ODS('vb: "%s"; nb: %d; Code: %d', [vb, nb, Code]);
      if Code in [0, 1] then
        vb := ''
      else
        vb := Copy(vb, Code + 1);
    end;

    Result := na - nb;
//ODS('Result = %d := %d - %d', [Result, nb, na]);
    if Result <> 0 then
      Exit;
  until (va = '') and (vb = '');
end {TPluginUpdate.CompareVersions};

end.







>
>







 







>

>



<
<
>
|
|
|
|
|
|
|
|
|
>
|
|
|
>
>
>
|
|
|
>
>
>
>
>
>
|
>
>
|
|
|
|
|
|
|
>
>






|

>
>







 







|







<







 







|







6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
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
...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  SysUtils;

type
  TPluginUpdate = class
  private
    FURL: string;
//    FOnProgress: TProgressEvent; // TODO: see L_HttpClient
    FLatestVersion: string;
    FChanges: string;

    function GetCurrentVersion: string;
    function GetLatestVersion: string;
  public
    constructor Create{(const AURL: string)};

    function IsUpdateAvailable(out NewVersion, Changes: string): Boolean;
................................................................................
  end;
end {TPluginUpdate.GetCurrentVersion};

{ ------------------------------------------------------------------------------------------------ }
function TPluginUpdate.GetLatestVersion: string;
var
  Http: THttpClient;
  Notes: string;
  RxVersion: TRegEx;
  Matches: TMatchCollection;
  Match: TMatch;
  i: Integer;
begin


  if FLatestVersion = '' then begin
    // Download the current release notes
    ODS('Create HttpClient');
    Http := THttpClient.Create('http://fossil.2of4.net/npp_preview/doc/publish/ReleaseNotes.txt');
    try
      ODS('Http.Get');
      if Http.Get() = 200 then begin
        ODS('%d %s', [Http.StatusCode, Http.StatusText]);
        for i := 0 to Http.ResponseHeaders.Count - 1 do
          ODS(Http.ResponseHeaders[i]);
        Notes := Http.ResponseString;
        ODS('Response: "%s"', [Copy(StringReplace(StringReplace(Notes, #10, '·', [rfReplaceAll]), #13, '·', [rfReplaceAll]), 1, 250)]);
        // get the response stream, and look for the latest version in there
        Matches := TRegEx.Matches(Notes, 'v[0-9]+(\.[0-9]+){3}');
        ODS('Matches: %d', [Matches.Count]);
        if Matches.Count > 0 then begin
          Match := Matches.Item[0];
          ODS('Match: Success=%s; Value="%s"; Index=%d', [BoolToStr(Match.Success, True), Match.Value, Match.Index]);
          if Match.Success then begin
            FLatestVersion := Match.Value;
            {$MESSAGE WARN 'TODO: read all versions until we find the current one. Otherwise stop at </pre>.'}
            if (Matches.Count > 1) and Matches[1].Success then begin
              FChanges := Notes.Substring(Match.Index - 1, Matches[1].Index - Match.Index);
            end else begin
              i := Pos('</pre>', Notes);
              FChanges := Notes.Substring(Match.Index - 1, i - Match.Index);
            end;
          end;
        end;
      end else begin
        // TODO: show message, open project's main page?
        raise EUpdateError.CreateFmt('%d %s', [Http.StatusCode, Http.StatusText]);
      end;
    finally
      Http.Free;
    end;
  end;
  Result := FLatestVersion;
end {TPluginUpdate.GetLatestVersion};

{ ------------------------------------------------------------------------------------------------ }
function TPluginUpdate.IsUpdateAvailable(out NewVersion, Changes: string): Boolean;
begin
  NewVersion := LatestVersion;
  Result := CompareVersions(CurrentVersion, NewVersion) > 0;
  // TODO: Populate Changes from the text between the match of the first version number, and the next (or, if there is no next, the </pre> tag).
//  if Result then
    Changes := FChanges;
end {TPluginUpdate.IsUpdateAvailable};

{ ------------------------------------------------------------------------------------------------ }
function TPluginUpdate.DownloadUpdate: string;
begin
  // TODO: Download http://fossil.2of4.net/npp_preview/zip/Preview_Plugin.zip?uuid=publish&name=plugins to a temp dir,
  //  extract it to a custom temp folder, and return that folder's path
................................................................................
end {TPluginUpdate.DownloadUpdate};

{ ------------------------------------------------------------------------------------------------ }
function TPluginUpdate.ReplacePlugin(const PathExtracted: string): Boolean;
begin
  // TODO: Rename the current DLL to ChangeFileExt(DllName, '-' + OwnVersion + '.~dll')
  // HardlinkOrCopy all files in extract location to path relative to plugins folder. ./Config should
  //  be translated to PluginsConfigFolder. ReleaseNotes.txt gets special treatment: it's in the
  //  root folder, but should be moved to ./Doc/PreviewHTML.
end {TPluginUpdate.ReplacePlugin};

{ ------------------------------------------------------------------------------------------------ }
class function TPluginUpdate.CompareVersions(const VersionA, VersionB: string): Integer;
var
  va, vb: string;

  na, nb: Integer;
  Code: Integer;
begin
  Result := 0;
  if (VersionA = '') or (VersionB = '') then
    Exit;

................................................................................
//ODS('vb: "%s"; nb: %d; Code: %d', [vb, nb, Code]);
      if Code in [0, 1] then
        vb := ''
      else
        vb := Copy(vb, Code + 1);
    end;

    Result := nb - na;
//ODS('Result = %d := %d - %d', [Result, nb, na]);
    if Result <> 0 then
      Exit;
  until (va = '') and (vb = '');
end {TPluginUpdate.CompareVersions};

end.

Changes to src/U_Npp_PreviewHTML.pas.

178
179
180
181
182
183
184




185
186
187
188
189
190
191
192
193


194
195
196
197
198
199

200
201
202
203
204
205
206




207
208
209
210
211
212
213

  self.AddFuncSeparator;

  self.AddFuncItem('Edit &settings', _FuncOpenSettings);
  self.AddFuncItem('Edit &filter definitions', _FuncOpenFilters);

  self.AddFuncSeparator;





  self.AddFuncItem('&About', _FuncShowAbout);
end {TNppPluginPreviewHTML.SetInfo};

{ ------------------------------------------------------------------------------------------------ }
procedure TNppPluginPreviewHTML.CommandCheckUpdate;
var
  Cur, Next: string;
  Update: TPluginUpdate;


begin
  try
    Update := TPluginUpdate.Create;
    try
      Cur := Update.CurrentVersion;
      Next := Update.LatestVersion;

    finally
      Update.Free;
    end;
    MessageBox(Npp.NppData.NppHandle,
              PChar(Format('Current: "%s"; Latest: "%s"; Difference: %d',
                          [Cur, Next, TPluginUpdate.CompareVersions(Cur, Next)])),
              PChar(Caption), MB_ICONINFORMATION);




  except
    ShowException(ExceptObject, ExceptAddr);
  end;
end {TNppPluginPreviewHTML.CommandCheckUpdate};

{ ------------------------------------------------------------------------------------------------ }
procedure TNppPluginPreviewHTML.CommandOpenFile(const Filename: nppString);







>
>
>
>









>
>






>







>
>
>
>







178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

  self.AddFuncSeparator;

  self.AddFuncItem('Edit &settings', _FuncOpenSettings);
  self.AddFuncItem('Edit &filter definitions', _FuncOpenFilters);

  self.AddFuncSeparator;

  self.AddFuncItem('Check for &updates', _FuncCheckUpdate);

  self.AddFuncSeparator;

  self.AddFuncItem('&About', _FuncShowAbout);
end {TNppPluginPreviewHTML.SetInfo};

{ ------------------------------------------------------------------------------------------------ }
procedure TNppPluginPreviewHTML.CommandCheckUpdate;
var
  Cur, Next: string;
  Update: TPluginUpdate;
  Notes: string;
  Yay: Boolean;
begin
  try
    Update := TPluginUpdate.Create;
    try
      Cur := Update.CurrentVersion;
      Next := Update.LatestVersion;
      Yay := Update.IsUpdateAvailable(Next, Notes);
    finally
      Update.Free;
    end;
    MessageBox(Npp.NppData.NppHandle,
              PChar(Format('Current: "%s"; Latest: "%s"; Difference: %d',
                          [Cur, Next, TPluginUpdate.CompareVersions(Cur, Next)])),
              PChar(Caption), MB_ICONINFORMATION);
    MessageBox(Npp.NppData.NppHandle,
              PChar(Format('Is update available? %s! Latest version: "%s"'#10#10'%s',
                          [BoolToStr(Yay, True), Next, Notes])),
              PChar(Caption), MB_ICONINFORMATION);
  except
    ShowException(ExceptObject, ExceptAddr);
  end;
end {TNppPluginPreviewHTML.CommandCheckUpdate};

{ ------------------------------------------------------------------------------------------------ }
procedure TNppPluginPreviewHTML.CommandOpenFile(const Filename: nppString);