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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
|
unit ProjectTemplates;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, IniFiles,
// LazUtils
FileUtil, LazFileUtils, LazUTF8Classes;
type
{ TProjectTemplates }
TProjectTemplate = Class(TCollectionItem)
private
FAuthor: String;
FDescription: String;
FDirectory: String;
FExclude: String;
FName: String;
FProjectFile: String;
FRecurse: Boolean;
FFiles : TStrings;
FVariables: TStrings;
function DefaultFileSubstitutes(AFileName: String): string;
function GetFileCount: Integer;
function GetFileName(FileIndex : Integer): String;
procedure SetVariables(const AValue: TStrings);
procedure GetFileList(Const Dir : String);
Protected
Function SpecialFile(Const AName : String) : Boolean;
procedure InitFromDir(Const DirName : String);
procedure CopyAndSubstituteDir(Const SrcDir,DestDir : String; Values : TStrings);
procedure CopyAndSubstituteFile(Const SrcFN,DestFN : String; Values : Tstrings);
Public
constructor Create(ACollection: TCollection); override;
Destructor Destroy; override;
Procedure CreateProject(Const ProjectDir : String; Values : TStrings);
Procedure CreateFile(FileIndex : Integer; Source,Values : TStrings);
Procedure CreateFile(Const FileName: String; Source,Values : TStrings);
Procedure CreateProjectDirs(Const BaseDir : String; Values : TStrings);
Function TargetFileName(FN : String; Values : TStrings) : String;
Function TargetFileName(I : Integer; Values : TStrings) : String;
Property FileCount : Integer read GetFileCount;
Property FileNames[FileIndex : Integer] : String Read GetFileName;
published
Property Name : String Read FName;
Property Directory : String Read FDirectory;
Property Description : String Read FDescription Write FDescription;
Property Variables : TStrings Read FVariables Write SetVariables;
Property ProjectFile : String Read FProjectFile;
Property Author : String Read FAuthor;
Property Recurse : Boolean Read FRecurse;
Property Exclude : String Read FExclude;
end;
{ TProjectTemplates }
TProjectTemplates = class(TCollection)
private
FTemplateDir: String;
function GetTemplate(Index : Integer): TProjectTemplate;
function GetTemplateName(Index : Integer): String;
procedure SetTemplate(Index : Integer; const AValue: TProjectTemplate);
public
Constructor Create(Const ATemplateDir : String);
Procedure Initialize(Const ATemplateDir : String);
Procedure CreateProject(Const ProjectName, ProjectDir : String; Variables : TStrings);
Function IndexOfProject(Const ProjectName : String) : Integer;
Function ProjectTemplateByName(Const ProjectName : String) : TProjectTemplate;
Property TemplateDir : String Read FTemplateDir;
Property Names [Index : Integer] : String Read GetTemplateName;
Property Templates[Index : Integer] : TProjectTemplate Read GetTemplate Write SetTemplate;default;
end;
ETemplateError=Class(Exception);
Const
// Section & Key names for ini file.
SProject = 'Project';
SVariables = 'Variables';
KeyName = 'Name';
KeyAuthor = 'Author';
KeyDescription = 'Description';
KeyRecurse = 'Recurse';
KeyExclude = 'Exclude';
KeyProjectFile = 'ProjectFile';
varprefixstr = '__'; // subtitution pattern is "__varname__"
varpostfixstr = '__';
resourcestring
SbtnOK = '&OK';
SbtnCancel = 'Cancel';
Function SubstituteString(Const S : String; Variables : TStrings): String;
Function SimpleFileCopy(Const Source,Dest : String) : Boolean;
implementation
resourcestring
SErrNoSuchTemplate = '"%s": No such template.';
SErrCouldNotCreateDir = 'Could not create directory "%s"';
SErrFailedToCopyFile = 'Failed to copy file "%s" to "%s"';
{ Auxiliary function }
Function SubstituteString(Const S : String; Variables : TStrings): String;
Var
T : String;
P : Integer;
begin
T:=S;
Result:='';
Repeat
P:=Pos(varprefixstr,T);
If (P=0) then
begin
Result:=Result+T;
T:='';
end
else
begin
Result:=Result+Copy(T,1,P-1);
Delete(T,1,P+1);
P:=Pos(varpostfixstr,T);
If (P=0) then
begin
Result:=Result+varprefixstr+T;
T:='';
end
else
begin
Result:=Result+Variables.Values[Copy(T,1,P-1)];
Delete(T,1,P+1);
end;
end;
until (T='');
end;
Function SimpleFileCopy(Const Source,Dest : String) : Boolean;
Var
F1,F2 : TFileStream;
begin
Result:=False;
try
F1:=TFileStream.Create(Source,fmOpenRead);
try
F2:=TFileStream.Create(Dest,fmCreate);
try
F2.CopyFrom(F1,0);
finally
F2.Free;
end;
finally
F1.Free;
end;
Result:=True;
except
//
end;
end;
{ TProjectTemplates }
function TProjectTemplates.GetTemplateName(Index : Integer): String;
begin
Result:=GetTemplate(Index).Name;
end;
function TProjectTemplates.GetTemplate(Index : Integer): TProjectTemplate;
begin
Result:=Items[Index] as TProjectTemplate
end;
procedure TProjectTemplates.SetTemplate(Index : Integer;
const AValue: TProjectTemplate);
begin
Items[Index]:=AValue;
end;
constructor TProjectTemplates.Create(const ATemplateDir: String);
begin
Inherited Create(TProjectTemplate);
Initialize(ATemplateDir);
end;
function TProjectTemplates.IndexOfProject(const ProjectName: String): Integer;
begin
Result:=Count-1;
While (Result>=0) and (CompareText(GetTemplate(Result).Name,ProjectName)<>0) do
Dec(Result)
end;
function TProjectTemplates.ProjectTemplateByName(const ProjectName: String): TProjectTemplate;
Var
Index : Integer;
begin
Index:=IndexOfProject(ProjectName);
If (Index=-1) then
Raise ETemplateError.CreateFmt(SErrNoSuchTemplate,[ProjectName]);
Result:=GetTemplate(Index);
end;
procedure TProjectTemplates.Initialize(const ATemplateDir: String);
Var
Info : TSearchRec;
D : String;
begin
Clear;
FTemplateDir:=IncludeTrailingPathDelimiter(ATemplateDir);
D:=FTemplateDir;
try
If FindFirstUTF8(D+GetAllFilesMask,faDirectory,Info)=0 then
Repeat
If ((Info.Attr and faDirectory)<>0)
and not ((Info.Name='.') or (Info.Name='..') or (Info.Name='')) then
With Add as TProjectTemplate do
begin
InitFromDir(D+Info.Name);
if (name='') or (directory='') // skip invalid template folders
then delete(count-1); // this prevents IDE hanging
end;
Until FindNextUTF8(Info)<>0;
finally
FindCloseUTF8(Info);
end;
end;
procedure TProjectTemplates.CreateProject(const ProjectName, ProjectDir: String; Variables : Tstrings);
Var
T : TProjectTemplate;
begin
T:=ProjectTemplateByName(ProjectName);
T.CreateProject(ProjectDir,Variables);
end;
{ TProjectTemplate }
constructor TProjectTemplate.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FVariables:=TStringList.Create;
FFiles:=TStringList.Create;
FProjectFile:='project' // Do not localize
end;
destructor TProjectTemplate.Destroy;
begin
FreeAndNil(FVariables);
FreeAndNil(FFiles);
inherited Destroy;
end;
procedure TProjectTemplate.SetVariables(const AValue: TStrings);
begin
FVariables.Assign(AValue);
end;
function TProjectTemplate.GetFileName(FileIndex : Integer): String;
begin
Result:=FFiles[FileIndex];
end;
function TProjectTemplate.GetFileCount: Integer;
begin
Result:=FFiles.Count;
end;
procedure TProjectTemplate.InitFromDir(const DirName: String);
Var
L : TStringListUTF8;
FN : String;
begin
FDirectory:=IncludeTrailingPathDelimiter(DirName);
L:=TStringListUTF8.Create;
Try
FN:=FDirectory+'project.ini';
If FileExistsUTF8(FN) then
begin
With TMemInifile.Create(FN) do
try
FProjectFile:=ReadString(SProject,KeyProjectFile,FProjectFile);
FName:=ReadString(SProject,KeyName,DirName);
FAuthor:=ReadString(SProject,KeyAuthor,'');
FDescription:=ReadString(SProject,KeyDescription,'');
FRecurse:=ReadBool(SProject,KeyRecurse,False);
FExclude:=ReadString(SProject,KeyExclude,'');
If (FExclude<>'') then
FExclude:=FExclude+',';
ReadSectionValues(SVariables,FVariables);
Finally
Free;
end;
end;
FN:=Directory+'description.txt';
If FileExistsUTF8(FN) then
begin
L.LoadFromFile(FN);
FDescription:=L.Text;
end;
GetFileList(FDirectory);
Finally
L.Free;
end;
end;
procedure TProjectTemplate.CreateFile(FileIndex: Integer; Source, Values: TStrings);
begin
CreateFile(FileNames[FileIndex],Source,Values);
end;
procedure TProjectTemplate.CreateFile(const FileName: String; Source,
Values: TStrings);
Var
F : Text;
Line : String;
begin
AssignFile(F,FileName);
Reset(F);
Try
While not EOF(F) do
begin
ReadLn(F,Line);
Source.Add(SubstituteString(Line,Values));
end;
Finally
CloseFile(F);
end;
end;
procedure TProjectTemplate.CreateProjectDirs(const BaseDir: String; Values : TStrings);
Var
RFN : String;
I : Integer;
begin
If not ForceDirectoriesUTF8(BaseDir) then
Raise ETemplateError.CreateFmt(SErrCouldNotCreateDir,[BaseDir]);
For I:=0 to FileCount-1 do
begin
RFN:=ExtractRelativePath(Directory,FileNames[i]);
RFN:=SubstituteString(ExtractFilePath(RFN),Values);
If (RFN<>'') Then
If not ForceDirectoriesUTF8(BaseDir+RFN) then
Raise ETemplateError.CreateFmt(SErrCouldNotCreateDir,[BaseDir+RFN]);
end;
end;
function TProjectTemplate.DefaultFileSubstitutes(AFileName : String) : string;
begin
Result:=AFileName;
If SameFileName(ChangeFileExt(ExtractFileName(Result),''),ProjectFile) then
Result:=ExtractFilePath(Result)+VarPrefixStr+'ProjName'+varpostfixstr+ExtractFileExt(Result);
end;
function TProjectTemplate.TargetFileName(FN: String; Values: TStrings): String;
begin
Result:=ExtractRelativePath(Directory,FN);
Result:=DefaultFileSubstitutes(Result);
Result:=SubstituteString(Result,Values);
end;
function TProjectTemplate.TargetFileName(I: Integer; Values: TStrings): String;
begin
Result:=TargetFileName(FileNames[I],Values);
end;
procedure TProjectTemplate.CopyAndSubstituteFile(Const SrcFN,DestFN : String; Values : Tstrings);
Var
L : TStringListUTF8;
begin
If pos(ExtractFileExt(SrcFN)+',',Exclude)<>0 then
begin
If not SimpleFileCopy(SrcFN,DestFN) then
Raise ETemplateError.CreateFmt(SErrFailedToCopyFile,[SrcFN,DestFN]);
end
else
begin
L:=TStringListUTF8.Create;
try
CreateFile(SrcFN,L,Values);
L.SaveToFile(DestFN);
Finally
L.Free;
end;
end;
end;
procedure TProjectTemplate.GetFileList(Const Dir : String);
Var
Info : TSearchRec;
begin
If FindFirstUTF8(Dir+GetAllFilesMask,0,Info)=0 then
try
repeat
if Not SpecialFile(info.name) then
FFiles.Add(Dir+Info.Name);
Until (FindNextUTF8(Info)<>0);
finally
FindCloseUTF8(Info);
end;
if Recurse then
If (FindFirstUTF8(Dir+GetAllFilesMask,0,Info)=0) then
try
repeat
if ((Info.attr and faDirectory)<>0) and
(Info.Name<>'.') and (info.Name<>'..') and (Info.Name<>'') then
GetFileList(Dir+Info.Name+PathSeparator);
until FindNextUTF8(Info)<>0;
finally
FindCloseUTF8(Info);
end;
end;
function TProjectTemplate.SpecialFile(const AName: String): Boolean;
begin
Result:=SameFileName(AName,'description.txt') or SameFileName(AName,'project.ini');
end;
procedure TProjectTemplate.CopyAndSubstituteDir(Const SrcDir,DestDir : String; Values: Tstrings);
Var
D1,D2 : String;
Info : TSearchRec;
N : String;
begin
D1:=IncludeTrailingPathDelimiter(SrcDir);
D2:=IncludeTrailingPathDelimiter(DestDir);
If not ForceDirectoriesUTF8(D2) then
Raise ETemplateError.CreateFmt(SErrCouldNotCreateDir,[D2]);
If FindFirstUTF8(D1+GetAllFilesMask,0,Info)=0 then
try
repeat
N:=Info.Name;
if Not SpecialFile(N) then
begin
// Anything that has projectfile as a name, is also substituted
N:=DefaultFileSubstitutes(N);
CopyAndSubstituteFile(D1+Info.Name,D2+SubstituteString(N,Values),Values);
end;
Until (FindNextUTF8(Info)<>0);
finally
FindCloseUTF8(Info);
end;
if Recurse then
If (FindFirstUTF8(D1+GetAllFilesMask,0,Info)<>0) then
try
repeat
if ((Info.attr and faDirectory)<>0) and
(Info.Name<>'.') and (info.Name<>'..') and (Info.Name<>'')
then
CopyAndSubstituteDir(D1+Info.Name,D2+SubstituteString(Info.Name,Values),Values);
until FindNextUTF8(Info)<>0;
finally
FindCloseUTF8(Info);
end;
end;
procedure TProjectTemplate.CreateProject(const ProjectDir: String;
Values: TStrings);
begin
CopyAndSubstituteDir(Directory,ProjectDir,Values);
end;
end.
|