spiegel-keyman/developer/src/tike/main/Keyman.Developer.System.ProjectOwningFile.pas
Marc Durdin 39b7c882a3 feat(developer): Multi-process model for projects 🦕
This is a bit of an omnibus commit, apologies for that. This commit
moves Keyman Developer to a proper multi-process model, where editing
files from multiple projects is handled much more cleanly, with each
project loaded in a separate process.

Files that are edited outside of a project structure are loaded into a
'temporary project' in a single process, which provides a pathway for
existing users who may have legacy files outside the normal Keyman
Developer project model.

There are several components to this:

1. Inter-process communication (Multiprocess, CopyDataHelper units and
   multiprocess test project). These modules establish a method of
   enumerating running Keyman Developer instances (with EnumWindow),
   recording relevant metadata for each instance (by thread id) in the
   registry (registry used because it manages contention without
   additional effort from us), and communicating between processes with
   WM_COPYDATA.
2. Command-line parsing (TikeCommandLine). Determines project ownership
   (ProjectOwningFile) for each filename passed on the command-line, and
   passes these over to existing instances of Keyman Developer that have
   that project loaded, or starts new instances as needed.
3. Temporary project management. Mostly in ProjectUI.

Also, moved GlobalProjectStateWnd management out of ProjectFile.pas and
into Project.pas, alongside other global project variables.

A follow-up will add functionality to determine if a file opened within
the Keyman Developer UI should open in the same instance or in a
separate instance (see TfrmKeymanDeveloper.OpenFileInProject). This will
use the same methodology as TikeCommandLine does now, so may involve
further refactoring.

This commit establishes the idea of 'keyman.kpj' as a future default
filename for Keyman Developer projects, but does not enable it, in
ProjectOwningFile. It is planned to introduce this fixed filename in
version 18.0 (see #10113).
2023-12-01 11:48:09 +07:00

82 lines
2 KiB
ObjectPascal

unit Keyman.Developer.System.ProjectOwningFile;
interface
function FindOwnerProjectForFile(const filename: string): string;
implementation
uses
System.SysUtils,
Keyman.Developer.System.Project.ProjectFile;
function CheckOwnerProjectForFile(const project, filename: string): Boolean; forward;
{
#2761
Given an arbitrary/path/to/file.kmn, search for a project:
[* arbitrary/path/to/keyman.kpj]
* arbitrary/path/to/to.kpj (to.kpj matches folder name)
[* arbitrary/path/keyman.kpj]
* arbitrary/path/path.kpj (path.kpj matches folder name)
For each project file, if found, verify that file.kmn is in the project:
* For a v2.0 project, file.kmn is in the project if it is in $SOURCEPATH
* For a v1.0 project, file.kmn must be listed explicitly.
}
function FindOwnerProjectForFile(const filename: string): string;
var
path: string;
begin
path := ExtractFilePath(filename);
{ TODO: 18.0, see #10113
// Check arbitrary/path/to/keyman.kpj
Result := path + C_ProjectStandardFilename;
if CheckOwnerProjectForFile(Result, filename) then
Exit;
}
// Check arbitrary/path/to/to.kpj
Result := path + ExtractFileName(ExcludeTrailingPathDelimiter(path)) + '.kpj';
if CheckOwnerProjectForFile(Result, filename) then
Exit;
path := ExtractFilePath(ExcludeTrailingPathDelimiter(path));
{ TODO: 18.0, see #10113
// Check arbitrary/path/keyman.kpj
Result := path + C_ProjectStandardFilename;
if CheckOwnerProjectForFile(Result, filename) then
Exit;
}
// Check arbitrary/path/path.kpj
Result := path + ExtractFileName(ExcludeTrailingPathDelimiter(path)) + '.kpj';
if CheckOwnerProjectForFile(Result, filename) then
Exit;
Result := '';
end;
function CheckOwnerProjectForFile(const project, filename: string): Boolean;
var
p: TProject;
begin
if not FileExists(project) then
Exit(False);
if SameFileName(project, filename) then
Exit(True);
p := TProject.Create(ptUnknown, project, True);
try
Result := p.Files.IndexOfFileName(filename) >= 0;
finally
p.Free;
end;
end;
end.