|
| 1 | +# rules_cpan e2e example |
| 2 | + |
| 3 | +Demonstrates how to use rules_cpan by building https://github.com/linux-test-project/lcov. |
| 4 | + |
| 5 | +## cpanfile |
| 6 | + |
| 7 | +Some projects already deliver a [cpanfile](https://metacpan.org/dist/Module-CPANfile). |
| 8 | + |
| 9 | +In the case of Lcov the Perl dependencies are just listed in the Readme: https://github.com/linux-test-project/lcov/blob/v2.1/README#L116. |
| 10 | +It's straightforward to convert this into `requires 'Capture::Tiny';` (and so on) for the cpanfile. |
| 11 | + |
| 12 | +## cpanfile.snapshot |
| 13 | + |
| 14 | +rules_cpan needs a `cpanfile.snapshot` as input. |
| 15 | + |
| 16 | +The user needs to install and run [Carton](https://metacpan.org/pod/Carton) manually to generate this file. |
| 17 | + |
| 18 | +This will actually install the dependencies into a local directory. |
| 19 | +We just remove this directory afterwards. |
| 20 | + |
| 21 | +```sh |
| 22 | +curl -L https://cpanmin.us | perl - --sudo App::cpanminus |
| 23 | +sudo cpanm --notest Carton |
| 24 | + |
| 25 | +cd e2e |
| 26 | +carton |
| 27 | + |
| 28 | +rm -rf local |
| 29 | +``` |
| 30 | + |
| 31 | +## cpanfile.snapshot.lock.json |
| 32 | + |
| 33 | +The `cpanfile.snapshot.lock.json` is generated by lock.py of rules_cpan: |
| 34 | + |
| 35 | +```sh |
| 36 | +bazel run @rules_cpan//lock -- cpanfile.snapshot cpanfile.snapshot.lock.json |
| 37 | +``` |
| 38 | + |
| 39 | +It contains the URLs and checksums of the cpan dependencies that will be downloaded by Bazel. |
| 40 | + |
| 41 | +## MODULE.bazel |
| 42 | + |
| 43 | +Until https://github.com/bazelbuild/rules_perl/pull/62 is merged you need to add a rules_perl dependency with a git_override: |
| 44 | + |
| 45 | +```Starlark |
| 46 | +bazel_dep(name = "rules_perl") |
| 47 | +git_override( |
| 48 | + module_name = "rules_perl", |
| 49 | + remote = "https://github.com/lalten/rules_perl", |
| 50 | + commit = "973efb79defe0c417aa9655ac24a09148d599e9e", |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | +Now you can use the cpan extension of rules_cpan to generate the perl_library targets. |
| 55 | + |
| 56 | +```Starlark |
| 57 | +cpan = use_extension("@rules_cpan//cpan:extensions.bzl", "cpan") |
| 58 | +cpan.install( |
| 59 | + name = "cpan_deps", |
| 60 | + lock = "//:cpanfile.snapshot.lock.json", |
| 61 | +) |
| 62 | +use_repo(cpan, "cpan_deps") |
| 63 | +``` |
| 64 | + |
| 65 | +In this example we don't have any Perl code of our own and just pull in the latest Lcov release as `http_archive`. |
| 66 | + |
| 67 | +## BUILD |
| 68 | + |
| 69 | +Now we can finally depend on the cpan targets in our BUILD file: |
| 70 | + |
| 71 | +```Starlark |
| 72 | +load("@rules_perl//perl:perl.bzl", "perl_binary", "perl_library") |
| 73 | + |
| 74 | +perl_library( |
| 75 | + name = "liblcov", |
| 76 | + srcs = glob(["lib/**/*"]), |
| 77 | + deps = ["@cpan_deps"], |
| 78 | +) |
| 79 | +``` |
| 80 | + |
| 81 | +## Testing |
| 82 | + |
| 83 | +In this example we have an integration test that runs the `gehtml --version`: |
| 84 | +```sh |
| 85 | +bazel test //:integration_test --test_output=all |
| 86 | +``` |
0 commit comments