Converter: Convert also units in project directory which are not part of the project. Issue #22677

git-svn-id: trunk@38903 -
This commit is contained in:
juha 2012-09-29 14:44:47 +00:00
parent b8f1a26235
commit 7d7d464100
2 changed files with 63 additions and 38 deletions

View File

@ -181,6 +181,7 @@ type
function ExtractOptionsFromDOF(const DOFFilename: string): TModalResult;
function ExtractOptionsFromCFG(const CFGFilename: string): TModalResult;
function DoMissingUnits(AUsedUnitsTool: TUsedUnitsTool): integer; override;
function CheckPackageDependency(AUnitName: string): Boolean;
protected
function CreateInstance: TModalResult; virtual; abstract;
function CreateMainSourceFile: TModalResult; virtual;
@ -202,7 +203,6 @@ type
constructor Create(const AFilename, ADescription: string);
destructor Destroy; override;
function Convert: TModalResult;
function CheckPackageDependency(AUnitName: string): Boolean;
public
property CompOpts: TBaseCompilerOptions read GetCompOpts;
property CustomDefines: TDefineTemplate read GetCustomDefines;
@ -218,6 +218,7 @@ type
// Resource code
fMainUnitConverter: TDelphiUnit;
function AddUnit(AFileName: string; out OutUnitInfo: TUnitInfo): TModalResult;
function CheckUnitForConversion(aFileName: string): Boolean;
function GetLazProject: TProject;
procedure SetLazProject(const AValue: TProject);
protected
@ -619,13 +620,16 @@ begin
// Fix include file names.
Result:=FixIncludeFiles;
if Result<>mrOk then exit;
// Create a toold for missing units.
// Create a tool for missing units.
fUsedUnitsTool:=TUsedUnitsTool.Create(fCTLink, fOrigUnitFilename);
if fOwnerConverter is TConvertDelphiProjPack then
with fOwnerConverter as TConvertDelphiProjPack do begin
fUsedUnitsTool.CheckPackDepEvent:=@CheckPackageDependency;
with TConvertDelphiProjPack(fOwnerConverter) do begin
fUsedUnitsTool.OnCheckPackageDependency:=@CheckPackageDependency;
fUsedUnitsTool.IsConsoleApp:=fIsConsoleApp;
end;
if fOwnerConverter is TConvertDelphiProject then
with TConvertDelphiProject(fOwnerConverter) do
fUsedUnitsTool.OnCheckUnitForConversion:=@CheckUnitForConversion;
end;
function TDelphiUnit.FixLfmFilenameAndLoad(ADfmFilename: string): TModalResult;
@ -1213,30 +1217,6 @@ begin
Options.SrcPath:=CleanProjectSearchPath(Options.SrcPath);
end;
function TConvertDelphiProjPack.CheckPackageDependency(AUnitName: string): Boolean;
var
Pack: TPkgFile;
Dep: TPkgDependency;
s: String;
begin
Result:=False;
Pack:=PackageGraph.FindUnitInAllPackages(AUnitName, True);
if Assigned(Pack) then begin
// Found from package: add package to project dependencies and open it.
s:=Pack.LazPackage.Name;
if s='LCLBase' then
s:='LCL';
AddPackageDependency(s);
IDEMessagesWindow.AddMsg(Format(lisConvDelphiAddedPackageRequirement, [s]), '', -1);
Dep:=FindDependencyByName(s);
if Assigned(Dep) then
PackageGraph.OpenDependency(Dep,false);
Result:=True;
end else begin;
// ToDo: Install the required package automatically from a repository...
end;
end;
function TConvertDelphiProjPack.DoMissingUnits(AUsedUnitsTool: TUsedUnitsTool): integer;
// Locate unit names from earlier cached list or from packages.
// Return the number of units still missing.
@ -1275,6 +1255,30 @@ begin
Result:=AUsedUnitsTool.MissingUnitCount;
end;
function TConvertDelphiProjPack.CheckPackageDependency(AUnitName: string): Boolean;
var
Pack: TPkgFile;
Dep: TPkgDependency;
s: String;
begin
Result:=False;
Pack:=PackageGraph.FindUnitInAllPackages(AUnitName, True);
if Assigned(Pack) then begin
// Found from package: add package to project dependencies and open it.
s:=Pack.LazPackage.Name;
if s='LCLBase' then
s:='LCL';
AddPackageDependency(s);
IDEMessagesWindow.AddMsg(Format(lisConvDelphiAddedPackageRequirement, [s]), '', -1);
Dep:=FindDependencyByName(s);
if Assigned(Dep) then
PackageGraph.OpenDependency(Dep,false);
Result:=True;
end else begin;
// ToDo: Install the required package automatically from a repository...
end;
end;
function TConvertDelphiProjPack.CreateMainSourceFile: TModalResult;
begin
Result:=mrOK; // Do nothing. Overridden in project.
@ -1444,6 +1448,21 @@ begin
end;
end;
function TConvertDelphiProject.CheckUnitForConversion(aFileName: string): Boolean;
var
UnitInfo: TUnitInfo;
Path: string;
begin
// Units in project directory but not part of the project are not reported
// as missing and would not be converted. Now add them to the project.
if ExtractFilePath(aFileName)=LazProject.ProjectDirectory then begin
UnitInfo:=LazProject.UnitInfoWithFilename(aFileName);
Result:=Assigned(UnitInfo);
if not Result then
fUnitsToAddToProject.Add(aFileName); // Will be added later to project.
end;
end;
function TConvertDelphiProject.FindAllUnits: TModalResult;
var
FoundUnits, MisUnits, NormalUnits: TStrings;
@ -1523,7 +1542,7 @@ var
i: Integer;
begin
Result:=mrOk;
ConvUnits:=TObjectList.create;
ConvUnits:=TObjectList.Create;
try
// convert all units and fix .lfm files
IDEMessagesWindow.AddMsg(lisConvDelphiConvertingUnitFiles, '', -1);

