Update parent to latest version
[yosql.git] / yosql-website / content / tooling / bazel.md
bloba25fcb595495da7730340aa5eb582ea629469b1b
1 ---
2 title: Bazel
3 date: 2019-06-16T18:23:45+02:00
4 menu:
5   main:
6     parent: Tooling
7 categories:
8   - Tooling
9 tags:
10   - Bazel
11 ---
13 [bazel](https://bazel.build/) users can use the `yosql-cli` in their builds by following the steps. Replace placeholders with values from the actual release notes.
15 1. Add git repository to your `WORKSPACE`:
17 ```
18 git_repository(
19     name = "yosql",
20     remote = "https://github.com/metio/yosql.git",
21     tag = "0.0.1-bazel",
23 ```
25 2. Write .sql files in a directory of your choice (e.g. `persistence`)
27 ```
28 project/
29 ├── WORKSPACE
30 ├── BUILD
31 └── persistence/   
32     └── user/
33         ├── findUser.sql
34         └── addUser.sql
35     └── item/
36         ├── queryAllItems.sql
37         └── createItemTable.sql
38 ```
40 3. Declare a `filegroup` that contains all of your SQL files:
42 ```
43 filegroup(
44   name = "your-sql-files",
45   srcs = glob(["persistence/**/*.sql"]),
47 ```
49 4. Generate utilities first since they dont change that often:
51 ```
52 genrule(
53   name = "your-utilities",
54   srcs = [":your-sql-files"],
55   outs = [
56     "com/example/persistence/util/ResultRow.java",
57     "com/example/persistence/util/ResultState.java",
58     "com/example/persistence/util/FlowState.java",
59   ],
60   cmd = """
61     $(location @yosql//yosql-cli) generate utilities
62   """,
63   tools = ["@yosql//yosql-cli"],
65 ```
67 5. Generate converters next, in order to connect user code to repositories
70 ```
71 genrule(
72   name = "your-converters",
73   srcs = [":your-sql-files"],
74   outs = [
75     "com/example/persistence/converter/ToResultRowConverter.java",
76   ],
77   cmd = """
78     $(location @yosql//yosql-cli) generate converters
79   """,
80   tools = ["@yosql//yosql-cli"],
82 ```
85 6. Generate repositories last, since they'll change everytime a SQL file changes as well:
87 ```
88 genrule(
89   name = "your-repositories",
90   srcs = [":your-sql-files"],
91   outs = [
92     "com/example/persistence/UserRepository.java",
93     "com/example/persistence/ItemRepository.java",
94   ],
95   cmd = """
96     $(location @yosql//yosql-cli) generate repositories
97   """,
98   tools = ["@yosql//yosql-cli"],
102 8. Depend on the generated sources by using the target name of the generated code in the `srcs` of another rule.