Menu

[r2035]: / trunk / Src / USaveUnitMgr.pas  Maximize  Restore  History

Download this file

292 lines (259 with data), 8.9 kB

  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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
{* USaveUnitMgr.pas
*
* Defines a class that manages generation, previewing and saving of a pascal
* unit.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is USaveUnitMgr.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2006-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit USaveUnitMgr;
interface
uses
// Project
DB.USnippet, UIStringList, USourceFileInfo, USaveSourceMgr, USourceGen;
type
/// <summary>
/// Manages generation, previewing and saving of a Pascal unit to disk.
/// </summary>
/// <remarks>
/// Generated file can be a valid Pascal unit, a plain text file, an HTML
/// file or a RTF file. The last two file types can optionally be syntax
/// highlighted.
/// </remarks>
TSaveUnitMgr = class(TSaveSourceMgr)
strict private
var
/// <summary>Used to generate source code unit.</summary>
fSourceGen: TSourceGen;
/// <summary>Name of generated unit.</summary>
/// <remarks>If empty string a default name is used.</remarks>
fUnitName: string;
/// <summary>Flag true if unit contains at least one snippet from main
/// database, False only if unit is completely user defined.</summary>
fContainsMainDBSnippets: Boolean;
/// <summary>Gets name of unit to be used in generated code.</summary>
function UnitName: string;
/// <summary>Creates a string list containing comments to be written to
/// head of unit.</summary>
function CreateHeaderComments: IStringList;
strict protected
/// <summary>Object constuctor. Sets up object to save a unit containing
/// all snippets in given list.</summary>
constructor InternalCreate(const Snips: TSnippetList);
/// <summary>Gets description of given source code file type.</summary>
function GetFileTypeDesc(const FileType: TSourceFileType): string; override;
/// <summary>Gets default file name to display in dialog box.</summary>
function GetDefaultFileName: string; override;
/// <summary>Gets dialog box title.</summary>
function GetDlgTitle: string; override;
/// <summary>Get dialog box's help keyword.</summary>
function GetDlgHelpKeyword: string; override;
/// <summary>Gets title to be used for source document.</summary>
function GetDocTitle: string; override;
/// <summary>Generates raw, un-highlighted, source code.</summary>
/// <param name="CommentStyle">TCommentStyle [in] Style of commenting to be
/// used in source code.</param>
/// <returns>String containing generated source code.</returns>
function GenerateSource(const CommentStyle: TCommentStyle): string;
override;
/// <summary>Checks if a file name is valid for the kind of file being
/// saved.</summary>
/// <param name="FileName">string [in] Name of file to check.</param>
/// <param name="NameOK">Boolean [out] Set True if file name OK, False if
/// not.</param>
/// <param name="ErrorMessage">string [out] Error message describing
/// problem with invalid file name. Undefined if NameOK is True.</param>
procedure CheckFileName(const FileName: string; out NameOK: Boolean;
out ErrorMessage: string); override;
public
/// <summary>Object destructor. Tears down object.</summary>
destructor Destroy; override;
/// <summary>Creates and outputs a Pascal unit file containing specified
/// snippets with name and format speficied by user.</summary>
/// <param name="Snips">TSnippetList [in] List of snippets to include in
/// unit.</param>
class procedure Execute(const Snips: TSnippetList);
end;
implementation
uses
// Delphi
SysUtils,
// Project
UAppInfo, UUtils, Web.UInfo;
resourcestring
// Dialog box title
sSaveDlgTitle = 'Save Unit';
// File filter strings
sHTMLDesc = 'HTML file';
sRTFDesc = 'Rich text file';
sPascalDesc = 'Pascal unit';
sTextDesc = 'Plain text file';
// Default file / unit name
sDefUnitName = 'Snippets';
// Error message
sErrorMsg = 'Filename is not valid for a Pascal unit';
// Unit header comments
sLicense = 'This unit may be freely distributed and used on the condition '
+ 'that this comment is not removed from the unit.';
sMainDescription = 'The unit was generated automatically from a '
+ 'selection of source code taken from the Code Snippets Database at %0:s.';
sDisclaimer = 'The source code contained in this unit is made available on '
+ 'an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or '
+ 'implied. The code is used entirely at your own risk.';
sGenerated = 'Generated on : %0:s.';
sGenerator = 'Generated by : %0:s %1:s.';
sAdvert = 'The latest version of %0:s is available from the %1:s website '
+ 'at %2:s.';
sUserDescription = 'This unit was generated automatically.';
// Output document title
sDocTitle = 'Pascal unit generated by %s';
{ TSaveUnitMgr }
procedure TSaveUnitMgr.CheckFileName(const FileName: string;
out NameOK: Boolean; out ErrorMessage: string);
begin
NameOK := TSourceGen.IsFileNameValidUnitName(FileName);
if NameOK then
fUnitName := TSourceGen.UnitNameFromFileName(FileName)
else
begin
fUnitName := '';
ErrorMessage := sErrorMsg
end;
end;
function TSaveUnitMgr.CreateHeaderComments: IStringList;
begin
Result := TIStringList.Create;
if fContainsMainDBSnippets then
begin
// Result used for units that contain at snippet(s) from main database
Result.Add(sLicense);
Result.Add('');
Result.Add(Format(sMainDescription, [TWebInfo.DatabaseURL]));
Result.Add('');
Result.Add(sDisclaimer);
Result.Add('');
Result.Add(Format(sGenerated, [DateStamp]));
Result.Add(
Format(
sGenerator, [TAppInfo.FullProgramName, TAppInfo.ProgramReleaseInfo]
)
);
Result.Add('');
Result.Add(
Format(
sAdvert,
[TAppInfo.ProgramName, TAppInfo.CompanyName, TWebInfo.ProgramHomeURL]
)
);
end
else
begin
// Result used for units that contain only user defined snippets
Result.Add(sUserDescription);
Result.Add('');
Result.Add(Format(sGenerated, [DateStamp]));
Result.Add(
Format(
sGenerator, [TAppInfo.FullProgramName, TAppInfo.ProgramReleaseInfo]
)
);
end;
end;
destructor TSaveUnitMgr.Destroy;
begin
fSourceGen.Free;
inherited;
end;
class procedure TSaveUnitMgr.Execute(const Snips: TSnippetList);
begin
with InternalCreate(Snips) do
try
DoExecute;
finally
Free;
end;
end;
function TSaveUnitMgr.GenerateSource(const CommentStyle: TCommentStyle): string;
begin
Result := fSourceGen.UnitAsString(
UnitName, CommentStyle, CreateHeaderComments
);
end;
function TSaveUnitMgr.GetDefaultFileName: string;
begin
Result := sDefUnitName;
end;
function TSaveUnitMgr.GetDlgHelpKeyword: string;
begin
Result := 'SaveUnitDlg';
end;
function TSaveUnitMgr.GetDlgTitle: string;
begin
Result := sSaveDlgTitle;
end;
function TSaveUnitMgr.GetDocTitle: string;
begin
Result := Format(sDocTitle, [TAppInfo.ProgramName]);
end;
function TSaveUnitMgr.GetFileTypeDesc(const FileType: TSourceFileType): string;
const
Descriptions: array[TSourceFileType] of string = (
sTextDesc, sPascalDesc, sHTMLDesc, sRTFDesc
);
begin
Result := Descriptions[FileType];
end;
constructor TSaveUnitMgr.InternalCreate(const Snips: TSnippetList);
var
Snippet: TSnippet; // references each snippet in list
begin
Assert(Assigned(Snips), ClassName + '.InternalCreate: Snips is nil');
inherited InternalCreate;
// Create source generator and initialize it with required snippets
fSourceGen := TSourceGen.Create;
fSourceGen.IncludeSnippets(Snips);
// Determine if snippet list contains at least one snippet from main database
fContainsMainDBSnippets := False;
for Snippet in Snips do
begin
if not Snippet.UserDefined then
begin
fContainsMainDBSnippets := True;
Break;
end;
end;
end;
function TSaveUnitMgr.UnitName: string;
begin
// If we have valid unit name based on file, use it, otherwise use default
if fUnitName <> '' then
Result := fUnitName
else
Result := sDefUnitName;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.