Bump dawidd6/action-send-mail from 3 to 4
[yosql.git] / yosql-website / content / tooling / bazel.md
blob4fcfabbfc813a8bd5714435af8d182ebc48662c0
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-tooling-cli](../cli/) in their builds by following these steps:
15 1. Download the `yosql-tooling-cli` zip file from the [latest release](https://github.com/metio/yosql/releases/latest) (or any prior version).
16 2. Use a [java_import](https://bazel.build/reference/be/java#java_import) rule to capture all `.jar` files used by `yosql-tooling-cli`
18 ```
19 java_import(
20     name = "yosql_tooling_cli",
21     jars = [
22         "lib/yosql-tooling-cli-x.y.z.jar",
23         "lib/yosql-codegen-x.y.z.jar",
24         "lib/yosql-models-immutables-x.y.z.jar",
25         ... every other jar file from the 'lib' folder
26     ],
28 ```
30 3. Use a [java_binary](https://bazel.build/reference/be/java#java_binary) rule to create a runnable binary for bazel
32 ```
33 java_binary(
34     name = "yosql",
35     deps = [
36         ":yosql_tooling_cli",
37     ],
38     main_class = "wtf.metio.yosql.tooling.cli.YoSQL",
40 ```
42 4. Write .sql files in a directory of your choice (e.g. `persistence`)
44 ```
45 project/
46 ├── WORKSPACE
47 ├── BUILD
48 └── persistence/   
49     └── user/
50         ├── findUser.sql
51         └── addUser.sql
52     └── item/
53         ├── queryAllItems.sql
54         └── createItemTable.sql
55 ```
57 5. Declare a [filegroup](https://bazel.build/reference/be/general#filegroup) that contains all of your SQL files:
59 ```
60 filegroup(
61   name = "your-sql-files",
62   srcs = glob(["persistence/**/*.sql"]),
64 ```
66 6. Generate Java code by calling the previously defined `java_binary`:
68 ```
69 genrule(
70   name = "your-repositories",
71   srcs = [":your-sql-files"],
72   outs = [
73     "com/example/persistence/UserRepository.java",
74     "com/example/persistence/ItemRepository.java",
75     ... all of your generated code
76   ],
77   cmd = """
78     $(location :yosql) generate
79   """,
80   tools = [":yosql"],
82 ```
84 7. Depend on the generated sources by using the target name of the generated code in the `srcs` of another rule.