summaryrefslogtreecommitdiff
path: root/utils/fpdoc/mgrfpdocproj.pp
blob: f977888cdbde1076c706ff478cdef16a0f624800 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
unit mgrfpdocproj;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpdocproj, fpdocxmlopts;

Type
  { TFPDocProjectManager }

  TFPDocProjectManager = Class(TComponent)
  Private
    FProject : TFPDocProject;
    FPackage : TFPDocPackage;
    FExpandMacros: Boolean;
    FMacros: TStrings;
    procedure SetMacros(AValue: TStrings);
  protected
    Procedure CheckPackage;
    procedure GetItemsFromDirectory(AList: TStrings; ADirectory, AMask: String; ARecurse: Boolean);
    procedure DoMacro(Sender: TObject; const TagString: String; TagParams: TStringList; out ReplaceText: String); virtual;
    function ExpandMacrosInFile(AFileName: String): TStream; virtual;
  Public
    Constructor Create(AOwner : TComponent); override;
    Destructor Destroy; override;
    procedure AddDescrFilesFromDirectory(Const ADirectory, AMask : String; ARecurse: Boolean);
    procedure AddInputFilesFromDirectory(Const ADirectory, AMask, AOptions: String; ARecurse: Boolean);
    procedure AddInputFile(Const AFile : String; AOptions : String = '');
    procedure AddImportFile(Const AFile,APrefix : String);
    procedure AddDescrFile(Const AFile : String);
    procedure RemoveInputFile(Const AFile : String);
    procedure RemoveDescrFile(Const AFile : String);
    procedure WriteOptionFile(const AFileName: String);
    procedure ReadOptionFile(const AFileName: String);
    Procedure Selectpackage(Const APackageName : String);
    Procedure AddPackage (Const APackageName : String);
    procedure SetOption(Const AOption : String; Enable : Boolean = True);
    Property Project : TFPDocProject Read FProject;
    Property SelectedPackage : TFPDocPackage Read FPackage;
    Property Macros : TStrings Read FMacros Write SetMacros;
    Property ExpandMacros : Boolean Read FExpandMacros Write FExpandMacros;
  end;
  EMgrFPDoc = Class(Exception);

implementation

uses dom,xmlread,fptemplate;

procedure TFPDocProjectManager.SetMacros(AValue: TStrings);
begin
  if FMacros=AValue then Exit;
  FMacros.Assign(AValue);
end;

procedure TFPDocProjectManager.DoMacro(Sender: TObject; const TagString: String;
  TagParams: TStringList; out ReplaceText: String);
begin
  ReplaceText:=FMacros.Values[TagString];
end;


Procedure TFPDocProjectManager.GetItemsFromDirectory(AList : TStrings; ADirectory,AMask : String; ARecurse : Boolean);

Var
  D : String;
  Info : TSearchRec;

begin
  D:=ADirectory;
  if (D='.') then
    D:='';
  if (D<>'') then
    D:=includeTrailingPathDelimiter(D);
  If FindFirst(D+AMask,0,info)=0 then
    try
      Repeat
      if ((Info.Attr and faDirectory)=0) then
        AList.add(D+Info.Name);
      Until (FindNext(Info)<>0);
    finally
      FindClose(Info);
    end;
  If ARecurse and (FindFirst(ADirectory+AMask,0,info)=0) then
    try
      Repeat
      if ((Info.Attr and faDirectory)<>0) then
        GetItemsFromDirectory(Alist,IncludeTrailingPathDelimiter(D+Info.Name),AMask,ARecurse);
      Until (FindNext(Info)<>0);
    finally
      FindClose(Info);
    end;
end;

constructor TFPDocProjectManager.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FProject:=TFPDocProject.Create(Self);
  FMacros:=TStringList.Create;
end;

destructor TFPDocProjectManager.Destroy;
begin
  FreeAndNil(FMacros);
  FreeAndNil(FProject);
  inherited Destroy;
end;

Function TFPDocProjectManager.ExpandMacrosInFile(AFileName : String) : TStream;

Var
  F : TFileStream;
  T : TTemplateParser;

begin
  F:=TFileStream.Create(AFileName,fmOpenRead or fmShareDenyWrite);
  try
    Result:=TMemoryStream.Create;
    try
      T:=TTemplateParser.Create;
      try
        T.StartDelimiter:='$(';
        T.EndDelimiter:=')';
        T.AllowTagParams:=true;
        T.OnReplaceTag:=@DoMacro;
        T.ParseStream(F,Result);
      finally
        T.Free;
      end;
      Result.Position:=0;
    except
      FreeAndNil(Result);
      Raise;
    end;
  finally
    F.Free;
  end;
end;

Procedure TFPDocProjectManager.AddDescrFilesFromDirectory(const ADirectory,AMask : String; ARecurse : Boolean);

Var
  L : TStringList;
  M : String;