View File

@ -117,7 +117,8 @@ type
fIsConsoleApp: Boolean;
fMainUsedUnits: TUsedUnits;
fImplUsedUnits: TUsedUnits;
fCheckPackageDependencyEvent: TCheckUnitEvent;
fOnCheckPackageDependency: TCheckUnitEvent;
fOnCheckUnitForConversion: TCheckUnitEvent;
function GetMissingUnitCount: integer;
public
constructor Create(ACTLink: TCodeToolLink; AFilename: string);
@ -134,8 +135,10 @@ type
property MainUsedUnits: TUsedUnits read fMainUsedUnits;
property ImplUsedUnits: TUsedUnits read fImplUsedUnits;
property MissingUnitCount: integer read GetMissingUnitCount;
property CheckPackDepEvent: TCheckUnitEvent read fCheckPackageDependencyEvent
write fCheckPackageDependencyEvent;
property OnCheckPackageDependency: TCheckUnitEvent
read fOnCheckPackageDependency write fOnCheckPackageDependency;
property OnCheckUnitForConversion: TCheckUnitEvent
read fOnCheckUnitForConversion write fOnCheckUnitForConversion;
end;
@ -233,8 +236,8 @@ begin
if AFilename<>'' then begin // unit found
if (NewUnitName<>OldUnitName) and not Settings.OmitProjUnits.Find(NewUnitName,x)
then begin
// Character case differs and it will not be replaced.
fUnitsToFixCase[OldUnitName]:=NewUnitName; // fix case
// Character case differs, fix it.
fUnitsToFixCase[OldUnitName]:=NewUnitName;
IDEMessagesWindow.AddMsg(Format(lisConvDelphiFixedUnitCase,
[OldUnitName, NewUnitName]), '', -1);
end;
@ -242,6 +245,9 @@ begin
// Needed if work-platform is Windows (kind of a hack).
if Settings.MultiPlatform and IsWinSpecificUnit(LowNU) then
fMissingUnits.Add(s);
// Check if the unit is not part of project and needs conversion, too.
if Assigned(fOwnerTool.OnCheckUnitForConversion) then
fOwnerTool.OnCheckUnitForConversion(AFilename);
end
else begin
// If the unit is not found, add it to fMissingUnits, but don't add
@ -277,8 +283,8 @@ begin
// If the unit is not found, open the package containing it.
UnitInFileName:='';
if fCTLink.CodeTool.FindUnitCaseInsensitive(ANewName,UnitInFileName) = '' then
if Assigned(fOwnerTool.CheckPackDepEvent) then
if not fOwnerTool.CheckPackDepEvent(ANewName) then
if Assigned(fOwnerTool.OnCheckPackageDependency) then
if not fOwnerTool.OnCheckPackageDependency(ANewName) then
;
end
else begin
@ -686,8 +692,8 @@ begin
// If the unit is not found, open the package containing it.
UnitInFileName:='';
if fCTLink.CodeTool.FindUnitCaseInsensitive(AUnitName,UnitInFileName) = '' then
if Assigned(fCheckPackageDependencyEvent) then
if not fCheckPackageDependencyEvent(AUnitName) then
if Assigned(fOnCheckPackageDependency) then
if not fOnCheckPackageDependency(AUnitName) then
;
end;
end;