Skip to content

Commit b5e29a7

Browse files
authored
(chore): Update README.md
1 parent 541265f commit b5e29a7

File tree

1 file changed

+103
-40
lines changed

1 file changed

+103
-40
lines changed

README.md

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
# Scribble GYB (Generate Your Boilerplate)
22

3-
## Overview
3+
The Scribble GYB configuration simplifies boilerplate code generation for ScribbleLabApp projects. By utilizing Python scripting capabilities, GYB provides a flexible approach to reducing repetitive code and improving consistency in Swift, Objective-C, and Objective-C++ projects.
4+
5+
## Table of Contents
46

5-
This repository provides a GYB (Generate Your Boilerplate) configuration tailored for ScribbleLabApp projects. GYB is a tool for generating Swift code using templates, which helps in maintaining a consistent and DRY (Don't Repeat Yourself) codebase. This setup automates boilerplate code generation, reducing manual coding effort and minimizing errors.
7+
1. [Overview](#overview)
8+
2. [Installation](#installation)
9+
10+
2.1 [Add scribble-gyb as git submodule](#option-1-add-scribble-gyb-as-git-submodule)
11+
12+
2.2 [Manually Copy Files](#option-2-manually-copy-files-recommended)
613

7-
## Purpose
14+
3. [Template Syntax](#template-syntax)
15+
16+
3.1 [Code Blocks](#python-code-blocks)
17+
18+
3.2 [Variables & Placeholders](#variables-and-placeholders)
19+
20+
3.3 [Conditional Logic](#conditional-logic)
821

9-
The Scribble GYB configuration streamlines the generation of boilerplate code for Swift projects. By using this configuration, you can ensure repetitive code patterns are handled efficiently and consistently across your projects.
22+
## Overview
1023

11-
## Getting Started
24+
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.
1225

13-
### Installation
26+
## Installation
1427

15-
To use the Scribble GYB configuration, you can either add it to your project as a submodule or manually copy the necessary files.
28+
Scribble GYB can be integrated into your project either as a Git submodule or by manually copying files into the project directory.
1629

17-
#### Option 1: Add scribble-gyb as git submodule
30+
### Option 1: Add scribble-gyb as git submodule
1831

1932
1. Navigate to your project you want to use scribble-gyb in:
2033

@@ -42,31 +55,29 @@ To use the Scribble GYB configuration, you can either add it to your project as
4255
git clone https://github.com/ScribbleLabApp/scribble-gyb.git
4356
```
4457

45-
2. Create a utils directory in your project:
58+
2. Create a `utils` directory in your project:
4659
```shell
47-
mkdir -p utils
60+
mkdir -p /path/to/your-project/utils
4861
```
4962

50-
3. Navigate to the cloned repository and copy the required files:
63+
3. Copy the gyb runtime preprocessor to your repo
5164

5265
```shell
53-
cd scribble-gyb
54-
cp generate-sources.sh gyb.py gyb_utils.py gyb location-to-your-project/utils/
66+
cp -R scribble-gyb/* /path/to/your-project/utils/
5567
```
5668

5769
4. Navigate to your project directory and ensure the files are in place under the utils directory.
5870

59-
## Usage
71+
## Template Syntax
6072

61-
The GYB (Generate Your Boilerplate) tool is configured in this repository to allow you to integrate Python scripting with Swift code generation. This setup provides a flexible and powerful way to generate boilerplate code dynamically.
73+
GYB templates are composed of two primary elements:
6274

63-
### Scribble-GYB Syntax
64-
65-
In GYB templates, you can mix Python code with Swift code. The syntax allows you to embed Python code within Swift files using special markers and placeholders.
75+
- **Python code blocks** for logic and control flow.
76+
- **Code placeholders** that insert Python values into generated code.
6677

6778
#### Python Code Blocks
6879

69-
To include Python code in your Swift files, use the following markers:
80+
Python code is either written inside `%{ ... }%` or `${...}$` blocks. These blocks are executed at template processing time, allowing for dynamic code generation based on runtime values or logic.
7081

7182
```python
7283
%{
@@ -77,59 +88,111 @@ To include Python code in your Swift files, use the following markers:
7788

7889
#### Variables and Placeholders
7990

80-
You can define Python variables and use them as placeholders in your Swift code. Use the `${var}` syntax to insert Python variable values into your Swift code.
91+
Python variables defined inside a GYB template can be inserted into Swift, Objective-C, or Objective-C++ code using `${var}` syntax.
8192

8293
```swift
8394
%{
84-
my_variable = "Hello, GYB!"
95+
py_greeting = "Hello, GYB!"
8596
}%
8697
8798
public struct Greeting {
88-
public let message: String = "${my_variable}"
99+
public let message: String = "${py_greeting}"
89100
}
90101
91-
print(Greeting.message) // ~> Hello, GYB!
102+
print(Greeting.message) // [OUTPUT] ~> Hello, GYB!
103+
```
104+
105+
```objc
106+
%{
107+
message = "Hello, GYB ObjC!"
108+
}%
109+
110+
NSString *greeting = @"${message}"; // [OUTPUT] ~> Hello, GYB ObjC!
92111
```
93112

94113
#### Conditional Logic
95114

96-
You can use Python's if statements within your templates to include or exclude code based on conditions:
115+
GYB supports Python control flow constructs such as if statements, enabling conditional code generation.
97116

98117
```swift
99118
%{
100-
x = 10
119+
is_logged_in = True
101120
}%
102121
103-
% if x > 5:
104-
public struct LargeNumber {
105-
public let value: Int = ${x}
106-
}
107-
122+
% if is_logged_in:
123+
print("Welcome back!")
108124
% else:
109-
public struct SmallNumber {
110-
public let value: Int = 0
111-
}
125+
print("Please log in.")
112126
% end
113127
```
114128
115-
In this example, if x is greater than 5, the LargeNumber struct is included in the generated Swift code. Otherwise, SmallNumber is included.
129+
```objc
130+
%{
131+
use_large_buffer = True
132+
}%
133+
134+
% if use_large_buffer:
135+
#define BUFFER_SIZE 1024
136+
% else:
137+
#define BUFFER_SIZE 256
138+
% end
139+
140+
char buffer[BUFFER_SIZE];
141+
```
116142
117143
### Loops
118144
119-
Use Python's for loops to iterate over a range or collection and generate repetitive code:
145+
GYB templates can use Python's `for` loops to generate repeated code blocks.
120146
121147
```swift
122148
%{
123-
items = ['a', 'b', 'c']
149+
fields = ['name', 'age', 'email']
124150
}%
125151
126-
public struct Letters {
127-
% for item in items:
128-
public let ${item}: String = "${item}"
129-
% end
152+
struct User {
153+
% for field in fields:
154+
var ${field}: String
155+
% end
130156
}
131157
```
132158
159+
##### Autogenerated Source
160+
161+
```swift
162+
struct User {
163+
var name: String
164+
var age: String
165+
var email: String
166+
}
167+
```
168+
169+
```objc
170+
%{
171+
methods = ['getName', 'getAge', 'getEmail']
172+
}%
173+
174+
@interface User : NSObject
175+
% for method in methods:
176+
- (NSString *)${method};
177+
% end
178+
@end
179+
```
180+
181+
##### Autogenerated Source
182+
183+
```objc
184+
@interface User : NSObject
185+
186+
- (NSString *)getName;
187+
- (NSString *)getAge;
188+
- (NSString *)getEmail;
189+
190+
@end
191+
```
192+
193+
## Using gyb_utils.py for Shared Utilities
194+
195+
133196
### Running Scribble-GYB
134197
135198
To process your GYB templates and generate Swift code, use the generate-sources.sh script.

0 commit comments

Comments
 (0)