You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+80-14Lines changed: 80 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,12 +21,26 @@ The Scribble GYB configuration simplifies boilerplate code generation for Scribb
21
21
22
22
3.4 [Loops](#loops)
23
23
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)
25
27
26
28
4.1 [Declaring global variables](#declaring-global-utilities-in-gyb-utils-py)
27
29
28
30
4.2 [Importing and Using Utilities](#importing-and-using-utilities-in-gyb-templates)
29
31
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
+
30
44
## Overview
31
45
32
46
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 {
198
212
@end
199
213
```
200
214
215
+
## Supported Types
216
+
201
217
## Using gyb_utils.py for Shared Utilities
202
218
203
219
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
231
247
232
248
## Using GYB with Objective-C and Objective-C++
233
249
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.
234
273
274
+
```cpp
275
+
%{
276
+
member_types = ['int', 'float', 'std::string']
277
+
member_names = ['id', 'score', 'name']
278
+
}%
235
279
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
+
```
236
293
237
-
### Running Scribble-GYB
294
+
The following template demonstrates generating member variables and constructors for a C++ class.
238
295
239
-
To process your GYB templates and generate Swift code, use the generate-sources.sh script.
296
+
## Running Scribble-GYB
240
297
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.
242
299
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
244
301
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.
246
303
247
-
##### Running the Script
304
+
| Language | GYB Extension |
305
+
| :------: | :------------: |
306
+
| Swift | `.swift.gyb` |
307
+
| ObjC | `.m.gyb` |
308
+
| ObjC++ | `.mm.gyb` |
248
309
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.
250
311
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
+
```
252
316
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
257
320
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.
0 commit comments