|
| 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