View Issue Details

IDProjectCategoryView StatusLast Update
0038244LazarusIDEpublic2021-03-10 08:45
ReporterRolf Wetjen Assigned ToJuha Manninen  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Platformx64OSWindows 10 
Product Version2.0.10 
Summary0038244: projecttemplates doesn't use the template.lpi file for a new project
DescriptionMy template has a customized file project1.lpi. Projecttemplates copies this file to the ProjDir (and all other files of course) but overwrites the lpi file with some defaults later. function TTemplateProjectDescriptor.InitProject in idetemplateproject.pp sets some defaults for AProject but doesn't take care of the settings from the existing lpi file (the template's one). So the template's lpi file is overwritten with the defaults set here.

My project.ini:
[Variables]
ProjDir=D:\Rolf\Lazarus\Rolf\Test\
ProjName=project1

[Project]
Name=Test Anwendung
Author=Rolf Wetjen
Description=Eine Anwendung zum Testen
ProjectFile=project1
Recurse=0

Rolf
TagsNo tags attached.
Fixed in Revisionr64307
LazTarget-
WidgetsetWin32/Win64
Attached Files

Activities

Rolf Wetjen

2020-12-26 13:56

reporter   ~0127805

Patch for Lazarus 2.1.0 / SVN 63089M attached. Solves this issue an some fine tuning.
Rolf
projecttemplates.patch (7,989 bytes)   
Index: frmtemplatevariables.lfm
===================================================================
--- frmtemplatevariables.lfm	(revision 64270)
+++ frmtemplatevariables.lfm	(working copy)
@@ -9,14 +9,14 @@
   OnCreate = FormCreate
   OnShow = ProjectVariablesFormShow
   Position = poScreenCenter
-  LCLVersion = '1.9.0.0'
+  LCLVersion = '2.1.0.0'
   object ProjNameLabel: TLabel
     AnchorSideTop.Control = EProjectName
     AnchorSideTop.Side = asrCenter
     Left = 8
-    Height = 13
-    Top = 21
-    Width = 124
+    Height = 15
+    Top = 20
+    Width = 118
     Alignment = taRightJustify
     Caption = '&Name for new project:'
     Layout = tlCenter
@@ -26,9 +26,9 @@
     AnchorSideTop.Control = DEDestDir
     AnchorSideTop.Side = asrCenter
     Left = 8
-    Height = 13
-    Top = 53
-    Width = 107
+    Height = 15
+    Top = 52
+    Width = 100
     Alignment = taRightJustify
     Caption = 'Create in &directory:'
     Layout = tlCenter
@@ -60,7 +60,7 @@
     AnchorSideRight.Side = asrBottom
     AnchorSideBottom.Control = ButtonPanel1
     Left = 6
-    Height = 156
+    Height = 158
     Top = 141
     Width = 527
     Anchors = [akTop, akLeft, akRight, akBottom]
@@ -70,10 +70,11 @@
     DefaultColWidth = 120
     Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goColSizing, goEditing, goAlwaysShowEditor, goDblClickAutoSize, goSmoothScroll]
     TabOrder = 3
+    OnSelectEditor = SGVariablesSelectEditor
     ColWidths = (
       120
+      201
       202
-      203
     )
   end
   object EProjectName: TEdit
@@ -81,10 +82,10 @@
     AnchorSideLeft.Side = asrBottom
     AnchorSideRight.Control = Owner
     AnchorSideRight.Side = asrBottom
-    Left = 138
+    Left = 132
     Height = 23
     Top = 16
-    Width = 395
+    Width = 401
     Anchors = [akTop, akLeft, akRight]
     BorderSpacing.Left = 6
     BorderSpacing.Right = 6
@@ -93,10 +94,10 @@
   object DEDestDir: TDirectoryEdit
     AnchorSideLeft.Control = EProjectName
     AnchorSideRight.Side = asrBottom
-    Left = 138
+    Left = 132
     Height = 23
     Top = 48
-    Width = 393
+    Width = 399
     ShowHidden = False
     ButtonWidth = 23
     NumGlyphs = 1
@@ -106,8 +107,8 @@
   end
   object ButtonPanel1: TButtonPanel
     Left = 6
-    Height = 36
-    Top = 303
+    Height = 34
+    Top = 305
     Width = 527
     OKButton.Name = 'OKButton'
     OKButton.DefaultCaption = True
Index: frmtemplatevariables.pas
===================================================================
--- frmtemplatevariables.pas	(revision 64270)
+++ frmtemplatevariables.pas	(working copy)
@@ -9,7 +9,7 @@
   // LCL
   Forms, ExtCtrls, Grids, StdCtrls, EditBtn, ButtonPanel,
   // ProjectTemplates
-  ProjectTemplates, ptstrconst;
+  ProjectTemplates, ptstrconst, Controls;
 
 type
 
@@ -26,6 +26,8 @@
     procedure BOKClick(Sender: TObject);
     procedure FormCreate(Sender: TObject);
     procedure ProjectVariablesFormShow(Sender: TObject);
+    procedure SGVariablesSelectEditor(Sender: TObject; aCol, aRow: Integer;
+      var Editor: TWinControl);
   private
     FSChanged: Boolean;
     FTemplates: TProjectTemplates;
@@ -57,6 +59,13 @@
   SGVariables.Cells[2,0]:=SDescription;
 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);
 
 Var
Index: idetemplateproject.pp
===================================================================
--- idetemplateproject.pp	(revision 64270)
+++ idetemplateproject.pp	(working copy)
@@ -29,13 +29,13 @@
     Function ShowOptionsDialog : TModalResult;
   public
     constructor Create(ATemplate : TProjectTemplate); overload;