begin
  CheckPackage;
  M:=AMask;
  if (M='') then
    M:='*.xml';
  L:=TStringList.Create;
  try
    GetItemsFromDirectory(L,ADirectory,M,ARecurse);
    FPackage.Descriptions.AddStrings(L);
  finally
    L.Free;
  end;
end;

Procedure TFPDocProjectManager.AddInputFilesFromDirectory(Const ADirectory,AMask,AOptions : String; ARecurse : Boolean);

Var
  L : TStringList;
  I : integer;
  M : String;

begin
  CheckPackage;
  M:=AMask;
  if (M='') then
    M:='*.pp';
  L:=TStringList.Create;
  try
    GetItemsFromDirectory(L,ADirectory,M,ARecurse);
    For I:=0 to L.Count-1 do
      AddInputFile(L[i],AOPtions);
  finally
    L.Free;
  end;
end;

procedure TFPDocProjectManager.AddInputFile(const AFile: String; AOptions : String = '');

Var
  S : String;

begin
  CheckPackage;
  S:=AFile;
  If (AOptions<>'') then
    S:=AOptions+' '+S;
  FPackage.Inputs.Add(S);
end;

procedure TFPDocProjectManager.AddImportFile(const AFile, APrefix: String);

begin
  CheckPackage;
  FPackage.Imports.Add(AFile+','+APrefix);
end;

procedure TFPDocProjectManager.AddDescrFile(const AFile: String);

begin
  CheckPackage;
  if FPackage.Descriptions.IndexOf(AFile)<>-1 then
    Raise EMgrFPDoc.Createfmt('Duplicate description file : "%s"',[AFile]);
  FPackage.Descriptions.Add(AFile);
end;

procedure TFPDocProjectManager.RemoveInputFile(const AFile: String);

Var
  I : Integer;

begin
  I:=FPackage.Inputs.IndexOf(AFile);
  If (I<>-1) then
    FPackage.Inputs.Delete(I);
end;

procedure TFPDocProjectManager.RemoveDescrFile(const AFile: String);

Var
  I : Integer;

begin
  I:=FPackage.Descriptions.IndexOf(AFile);
  If (I<>-1) then
    FPackage.Descriptions.Delete(I);
end;

procedure TFPDocProjectManager.ReadOptionFile(Const AFileName : String);

Var
  XML : TXMLDocument;
  S : TStream;

begin
  With TXMLFPDocOptions.Create(Self) do
    try
      if not (ExpandMacros) then
        LoadOptionsFromFile(FProject,AFileName)
      else
        begin
        S:=ExpandMacrosInFile(AFileName);
        try
          ReadXMLFile(XML,S,AFileName);
          try
            LoadFromXml(FProject,XML)
          finally
            XML.Free;
          end;
        finally
          S.Free;
        end;
        end;
    finally
      Free;
    end;
end;

procedure TFPDocProjectManager.Selectpackage(const APackageName: String);
begin
  FPackage:=FProject.Packages.FindPackage(APackageName);
  If (FPackage=Nil) then
    Raise EMgrFPDoc.CreateFmt('Unknown package : "%s"',[APackageName]);
end;

procedure TFPDocProjectManager.AddPackage(const APackageName: String);
begin
  if FProject.Packages.FindPackage(APackageName)<>Nil then
    Raise EMgrFPDoc.CreateFmt('Duplicate package : "%s"',[APackageName]);
  FPackage:=FProject.Packages.Add as TFPDocPackage;
  FPackage.Name:=APackageName;
end;

procedure TFPDocProjectManager.SetOption(const AOption: String;
  Enable: Boolean = true);

Var
  O,V : String;
  P : Integer;
  EO : TEngineOptions;

begin
  V:=LowerCase(AOption);
  P:=Pos('=',V);
  If (P=0) then
    P:=Length(V)+1;
  O:=Copy(V,1,P-1);
  Delete(V,1,P);
  EO:=FProject.Options;
  Case IndexOfString(o,OptionNames) of
    0 : EO.HideProtected:=Enable;
    1 : EO.WarnNoNode:=Enable;
    2 : EO.ShowPrivate:=Enable;
    3 : EO.StopOnParseError:=Enable;
    4 : EO.ostarget:=v;
    5 : EO.cputarget:=v;
    6 : EO.MoDir:=V;
    7 : EO.InterfaceOnly:=Not Enable;
    8 : EO.Backend:=V;
    9 : EO.Language:=v;
    10 : EO.DefaultPackageName:=V;
    11 : EO.DontTrim:=Enable;
  else
    EO.BackendOptions.add('--'+O);
    EO.BackendOptions.add(v);
  end;
end;

procedure TFPDocProjectManager.WriteOptionFile(Const AFileName : String);

begin
  With TXMLFPDocOptions.Create(Self) do
    try
      SaveOptionsToFile(FProject,AFileName);
    finally
      Free;
    end;
end;

procedure TFPDocProjectManager.CheckPackage;

begin
  if (FPackage=Nil) then
    Raise EMgrFPDoc.Create('Error: No package selected');
end;



end.