In Java we can use annotations to generate code automatically at compile time, this feature is amazing for reducing bioler plate code. Many great libraries such as Retrofit, Room and AutoValue use it.
Does Dart have similar features?
Dart indeed allows for code-generation using the same principles.
An examples would be json_serializable, which generates fromJSON constructor and toJSON methods for classes inside yourfile.g.dart
In general, dart team provided an awesome tooling for code generation. If you want to create your own code generator, feel free to take a look at source_gen or simply build
Related
I am trying to utilize clang tooling library for the purpose of my future tool.
What I would like to do with this tool is:
1. parse all the source code (with includes) and detect any of my keywords in the comments (comments will be some kind of interface between the programmer and my tool, which will do various things with the rest of the source code according to commands placed in the comments).
2. according to commands from the source code, do some refactoring of it
The refactoring itself will be done using clang AST, like from example below:
http://eli.thegreenplace.net/2014/07/29/ast-matchers-and-clang-refactoring-tools
The thing I am looking for currently is how to parse the comments, within the same run of clang tooling procedures. I do not want to make separate step just for parsing the source code, because it have to be already done in tooling library.
Do you know how to somehow get the information about comments included in the source code I am parsing by tooling library?
Try the options -Wdocumentation and associated options (as -fparse-all-comments). If U use some tools (as clang-check or clang-tidy, adds these options in the compile commands db.
When using a third-party JavaScript library in my Dart project, I manually go through the library's documentation and iterate through its properties and methods to build the Dart code through a series of tedious context and callMethod calls. Has anyone figured out a way to automate this?
I tried to first find a command-line interface that introspects the JavaScript library so that I can auto-generate the Dart source code. I've been unsuccessful in my search.
I've tried to make my implementation of .d.ts -> dart2js annotations converter.
This is possible way to automate the process.
Please, see https://github.com/denis-aes/DefinitelyTyped.dart
Introspecting JS lib can be really hard due to the dynamic face of the JS language.
In the Typescript world there are *.d.ts files used to provide types to existing libraries. As far as I can tell most of those files are manually writen.
For now such a tool isn't yet available.
I would like to define my objects in a JSON file, and then instantiate them using Typhoon. Is this currently possible with Typhoon? I've downloaded the code from github, and looked through the code and docs, but I don't see a way.
Thanks in advance!
Since Typhoon 2.0 we only support the native format (recommended) along with auto-wiring macros. The main benefits are:
IDE refactoring and code-completion works without any additional plugins
No "magic strings" when wiring by reference.
Components can be resolved using the assembly interface. Since version 2.0 this includes supplying runtime arguments along with static dependencies.
In version 1.x, we supported XML along with the above styles, however it was not at all a popular feature. The main (valid) criticisms were that XML doesn't support the above benefits of the native style. This along with there being some overheads in maintenance lead us to decide to discontinue support in version 2.0.
There was one benefit - the ability to define assemblies at runtime. The closest thing that we have at the moment is Typhoon Config, which allows defining configurations in a text file.
Proceeding with JSON:
It would be quite simple to define a JSON parser.
Create a similar class to v1.8.2's XML parser.
Register the components from the parser. Either manually or by creating a TyphoonComponentFactory sub-class.
Unless you have a strong reason for using JSON, we recommend the native style.
Years ago, I wrote a code template that took a few simple parameters (points) and produced a class skeleton. Since some of the method bodies had code in them, I couldn't call InvokeClassCompletion and just placed the implementations with the declarations (user had to move them).
It has occurred to me that I can write a code template script engine to insert these methods in the correct position, but I don't see how I'm supposed to navigate the file. There are declarations for IOTAEditReader and IOTAEditWriter in ToolsAPI.pas, but I'm guessing there is a step missing - certainly I don't need to manually parse the Delphi code just to find the implementation section?
Does anyone have experience with it?
The IDE has plenty of parsers in it, but not one is made available for IDE plugins (ToolsAPI). So you have to write your own parser or use an existing one like http://delphiblog.twodesk.com/using-the-castalia-delphi-parser
You might find that GExperts or CNPack contains almost all the code you need, in one of its editor wizards.
If not, then, those two are the best reference I know for writing IDE plugins. As far as writing parsers, see Andreas' answer.
If you just want better code templates, you could consider buying Castalia, as it has an improved code template feature over the builtin delphi IDE plugin features. Castalia internally uses the Castalia delphi parser mentioned by andreas. It's quite good.
There is a large project, and I need to see the hierarchy of Units which each file references in it's USES clause.
ex.
Project Source (program.dpk)
HelperUnit Forms ThirdPartyUnit
MyUtils MyConsts MyDownload TPShellShock TPWhatever
How can I see this dependency tree of sorts? I remember there being some feature like this in one of the newer Delphi versions, but I don't remember what it was called. It may have been a plugin.
Any hints on how to do this would be great. I started writing my own program to do it but I've already wasted like 3 hours and am still working on the parser for pas files.
There also is a unit dependancy analyzer built inside of GExperts. Look for the option labled "Project Dependencies". This does work from an expert within Delphi against the currently loaded project.
I don't know a IDE integrated tool to do what you want, but you shall take a look at Unit Dependency Analyzer, from ModelMaker. The tool is free of charge.
You've got such graphs created in an automated manner by our Open Source documentation tool, called SynProject.
It uses internal a fork of PasDoc to parse the interface part of your project units, then extract the comments to get detailed info about each class/method/function/unit.
There are both unit dependencies diagrams and class hierarchy diagrams created from this data.
Then this information is merged into a whole technical documentation process, starting from Requirements, then Architecture, then Design, then Validation Plan, then Testing, then Release.
See http://synopse.info/fossil/wiki?name=SynProject
Icarus is another tool to analyzing your "uses" dependencies; it has a great feature that lists units that don't need to be in your uses clauses.
http://www.peganza.com/
It's free.