Skip to content

Commit

Permalink
(chore): Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
N3v1 authored Oct 22, 2024
1 parent 430408c commit 02b16f5
Showing 1 changed file with 80 additions and 14 deletions.
94 changes: 80 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,26 @@ The Scribble GYB configuration simplifies boilerplate code generation for Scribb

3.4 [Loops](#loops)

4. [Using gyb_utils.py for Shared Utilities](#using-gyb-utils-py-for-shared-utilities)
4. [Supported Types](#supported-types)

5. [Using gyb_utils.py for Shared Utilities](#using-gyb-utils-py-for-shared-utilities)

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

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

6. [Using GYB with ObjC and ObjC++](#using-gyb-with-objective-c-and-objective-c++)

7. [Running Scribble-GYB](#running-scribble-gyb)

7.1 [Steps to generate Source files](#steps-to-generate-code)

8. [Best Practices](#best-practices)

9. [Copyright Notice](#copyright-notice)

10. [Support Us](#support-us)

## Overview

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.
Expand Down Expand Up @@ -198,6 +212,8 @@ struct User {
@end
```
## Supported Types
## Using gyb_utils.py for Shared Utilities
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.
Expand Down Expand Up @@ -231,31 +247,81 @@ This allows you to maintain clean, reusable code in your GYB templates, avoiding
## Using GYB with Objective-C and Objective-C++
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.
```objc
%{
properties = ['name', 'age', 'email']
}%
@implementation User {
% for prop in properties:
NSString *_${prop};
% end
}
% for prop in properties:
- (NSString *)${prop} {
return _${prop};
}
% end
@end
```
This example generates getter methods dynamically based on the properties defined in a list.
```cpp
%{
member_types = ['int', 'float', 'std::string']
member_names = ['id', 'score', 'name']
}%
class Player {
% for member_type, member_name in zip(member_types, member_names):
${member_type} ${member_name};
% end
public:
Player() {
% for member_name in member_names:
this->${member_name} = {};
% end
}
};
```
### Running Scribble-GYB
The following template demonstrates generating member variables and constructors for a C++ class.
To process your GYB templates and generate Swift code, use the generate-sources.sh script.
## Running Scribble-GYB
#### Generate Source code
To process your `.gyb` templates and generate source files, you must use the `generate-sources.sh` script provided in the repository.
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.
### Steps to Generate Code
The `generate-sources.sh` script processes GYB templates and generates the corresponding Swift source code files.
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.
##### Running the Script
| Language | GYB Extension |
| :------: | :------------: |
| Swift | `.swift.gyb` |
| ObjC | `.m.gyb` |
| ObjC++ | `.mm.gyb` |
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.
2. Run the `generate-sources.sh` script to generate the Swift, Objective-C, or Objective-C++ source files.
2. **Execute the Script:** Run the generate-sources.sh script to generate Swift source files from your GYB templates.
```sh
chmod +x generate-sources.sh
./generate-sources.sh
```
```shell
chmod +x generate-sources.sh
./generate-sources.sh
```
The generated files will be saved in subdirectories under Sources or Tests, mirroring the location of the original GYB templates.
## Best Practices
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.
- **Modular Templates:** Keep your GYB templates as modular as possible. Small, reusable templates improve maintainability.
- **Consistent Naming:** Use clear and consistent naming conventions for your GYB variables and placeholders to improve readability.
- **Leverage gyb_utils.py:** Use gyb_utils.py to declare shared utilities or constants across multiple templates, reducing redundancy.
- **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.
## Copyright Notice
Expand Down

0 comments on commit 02b16f5

Please sign in to comment.