ProjectTemplates: Use the template.lpi file for a new project. Issue #38244, patch from Rolf Wetjen.

git-svn-id: trunk@64307 -
This commit is contained in:
juha 2020-12-30 10:24:02 +00:00
parent 9cc893672d
commit 3f43cd260e
4 changed files with 90 additions and 29 deletions

View File

@ -9,14 +9,14 @@ object ProjectVariablesForm: TProjectVariablesForm
OnCreate = FormCreate OnCreate = FormCreate
OnShow = ProjectVariablesFormShow OnShow = ProjectVariablesFormShow
Position = poScreenCenter Position = poScreenCenter
LCLVersion = '1.9.0.0' LCLVersion = '2.1.0.0'
object ProjNameLabel: TLabel object ProjNameLabel: TLabel
AnchorSideTop.Control = EProjectName AnchorSideTop.Control = EProjectName
AnchorSideTop.Side = asrCenter AnchorSideTop.Side = asrCenter
Left = 8 Left = 8
Height = 13 Height = 15
Top = 21 Top = 20
Width = 124 Width = 118
Alignment = taRightJustify Alignment = taRightJustify
Caption = '&Name for new project:' Caption = '&Name for new project:'
Layout = tlCenter Layout = tlCenter
@ -26,9 +26,9 @@ object ProjectVariablesForm: TProjectVariablesForm
AnchorSideTop.Control = DEDestDir AnchorSideTop.Control = DEDestDir
AnchorSideTop.Side = asrCenter AnchorSideTop.Side = asrCenter
Left = 8 Left = 8
Height = 13 Height = 15
Top = 53 Top = 52
Width = 107 Width = 100
Alignment = taRightJustify Alignment = taRightJustify
Caption = 'Create in &directory:' Caption = 'Create in &directory:'
Layout = tlCenter Layout = tlCenter
@ -60,7 +60,7 @@ object ProjectVariablesForm: TProjectVariablesForm
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ButtonPanel1 AnchorSideBottom.Control = ButtonPanel1
Left = 6 Left = 6
Height = 156 Height = 158
Top = 141 Top = 141
Width = 527 Width = 527
Anchors = [akTop, akLeft, akRight, akBottom] Anchors = [akTop, akLeft, akRight, akBottom]
@ -70,10 +70,11 @@ object ProjectVariablesForm: TProjectVariablesForm
DefaultColWidth = 120 DefaultColWidth = 120
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goColSizing, goEditing, goAlwaysShowEditor, goDblClickAutoSize, goSmoothScroll] Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goColSizing, goEditing, goAlwaysShowEditor, goDblClickAutoSize, goSmoothScroll]
TabOrder = 3 TabOrder = 3
OnSelectEditor = SGVariablesSelectEditor
ColWidths = ( ColWidths = (
120 120
201
202 202
203
) )
end end
object EProjectName: TEdit object EProjectName: TEdit
@ -81,10 +82,10 @@ object ProjectVariablesForm: TProjectVariablesForm
AnchorSideLeft.Side = asrBottom AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = Owner AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
Left = 138 Left = 132
Height = 23 Height = 23
Top = 16 Top = 16
Width = 395 Width = 401
Anchors = [akTop, akLeft, akRight] Anchors = [akTop, akLeft, akRight]
BorderSpacing.Left = 6 BorderSpacing.Left = 6
BorderSpacing.Right = 6 BorderSpacing.Right = 6
@ -93,10 +94,10 @@ object ProjectVariablesForm: TProjectVariablesForm
object DEDestDir: TDirectoryEdit object DEDestDir: TDirectoryEdit
AnchorSideLeft.Control = EProjectName AnchorSideLeft.Control = EProjectName
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
Left = 138 Left = 132
Height = 23 Height = 23
Top = 48 Top = 48
Width = 393 Width = 399
ShowHidden = False ShowHidden = False
ButtonWidth = 23 ButtonWidth = 23
NumGlyphs = 1 NumGlyphs = 1
@ -106,8 +107,8 @@ object ProjectVariablesForm: TProjectVariablesForm
end end
object ButtonPanel1: TButtonPanel object ButtonPanel1: TButtonPanel
Left = 6 Left = 6
Height = 36 Height = 34
Top = 303 Top = 305
Width = 527 Width = 527
OKButton.Name = 'OKButton' OKButton.Name = 'OKButton'
OKButton.DefaultCaption = True OKButton.DefaultCaption = True

