Skip to content

Commit

Permalink
Merge pull request #27 from fremtind/workshop-edits-ragna
Browse files Browse the repository at this point in the history
Workshop edits ragna
  • Loading branch information
ragnaorsal authored Dec 16, 2024
2 parents 92d491e + c42e11d commit 1a13e73
Show file tree
Hide file tree
Showing 51 changed files with 468 additions and 590 deletions.
2 changes: 2 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ examples/case7/node_modules
teams/team-2/remix-app/node_modules
teams/team-2/vite-app/node_modules
teams/libs/frontend/greeting-component/node_modules
workshop/case2/node_modules
workshop/case3/node_modules
workshop/case4/node_modules
workshop/case5/node_modules
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ It also contains a CI workflow that shows how to build and test all changed targ

### Workshop
- Case 1: [Hello World in Bazel](workshop/case1/README.md)
- Case 2: [Simple Java application](workshop/case2/README.md)
- Case 3: [Generate code from OpenApi specifications](workshop/case3/README.md)
- Case 4: [Spring Boot in Bazel](workshop/case4/README.md)
- Case 5: [Vite + React frontend in Bazel](workshop/case5/README.md)
- Case 6: [Bazel in CI / multiple teams](workshop/case6/README.md)
- Case 2: [Generate code from OpenApi specifications](workshop/case2/README.md)
- Case 3: [Spring Boot in Bazel](workshop/case3/README.md)
- Case 4: [Vite + React frontend in Bazel](workshop/case4/README.md)
- Case 5: [Bazel in CI / multiple teams](workshop/case5/README.md)

