-
Notifications
You must be signed in to change notification settings - Fork 9
/
Octoid.Config.pas
170 lines (147 loc) · 5.16 KB
/
Octoid.Config.pas
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
unit Octoid.Config;
interface
type
TBannerPosition = (BeforeUnit, BeforeInterface);
TJsonConfig = class(TObject)
public
class procedure CreateFromFile<T: TJsonConfig, constructor>(out AObject: T);
class function GetConfigFileName: string; virtual;
public
procedure Save;
end;
TSDKPathMRU = TArray<string>;
TOctoidConfig = class(TJsonConfig)
private
class var FCurrent: TOctoidConfig;
class destructor DestroyClass;
class function GetCurrent: TOctoidConfig; static;
private
FAdditionalOptions: string;
FBannerPosition: TBannerPosition;
FDeprecationCommentFirst: Boolean;
FErrorLimit: Integer;
FIncludeConstTypeComments: Boolean;
FIncludeDeprecationComments: Boolean;
FIncludeTodoComments: Boolean;
FOutputPath: string;
FSDKPathMRU: TSDKPathMRU;
FSDKPath: string;
FSelectedFramework: string;
FStyleName: string;
FTypeMapFileName: string;
FTypeUnitMapFileName: string;
FUseBannerAsIs: Boolean;
procedure CheckSDKPathMRU;
public
class function GetBannerFileName: string;
class function GetConfigFileName: string; override;
class function GetConfigPath: string;
class property Current: TOctoidConfig read GetCurrent;
public
property AdditionalOptions: string read FAdditionalOptions write FAdditionalOptions;
property BannerPosition: TBannerPosition read FBannerPosition write FBannerPosition;
property DeprecationCommentFirst: Boolean read FDeprecationCommentFirst write FDeprecationCommentFirst;
property ErrorLimit: Integer read FErrorLimit write FErrorLimit;
property IncludeConstTypeComments: Boolean read FIncludeConstTypeComments write FIncludeConstTypeComments;
property IncludeDeprecationComments: Boolean read FIncludeDeprecationComments write FIncludeDeprecationComments;
property IncludeTodoComments: Boolean read FIncludeTodoComments write FIncludeTodoComments;
property SDKPathMRU: TSDKPathMRU read FSDKPathMRU write FSDKPathMRU;
property OutputPath: string read FOutputPath write FOutputPath;
property SDKPath: string read FSDKPath write FSDKPath;
property SelectedFramework: string read FSelectedFramework write FSelectedFramework;
property StyleName: string read FStyleName write FStyleName;
property TypeMapFileName: string read FTypeMapFileName write FTypeMapFileName;
property TypeUnitMapFileName: string read FTypeUnitMapFileName write FTypeUnitMapFileName;
property UseBannerAsIs: Boolean read FUseBannerAsIs write FUseBannerAsIs;
end;
implementation
uses
System.SysUtils, System.IOUtils, REST.Json, System.Math,
Octoid.Consts;
type
TJsonHelper = class helper for TJson
public
class function FileToObject<T: class, constructor>(out AObject: T; const AFileName: string;
AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): Boolean; static;
class procedure SaveToFile(const AObject: TObject; const AFileName: string); static;
end;
{ TJsonHelper }
class function TJsonHelper.FileToObject<T>(out AObject: T; const AFileName: string;
AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): Boolean;
var
LJSON: string;
begin
Result := False;
LJSON := '';
if System.IOUtils.TFile.Exists(AFileName) then
LJSON := System.IOUtils.TFile.ReadAllText(AFileName);
if not LJSON.IsEmpty then
begin
AObject := JsonToObject<T>(LJSON, AOptions);
Result := True;
end;
end;
class procedure TJsonHelper.SaveToFile(const AObject: TObject; const AFileName: string);
var
LJSON: string;
begin
if AObject = nil then
LJSON := '{}'
else
LJSON := ObjectToJsonString(AObject);
System.IOUtils.TFile.WriteAllText(AFileName, LJSON);
end;
{ TJsonConfig }
class procedure TJsonConfig.CreateFromFile<T>(out AObject: T);
begin
if not TJson.FileToObject(AObject, T.GetConfigFileName) then
AObject := T.Create;
end;
class function TJsonConfig.GetConfigFileName: string;
begin
Result := '';
end;
procedure TJsonConfig.Save;
var
LFileName: string;
begin
LFileName := GetConfigFileName;
ForceDirectories(TPath.GetDirectoryName(LFileName));
TJson.SaveToFile(Self, GetConfigFilename);
end;
{ TOctoidConfig }
class destructor TOctoidConfig.DestroyClass;
begin
FCurrent.Free;
end;
class function TOctoidConfig.GetBannerFileName: string;
begin
Result := TPath.Combine(GetConfigPath, 'banner.txt');
end;
class function TOctoidConfig.GetConfigFileName: string;
begin
Result := TPath.Combine(GetConfigPath, 'config.json');
end;
class function TOctoidConfig.GetConfigPath: string;
begin
Result := TPath.Combine(TPath.GetDocumentsPath, 'Octoid')
end;
class function TOctoidConfig.GetCurrent: TOctoidConfig;
begin
if FCurrent = nil then
CreateFromFile<TOctoidConfig>(FCurrent);
FCurrent.ErrorLimit := Max(cErrorLimitDefault, FCurrent.ErrorLimit);
FCurrent.CheckSDKPathMRU;
Result := FCurrent;
end;
procedure TOctoidConfig.CheckSDKPathMRU;
var
I: Integer;
begin
for I := Length(FSDKPathMRU) - 1 downto 0 do
begin
if not TDirectory.Exists(FSDKPathMRU[I]) then
Delete(FSDKPathMRU, I, 1);
end;
end;
end.