Converter: refactor the code for background file scanning

git-svn-id: trunk@38358 -
This commit is contained in:
juha 2012-08-24 18:52:02 +00:00
parent c04a85a663
commit dbb987ca5b

View File

@ -64,11 +64,8 @@ type
TCacheUnitsThread = class(TThread) TCacheUnitsThread = class(TThread)
private private
fConverter: TConvertDelphiPBase;
fPath: string; fPath: string;
fPasFileList: TStringList;
fSearcher: TFileSearcher; fSearcher: TFileSearcher;
procedure CacheUnitsInPath;
protected protected
procedure Execute; override; procedure Execute; override;
public public
@ -390,27 +387,34 @@ type
TUnitsSearcher = class(TFileSearcher) TUnitsSearcher = class(TFileSearcher)
private private
FList: TStrings; fConverter: TConvertDelphiPBase;
FOwnerThread: TThread;
protected protected
procedure DoFileFound; override; procedure DoFileFound; override;
public public
constructor Create(AOwnerThread: TThread; AList: TStrings); constructor Create(aConverter: TConvertDelphiPBase);
end; end;
{ TUnitsSearcher } { TUnitsSearcher }
procedure TUnitsSearcher.DoFileFound; procedure TUnitsSearcher.DoFileFound;
var
RelPath, SubPath, sUnitName, fn: String;
begin begin
// if FOwnerThread.Terminated then <- Does not work, Terminated is protected RelPath:=FileUtil.CreateRelativePath(FileName, fConverter.fSettings.MainPath);
// Stop; SubPath:=ExtractFilePath(RelPath);
FList.Add(FileName); fn:=ExtractFileName(RelPath);
sUnitName:=ExtractFileNameOnly(fn);
if (SubPath<>'') and (sUnitName<>'') then begin
// Map path by unit name.
fConverter.fCachedUnitNames[sUnitName]:=SubPath;
// Map real unit name by uppercase unit name.
fConverter.fCachedRealFileNames[UpperCase(sUnitName)]:=fn;
end;
end; end;
constructor TUnitsSearcher.Create(AOwnerThread: TThread; AList: TStrings); constructor TUnitsSearcher.Create(aConverter: TConvertDelphiPBase);
begin begin
FOwnerThread := AOwnerThread; fConverter := aConverter;
FList := AList;
end; end;
{ TCacheUnitsThread } { TCacheUnitsThread }
@ -419,50 +423,24 @@ constructor TCacheUnitsThread.Create(aConverter: TConvertDelphiPBase);
begin begin
inherited Create(True); inherited Create(True);
FreeOnTerminate:=True; FreeOnTerminate:=True;
fConverter:=aConverter; // Will scan one level up from base path. // Create searcher already now. Its Stop method can be called anytime.
// Create file list and searcher already now. Its Stop method can be called anytime fSearcher:=TUnitsSearcher.Create(aConverter);
fPasFileList:=TStringList.Create;
fSearcher:=TUnitsSearcher.Create(Self, fPasFileList);
// The parent directory to be scanned // The parent directory to be scanned
fPath:=TrimFilename(fConverter.fSettings.MainPath+'..'+DirectorySeparator); fPath:=TrimFilename(aConverter.fSettings.MainPath+'..'+DirectorySeparator);
end; end;
destructor TCacheUnitsThread.Destroy; destructor TCacheUnitsThread.Destroy;
begin begin
fSearcher.Free; fSearcher.Free;
fPasFileList.Free;
inherited Destroy; inherited Destroy;
end; end;
procedure TCacheUnitsThread.CacheUnitsInPath;
// Search all pascal units in fPath and store them in fCachedUnitNames
// with a path relative to fSettings.MainPath.
var
i: Integer;
PasFile, RelPath, SubPath, sUnitName, FileName: String;
begin
fSearcher.Search(fPath, '*.pas');
for i:=0 to fPasFileList.Count-1 do begin
PasFile:=fPasFileList[i];
RelPath:=FileUtil.CreateRelativePath(PasFile, fConverter.fSettings.MainPath);
SubPath:=ExtractFilePath(RelPath);
FileName:=ExtractFileName(RelPath);
sUnitName:=ExtractFileNameOnly(FileName);
if (SubPath<>'') and (sUnitName<>'') then begin
// Map path by unit name.
fConverter.fCachedUnitNames[sUnitName]:=SubPath;
// Map real unit name by uppercase unit name.
fConverter.fCachedRealFileNames[UpperCase(sUnitName)]:=FileName;
end;
end;
end;
procedure TCacheUnitsThread.Execute; procedure TCacheUnitsThread.Execute;
// This assumes that cache is not used while updating it. // Scan for unit files. This assumes that cache is not used while updating it.
// The main GUI thread must wait for this thread before starting conversion. // The main GUI thread must wait for this thread before starting conversion.
function IsRootPath(APath: String): Boolean; function IsRootPath(APath: String): Boolean;
//crude function, it maybe needs support for UNC drives // Crude function, it maybe needs support for UNC drives
var var
D: String; D: String;
Len: Integer; Len: Integer;
@ -479,7 +457,8 @@ begin
if IsRootPath(fPath) then if IsRootPath(fPath) then
Sleep(1) // Let the main thread execute, avoid possible synchr. problems. Sleep(1) // Let the main thread execute, avoid possible synchr. problems.
else else
CacheUnitsInPath; // Scan for unit files. // Scan for files and store to fCachedUnitNames, path relative to fSettings.MainPath.
fSearcher.Search(fPath, '*.pas');
end; end;
{ TConvertDelphiUnit } { TConvertDelphiUnit }