### Remote caching
This workshop works with a free online remote cache to speed up builds, called [NativeLink](https://app.nativelink.com/).
Expand Down
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 46 additions & 15 deletions workshop/case1/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")

# Generere en fil som inneholder "Hello"
genrule(
name = "hello",
outs = ["hello.txt"],
cmd = "echo 'Hello, World!' > $@",
cmd = "echo 'Hello' > $@",
)

# Generere en fil som inneholder "World"
genrule(
name = "hello_replaced",
srcs = [":hello"],
outs = ["replaced.txt"],
cmd = "cat '$(location :hello)' | sed 's/World/Workshop/' > $@",
name = "world",
outs = ["world.txt"],
cmd = "echo 'World!' > $@",
)

# Small test to check if the files were generated correctly
assert_contains(
name = "test_hello",
actual = ":hello",
expected = "Hello, World!",
# Konkatener filene "hello" og "world" til en ny fil
genrule(
name = "hello_world",
srcs = [
":hello",
":world",
],
outs = ["hello-world.txt"],
cmd = "cat $(locations :hello) $(location :world) > $@",
)

assert_contains(
name = "test_replaced",
actual = ":hello_replaced",
expected = "Hello, Workshop!",
)
### OPPGAVE 1
# generer en ny fil replaced.txt som erstatter "World" med "Workshop"
# hint: cmd = "cat '$(location :hello)' | sed 's/World/Workshop/' > $@"
#genrule(
# name = "targetnavnet_mitt",
# srcs = [":target_label"],
# outs = ["outputfil.fil"],
# cmd = "kommandoen jeg vil kjøre",
#)

### OPPGAVE 2
# Lag en test som sjekker at filen "hello" inneholder "Hello, World!"
# Få testen til å passere

# Da må du bruke regelen assert_contains som trenger disse parametrene:
# name: navnet på testtarget (som du bestemmer)
# actual: targetet som inneholder den faktiske teksten
# expected: teksten som skal sjekkes mot

#assert_contains(
# name = "mitt_test_target",
# actual = ":referanse_til_target",
# expected = "Forventet tekst",
#)

### OPPGAVE 3
# Lag en ny pakke i workshop folderen som heter min-pakke, eller noe annet
# Flytt :hello og :world targetsa til den nye pakken
# Endre referansene til targetsa i denne pakken til å referere til den nye pakken
# Får du fremdeles testen til å passere?
# Hint: Feiler bygget? Kan det være at label på targetsa :hello og :world må oppdateres?
Binary file removed workshop/case1/case1.png
Binary file not shown.
40 changes: 9 additions & 31 deletions workshop/case2/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
load("@contrib_rules_jvm//java:defs.bzl", "java_junit5_test", "junit5_deps")
# This loads macros we have created in the tools/openapi.bzl file to make reuse of OpenAPI code generation easier.
load("//tools:openapi.bzl", "openapi_spring", "openapi_typescript")

java_library(
name = "greeter-lib",
srcs = ["Greeter.java"],
openapi_spring(
name = "openapi_spring",
package_name = "com.workshop.case2",
src = "api.yaml",
visibility = ["//visibility:public"],
)

java_binary(
name = "hello",
srcs = ["HelloWorld.java"],
main_class = "workshop.case2.HelloWorld",
deps = [":greeter-lib"],
)

sh_test(
name = "acceptance_test",
size = "small",
srcs = ["hello_world_test.sh"],
args = ["$(location :hello)"], # Pass the relative location of the :hello target as an argument
data = [
":hello",
],
)

java_junit5_test(
name = "greeter-test",
size = "small",
srcs = ["GreeterTest.java"],
test_class = "workshop.case2.GreeterTest",
deps = [
"//workshop/case2:greeter-lib",
"@maven//:org_assertj_assertj_core",
"@maven//:org_junit_jupiter_junit_jupiter_api",
] + junit5_deps(),
openapi_typescript(
name = "openapi_typescript",
src = "api.yaml",
)
13 changes: 0 additions & 13 deletions workshop/case2/Greeter.java

This file was deleted.

21 changes: 0 additions & 21 deletions workshop/case2/GreeterTest.java

This file was deleted.

8 changes: 0 additions & 8 deletions workshop/case2/HelloWorld.java

This file was deleted.

73 changes: 13 additions & 60 deletions workshop/case2/README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,21 @@
# Case 2: Hello, Java!
# Case 2: OpenAPI specs and multi-language builds

## About the case
In this case, we will use the OpenAPI specs to generate library code in multiple languages, which we in turn will use in our workshop.

This case uses the native java_binary and java_library rules.
The java_library rule compiles and links sources into a .jar file
The java_binary rule builds a jar file, plus a wrapper shell script.
See bazel-bin/workshop/case2/hello.runfiles/_main/workshop/case2/hello for the wrapper script.
## Things to try

This is also the first example that uses external depencencies from Maven, for example
`@maven//:org_assertj_assertj_core` which is a dependency for the `greeter-test` test-target.
### Figure out which cases depends on the `openapi_spring` target
In the previous case there is an example of querying reverse dependencies - can you figure out which case depends on the `openapi_spring` target?

All dependencies are resolved using the `MODULE.bazel` and the `maven_install.json` lockfile.
This generates the `@maven//` namespace and the Starlark-targets for the dependencies.
### Can you locate the generated code in the `bazel-bin` directory?

## Things to try out
### Build the lib
`bazel build //workshop/case2:greeter-lib` - builds the greeter-lib target and outputs the location for the resulting jar file.
### What happens if you try to change the visibility of the `openapi_spring` target to for example `//teams:__subpackages__`?
Visibility of a target is a key feature in Bazel to control who can depend on a target. Try changing the visibility of the `openapi_spring` target and then run `bazel build //workshop/...` to rebuild all workshop cases

### Run the binary
`bazel run //workshop/case2:hello` - runs the hello target, which depends on the greeter-lib target and outputs the result.
## Additional things to try

### Modifying the different parts, and see what recompiles
If you modify the `Greeter.java` file and run `bazel run //workshop/case2:hello`, the `greeter-lib` target will recompile and then the `hello` will rebuild before executing.
### What happens when you rename the `hello` property in `HelloWorldResponse`?
Try running `bazel test --build_tests_only //workshop/...` after removing the property

If you modify the `HelloWorld.java` file and then run `bazel run //workshop/case2:hello`, the `hello` target will recompile.

You can see how many processes were needed before the build was complete by looking at the output:
```
bazel run //workshop/case2:hello
INFO: Invocation ID: 1ca27de2-08e0-4ff0-93b9-a83b6f518f70
INFO: Analyzed target //workshop/case2:hello (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //workshop/case2:hello up-to-date:
bazel-bin/workshop/case2/hello
bazel-bin/workshop/case2/hello.jar
INFO: Elapsed time: 0.712s, Critical Path: 0.56s
INFO: 2 processes: 1 internal, 1 worker.
INFO: Build completed successfully, 2 total actions
INFO: Running command line: bazel-bin/workshop/case2/hello
Hello, World!
```

Here you can see the line `INFO: 2 processes: 1 internal, 1 worker.` which shows that 2 processes were used to build the target.

If you run it again without modifying anything, you will see that the target is up-to-date and only 1 process are used:
```
INFO: 1 process: 1 internal.
```

## Additional things to try out

### Query Bazel for the dependency graph of the hello target:
`bazel query "deps(//workshop/case2:hello)"`
Here you can see the full dependency graph of the hello target (including all the tools etc bazel use)
![graph](case2_deps2.png)

vs

`bazel query "deps('//workshop/case2:hello') intersect //..."`
Here you only see the dependencies that are part of the workspace (e.g //...)

![graph](case2_deps3.png)

### Show files generated by a target:
`bazel cquery //workshop/case2:hello --output=files`

### Create another library yourself and consume it in the `hello` target
## Gotchas with this approach
While this works as expected, if you only use pnpm for frontend-builds (not Bazel), you have to manually rebuild the OpenAPI target in Bazel to get updates to the package.
File renamed without changes.
Binary file removed workshop/case2/case2_deps2.png
Binary file not shown.
Binary file removed workshop/case2/case2_deps3.png
Binary file not shown.
13 changes: 0 additions & 13 deletions workshop/case2/hello_world_test.sh

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 1a13e73

Please sign in to comment.