Quick.Config.YAML.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. { ***************************************************************************
  2. Copyright (c) 2015-2019 Kike Pérez
  3. Unit : Quick.Config.YAML
  4. Description : Save config to YAML file
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 12/04/2019
  8. Modified : 27/04/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Config.YAML;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. {$IFDEF DELPHIXE_UP}
  28. IOUtils,
  29. {$ELSE}
  30. Quick.Files,
  31. {$ENDIF}
  32. Rtti,
  33. Quick.YAML.Serializer,
  34. Quick.FileMonitor,
  35. Quick.Config.Base;
  36. type
  37. TFileModifiedEvent = procedure of object;
  38. TLoadConfigEvent = procedure of object;
  39. {$IFNDEF FPC}
  40. TNotSerializableProperty = Quick.YAML.Serializer.TNotSerializableProperty;
  41. TCommentProperty = Quick.YAML.Serializer.TCommentProperty;
  42. TCustomNameProperty = Quick.YAML.Serializer.TCustomNameProperty;
  43. {$ENDIF}
  44. TAppConfigYMALProvider = class(TAppConfigProvider)
  45. private
  46. fFilename : string;
  47. fFileMonitor : TFileMonitor;
  48. fOnFileModified : TFileModifiedEvent;
  49. fLoaded : Boolean;
  50. fReloadIfFileChanged : Boolean;
  51. fOnConfigLoaded : TLoadConfigEvent;
  52. fOnConfigReloaded : TLoadConfigEvent;
  53. fNotifyReload : TLoadConfigEvent;
  54. procedure CreateFileMonitor;
  55. procedure FileModifiedNotify(MonitorNotify : TMonitorNotify);
  56. procedure SetFileName(const Value: string);
  57. procedure SetReloadIfFileChanged(const Value: Boolean);
  58. procedure SetReloadNotify(aNotifyReload : TLoadConfigEvent);
  59. procedure DoNofifyReload;
  60. protected
  61. procedure Load(cConfig : TAppConfig); override;
  62. procedure Save(cConfig : TAppConfig); override;
  63. public
  64. constructor Create(const aFilename : string; aReloadIfFileChanged : Boolean = False); overload;
  65. destructor Destroy; override;
  66. property Filename : string read fFilename write SetFileName;
  67. property ReloadIfFileChanged : Boolean read fReloadIfFileChanged write SetReloadIfFileChanged;
  68. property IsLoaded : Boolean read fLoaded;
  69. property OnFileModified : TFileModifiedEvent read fOnFileModified write fOnFileModified;
  70. property OnConfigLoaded : TLoadConfigEvent read fOnConfigLoaded write fOnConfigLoaded;
  71. property OnConfigReloaded : TLoadConfigEvent read fOnConfigReloaded write fOnConfigReloaded;
  72. end;
  73. TAppConfigYAML = class(TAppConfig)
  74. private
  75. function GetProvider : TAppConfigYMALProvider;
  76. procedure ReloadNotify;
  77. public
  78. constructor Create(const aFilename : string; aReloadIfFileChanged : Boolean = False);
  79. destructor Destroy; override;
  80. property Provider : TAppConfigYMALProvider read GetProvider;
  81. function ToYAML : string;
  82. procedure FromYAML(const yaml : string);
  83. end;
  84. {Usage: create a descend class from TAppConfigYAML and add published properties to be loaded/saved
  85. TMyConfig = class(TAppConfigYAML)
  86. private
  87. fName : string;
  88. fSurname : string;
  89. fStatus : Integer;
  90. published
  91. property Name : string read fName write fName;
  92. property SurName : string read fSurname write fSurname;
  93. property Status : Integer read fStatus write fStatus;
  94. end;
  95. MyConfig := TMyConfig.Create;
  96. MyConfig.Provider.FileName := '.\MyAppName.yml';
  97. MyConfig.Name := 'John';
  98. MyConfig.Save;
  99. }
  100. implementation
  101. constructor TAppConfigYMALProvider.Create(const aFilename : string; aReloadIfFileChanged : Boolean = False);
  102. begin
  103. inherited Create;
  104. if aFilename = '' then fFilename := TPath.ChangeExtension(ParamStr(0),'yml')
  105. else fFilename := aFilename;
  106. fLoaded := False;
  107. fReloadIfFileChanged := aReloadIfFileChanged;
  108. if aReloadIfFileChanged then CreateFileMonitor;
  109. end;
  110. procedure TAppConfigYMALProvider.CreateFileMonitor;
  111. begin
  112. fFileMonitor := TQuickFileMonitor.Create;
  113. fFileMonitor.FileName := fFilename;
  114. fFileMonitor.Interval := 2000;
  115. fFileMonitor.Notifies := [TMonitorNotify.mnFileModified];
  116. fFileMonitor.OnFileChange := FileModifiedNotify;
  117. fFileMonitor.Enabled := True;
  118. end;
  119. destructor TAppConfigYMALProvider.Destroy;
  120. begin
  121. if Assigned(fFileMonitor) then fFileMonitor.Free;
  122. inherited;
  123. end;
  124. procedure TAppConfigYMALProvider.DoNofifyReload;
  125. begin
  126. if Assigned(fNotifyReload) then fNotifyReload
  127. else raise EAppConfig.Create('Not config assigned to reload!');
  128. end;
  129. procedure TAppConfigYMALProvider.FileModifiedNotify(MonitorNotify : TMonitorNotify);
  130. begin
  131. if MonitorNotify = TMonitorNotify.mnFileModified then
  132. begin
  133. if Assigned(fOnFileModified) then fOnFileModified;
  134. if fReloadIfFileChanged then DoNofifyReload;
  135. end;
  136. end;
  137. procedure TAppConfigYMALProvider.Load(cConfig : TAppConfig);
  138. var
  139. yaml : TStrings;
  140. serializer : TYamlSerializer;
  141. begin
  142. if (not FileExists(fFilename)) and (CreateIfNotExists) then
  143. begin
  144. TAppConfig(cConfig).DefaultValues;
  145. Self.Save(cConfig);
  146. end;
  147. try
  148. yaml := TStringList.Create;
  149. try
  150. yaml.LoadFromFile(fFilename);
  151. if yaml.Count > 0 then
  152. begin
  153. serializer := TYamlSerializer.Create(slPublishedProperty,UseEnumNames);
  154. try
  155. //Streamer.Options := Streamer.Options + [jsoDateTimeAsString ,jsoUseFormatString];
  156. //Streamer.DateTimeFormat := 'yyyy-mm-dd"T"hh:mm:ss.zz';
  157. serializer.YamlToObject(cConfig,yaml.Text);
  158. finally
  159. serializer.Free;
  160. end;
  161. end else
  162. TAppConfig(cConfig).DefaultValues;
  163. finally
  164. yaml.Free;
  165. end;
  166. if not fLoaded then
  167. begin
  168. fLoaded := True;
  169. if Assigned(fOnConfigLoaded) then fOnConfigLoaded;
  170. end
  171. else if Assigned(fOnConfigReloaded) then fOnConfigReloaded;
  172. except
  173. on e : Exception do raise EAppConfig.Create(e.Message);
  174. end;
  175. end;
  176. procedure TAppConfigYMALProvider.Save(cConfig : TAppConfig);
  177. var
  178. yaml : TStrings;
  179. Serializer : TYamlSerializer;
  180. begin
  181. if not Assigned(cConfig) then cConfig := TAppConfigYAML.Create(fFilename,fReloadIfFileChanged);
  182. try
  183. yaml := TStringList.Create;
  184. try
  185. serializer := TYamlSerializer.Create(TSerializeLevel.slPublishedProperty,UseEnumNames);
  186. try
  187. //Streamer.Options := Streamer.Options + [jsoDateTimeAsString ,jsoUseFormatString];
  188. //Streamer.DateTimeFormat := 'yyyy-mm-dd"T"hh:mm:ss.zz';
  189. yaml.Text := serializer.ObjectToYaml(cConfig);
  190. finally
  191. serializer.Free;
  192. end;
  193. yaml.SaveToFile(fFilename);
  194. finally
  195. yaml.Free;
  196. end;
  197. except
  198. on e : Exception do raise EAppConfig.Create(e.Message);
  199. end;
  200. end;
  201. procedure TAppConfigYMALProvider.SetFileName(const Value: string);
  202. begin
  203. fFilename := Value;
  204. if Assigned(fFileMonitor) then fFileMonitor.Free;
  205. if fReloadIfFileChanged then CreateFileMonitor;
  206. end;
  207. procedure TAppConfigYMALProvider.SetReloadIfFileChanged(const Value: Boolean);
  208. begin
  209. if Value = fReloadIfFileChanged then Exit;
  210. fReloadIfFileChanged := Value;
  211. if Assigned(fFileMonitor) then fFileMonitor.Free;
  212. if fReloadIfFileChanged then CreateFileMonitor;
  213. end;
  214. procedure TAppConfigYMALProvider.SetReloadNotify(aNotifyReload: TLoadConfigEvent);
  215. begin
  216. fNotifyReload := aNotifyReload;
  217. end;
  218. { TAppConfigYAML }
  219. constructor TAppConfigYAML.Create(const aFilename : string; aReloadIfFileChanged : Boolean = False);
  220. begin
  221. inherited Create(TAppConfigYMALProvider.Create(aFileName,aReloadIfFileChanged));
  222. TAppConfigYMALProvider(fProvider).SetReloadNotify(ReloadNotify);
  223. end;
  224. destructor TAppConfigYAML.Destroy;
  225. begin
  226. inherited;
  227. end;
  228. function TAppConfigYAML.GetProvider: TAppConfigYMALProvider;
  229. begin
  230. if not Assigned(fProvider) then raise EAppConfig.Create('No provider assigned!');
  231. Result := TAppConfigYMALProvider(fProvider);
  232. end;
  233. procedure TAppConfigYAML.ReloadNotify;
  234. begin
  235. Self.Load;
  236. end;
  237. function TAppConfigYAML.ToYAML: string;
  238. var
  239. serializer : TYamlSerializer;
  240. begin
  241. serializer := TYamlSerializer.Create(slPublishedProperty,fProvider.UseEnumNames);
  242. try
  243. Result := serializer.ObjectToYaml(Self);
  244. finally
  245. serializer.Free;
  246. end;
  247. end;
  248. procedure TAppConfigYAML.FromYAML(const yaml: string);
  249. var
  250. serializer : TYamlSerializer;
  251. begin
  252. serializer := TYamlSerializer.Create(slPublishedProperty,fProvider.UseEnumNames);
  253. try
  254. serializer.YamlToObject(Self,yaml);
  255. finally
  256. serializer.Free;
  257. end;
  258. end;
  259. end.