View File

@ -7,7 +7,7 @@ interface
uses uses
Classes, Classes,
// LCL // LCL
Forms, ExtCtrls, Grids, StdCtrls, EditBtn, ButtonPanel, Controls, Forms, ExtCtrls, Grids, StdCtrls, EditBtn, ButtonPanel,
// ProjectTemplates // ProjectTemplates
ProjectTemplates, ptstrconst; ProjectTemplates, ptstrconst;
@ -26,6 +26,8 @@ type
procedure BOKClick(Sender: TObject); procedure BOKClick(Sender: TObject);
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
procedure ProjectVariablesFormShow(Sender: TObject); procedure ProjectVariablesFormShow(Sender: TObject);
procedure SGVariablesSelectEditor(Sender: TObject; aCol, aRow: Integer;
var Editor: TWinControl);
private private
FSChanged: Boolean; FSChanged: Boolean;
FTemplates: TProjectTemplates; FTemplates: TProjectTemplates;
@ -57,6 +59,13 @@ begin
SGVariables.Cells[2,0]:=SDescription; SGVariables.Cells[2,0]:=SDescription;
end; end;
procedure TProjectVariablesForm.SGVariablesSelectEditor(Sender: TObject; aCol,
aRow: Integer; var Editor: TWinControl);
begin
if aCol<>1 then
Editor:=nil;
end;
procedure TProjectVariablesForm.BOKClick(Sender: TObject); procedure TProjectVariablesForm.BOKClick(Sender: TObject);
Var Var

View File

