Skip to content

Commit c478a30

Browse files
author
delphidabbler
committed
Modify header comments in generated code to get license and copyright from database meta data
1 parent edb2b86 commit c478a30

File tree

3 files changed

+100
-38
lines changed

3 files changed

+100
-38
lines changed

Src/USaveUnitMgr.pas

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ implementation
9696
// Delphi
9797
SysUtils,
9898
// Project
99-
UAppInfo, UUrl, UUtils;
99+
DB.UMetaData,
100+
UAppInfo,
101+
UUrl,
102+
UUtils;
100103

101-
// TODO -cDatabase: get license info from database rather than use literal text.
102104

103105
resourcestring
104106
// Dialog box title
@@ -113,11 +115,9 @@ implementation
113115
// Error message
114116
sErrorMsg = 'Filename is not valid for a Pascal unit';
115117
// Unit header comments
116-
// TODO -cWeb: Change following to get license from database meta data
117-
sLicense = 'This unit is copyright © %0:s by the Peter Johnson and Code '
118-
+ 'Snippets Database contributors and is licensed under the MIT license '
119-
+ '(http://opensource.org/licenses/MIT).';
120-
sMainDescription = 'The unit was generated automatically and incorporates a '
118+
sLicense = 'The unit is copyright © %0:s by %1:s and is licensed under '
119+
+ 'the %2:s.';
120+
sMainDescription = 'This unit was generated automatically and incorporates a '
121121
+ 'selection of source code taken from the Code Snippets Database at %0:s.';
122122
sGenerated = 'Generated on : %0:s.';
123123
sGenerator = 'Generated by : %0:s %1:s.';
@@ -144,15 +144,27 @@ procedure TSaveUnitMgr.CheckFileName(const FileName: string;
144144
end;
145145

146146
function TSaveUnitMgr.CreateHeaderComments: IStringList;
147+
var
148+
DBMetaData: IDBMetaData;
147149
begin
148150
Result := TIStringList.Create;
149151
if fContainsMainDBSnippets then
150152
begin
151-
// Result used for units that contain at snippet(s) from main database
152-
Result.Add(Format(sLicense, [FormatDateTime('YYYY', Now)]));
153-
Result.Add('');
153+
// Result used for units that contain any snippet(s) from main database
154154
Result.Add(Format(sMainDescription, [TURL.CodeSnipRepo]));
155155
Result.Add('');
156+
DBMetaData := TMainDBMetaDataFactory.MainDBMetaDataInstance;
157+
Result.Add(
158+
Format(
159+
sLicense,
160+
[
161+
DBMetaData.GetCopyrightInfo.Date,
162+
DBMetaData.GetCopyrightInfo.Holder,
163+
DBMetaData.GetLicenseInfo.NameWithURL
164+
]
165+
)
166+
);
167+
Result.Add('');
156168
Result.Add(Format(sGenerated, [RFC1123DateStamp]));
157169
Result.Add(
158170
Format(

Src/USnippetDoc.pas

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ TSnippetDoc = class(TObject)
5252
/// <summary>Creates and returns an array of compiler compatibility
5353
/// information for given snippet.</summary>
5454
function CompilerInfo(const Snippet: TSnippet): TCompileDocInfoArray;
55-
{Gets compiler compatibility information for a snippet.
56-
@param Snippet [in] Snippet for which compiler information is required.
57-
@return Array of compiler compatibility information.
58-
}
55+
/// <summary>Generates and returns a string containing information about
56+
/// the main database.</summary>
57+
function MainDBInfo: string;
5958
strict protected
6059
/// <summary>Initialise document.</summary>
6160
/// <remarks>Does nothing. Descendant classes should perform any required
@@ -111,7 +110,12 @@ implementation
111110
// Delphi
112111
SysUtils,
113112
// Project
114-
Compilers.UCompilers, DB.UMain, DB.USnippetKind, UStrUtils, UUrl;
113+
Compilers.UCompilers,
114+
DB.UMain,
115+
DB.UMetaData,
116+
DB.USnippetKind,
117+
UStrUtils,
118+
UUrl;
115119

116120

117121
{ TSnippetDoc }
@@ -155,10 +159,6 @@ function TSnippetDoc.Generate(const Snippet: TSnippet): TEncodedData;
155159
sDependListTitle = 'Required snippets:';
156160
sXRefListTitle = 'See also:';
157161
sCompilers = 'Supported compilers:';
158-
// TODO -cWeb: Change following to get license info from DB meta data
159-
sMainDatabaseInfo = 'A snippet from the DelphiDabbler CodeSnip Database '
160-
+ '(%s), licensed under the MIT License '
161-
+ '(http://opensource.org/licenses/MIT).';
162162
begin
163163
Assert(Assigned(Snippet), ClassName + '.Create: Snippet is nil');
164164
// generate document
@@ -181,7 +181,7 @@ function TSnippetDoc.Generate(const Snippet: TSnippet): TEncodedData;
181181
RenderExtra(Snippet.Extra);
182182
if not Snippet.UserDefined then
183183
// database info written only if snippet is from main database
184-
RenderDBInfo(Format(sMainDatabaseInfo, [TURL.CodeSnipRepo]));
184+
RenderDBInfo(MainDBInfo);
185185
Result := FinaliseDoc;
186186
end;
187187

@@ -190,6 +190,23 @@ procedure TSnippetDoc.InitialiseDoc;
190190
// Do nothing
191191
end;
192192

193+
function TSnippetDoc.MainDBInfo: string;
194+
resourcestring
195+
sMainDBInfo = 'A snippet from the DelphiDabbler Code Snippets Database '
196+
+ '(%0:s), licensed under the %1:s.';
197+
var
198+
DBMetaData: IDBMetaData;
199+
begin
200+
DBMetaData := TMainDBMetaDataFactory.MainDBMetaDataInstance;
201+
Result := Format(
202+
sMainDBInfo,
203+
[
204+
TURL.CodeSnipRepo,
205+
DBMetaData.GetLicenseInfo.NameWithURL
206+
]
207+
);
208+
end;
209+
193210
function TSnippetDoc.SnippetsToStrings(const SnippetList: TSnippetList):
194211
IStringList;
195212
var

Src/USnippetSourceGen.pas

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ implementation
8787
// Delphi
8888
SysUtils,
8989
// Project
90-
DB.USnippet, DB.USnippetKind, UAppInfo, UQuery, UUtils;
90+
DB.UMetaData,
91+
DB.USnippet,
92+
DB.USnippetKind,
93+
UAppInfo,
94+
UQuery,
95+
UUtils;
9196

9297

9398
{ TSnippetSourceGen }
@@ -97,31 +102,59 @@ function TSnippetSourceGen.BuildHeaderComments: IStringList;
97102
@return String list containing comments.
98103
}
99104
var
100-
Header: string; // format string for header comment
105+
DBMetaData: IDBMetaData;
101106
resourcestring
102107
// Comment to be included at top of snippet
103-
// header used if snippet includes code from main database
104-
sMainHeader = 'This code snippet was generated by %0:s %1:s on %2:s. It '
105-
+ 'includes code taken from the DelphiDabbler Code Snippets database that '
106-
+ 'is covered by the MIT license (http://opensource.org/licenses/MIT).';
107-
// header used if snippet contains only user defined snippets
108-
sUserHeader = 'This user defined code snippet was generated by '
108+
// when snippets include those from main database
109+
sMainDBGenerator = 'This code snippet was generated by %0:s %1:s on %2:s.';
110+
sMainDBLicense = 'It includes code taken from the DelphiDabbler Code '
111+
+ 'Snippets database that is copyright © %0:s by %1:s and is licensed '
112+
+ 'under the %2:s.';
113+
// when snippets are all from user defined database
114+
sUserGenerator = 'This user defined code snippet was generated by '
109115
+ '%0:s %1:s on %2:s.';
110116
begin
111117
// Header depends on whether snippets contain any from main database or are
112118
// all user defined
119+
Result := TIStringList.Create;
113120
if fContainsMainDBSnippets then
114-
Header := sMainHeader
121+
begin
122+
Result.Add(
123+
Format(
124+
sMainDBGenerator,
125+
[
126+
TAppInfo.FullProgramName,
127+
TAppInfo.ProgramReleaseInfo,
128+
RFC1123DateStamp
129+
]
130+
)
131+
);
132+
Result.Add('');
133+
DBMetaData := TMainDBMetaDataFactory.MainDBMetaDataInstance;
134+
Result.Add(
135+
Format(
136+
sMainDBLicense,
137+
[
138+
DBMetaData.GetCopyrightInfo.Date,
139+
DBMetaData.GetCopyrightInfo.Holder,
140+
DBMetaData.GetLicenseInfo.NameWithURL
141+
]
142+
)
143+
);
144+
end
115145
else
116-
Header := sUserHeader;
117-
// Add the comments
118-
Result := TIStringList.Create;
119-
Result.Add(
120-
Format(
121-
Header,
122-
[TAppInfo.FullProgramName, TAppInfo.ProgramReleaseInfo, RFC1123DateStamp]
123-
)
124-
);
146+
begin
147+
Result.Add(
148+
Format(
149+
sUserGenerator,
150+
[
151+
TAppInfo.FullProgramName,
152+
TAppInfo.ProgramReleaseInfo,
153+
RFC1123DateStamp
154+
]
155+
)
156+
);
157+
end;
125158
end;
126159

127160
class function TSnippetSourceGen.CanGenerate(View: IView): Boolean;

0 commit comments

Comments
 (0)