Skip to content

Commit

Permalink
Wizard generates sample entity and service container
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Apr 29, 2024
1 parent 9b079a9 commit 91a019c
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 54 deletions.
206 changes: 174 additions & 32 deletions ideexpert/DMVC.Expert.CodeGen.Commands.pas
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ TUnitJSONRPCDeclarationCommand = class(TCustomCommand)
); override;
end;

TUnitServicesDeclarationCommand = class(TCustomCommand)
public
procedure ExecuteInterface(
Section: TStringBuilder;
Model: TJSONObject
); override;
procedure ExecuteImplementation(
Section: TStringBuilder;
Model: TJsonObject
); override;
end;

TUnitMustacheHelpersDeclarationCommand = class(TCustomCommand)
public
procedure ExecuteInterface(
Expand Down Expand Up @@ -286,18 +298,23 @@ procedure TUnitProgramCommand.ExecuteInterface(
.AppendLine(' Web.WebReq,')
.AppendLine(' Web.WebBroker,')
.AppendLine(' IdContext,')
.AppendLine(' IdHTTPWebBrokerBridge,');
.AppendLine(' IdHTTPWebBrokerBridge,')
.AppendLine(' MVCFramework,')
.AppendLine(' MVCFramework.Logger,')
.AppendLine(' MVCFramework.DotEnv,')
.AppendLine(' MVCFramework.Commons,');
if Model.B[TConfigKey.program_ssv_mustache] then
begin
Section
.AppendLine(' MVCFramework.View.Renderers.Mustache,')
.AppendLine(' SynMustache,');
end;
if Model.B[TConfigKey.program_service_container_generate] then
begin
Section
.AppendLine(' MVCFramework.Container,')
end;
Section
.AppendLine(' MVCFramework,')
.AppendLine(' MVCFramework.Logger,')
.AppendLine(' MVCFramework.DotEnv,')
.AppendLine(' MVCFramework.Commons,')
.AppendLine(' MVCFramework.Signal;')
.AppendLine()
.AppendLine('{$R *.res}')
Expand Down Expand Up @@ -331,9 +348,15 @@ procedure TUnitControllerCommand.ExecuteInterface(Section: TStringBuilder;
.Append(' MVCFramework, MVCFramework.Commons, ');
if Model.B[TConfigKey.entity_generate] then
begin
Section.Append('MVCFramework.Nullables, ');
Section
.Append('MVCFramework.Nullables, ')
.Append(Model[TConfigKey.entity_unit_name] + ', ');
end;
if Model.B[TConfigKey.program_service_container_generate] then
begin
Section
.Append(Model[TConfigKey.program_service_container_unit_name] + ', ');
end;

Section
.AppendLine('MVCFramework.Serializer.Commons, System.Generics.Collections;')
.AppendLine
Expand All @@ -346,6 +369,8 @@ procedure TUnitControllerEntityDeclarationCommand.ExecuteImplementation(
inherited;
if not Model.B[TConfigKey.entity_generate] then Exit;
Section
.AppendLine('implementation')
.AppendLine
.AppendLine('constructor ' + Model[TConfigKey.entity_classname] + '.Create(ID: Integer; FirstName, LastName: String; DOB: TDate);')
.AppendLine('begin')
.AppendLine(' inherited Create;')
Expand All @@ -354,6 +379,9 @@ procedure TUnitControllerEntityDeclarationCommand.ExecuteImplementation(
.AppendLine(' fLastName := LastName;')
.AppendLine(' fDOB := DOB;')
.AppendLine('end;')
.AppendLine
.AppendLine
.AppendLine('end.')
end;

{ TUnitControllerEntitiesCommand }
Expand All @@ -366,6 +394,14 @@ procedure TUnitControllerEntityDeclarationCommand.ExecuteInterface(Section: TStr

CheckFor('entity.classname', Model);
Section
.AppendLine('unit ' + Model[TConfigKey.entity_unit_name] + ';')
.AppendLine
.AppendLine('interface')
.AppendLine
.AppendLine('uses')
.AppendLine(' MVCFramework.Nullables, MVCFramework.Serializer.Commons;')
.AppendLine
.AppendLine('type')
.AppendLine(' [MVCNameCase(ncCamelCase)]')
.AppendLine(' ' + Model[TConfigKey.entity_classname] + ' = class')
.AppendLine(' private')
Expand Down Expand Up @@ -462,33 +498,57 @@ procedure TUnitControllerControllerDeclarationCommand.ExecuteImplementation(

if Model.B[TConfigKey.controller_crud_methods_generate] then
begin
Section
.AppendLine
.AppendLine('//Sample CRUD Actions for a "People" entity')
.AppendLine('function ' + Model[TConfigKey.controller_classname] + '.GetPeople: IMVCResponse;')
.AppendLine('var')
.AppendLine(' lPeople: TObjectList<TPerson>;')
.AppendLine('begin');
if Model.B[TConfigKey.controller_actions_profiling_generate] then
if Model.B[TConfigKey.program_service_container_generate] then
begin
Section
.AppendLine('{$IF CompilerVersion >= 34} //SYDNEY+')
.AppendLine(' var lProf := Profiler.Start(Context.ActionQualifiedName);')
.AppendLine('{$ENDIF}')
.AppendLine;
.AppendLine
.AppendLine('//Sample CRUD Actions for a "People" entity (with service injection)')
.AppendLine('function ' + Model[TConfigKey.controller_classname] + '.GetPeople(PeopleService: IPeopleService): IMVCResponse;')
.AppendLine('begin');
if Model.B[TConfigKey.controller_actions_profiling_generate] then
begin
Section
.AppendLine('{$IF CompilerVersion >= 34} //SYDNEY+')
.AppendLine(' var lProf := Profiler.Start(Context.ActionQualifiedName);')
.AppendLine('{$ENDIF}')
.AppendLine;
end;
Section
.AppendLine(' Result := OkResponse(PeopleService.GetAll);')
.AppendLine('end;')
end
else
begin
Section
.AppendLine
.AppendLine('//Sample CRUD Actions for a "People" entity (no service injection)')
.AppendLine('function ' + Model[TConfigKey.controller_classname] + '.GetPeople: IMVCResponse;')
.AppendLine('var')
.AppendLine(' lPeople: TObjectList<TPerson>;')
.AppendLine('begin');
if Model.B[TConfigKey.controller_actions_profiling_generate] then
begin
Section
.AppendLine('{$IF CompilerVersion >= 34} //SYDNEY+')
.AppendLine(' var lProf := Profiler.Start(Context.ActionQualifiedName);')
.AppendLine('{$ENDIF}')
.AppendLine;
end;
Section
.AppendLine(' lPeople := TObjectList<TPerson>.Create(True);')
.AppendLine(' try')
.AppendLine(' lPeople.Add(TPerson.Create(1, ''Peter'',''Parker'', EncodeDate(1965, 10, 4)));')
.AppendLine(' lPeople.Add(TPerson.Create(2, ''Bruce'',''Banner'', EncodeDate(1945, 9, 6)));')
.AppendLine(' lPeople.Add(TPerson.Create(3, ''Reed'',''Richards'', EncodeDate(1955, 3, 7)));')
.AppendLine(' Result := OkResponse(lPeople);')
.AppendLine(' except')
.AppendLine(' lPeople.Free;')
.AppendLine(' raise;')
.AppendLine(' end;')
.AppendLine('end;')
end;

Section
.AppendLine(' lPeople := TObjectList<TPerson>.Create(True);')
.AppendLine(' try')
.AppendLine(' lPeople.Add(TPerson.Create(1, ''Peter'',''Parker'', EncodeDate(1965, 10, 4)));')
.AppendLine(' lPeople.Add(TPerson.Create(2, ''Bruce'',''Banner'', EncodeDate(1945, 9, 6)));')
.AppendLine(' lPeople.Add(TPerson.Create(3, ''Reed'',''Richards'', EncodeDate(1955, 3, 7)));')
.AppendLine(' Result := OkResponse(lPeople);')
.AppendLine(' except')
.AppendLine(' lPeople.Free;')
.AppendLine(' raise;')
.AppendLine(' end;')
.AppendLine('end;')
.AppendLine
.AppendLine('function ' + Model[TConfigKey.controller_classname] + '.GetPerson(ID: Integer): TPerson;')
.AppendLine('begin');
Expand Down Expand Up @@ -595,8 +655,20 @@ .AppendLine(' ' + Model[TConfigKey.controller_classname] + ' = class(TMVCCo
Section
.AppendLine(' //Sample CRUD Actions for a "People" entity')
.AppendLine(' [MVCPath(''/people'')]')
.AppendLine(' [MVCHTTPMethod([httpGET])]')
.AppendLine(' function GetPeople: IMVCResponse;')
.AppendLine(' [MVCHTTPMethod([httpGET])]');

if Model.B[TConfigKey.program_service_container_generate] then
begin
Section
.AppendLine(' function GetPeople([MVCInject] PeopleService: IPeopleService): IMVCResponse;');
end
else
begin
Section
.AppendLine(' function GetPeople: IMVCResponse;')
end;

Section
.AppendLine
.AppendLine(' [MVCPath(''/people/($ID)'')]')
.AppendLine(' [MVCHTTPMethod([httpGET])]')
Expand Down Expand Up @@ -946,6 +1018,7 @@ procedure TUnitMainBeginEndCommand.ExecuteImplementation(Section: TStringBuilder
.AppendLine(' end;')
.AppendLine('{$ENDIF}')
.AppendLine;

if Model.B[TConfigKey.program_ssv_mustache] then
begin
Section
Expand All @@ -957,6 +1030,15 @@ procedure TUnitMainBeginEndCommand.ExecuteImplementation(Section: TStringBuilder
.AppendLine(' end;')
.AppendLine;
end;

if Model.B[TConfigKey.program_service_container_generate] then
begin
Section
.AppendLine(' RegisterServices(DefaultMVCServiceContainer);')
.AppendLine(' DefaultMVCServiceContainer.Build;')
.AppendLine;
end;

Section
.AppendLine(' RunServer(dotEnv.Env(''dmvc.server.port'', ' + Model[TConfigKey.program_default_server_port] + '));')
.AppendLine(' except')
Expand Down Expand Up @@ -1142,4 +1224,64 @@ .AppendLine(' TMyMustacheHelpers = class sealed')
.AppendLine;
end;

{ TUnitServicesDeclarationCommand }

procedure TUnitServicesDeclarationCommand.ExecuteImplementation(
Section: TStringBuilder; Model: TJsonObject);
begin
Section
.AppendLine('implementation')
.AppendLine
.AppendLine('uses')
.AppendLine(' System.SysUtils;')
.AppendLine
.AppendLine('procedure RegisterServices(Container: IMVCServiceContainer);')
.AppendLine('begin')
.AppendLine(' Container.RegisterType(TPeopleService, IPeopleService, TRegistrationType.SingletonPerRequest);')
.AppendLine(' // Register other services here')
.AppendLine('end;')
.AppendLine
.AppendLine('function TPeopleService.GetAll: TObjectList<TPerson>;')
.AppendLine('begin')
.AppendLine(' Result := TObjectList<TPerson>.Create;')
.AppendLine(' Result.AddRange([')
.AppendLine(' TPerson.Create(1, ''Henry'', ''Ford'', EncodeDate(1863, 7, 30)),')
.AppendLine(' TPerson.Create(2, ''Guglielmo'', ''Marconi'', EncodeDate(1874, 4, 25)),')
.AppendLine(' TPerson.Create(3, ''Antonio'', ''Meucci'', EncodeDate(1808, 4, 13)),')
.AppendLine(' TPerson.Create(4, ''Michael'', ''Faraday'', EncodeDate(1867, 9, 22))')
.AppendLine(' ]);')
.AppendLine('end;')
.AppendLine
.AppendLine
.AppendLine('end.');
end;

procedure TUnitServicesDeclarationCommand.ExecuteInterface(
Section: TStringBuilder; Model: TJSONObject);
begin
Section
.AppendLine('unit ' + Model[TConfigKey.program_service_container_unit_name] + ';')
.AppendLine
.AppendLine('interface')
.AppendLine
.AppendLine('uses')
.AppendLine(' ' + Model[TConfigKey.entity_unit_name] + ',')
.AppendLine(' MVCFramework.Container, System.Generics.Collections;')
.AppendLine
.AppendLine('type')
.AppendLine(' IPeopleService = interface')
.AppendLine(' [''' + TGUID.NewGuid.ToString + ''']')
.AppendLine(' function GetAll: TObjectList<TPerson>;')
.AppendLine(' end;')
.AppendLine
.AppendLine(' TPeopleService = class(TInterfacedObject, IPeopleService)')
.AppendLine(' protected')
.AppendLine(' function GetAll: TObjectList<TPerson>;')
.AppendLine(' end;')
.AppendLine
.AppendLine('procedure RegisterServices(Container: IMVCServiceContainer);')
.AppendLine;

end;

end.
20 changes: 17 additions & 3 deletions ideexpert/DMVC.Expert.Commands.Templates.pas
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ procedure FillWebModuleTemplates(Gen: TMVCCodeGenerator);
procedure FillWebModuleDFMTemplates(Gen: TMVCCodeGenerator);
procedure FillJSONRPCTemplates(Gen: TMVCCodeGenerator);
procedure FillMustacheTemplates(Gen: TMVCCodeGenerator);
procedure FillEntitiesTemplates(Gen: TMVCCodeGenerator);
procedure FillServicesTemplates(Gen: TMVCCodeGenerator);

implementation

Expand All @@ -55,7 +57,6 @@ procedure FillControllerTemplates(Gen: TMVCCodeGenerator);
begin
Gen.Commands.AddRange([
TUnitControllerCommand.Create,
TUnitControllerEntityDeclarationCommand.Create,
TUnitControllerControllerDeclarationCommand.Create,
TUnitFooterCommand.Create
]);
Expand All @@ -82,13 +83,26 @@ procedure FillWebModuleDFMTemplates(Gen: TMVCCodeGenerator);
]);
end;



procedure FillMustacheTemplates(Gen: TMVCCodeGenerator);
begin
Gen.Commands.AddRange([
TUnitMustacheHelpersDeclarationCommand.Create
]);
end;

procedure FillServicesTemplates(Gen: TMVCCodeGenerator);
begin
Gen.Commands.AddRange([
TUnitServicesDeclarationCommand.Create
]);
end;

procedure FillEntitiesTemplates(Gen: TMVCCodeGenerator);
begin
Gen.Commands.AddRange([
TUnitControllerEntityDeclarationCommand.Create
]);
end;


end.
3 changes: 3 additions & 0 deletions ideexpert/DMVC.Expert.Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ TConfigKey = class sealed
program_msheap='program.msheap';
program_dotenv='program.dotenv';
program_ssv_mustache='program.ssv.mustache';
program_service_container_generate = 'program.service.container.generate';
program_service_container_unit_name = 'program.service.container.unit_name';
mustache_helpers_unit_name = 'mustache.helpers_unit_name';
controller_unit_name='controller.unit_name';
controller_classname= 'controller.classname';
Expand All @@ -67,6 +69,7 @@ TConfigKey = class sealed
controller_actions_profiling_generate= 'controller.actions.profiling.generate';
entity_generate= 'entity.generate';
entity_classname= 'entity.classname';
entity_unit_name='entity.unit_name';
jsonrpc_generate= 'jsonrpc.generate';
jsonrpc_classname= 'jsonrpc.classname';
jsonrpc_unit_name='jsonrpc.unit_name';
Expand Down
Loading

0 comments on commit 91a019c

Please sign in to comment.