-    destructor destroy; override;
+    destructor Destroy; override;
     Function DoInitDescriptor : TModalResult; override;
     function GetLocalizedName: string; override;
     function GetLocalizedDescription: string; override;
     function InitProject(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;
   
 procedure Register;
@@ -42,6 +42,9 @@
 
 implementation
 
+uses
+  StrUtils;
+
 Var
   IDETemplates : TProjectTemplates = nil;
   itmFileNewFromTemplate : TIDEMenuSection;
@@ -108,11 +111,28 @@
   NewIDEItems.Add(TNewIDEItemCategory.Create(STemplateCategory));
 end;
 
+procedure FileReplaceText(FN, AFrom, ATo: string);
+var
+  sl: TStringList;
+begin
+  if not FileExistsUTF8(FN) then
+    exit;
+  sl:=TStringList.Create;
+  try
+    sl.LoadFromFile(FN);
+    sl.Text:=ReplaceText(sl.Text,AFrom,ATo);
+    sl.SaveToFile(fn);
+  finally
+    sl.Free;
+  end;
+end;
+
 Procedure DoProject(Sender : TObject);
 
 Var
   I : Integer;
   Desc : TTemplateProjectDescriptor;
+  fn: string;
 
 begin
   I:=MenuList.count-1;
@@ -124,8 +144,18 @@
         Desc:=FProjDesc;
     Dec(i);
     end;
-  If Desc<>Nil then
-    LazarusIDE.DoNewProject(Desc);
+  If Desc=Nil then
+    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;
 
 procedure RegisterKnowntemplates;
@@ -211,10 +241,16 @@
       FVariables.Assign(FTemplate.Variables);
       I:=FVariables.IndexOfName('ProjName');
       if (I<>-1) then
+        begin
+        EProjectName.Text:=FVariables.Values['ProjName'];
         FVariables.Delete(I);
+        end;
       I:=FVariables.IndexOfName('ProjDir');
       if (I<>-1) then
+        begin
+        DEDestDir.Text:=FVariables.Values['ProjDir'];
         FVariables.Delete(I);
+        end;
       Templates:=Templates;
       Variables:=FVariables;
       Result:=ShowModal;
@@ -265,11 +301,23 @@
 
 
 function TTemplateProjectDescriptor.DoInitDescriptor: TModalResult;
-
+var
+  I : integer;
+  Desc : TTemplateProjectDescriptor;
 begin
-  Result:=ShowOptionsDialog;
-  If (Result=mrOK) then
-    FTemplate.CreateProject(FProjectDirectory,FVariables);
+  Result:=mrCancel;
+  I:=MenuList.count-1;
+  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;
 
 
@@ -317,6 +365,9 @@
     end
   else
     Result:=mrCancel;
+
+Result:=mrCancel;
+
 end;
 
 Function TTemplateProjectDescriptor.CreateStartFiles(AProject: TLazProject) : TModalresult;
Index: projecttemplates.pp
===================================================================
--- projecttemplates.pp	(revision 64270)
+++ projecttemplates.pp	(working copy)
@@ -136,10 +136,8 @@
 end;
 
 Function SimpleFileCopy(Const Source,Dest : String) : Boolean;
-
 Var
   F1,F2 : TFileStream;
-
 begin
   Result:=False;
   try
@@ -307,6 +305,11 @@
           FExclude:=ReadString(SProject,KeyExclude,'');
           If (FExclude<>'') then
             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);
         Finally
           Free;
@@ -494,6 +497,4 @@
   CopyAndSubstituteDir(Directory,ProjectDir,Values);
 end;
 
-
-
 end.
projecttemplates.patch (7,989 bytes)   

Juha Manninen

2020-12-30 01:09

developer   ~0127924

Last edited: 2020-12-30 01:15

View 2 revisions

Looking at the code, your procedure FileReplaceText is very slow.
With sl: TStringList this piece of code :
    sl.Text:=ReplaceText(sl.Text,AFrom,ATo);
first joins lines to one string, the runs ReplaceText, then splits the string again by NewLines for the StringList.
Iterating the list and replacing line by line may be faster. I didn't test yet, just looking it.
ReplaceText() calls StringReplace() which is slow as well, but should be OK here.

BTW, we usually put used units in the interface section unless really needed in implementation section for cyclic dependency.
It makes following and designing dependencies easier and prevents accidental cyclic dependencies.

Juha Manninen

2020-12-30 11:25

developer   ~0127929

I applied it with minor changes. Please test.
Thanks for the patch.

Rolf Wetjen

2021-01-05 17:55

reporter   ~0128096

Tested ok.
Thanks, Rolf

Issue History

Date Modified Username Field Change
2020-12-22 13:14 Rolf Wetjen New Issue
2020-12-26 13:56 Rolf Wetjen Note Added: 0127805
2020-12-26 13:56 Rolf Wetjen File Added: projecttemplates.patch
2020-12-30 01:09 Juha Manninen Note Added: 0127924
2020-12-30 01:15 Juha Manninen Note Edited: 0127924 View Revisions
2020-12-30 11:23 Juha Manninen Assigned To => Juha Manninen
2020-12-30 11:23 Juha Manninen Status new => assigned
2020-12-30 11:25 Juha Manninen Status assigned => resolved
2020-12-30 11:25 Juha Manninen Resolution open => fixed
2020-12-30 11:25 Juha Manninen Fixed in Revision => r64307
2020-12-30 11:25 Juha Manninen LazTarget => -
2020-12-30 11:25 Juha Manninen Widgetset Win32/Win64 => Win32/Win64
2020-12-30 11:25 Juha Manninen Note Added: 0127929
2021-01-05 17:55 Rolf Wetjen Note Added: 0128096
2021-03-10 08:45 Rolf Wetjen Status resolved => closed