Delphi Script Engine auto generate code based on file - delphi

Is there a way in which you can generate Delphi code from a file type which you define and know the definition of the language inside?
Basically I would like to perform the same operation as occurs in C# with some tools. Something like SpecFlow where you define a .feature file and it generates the NUnit code to match the spec in that file.
Also a way to hook this in for each compile, and to have it automatically hook into the file types extension name. This means that when you add a .feature file into the project, it knows to generate a pas file for that .feature file based on the contents.
The only way I can think of doing this at present would be to write an extension for Delphi. Is there something simpler that I am missing?
I have found Delphi Live Template Engine by Nick Hodges which seems to get me part of the way, however the auto file generation and linkage is the missing piece.

Related

Extracting project file from delphi.exe

I have all the files needed to open the unit and code but I can't make any changes or compile because the Project.dproj and Project.dpr files are missing. However, I have the .exe file. Is there any way by which the aforementioned files can be extracted from the .exe file?
If you have all of the source files, meaning the .pas and .dfm files, then the first thing I'd do is add them all into a new project and try to compile it. It'll either work, meaning you've got everything you need, or it will generate errors.
There would be three kinds of errors:
The first is the most likely -- the forms might use components that you don't have installed. This will be obivous if you try to open the forms in the IDE. If they open without fanfare, GREAT! If you get a warning box saying "Cannot locate component: remove references to it?" or something like that, then you're probably hosed.
The second is version-specific errors -- stuff that's old and crufty and no longer supported by the language.
Third, the program may use run-time libraries that you don't have.
The compiler adds RTTI metadata, including unit names, when it builds, and if you know about how the RTTI tables are laid out it's possible to extract this. The unit list is the principal component of the DPR file, but it takes some serious work to access it and I'm not aware of any tools out there that read the RTTI tables that have been kept up to date beyond the Delphi 7 era.
As for the .dproj file, you're out of luck. That doesn't actually contain any code that gets "compiled in" to the EXE; it's a set of build instructions for how to produce the EXE. But if the Delphi IDE doesn't have one, it can generate a default .dproj from the .dpr, if you can produce that.
I'm curious, though. How did you obtain the .pas files but not the .dpr?

Storing scripts as a resource

I have a few scripts that I would like my program to work with. However, I would like to know if it is possible for me to store these scripts (eg. batch, javascript, vb scripts etc) in my application as a resource.
How would I go about doing this?
You can store your scripts in resource files. But for example Batch Files need to be file on disk, so you will need to unpack them before working.
There are components to store arbitrary file/files or string in DFM
For example there are such in rxLib/JediVCL but i believe many VCL libs have one or another kind of DFM Storage component.
For example i used to store Firebird Embedded database in DFM to save it into TEMP and use while running.
However that is akin for manual re-reading file into DFM each time you update it. Rather annoying to say sincerely.
One more approach is linking text into resources. You can look into DUnit sources to see how it was done. You would also have .rc file included in project, so that it would be compiler into .res when making .exe
This approach is fragile towards ansi/unicode text interpretations.
Frankly, before i found DUnit in Delphi XE2 (it was disabled due to IDE bug) i tried to make SF's vanilla DUnit to run there. And i failed - the non-unicode text files linked into resource was totally corrupt when reading with unicode-enabled Delphi.
Look here and there, try both approaches and choose the one that suits you more.
Here is a good article that explains how you can add almost anything in a resource file and compile it with your application: http://delphi.about.com/od/objectpascalide/a/embed_resources.htm

How to keep uses list of several projects identical

I have a client application and a test application. If someone adds/removes units to the client application, I would like the same changes to happen to the test application.
I can think of 3 ways to do it, but all have drawbacks
1. Manually update the Test project uses list in the dpr.
The problem here is obvious, requires manual intervention per project.
2. Use a shared .inc file that contains the list of used units (list of frmXXX in '\forms\frmXXX.pas'...)
IDE doesn't like .inc files in the project file and would again require manual work to maintain
3. Same as #2, but use a shared unit instead of .inc file. So instead of updating the .inc you update the shared .pas
IDE would not really consider the files used by a shared unit as files in the project and they wouldn't get listed in View Unit dialog
Are there any other ways to keep uses lists of multiple projects in sync that I'm missing ?
Currently using D2007 but hopefully that doesn't matter.
You could use a build tool like Apache Ant, maintain the unit names in the script file (or a configuration file), and let Ant replace a placeholder in all *.dpr files using the replace task.
This would regenerate the dpr file automatically and keep them in sync.

Include files in ridl

The Delphi project I'm working on requires me to create a very large type library in order to add COM support. The problem is with the type library/ridl editor in Delphi is that it becomes difficult to manage (from a programmer point of view) such a large file, and I'd like to split it along functional lines. The problem I have is that I cannot see a way to include one ridl file in another. I'm aware of the importlib function, but I also cannot find a way to create more than one type library in the same project, or create a tlb file from within delphi. Can anyone give me ideas on how to better manage this?
Standard IDL has an include statement for pulling in other files into the TypeLibrary compilation. RIDL stands for REDUCED IDL, so it might not support it, though. You will have to try it and see.

Set Delphi 7 project "Version Info" from code?

Is it possible to set the "Version Info" settings from code? I'd like to set the version numbering and the value for the "Comments" property without using the project options dialog.
Rather than editing the binary RES file that the IDE manages for you, you may find it easier to maintain a text RC file that contains the version-info resource. Write the resource-script file, and then include it in your project with a line like this:
{$R resources.res resources.rc}
You should remove the {$R *.RES} line from your project's DPR file, or else you'll get duplicate version resources. Do not use your project name as the name of your custom resource file; that name is reserved by the IDE.
The IDE-managed resource file also contains the project icon, so you'll need to include that in your resource script as well.
You can edit the resource script by hand, or you can write a program to edit it or regenerate it as one of your build steps.
Using a text resource script has the added side effect that it's easier to track changes to it in whatever source-control system you use, like CVS.
Solution would be to edit a project resource file. Check this c++ example http://www.codeproject.com/KB/cpp/UpdateVersion.aspx
I would recommend using a build tool, like FinalBuilder (which I use a lot), which can do this for you according to an appropriate scheme. Then you know that the build options are all as you wish, your numbers are incremented appropriately, and you can do things like upload to FTP site and lots more. In my scripts, the build number is included all the way through from EXE to installer and all.
You have to write wizard for that. Check out IOTAProjectOptions in D7IOTA.HLP file, source code of ToolsAPI unit and this thread
You'll need to overwrite the application resources.
Good starting point would be probably XN Resource Editor which comes with source code http://www.wilsonc.demon.co.uk/d10resourceeditor.htm
worth reading is also Inno Setup (which does set icon for executable output) - http://jrsoftware.org/isdl.php

Resources