Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow targets to specify output of the rule #1

Merged
merged 8 commits into from
Dec 2, 2019

Conversation

regisd
Copy link
Contributor

@regisd regisd commented Oct 11, 2018

It's important that users of the rule can change the output.

For instance, we want to be able to generate "manual.html" and "manual.txt":

pandoc(
  name = "html",
  src " manual.md",
  from_format = "markdown",
  to_format = "html",
)
pandoc(
  name = "txt",
  src " manual.md",
  from_format = "markdown",
  to_format = "plain",
)

Today, the rule forces the output to be html.html and txt.txt.

Of course, you can argue that the name of the rule is used, and I could have "manual.html". However, two labels cannot be identical, so there is no easy way to generate "manual.txt".
A workaround could be to add a genrule that copies "txt.txt" to "manual.txt".

Instead, I propose to replace the pattern generated ctx.outputs by an output given in ctx.attr.

Also, this change makes src single file and mandatory.

.cirrus.bazelrc Outdated Show resolved Hide resolved
BUILD.bazel Outdated Show resolved Hide resolved
fake/BUILD Outdated Show resolved Hide resolved
pandoc.bzl Outdated
"to_format": attr.string(),
"src": attr.label(allow_single_file = True, mandatory = True),
"to_format": attr.string(mandatory = True),
"output": attr.output(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking. If we're making the output file configurable, maybe we could simply remove from_format and to_format entirely. Pandoc seems to automatically infer the intended source/destination format based on the file extension. If people need something custom, they could pass that in through options. What are your thoughts on this?

This would allow us to get rid of FORMAT_EXTENSIONS and PANDOC_FORMATS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know pandoc can infer the input & output formats.
I'm happy to remove the list of extensions, yes!
There is a little benefit in having the extensions in the bazel rule: we can fail early if a format is not specified.

The thing is: output is still not mandatory (my change is backward compatible).
Would you like to make output mandatory, then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly! Making the output mandatory sounds good in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do. But we still need the formats for the test suite. So let's start by moving the formats in a common place (#3).
We can also move the samples in their own package (#2)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, removing --from_format would be a breaking change for the users of the rule. I'll make it optional and keep the existing behaviour. Feel free to remove really it in a subsequent change.
Also, if the rule knows the format, bazel can do an early check at analysis phase, rather than invoking pandoc which will then fail.

to_format = fmt,
) for fmt in PANDOC_FORMATS]


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please run buildifier on these files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Out of curiosiity, do you have a git presubmit hook for that? It seems buildifier alreadys returns 0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have that set up right now. I usually just do a %!buildifier in Vim after making some edits. For a presubmit, we could always invoke buildifier and use cmp to compare changes against the input if we wanted one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's also one way to do it. Whatever works best for you :)

@regisd
Copy link
Contributor Author

regisd commented Oct 15, 2018

I've put too many things in this PR. Sorry! Let's split it in more actionable smaller pull requests.

It's important that users of the rule can change the output.
See #1

* Make "output": attr.output(mandatory = True),
* Change the output of the sample generation to "readme_format.format"
@regisd regisd changed the title Allow to specify output of the rule Force targets to specify output of the rule Oct 15, 2018
@regisd
Copy link
Contributor Author

regisd commented Oct 15, 2018

Please take another look.

Copy link
Member

@patbro patbro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply to your PR. I just reviewed the code once more.

BUILD.bazel Outdated Show resolved Hide resolved
pandoc.bzl Outdated Show resolved Hide resolved
@regisd regisd changed the title Force targets to specify output of the rule Allow targets to specify output of the rule Dec 2, 2019
@regisd
Copy link
Contributor Author

regisd commented Dec 2, 2019

PTAL

Copy link
Member

@patbro patbro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@patbro patbro merged commit 68bcf3f into ProdriveTechnologies:master Dec 2, 2019
regisd added a commit to regisd/jflex that referenced this pull request Dec 3, 2019
from
bazel-bin/docs/html.html
to
bazel-bin/docs/manual.html

Thanks to ProdriveTechnologies/bazel-pandoc#1
regisd added a commit to jflex-de/jflex that referenced this pull request Dec 4, 2019
* Use latest version of bazel_pandoc

* Add a filegroup "docs"

bazel build //docs
Targets:
  bazel-bin/docs/manual.pdf
  bazel-bin/docs/html.html

* Change output of //docs:html

from
bazel-bin/docs/html.html
to
bazel-bin/docs/manual.html

Thanks to ProdriveTechnologies/bazel-pandoc#1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants