Skip to content

Commit

Permalink
feat(developer): handle errors loading projects
Browse files Browse the repository at this point in the history
Relates to #9948.

Prevents loading of projects other than v1.0, v2.0, and also handles
error cases when attempting to load projects.
  • Loading branch information
mcdurdin committed Nov 13, 2023
1 parent c488460 commit 53b5793
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
14 changes: 13 additions & 1 deletion developer/src/tike/actions/dmActionsMain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ implementation
Keyman.System.KeyboardUtils,
Keyman.Developer.System.Project.Project,
Keyman.Developer.System.Project.ProjectFileType,
Keyman.Developer.System.Project.ProjectLoader,
Keyman.Developer.System.ServerAPI,
Keyman.Developer.UI.Project.ProjectFileUI,
Keyman.Developer.UI.Project.ProjectUI,
Expand Down Expand Up @@ -613,7 +614,18 @@ procedure TmodActionsMain.OpenProject(FileName: WideString);
if not SaveAndCloseAllFiles then Exit;
FreeGlobalProjectUI;
end;
LoadGlobalProjectUI(ptUnknown, FileName); // I4687
try
LoadGlobalProjectUI(ptUnknown, FileName); // I4687
except
on E:EProjectLoader do
begin
// Message will be displayed by LoadGlobalProjectUI
FreeGlobalProjectUI;
frmKeymanDeveloper.ShowProject;
frmKeymanDeveloper.UpdateCaption;
Exit;
end;
end;
frmKeymanDeveloper.ProjectMRU.Add(FGlobalProject.FileName);
frmKeymanDeveloper.ShowProject;
frmKeymanDeveloper.UpdateCaption;
Expand Down
13 changes: 12 additions & 1 deletion developer/src/tike/main/UfrmMain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ implementation
Keyman.Developer.System.Project.ProjectFile,
Keyman.Developer.System.Project.ProjectFileType,
Keyman.Developer.System.Project.WelcomeRenderer,
Keyman.Developer.System.Project.ProjectLoader,
Keyman.Developer.System.Project.ProjectLog,
Keyman.Developer.System.Project.XmlLdmlProjectFile,
Keyman.Developer.UI.Project.ProjectFileUI,
Expand Down Expand Up @@ -587,7 +588,17 @@ procedure TfrmKeymanDeveloper.FormCreate(Sender: TObject);
FActiveProject := '';

if FActiveProject <> '' then
LoadGlobalProjectUI(ptUnknown, FActiveProject);
begin
try
LoadGlobalProjectUI(ptUnknown, FActiveProject);
except
on E:EProjectLoader do
begin
// Message will be displayed by LoadGlobalProjectUI
FreeGlobalProjectUI;
end;
end;
end;

InitDock;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ interface
TProjectState = (psCreating, psReady, psLoading, psSaving, psDestroying);
TProjectType = (ptUnknown, ptKeyboard, ptLexicalModel); // distinct from utilfiletypes.TKeymanProjectType

TProjectVersion = (pv10, pv20);
TProjectVersion = (pvUnknown, pv10, pv20);

TProjectOptionsRecord = record
BuildPath: string;
Expand Down Expand Up @@ -114,7 +114,17 @@ TProjectOptions = class
function EqualsRecord(source: TProjectOptionsRecord): Boolean;
end;

const DefaultProjectOptions: array[TProjectVersion] of TProjectOptionsRecord = ((
const DefaultProjectOptions: array[TProjectVersion] of TProjectOptionsRecord = (
( // unknown version, this is unused
BuildPath: '';
SourcePath: '';
CompilerWarningsAsErrors: False;
WarnDeprecatedCode: False;
CheckFilenameConventions: False;
SkipMetadatafiles: False;
ProjectType: ptKeyboard;
Version: pvUnknown
), ( // 1.0
BuildPath: '';
SourcePath: '';
CompilerWarningsAsErrors: False;
Expand All @@ -123,7 +133,7 @@ TProjectOptions = class
SkipMetadatafiles: True;
ProjectType: ptKeyboard;
Version: pv10
), (
), ( // 2.0
BuildPath: '$PROJECTPATH/build';
SourcePath: '$PROJECTPATH/source';
CompilerWarningsAsErrors: False;
Expand Down Expand Up @@ -1472,12 +1482,13 @@ function ProjectVersionFromString(s: string): TProjectVersion;
begin
if SameText(s, '1.0') then Result := pv10
else if SameText(s, '2.0') then Result := pv20
else Result := pv10; // TODO is this adequate?
else Result := pvUnknown;
end;

function ProjectVersionToString(pv: TProjectVersion): string;
begin
case pv of
pvUnknown: Result := '';
pv10: Result := '1.0';
pv20: Result := '2.0';
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ procedure TProjectLoader.LoadProjectFromFile;
if node <> nil then
begin
if not VarIsNull(node.ChildValues['Version']) then
begin
FProject.Options.Version := ProjectVersionFromString(VarToStr(node.ChildValues['Version']));
if FProject.Options.Version = pvUnknown then
raise EProjectLoader.Create('This project format is version '+VarToStr(node.ChildValues['Version'])+', which is not supported by this version of Keyman Developer.');
end;

// Set default project options based on what we learned above
FProject.Options.Assign(DefaultProjectOptions[FProject.Options.Version]);
Expand Down

0 comments on commit 53b5793

Please sign in to comment.