IDE: package editor: move files: check if unit exists

git-svn-id: trunk@45512 -
This commit is contained in:
mattias 2014-06-15 11:39:50 +00:00
parent e4fe9431e6
commit b71a876acf

View File

@ -3021,6 +3021,8 @@ var
NewFileToOldPkgFile: TFilenameToPointerTree; NewFileToOldPkgFile: TFilenameToPointerTree;
ChangedFilenames: TFilenameToStringTree; // old to new file name ChangedFilenames: TFilenameToStringTree; // old to new file name
UnitFilenameToResFileList: TFilenameToPointerTree; // filename to TStringList UnitFilenameToResFileList: TFilenameToPointerTree; // filename to TStringList
TargetPkgList: TFPList;
SrcPkgList: TFPList;
procedure DeleteNonExistingPkgFiles; procedure DeleteNonExistingPkgFiles;
var var
@ -3124,17 +3126,80 @@ var
S2SItem: PStringToStringTreeItem; S2SItem: PStringToStringTreeItem;
OldFilename: String; OldFilename: String;
NewFilename: String; NewFilename: String;
ConflictFile: TPkgFile;
CurName: String;
ShortFilename: String;
r: TModalResult;
i: Integer;
WarnUnitClash: Boolean;
WarnNameClash: Boolean;
begin begin
Result:=false;
WarnUnitClash:=true;
WarnNameClash:=true;
for S2SItem in AllChangedFilenames do begin for S2SItem in AllChangedFilenames do begin
OldFilename:=S2SItem^.Name; OldFilename:=S2SItem^.Name;
NewFilename:=S2SItem^.Value; NewFilename:=S2SItem^.Value;
if CompareFilenames(OldFilename,NewFilename)=0 then continue; if CompareFilenames(OldFilename,NewFilename)=0 then continue;
if not FileExistsCached(NewFilename) then continue;
IDEMessageDialog('Conflict detected', // check file does not exist
'There is already a file'#13 if FileExistsCached(NewFilename) then begin
+NewFilename+#13 IDEMessageDialog('Conflict detected',
+'in package '+LazPackage.Name,mtError,[mbCancel]); 'There is already a file'#13
exit(false); +NewFilename+#13
+'in package '+LazPackage.Name,mtError,[mbCancel]);
exit;
end;
if (LazPackage<>SrcPackage) then begin
// warn duplicate names
if FilenameIsPascalUnit(NewFilename) then begin
// warn duplicate unit name
CurName:=ExtractFileNameOnly(NewFilename);
ConflictFile:=LazPackage.FindUnit(CurName,true);
if (ConflictFile<>nil) and WarnUnitClash then begin
ShortFilename:=NewFilename;
LazPackage.ShortenFilename(ShortFilename,true);
r:=IDEMessageDialog('Duplicate Unit',
'There is already a unit "'+CurName+'" in package '+LazPackage.Name+#13
+'Old: '+ConflictFile.GetShortFilename(true)+#13
+'New: '+ShortFilename+#13
+'You have to make sure that the unit search path of the package contains only one of them.'#13
+#13
+'Continue?'
,mtWarning,[mbYes,mbYesToAll,mbCancel]);
case r of
mrYes: ;
mrYesToAll: WarnUnitClash:=false;
else exit;
end;
end;
end else begin
// warn duplicate file
for i:=0 to LazPackage.FileCount-1 do begin
if not WarnNameClash then continue;
ConflictFile:=LazPackage.Files[i];
CurName:=ExtractFilename(NewFilename);
if UTF8CompareText(CurName,ExtractFileName(ConflictFile.Filename))<>0
then
continue;
ShortFilename:=NewFilename;
LazPackage.ShortenFilename(ShortFilename,true);
r:=IDEMessageDialog('Duplicate File Name',
'There is already a file "'+CurName+'" in package '+LazPackage.Name+#13
+'Old: '+ConflictFile.GetShortFilename(true)+#13
+'New: '+ShortFilename+#13
+#13
+'Continue?'
,mtWarning,[mbYes,mbYesToAll,mbCancel]);
case r of
mrYes: ;
mrYesToAll: WarnNameClash:=false;
else exit;
end;
end;
end;
end;
end; end;
Result:=true; Result:=true;
end; end;
@ -3184,7 +3249,6 @@ var
Node:=Node.NextBrother; Node:=Node.NextBrother;
if AnUnitName='' then continue; if AnUnitName='' then continue;
// find unit file // find unit file
PkgName:='';
NewCode:=Tool.FindUnitSource(AnUnitName,AnUnitInFilename,false,Node.StartPos); NewCode:=Tool.FindUnitSource(AnUnitName,AnUnitInFilename,false,Node.StartPos);
if (NewCode=nil) then begin if (NewCode=nil) then begin
// no source found // no source found
@ -3194,11 +3258,19 @@ var
NewCompiledUnitname,false); NewCompiledUnitname,false);
if CompiledFilename='' then begin if CompiledFilename='' then begin
// unit not found // unit not found
// => moving the unit will not make it worse // (that is ok, e.g. if the unit is used on another platform)
// => only warn
Msg:='unit '+AnUnitName+' not found';
if not Tool.CleanPosToCaret(ErrorPos,CodePos) then continue;
{$IFNDEF EnableOldExtTools}
IDEMessagesWindow.AddCustomMessage(mluWarning,Msg,
CodePos.Code.Filename,CodePos.Y,CodePos.X,'Move Files');
{$ELSE}
IDEMessagesWindow.AddMsg('Warning: '+Msg,'',-1);
{$ENDIF}
continue; continue;
end; end;
// ToDo: find package UnitFilename:=CompiledFilename;
end else begin end else begin
// unit found // unit found
UnitFilename:=NewCode.Filename; UnitFilename:=NewCode.Filename;
@ -3206,14 +3278,18 @@ var
// this unit will be moved too => ok // this unit will be moved too => ok
continue; continue;
end; end;
// ToDo: find package
end; end;
// UnitFilename is now either a .pas/pp/p or .ppu file
// ToDo: find package
PkgName:='';
if not Tool.CleanPosToCaret(ErrorPos,CodePos) then continue; if not Tool.CleanPosToCaret(ErrorPos,CodePos) then continue;
Msg:='unit '+AnUnitName+' requires package '+PkgName; Msg:='unit '+AnUnitName+' requires package '+PkgName;
{$IFNDEF EnableOldExtTools} {$IFNDEF EnableOldExtTools}
IDEMessagesWindow.AddCustomMessage(mluWarning,Msg, IDEMessagesWindow.AddCustomMessage(mluWarning,Msg,
CodePos.Code.Filename,CodePos.Y,CodePos.X,'Move Units'); CodePos.Code.Filename,CodePos.Y,CodePos.X,'Move Files');
{$ELSE} {$ELSE}
IDEMessagesWindow.AddMsg('Warning: '+Msg,'',-1); IDEMessagesWindow.AddMsg('Warning: '+Msg,'',-1);
{$ENDIF} {$ENDIF}
@ -3221,6 +3297,7 @@ var
end; end;
function CheckUsesSections: boolean; function CheckUsesSections: boolean;
// check that all used units are available in the target package
var var
i: Integer; i: Integer;
PkgFile: TPkgFile; PkgFile: TPkgFile;
@ -3291,6 +3368,8 @@ begin
AllChangedFilenames:=TFilenameToStringTree.Create(false); AllChangedFilenames:=TFilenameToStringTree.Create(false);
UnitFilenameToResFileList:=TFilenameToPointerTree.Create(false); UnitFilenameToResFileList:=TFilenameToPointerTree.Create(false);
UnitFilenameToResFileList.FreeValues:=true; UnitFilenameToResFileList.FreeValues:=true;
TargetPkgList:=nil;
SrcPkgList:=nil;
try try
// collect all affected files including resource files // collect all affected files including resource files
if not CollectFiles(MoveFileCount) then exit; if not CollectFiles(MoveFileCount) then exit;
@ -3329,14 +3408,18 @@ begin
DeleteOld:=true; DeleteOld:=true;
end; end;
// fetch used packages
PackageGraph.GetAllRequiredPackages(LazPackage,nil,TargetPkgList);
PackageGraph.GetAllRequiredPackages(SrcPackage,nil,SrcPkgList);
// check uses sections
if not CheckUsesSections then exit;
if DeleteOld then begin if DeleteOld then begin
// close files and res files in source editor // close files and res files in source editor
if not CloseSrcEditors then exit; if not CloseSrcEditors then exit;
end; end;
// check uses sections
if not CheckUsesSections then exit;
if (SrcPackage<>LazPackage) then begin if (SrcPackage<>LazPackage) then begin
// files will be moved to another directory // files will be moved to another directory
// => clear output directory of SrcPackage // => clear output directory of SrcPackage
@ -3345,13 +3428,17 @@ begin
end; end;
ShowMessage('Moving files is not yet implemented'); ShowMessage('Moving files is not yet implemented');
exit;
// move/copy file and res files, Note: some files are res files // move/copy file and res files, Note: some files are res files
// move/copy TPkgFile, make uses-unit unique // move/copy TPkgFile, make uses-unit unique
// extend unit/include path of LazPackage
// clean up unit/inlude path of SrcPkg // clean up unit/inlude path of SrcPkg
//Result:=true; Result:=true;
finally finally
TargetPkgList.Free;
SrcPkgList.Free;
UnitFilenameToResFileList.Free; UnitFilenameToResFileList.Free;
AllChangedFilenames.Free; AllChangedFilenames.Free;
ChangedFilenames.Free; ChangedFilenames.Free;