Skip to content

Commit d4f3b3e

Browse files
committed
New unit with class to read/validate SWAG VERSION files
1 parent fd55610 commit d4f3b3e

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

Src/CodeSnip.dpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ uses
180180
SWAG.UImporter in 'SWAG.UImporter.pas',
181181
SWAG.UReader in 'SWAG.UReader.pas',
182182
SWAG.UPacketCache in 'SWAG.UPacketCache.pas',
183+
SWAG.UVersion in 'SWAG.UVersion.pas',
183184
SWAG.UXMLProcessor in 'SWAG.UXMLProcessor.pas',
184185
UActionFactory in 'UActionFactory.pas',
185186
UAnchors in 'UAnchors.pas',

Src/CodeSnip.dproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
<DCCReference Include="SWAG.UImporter.pas"/>
378378
<DCCReference Include="SWAG.UReader.pas"/>
379379
<DCCReference Include="SWAG.UPacketCache.pas"/>
380+
<DCCReference Include="SWAG.UVersion.pas"/>
380381
<DCCReference Include="SWAG.UXMLProcessor.pas"/>
381382
<DCCReference Include="UActionFactory.pas"/>
382383
<DCCReference Include="UAnchors.pas"/>
@@ -565,6 +566,7 @@
565566
<DCCReference Include="UXMLDocConsts.pas"/>
566567
<DCCReference Include="UXMLDocHelper.pas"/>
567568
<DCCReference Include="UXMLDocumentEx.pas"/>
569+
<DCCReference Include="SWAG.UVersion.pas"/>
568570
<None Include="CodeSnip.todo"/>
569571
<BuildConfiguration Include="Base">
570572
<Key>Base</Key>

Src/SWAG.UVersion.pas

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
unit SWAG.UVersion;
2+
3+
4+
interface
5+
6+
7+
uses
8+
// VCL
9+
SysUtils,
10+
// Project
11+
UBaseObjects,
12+
UExceptions,
13+
UVersionInfo;
14+
15+
16+
type
17+
TSWAGVersion = class(TNoPublicConstructObject)
18+
strict private
19+
const
20+
LowestSupportedVersion: TVersionNumber = (V1: 1; V2: 0; V3: 0; V4: 0);
21+
LowestUnSupportedVersion: TVersionNumber = (V1: 1; V2: 1; V3: 0; V4: 0);
22+
SWAGVersionFileName = 'VERSION';
23+
var
24+
fSWAGFilePath: TFileName;
25+
function ReadVersionStr: string;
26+
function ReadAndValidateVersionFile: TVersionNumber;
27+
strict protected
28+
constructor InternalCreate(const SWAGDir: TFileName);
29+
public
30+
class procedure ValidateVersionFile(const SWAGDir: TFileName);
31+
class function GetVersion(const SWAGDir: TFileName): TVersionNumber;
32+
end;
33+
34+
ESWAGVersion = class(ECodeSnip);
35+
ECorruptSWAGVersion = class(ESWAGVersion);
36+
EUnsupportedSWAGVersion = class(ESWAGVersion);
37+
38+
39+
implementation
40+
41+
42+
uses
43+
// VCL
44+
IOUtils,
45+
// Project
46+
UIOUtils;
47+
48+
49+
{ TSWAGVersion }
50+
51+
class function TSWAGVersion.GetVersion(const SWAGDir: TFileName):
52+
TVersionNumber;
53+
begin
54+
with InternalCreate(SWAGDir) do
55+
try
56+
Result := ReadAndValidateVersionFile;
57+
finally
58+
Free;
59+
end;
60+
end;
61+
62+
constructor TSWAGVersion.InternalCreate(const SWAGDir: TFileName);
63+
begin
64+
inherited InternalCreate;
65+
fSWAGFilePath := IncludeTrailingPathDelimiter(SWAGDir) + SWAGVersionFileName;
66+
end;
67+
68+
function TSWAGVersion.ReadAndValidateVersionFile: TVersionNumber;
69+
var
70+
VerStr: string;
71+
resourcestring
72+
sMissingOrEmptyFile = 'Missing or empty %s file';
73+
sCorruptFile = 'Corrupt %s file';
74+
sOutOfRange = 'SWAG version %0:s is not supported. '
75+
+ 'The version must be at least v%1:s and less than v%2:s';
76+
begin
77+
VerStr := ReadVersionStr;
78+
if VerStr = '' then
79+
raise ECorruptSWAGVersion.CreateFmt(
80+
sMissingOrEmptyFile, [SWAGVersionFileName]
81+
);
82+
if not TVersionNumber.TryStrToVersionNumber(VerStr, Result) then
83+
raise ECorruptSWAGVersion.CreateFmt(sCorruptFile, [SWAGVersionFileName]);
84+
if (Result < LowestSupportedVersion)
85+
or (Result >= LowestUnSupportedVersion) then
86+
raise EUnsupportedSWAGVersion.CreateFmt(
87+
sOutOfRange,
88+
[
89+
string(Result),
90+
string(LowestSupportedVersion),
91+
string(LowestUnSupportedVersion)
92+
]
93+
);
94+
end;
95+
96+
function TSWAGVersion.ReadVersionStr: string;
97+
begin
98+
if not TFile.Exists(fSWAGFilePath) then
99+
Exit('');
100+
Result := TFileIO.ReadAllText(fSWAGFilePath, TEncoding.UTF8, False);
101+
end;
102+
103+
class procedure TSWAGVersion.ValidateVersionFile(const SWAGDir: TFileName);
104+
begin
105+
with InternalCreate(SWAGDir) do
106+
try
107+
ReadAndValidateVersionFile;
108+
finally
109+
Free;
110+
end;
111+
end;
112+
113+
end.

0 commit comments

Comments
 (0)