Skip to content

Commit 02b16f5

Browse files
authored
(chore): Update README.md
1 parent 430408c commit 02b16f5

File tree

1 file changed

+80
-14
lines changed

1 file changed

+80
-14
lines changed

README.md

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,26 @@ The Scribble GYB configuration simplifies boilerplate code generation for Scribb
2121

2222
3.4 [Loops](#loops)
2323

24-
4. [Using gyb_utils.py for Shared Utilities](#using-gyb-utils-py-for-shared-utilities)
24+
4. [Supported Types](#supported-types)
25+
26+
5. [Using gyb_utils.py for Shared Utilities](#using-gyb-utils-py-for-shared-utilities)
2527

2628
4.1 [Declaring global variables](#declaring-global-utilities-in-gyb-utils-py)
2729

2830
4.2 [Importing and Using Utilities](#importing-and-using-utilities-in-gyb-templates)
2931

32+
6. [Using GYB with ObjC and ObjC++](#using-gyb-with-objective-c-and-objective-c++)
33+
34+
7. [Running Scribble-GYB](#running-scribble-gyb)
35+
36+
7.1 [Steps to generate Source files](#steps-to-generate-code)
37+
38+
8. [Best Practices](#best-practices)
39+
40+
9. [Copyright Notice](#copyright-notice)
41+
42+
10. [Support Us](#support-us)
43+
3044
## Overview
3145

3246
GYB (Generate Your Boilerplate) is a tool used to generate source code by mixing Python code with template files. It allows developers to avoid redundancy by dynamically generating patterns across different files or languages. Scribble GYB offers a powerful way to ensure clean, consistent code while minimizing manual coding effort.
@@ -198,6 +212,8 @@ struct User {
198212
@end
199213
```
200214
215+
## Supported Types
216+
201217
## Using gyb_utils.py for Shared Utilities
202218
203219
The `gyb_utils.py` file in the Scribble GYB configuration allows you to declare global variables, helper functions, or imports that can be reused across different GYB templates. This is especially useful when you have common logic or variables that need to be shared.
@@ -231,31 +247,81 @@ This allows you to maintain clean, reusable code in your GYB templates, avoiding
231247
232248
## Using GYB with Objective-C and Objective-C++
233249
250+
GYB can be used to generate code for Objective-C (`.m.gyb`) and Objective-C++ (`.mm.gyb`) files. This is particularly useful when generating boilerplate getter and setter methods, constants, or configuration code.
251+
252+
```objc
253+
%{
254+
properties = ['name', 'age', 'email']
255+
}%
256+
257+
@implementation User {
258+
% for prop in properties:
259+
NSString *_${prop};
260+
% end
261+
}
262+
263+
% for prop in properties:
264+
- (NSString *)${prop} {
265+
return _${prop};
266+
}
267+
% end
268+
269+
@end
270+
```
271+
272+
This example generates getter methods dynamically based on the properties defined in a list.
234273
274+
```cpp
275+
%{
276+
member_types = ['int', 'float', 'std::string']
277+
member_names = ['id', 'score', 'name']
278+
}%
235279
280+
class Player {
281+
% for member_type, member_name in zip(member_types, member_names):
282+
${member_type} ${member_name};
283+
% end
284+
285+
public:
286+
Player() {
287+
% for member_name in member_names:
288+
this->${member_name} = {};
289+
% end
290+
}
291+
};
292+
```
236293
237-
### Running Scribble-GYB
294+
The following template demonstrates generating member variables and constructors for a C++ class.
238295
239-
To process your GYB templates and generate Swift code, use the generate-sources.sh script.
296+
## Running Scribble-GYB
240297
241-
#### Generate Source code
298+
To process your `.gyb` templates and generate source files, you must use the `generate-sources.sh` script provided in the repository.
242299
243-
GYB is a preprocessor, so you need to run it to generate the actual Swift source code files. Use the `generate-sources.sh` script provided in this repository.
300+
### Steps to Generate Code
244301
245-
The `generate-sources.sh` script processes GYB templates and generates the corresponding Swift source code files.
302+
1. Ensure your GYB template files are in place with appropriate extensions (`.swift.gyb`, `.m.gyb`, `.mm.gyb`). To review the appropiate extensions for the used language look at the table below.
246303
247-
##### Running the Script
304+
| Language | GYB Extension |
305+
| :------: | :------------: |
306+
| Swift | `.swift.gyb` |
307+
| ObjC | `.m.gyb` |
308+
| ObjC++ | `.mm.gyb` |
248309
249-
1. **Ensure GYB Templates are in Place:** Place your GYB template files in the `Sources` or `Tests` directory either with a `.swift.gyb` (Swift), `.m.gyb` (ObjC) or `.mm.gyb` (ObjC++) extension.
310+
2. Run the `generate-sources.sh` script to generate the Swift, Objective-C, or Objective-C++ source files.
250311
251-
2. **Execute the Script:** Run the generate-sources.sh script to generate Swift source files from your GYB templates.
312+
```sh
313+
chmod +x generate-sources.sh
314+
./generate-sources.sh
315+
```
252316
253-
```shell
254-
chmod +x generate-sources.sh
255-
./generate-sources.sh
256-
```
317+
The generated files will be saved in subdirectories under Sources or Tests, mirroring the location of the original GYB templates.
318+
319+
## Best Practices
257320
258-
3. Verify Generated Files: The generated Swift, ObjC or ObjC++ files will be placed in autogenerated subdirectories under Sources or Tests wherever your gyb template is placed.
321+
- **Modular Templates:** Keep your GYB templates as modular as possible. Small, reusable templates improve maintainability.
322+
- **Consistent Naming:** Use clear and consistent naming conventions for your GYB variables and placeholders to improve readability.
323+
- **Leverage gyb_utils.py:** Use gyb_utils.py to declare shared utilities or constants across multiple templates, reducing redundancy.
324+
- **Avoid Complex Logic:** Keep Python logic in GYB templates straightforward. Complex business logic should be placed in helper functions or scripts outside of the GYB templates.
259325
260326
## Copyright Notice
261327

0 commit comments

Comments
 (0)