-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from fremtind/workshop-edits-ragna
Workshop edits ragna
- Loading branch information
Showing
51 changed files
with
468 additions
and
590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
 | ||
|
||
vs | ||
|
||
`bazel query "deps('//workshop/case2:hello') intersect //..."` | ||
Here you only see the dependencies that are part of the workspace (e.g //...) | ||
|
||
 | ||
|
||
### 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 not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
Oops, something went wrong.