-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: transform guide markdown files with bazel #14103
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
Merged
andrewseguin
merged 2 commits into
angular:master
from
devversion:build/transform-markdown-html-bazel
Nov 14, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package(default_visibility=["//visibility:public"]) | ||
|
|
||
| load("//tools/markdown-to-html:index.bzl", "markdown_to_html") | ||
|
|
||
| markdown_to_html( | ||
| name = "guides", | ||
| srcs = glob(["**/*.md"]), | ||
| ) |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") | ||
| load("//tools:defaults.bzl", "ts_library") | ||
|
|
||
| ts_library( | ||
| name = "transform-markdown", | ||
| srcs = [":transform-markdown.ts"], | ||
| deps = ["@matdeps//@types/node"], | ||
| tsconfig = ":tsconfig.json", | ||
| ) | ||
|
|
||
| nodejs_binary( | ||
| name = "markdown-to-html", | ||
| entry_point = "angular_material/tools/markdown-to-html/transform-markdown.js", | ||
| data = [ | ||
| "@matdeps//highlight.js", | ||
| "@matdeps//marked", | ||
| "@matdeps//source-map-support", | ||
| ":transform-markdown", | ||
| ], | ||
| ) | ||
|
|
||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| """ | ||
| Implementation of the "markdown_to_html" rule. The implementation runs the transform | ||
| executable in order to create the outputs for the specified source files. | ||
| """ | ||
| def _markdown_to_html(ctx): | ||
| input_files = ctx.files.srcs; | ||
| args = ctx.actions.args() | ||
| expected_outputs = []; | ||
|
|
||
| for input_file in input_files: | ||
| basename = input_file.basename.replace('.md', '') | ||
devversion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| output_file = ctx.actions.declare_file("%s.html" % basename) | ||
| expected_outputs += [output_file] | ||
|
|
||
| # Add the input file and it's related output to the arguments that | ||
devversion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # will be passed to the transformer executable. | ||
| args.add("%s=%s" % (input_file.path, output_file.path)) | ||
devversion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Run the transform markdown executable that transforms the specified source files. | ||
| # Note that we should specify the outputs here because Bazel can then throw an error | ||
| # if the script didn't generate the required outputs. | ||
| ctx.actions.run( | ||
| inputs = input_files, | ||
| executable = ctx.executable._transform_markdown, | ||
| outputs = expected_outputs, | ||
| arguments = [args], | ||
| ) | ||
|
|
||
| return DefaultInfo(files = depset(expected_outputs)) | ||
|
|
||
| """ | ||
| Rule definition for the "markdown_to_html" rule that can accept arbritary source files | ||
| that will be transformed into HTML files. The outputs can be referenced through the | ||
| default output provider. | ||
| """ | ||
| markdown_to_html = rule( | ||
| implementation = _markdown_to_html, | ||
| attrs = { | ||
| "srcs": attr.label_list(allow_files = [".md"]), | ||
|
|
||
| # Executable for this rule that is responsible for converting the specified | ||
| # markdown files into HTML files. | ||
| "_transform_markdown": attr.label( | ||
| default = Label("//tools/markdown-to-html"), | ||
| executable = True, | ||
| cfg = "host" | ||
| )}, | ||
| ) | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * Script that will be used by the markdown_to_html Bazel rule in order to transform | ||
| * multiple markdown files into the equivalent HTML output. | ||
| */ | ||
|
|
||
| import {readFileSync, writeFileSync} from 'fs'; | ||
|
|
||
| // These types lack type definitions. | ||
| const marked = require('marked'); | ||
| const highlightJs = require('highlight.js'); | ||
|
|
||
| // Setup the default options for converting markdown to HTML. | ||
| marked.setOptions({ | ||
| // Implement a highlight function that converts the code block into a highlighted | ||
| // HTML snippet that uses HighlightJS. | ||
| highlight: (code: string, language: string): string => { | ||
| if (language) { | ||
| return highlightJs.highlight( | ||
| language.toLowerCase() === 'ts' ? 'typescript' : language, code).value; | ||
| } | ||
| return code; | ||
| } | ||
| }); | ||
|
|
||
| if (require.main === module) { | ||
| // The script expects the input files to be specified in the following format: | ||
| // {input_file_path}={output_file_path} | ||
| // We have to know the output paths because the input path and output path differ | ||
| // fundamentally within the Bazel sandbox. | ||
| const inputFiles = process.argv.slice(2).map(argument => argument.split('=')); | ||
|
|
||
| // Walk through each input file and write transformed markdown output to the specified | ||
| // output path. | ||
| inputFiles.forEach(([inputPath, outputPath]) => { | ||
| writeFileSync(outputPath, marked(readFileSync(inputPath, 'utf8'))); | ||
| }); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "lib": ["es2015"], | ||
| "module": "commonjs", | ||
| "target": "es5", | ||
| "sourceMap": true, | ||
| "types": ["node"] | ||
| }, | ||
| "bazelOptions": { | ||
| "suppressTsconfigOverrideWarnings": true | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I don't want to build everything that is also outside of
src. Running with//...seems to break our CI build, so I'll try to revisit that in a follow-up