@ -5,7 +5,7 @@ unit IDETemplateProject;
interface interface
uses uses
Classes, SysUtils, ContNrs, Classes, SysUtils, StrUtils, ContNrs,
// LCL // LCL
LResources, Forms, Controls, Graphics, Dialogs, LResources, Forms, Controls, Graphics, Dialogs,
// LazUtils // LazUtils
@ -29,13 +29,13 @@ type
Function ShowOptionsDialog : TModalResult; Function ShowOptionsDialog : TModalResult;
public public
constructor Create(ATemplate : TProjectTemplate); overload; constructor Create(ATemplate : TProjectTemplate); overload;
destructor destroy; override; destructor Destroy; override;
Function DoInitDescriptor : TModalResult; override; Function DoInitDescriptor : TModalResult; override;
function GetLocalizedName: string; override; function GetLocalizedName: string; override;
function GetLocalizedDescription: string; override; function GetLocalizedDescription: string; override;
function InitProject(AProject: TLazProject) : TModalResult; override; function InitProject(AProject: TLazProject) : TModalResult; override;
function CreateStartFiles({%H-}AProject: TLazProject) : TModalResult; override; function CreateStartFiles({%H-}AProject: TLazProject) : TModalResult; override;
Property template : TProjectTemplate Read FTemplate Write FTemplate; Property Template : TProjectTemplate Read FTemplate Write FTemplate;
end; end;
procedure Register; procedure Register;
@ -108,11 +108,30 @@ begin
NewIDEItems.Add(TNewIDEItemCategory.Create(STemplateCategory)); NewIDEItems.Add(TNewIDEItemCategory.Create(STemplateCategory));
end; end;
procedure FileReplaceText(FN, AFrom, ATo: string);
var
sl: TStringList;
i: Integer;
begin
if not FileExistsUTF8(FN) then
exit;
sl:=TStringList.Create;
try
sl.LoadFromFile(FN);
for i:=0 to sl.Count-1 do
sl[i]:=ReplaceText(sl[i],AFrom,ATo);
sl.SaveToFile(fn);
finally
sl.Free;
end;
end;
Procedure DoProject(Sender : TObject); Procedure DoProject(Sender : TObject);
Var Var
I : Integer; I : Integer;
Desc : TTemplateProjectDescriptor; Desc : TTemplateProjectDescriptor;
fn: string;
begin begin
I:=MenuList.count-1; I:=MenuList.count-1;
@ -124,8 +143,18 @@ begin
Desc:=FProjDesc; Desc:=FProjDesc;
Dec(i); Dec(i);
end; end;
If Desc<>Nil then If Desc=Nil then
LazarusIDE.DoNewProject(Desc); exit;
If Desc.ShowOptionsDialog<>mrOk then
exit;
Desc.Template.CreateProject(Desc.FProjectDirectory,Desc.FVariables);
fn:=Desc.FProjectDirectory+Desc.FProjectName;
FileReplaceText(fn+'.lpi',Desc.FTemplate.ProjectFile,Desc.FProjectName);
FileReplaceText(fn+'.lpr',Desc.FTemplate.ProjectFile,Desc.FProjectName);
FileReplaceText(fn+'.lps',Desc.FTemplate.ProjectFile,Desc.FProjectName);
LazarusIDE.DoOpenProjectFile(Desc.FProjectDirectory+Desc.FProjectName+'.lpi',
[ofProjectLoading,ofOnlyIfExists,ofConvertMacros,ofDoLoadResource]);
end; end;
procedure RegisterKnowntemplates; procedure RegisterKnowntemplates;
@ -211,10 +240,16 @@ begin
FVariables.Assign(FTemplate.Variables); FVariables.Assign(FTemplate.Variables);
I:=FVariables.IndexOfName('ProjName'); I:=FVariables.IndexOfName('ProjName');
if (I<>-1) then if (I<>-1) then
begin
EProjectName.Text:=FVariables.Values['ProjName'];
FVariables.Delete(I); FVariables.Delete(I);
end;
I:=FVariables.IndexOfName('ProjDir'); I:=FVariables.IndexOfName('ProjDir');
if (I<>-1) then if (I<>-1) then
begin
DEDestDir.Text:=FVariables.Values['ProjDir'];
FVariables.Delete(I); FVariables.Delete(I);
end;
Templates:=Templates; Templates:=Templates;
Variables:=FVariables; Variables:=FVariables;
Result:=ShowModal; Result:=ShowModal;
@ -265,11 +300,23 @@ end;
function TTemplateProjectDescriptor.DoInitDescriptor: TModalResult; function TTemplateProjectDescriptor.DoInitDescriptor: TModalResult;
var
I : integer;
Desc : TTemplateProjectDescriptor;
begin begin
Result:=ShowOptionsDialog; Result:=mrCancel;
If (Result=mrOK) then I:=MenuList.count-1;
FTemplate.CreateProject(FProjectDirectory,FVariables); Desc:=Nil;
While (Desc=Nil) and (I>=0) do
begin
With TIDEObject(MenuList[i]) do
if FProjDesc=self then
begin
DoProject(FProjMenu);
exit;
end;
Dec(i);
end;
end; end;
@ -317,6 +364,9 @@ begin
end end
else else
Result:=mrCancel; Result:=mrCancel;
Result:=mrCancel;
end; end;
Function TTemplateProjectDescriptor.CreateStartFiles(AProject: TLazProject) : TModalresult; Function TTemplateProjectDescriptor.CreateStartFiles(AProject: TLazProject) : TModalresult;

View File

@ -136,10 +136,8 @@ begin
end; end;
Function SimpleFileCopy(Const Source,Dest : String) : Boolean; Function SimpleFileCopy(Const Source,Dest : String) : Boolean;
Var Var
F1,F2 : TFileStream; F1,F2 : TFileStream;
begin begin
Result:=False; Result:=False;
try try
@ -307,6 +305,11 @@ begin
FExclude:=ReadString(SProject,KeyExclude,''); FExclude:=ReadString(SProject,KeyExclude,'');
If (FExclude<>'') then If (FExclude<>'') then
FExclude:=FExclude+','; FExclude:=FExclude+',';
// Don't change ico and res files
If pos('.ico,',FExclude)<=0 then
FExclude:=FExclude+'.ico,';
If pos('.res,',FExclude)<=0 then
FExclude:=FExclude+'.res,';
ReadSectionValues(SVariables,FVariables); ReadSectionValues(SVariables,FVariables);
Finally Finally
Free; Free;
@ -494,6 +497,4 @@ begin
CopyAndSubstituteDir(Directory,ProjectDir,Values); CopyAndSubstituteDir(Directory,ProjectDir,Values);
end; end;
end. end.