bearings 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +31 -0
- data/LICENSE +21 -0
- data/README.md +32 -0
- data/lib/bearings/checks/code_examples.rb +80 -0
- data/lib/bearings/checks/constant_names.rb +99 -0
- data/lib/bearings/checks/diagrams.rb +63 -0
- data/lib/bearings/checks/external_links/failure.rb +3 -0
- data/lib/bearings/checks/external_links.rb +53 -0
- data/lib/bearings/checks/images.rb +120 -0
- data/lib/bearings/checks/links.rb +230 -0
- data/lib/bearings/checks/manifest_matches_disk.rb +48 -0
- data/lib/bearings/checks/readme_links.rb +103 -0
- data/lib/bearings/document.rb +9 -0
- data/lib/bearings/guide.rb +28 -0
- data/lib/bearings/html_pages/diagram_failure.rb +3 -0
- data/lib/bearings/html_pages.rb +174 -0
- data/lib/bearings/images/dimensions.rb +82 -0
- data/lib/bearings/images.rb +83 -0
- data/lib/bearings/kramdown.rb +253 -0
- data/lib/bearings/llms_txt.rb +61 -0
- data/lib/bearings/manifest/checks.rb +3 -0
- data/lib/bearings/manifest/group.rb +3 -0
- data/lib/bearings/manifest/outputs.rb +3 -0
- data/lib/bearings/manifest/page.rb +3 -0
- data/lib/bearings/manifest/parser.rb +264 -0
- data/lib/bearings/manifest/redirect.rb +3 -0
- data/lib/bearings/manifest/site.rb +3 -0
- data/lib/bearings/manifest.rb +3 -0
- data/lib/bearings/markdown_copies.rb +141 -0
- data/lib/bearings/net_http.rb +47 -0
- data/lib/bearings/output_file.rb +3 -0
- data/lib/bearings/pipeline.rb +101 -0
- data/lib/bearings/problem.rb +3 -0
- data/lib/bearings/rake.rb +318 -0
- data/lib/bearings/rbs.rb +20 -0
- data/lib/bearings/redirects.rb +103 -0
- data/lib/bearings/repo_path.rb +19 -0
- data/lib/bearings/report.rb +40 -0
- data/lib/bearings/search_index.rb +47 -0
- data/lib/bearings/server.rb +115 -0
- data/lib/bearings/site/group.rb +3 -0
- data/lib/bearings/site/page.rb +27 -0
- data/lib/bearings/site.rb +135 -0
- data/lib/bearings/sitemap.rb +57 -0
- data/lib/bearings/version.rb +5 -0
- data/lib/bearings/watch.rb +31 -0
- data/lib/bearings/writer.rb +63 -0
- data/lib/bearings.rb +20 -0
- data/sig/_private/kramdown.rbs +33 -0
- data/sig/_private/psych.rbs +50 -0
- data/sig/_private/rake.rbs +12 -0
- data/sig/_private/socket.rbs +4 -0
- data/sig/generated/bearings/checks/code_examples.rbs +35 -0
- data/sig/generated/bearings/checks/constant_names.rbs +48 -0
- data/sig/generated/bearings/checks/diagrams.rbs +39 -0
- data/sig/generated/bearings/checks/external_links/failure.rbs +2 -0
- data/sig/generated/bearings/checks/external_links.rbs +29 -0
- data/sig/generated/bearings/checks/images.rbs +59 -0
- data/sig/generated/bearings/checks/links.rbs +123 -0
- data/sig/generated/bearings/checks/manifest_matches_disk.rbs +20 -0
- data/sig/generated/bearings/checks/readme_links.rbs +46 -0
- data/sig/generated/bearings/document.rbs +2 -0
- data/sig/generated/bearings/guide.rbs +15 -0
- data/sig/generated/bearings/html_pages/diagram_failure.rbs +2 -0
- data/sig/generated/bearings/html_pages.rbs +90 -0
- data/sig/generated/bearings/images/dimensions.rbs +56 -0
- data/sig/generated/bearings/images.rbs +46 -0
- data/sig/generated/bearings/kramdown.rbs +119 -0
- data/sig/generated/bearings/llms_txt.rbs +30 -0
- data/sig/generated/bearings/manifest/checks.rbs +2 -0
- data/sig/generated/bearings/manifest/group.rbs +2 -0
- data/sig/generated/bearings/manifest/outputs.rbs +2 -0
- data/sig/generated/bearings/manifest/page.rbs +2 -0
- data/sig/generated/bearings/manifest/parser.rbs +127 -0
- data/sig/generated/bearings/manifest/redirect.rbs +2 -0
- data/sig/generated/bearings/manifest/site.rbs +2 -0
- data/sig/generated/bearings/manifest.rbs +2 -0
- data/sig/generated/bearings/markdown_copies.rbs +69 -0
- data/sig/generated/bearings/net_http.rbs +24 -0
- data/sig/generated/bearings/output_file.rbs +2 -0
- data/sig/generated/bearings/pipeline.rbs +62 -0
- data/sig/generated/bearings/problem.rbs +2 -0
- data/sig/generated/bearings/rake.rbs +156 -0
- data/sig/generated/bearings/rbs.rbs +7 -0
- data/sig/generated/bearings/redirects.rbs +44 -0
- data/sig/generated/bearings/repo_path.rbs +10 -0
- data/sig/generated/bearings/report.rbs +25 -0
- data/sig/generated/bearings/search_index.rbs +44 -0
- data/sig/generated/bearings/server.rbs +55 -0
- data/sig/generated/bearings/site/group.rbs +2 -0
- data/sig/generated/bearings/site/page.rbs +7 -0
- data/sig/generated/bearings/site.rbs +48 -0
- data/sig/generated/bearings/sitemap.rbs +22 -0
- data/sig/generated/bearings/version.rbs +5 -0
- data/sig/generated/bearings/watch.rbs +19 -0
- data/sig/generated/bearings/writer.rbs +29 -0
- data/sig/generated/bearings.rbs +4 -0
- data/sig/manual/bearings/checks/code_examples.rbs +4 -0
- data/sig/manual/bearings/checks/constant_names.rbs +4 -0
- data/sig/manual/bearings/checks/diagrams.rbs +4 -0
- data/sig/manual/bearings/checks/external_links/failure.rbs +13 -0
- data/sig/manual/bearings/checks/external_links.rbs +8 -0
- data/sig/manual/bearings/checks/images.rbs +4 -0
- data/sig/manual/bearings/checks/links.rbs +4 -0
- data/sig/manual/bearings/checks/manifest_matches_disk.rbs +4 -0
- data/sig/manual/bearings/checks/readme_links.rbs +4 -0
- data/sig/manual/bearings/checks.rbs +3 -0
- data/sig/manual/bearings/document.rbs +109 -0
- data/sig/manual/bearings/guide.rbs +8 -0
- data/sig/manual/bearings/html_pages/diagram_failure.rbs +11 -0
- data/sig/manual/bearings/html_pages.rbs +8 -0
- data/sig/manual/bearings/images/dimensions.rbs +4 -0
- data/sig/manual/bearings/images.rbs +4 -0
- data/sig/manual/bearings/kramdown.rbs +4 -0
- data/sig/manual/bearings/llms_txt.rbs +4 -0
- data/sig/manual/bearings/manifest/checks.rbs +13 -0
- data/sig/manual/bearings/manifest/group.rbs +15 -0
- data/sig/manual/bearings/manifest/outputs.rbs +11 -0
- data/sig/manual/bearings/manifest/page.rbs +19 -0
- data/sig/manual/bearings/manifest/parser.rbs +4 -0
- data/sig/manual/bearings/manifest/redirect.rbs +15 -0
- data/sig/manual/bearings/manifest/site.rbs +21 -0
- data/sig/manual/bearings/manifest.rbs +21 -0
- data/sig/manual/bearings/markdown_copies.rbs +4 -0
- data/sig/manual/bearings/net_http.rbs +4 -0
- data/sig/manual/bearings/output_file.rbs +13 -0
- data/sig/manual/bearings/pipeline.rbs +4 -0
- data/sig/manual/bearings/problem.rbs +21 -0
- data/sig/manual/bearings/rake.rbs +4 -0
- data/sig/manual/bearings/rbs.rbs +4 -0
- data/sig/manual/bearings/redirects.rbs +4 -0
- data/sig/manual/bearings/repo_path.rbs +4 -0
- data/sig/manual/bearings/report.rbs +4 -0
- data/sig/manual/bearings/search_index.rbs +4 -0
- data/sig/manual/bearings/server.rbs +4 -0
- data/sig/manual/bearings/site/group.rbs +15 -0
- data/sig/manual/bearings/site/page.rbs +39 -0
- data/sig/manual/bearings/site.rbs +17 -0
- data/sig/manual/bearings/sitemap.rbs +4 -0
- data/sig/manual/bearings/watch.rbs +4 -0
- data/sig/manual/bearings/writer.rbs +4 -0
- metadata +242 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bearings::MarkdownCopies
|
|
4
|
+
extend self
|
|
5
|
+
|
|
6
|
+
# @rbs!
|
|
7
|
+
# type rewrite = [String, Integer, String]
|
|
8
|
+
|
|
9
|
+
CODE_SPAN = /(?<!`)(?<ticks>`+)(?!`).*?(?<!`)\k<ticks>(?!`)/ #: Regexp
|
|
10
|
+
# A code span or an escaped bracket comes first so a link-shaped string inside one is matched, and kept, as written.
|
|
11
|
+
INLINE = /(?<code>#{CODE_SPAN})|(?<escaped>\\\[[^\]\n]*\]\()|(?<lead>\]\([ \t]*<?)(?<destination>[^\s<>()]+)|(?<attribute>\s(?<source>#{Bearings::Images::SOURCE_URLS.keys.join('|')})=["'])(?<value>[^"'\n]*)/ #: Regexp
|
|
12
|
+
DEFINITION = /\A(?<lead> {0,3}\[(?!\^)[^\]\n]+\]:[ \t]*<?)(?<destination>[^\s<>]+)/ #: Regexp
|
|
13
|
+
|
|
14
|
+
# @rbs site: Bearings::Site
|
|
15
|
+
# @rbs files: Hash[String, String]
|
|
16
|
+
# @rbs return: Array[Bearings::OutputFile]
|
|
17
|
+
def call(site, files)
|
|
18
|
+
copies = site.pages.to_h { |page| [page.source_path, page.markdown_url] }
|
|
19
|
+
site_url = site.details.url&.delete_suffix('/')
|
|
20
|
+
site.pages.map do |page|
|
|
21
|
+
Bearings::OutputFile.new(
|
|
22
|
+
path: path(page),
|
|
23
|
+
content: copy(files.fetch(page.source_path), page.document, rewrites(page, copies, site_url))
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @rbs page: Bearings::Site::Page
|
|
29
|
+
# @rbs return: String
|
|
30
|
+
def path(page)
|
|
31
|
+
page.markdown_url.delete_prefix('/')
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @rbs document: Bearings::Document
|
|
35
|
+
# @rbs return: Array[Integer]
|
|
36
|
+
def fenced(document)
|
|
37
|
+
[*document.code_blocks, *document.diagrams].flat_map do |block|
|
|
38
|
+
(block.line..(block.line + block.code.lines.count + 1)).to_a
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
# @rbs page: Bearings::Site::Page
|
|
45
|
+
# @rbs copies: Hash[String, String]
|
|
46
|
+
# @rbs site_url: String?
|
|
47
|
+
# @rbs return: Array[rewrite]
|
|
48
|
+
def rewrites(page, copies, site_url)
|
|
49
|
+
links = page.document.links.filter_map do |link|
|
|
50
|
+
path, hash, fragment = link.href.partition('#')
|
|
51
|
+
url = Bearings::RepoPath.resolve(page.source_path, path)&.then { |in_repo| copies[in_repo] }
|
|
52
|
+
[link.href, link.line, "#{url}#{hash}#{fragment}"] if url
|
|
53
|
+
end #: Array[rewrite]
|
|
54
|
+
images = page.document.images.filter_map do |image|
|
|
55
|
+
path = Bearings::RepoPath.resolve(page.source_path, image.src)
|
|
56
|
+
[image.src, image.line, "#{site_url}#{Bearings::Images.url(path)}"] if path
|
|
57
|
+
end #: Array[rewrite]
|
|
58
|
+
links + images
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# @rbs source: String
|
|
62
|
+
# @rbs document: Bearings::Document
|
|
63
|
+
# @rbs rewrites: Array[rewrite]
|
|
64
|
+
# @rbs return: String
|
|
65
|
+
def copy(source, document, rewrites)
|
|
66
|
+
lines = source.lines
|
|
67
|
+
breaks = breaks(lines, document)
|
|
68
|
+
unused = rewrites
|
|
69
|
+
lines.map.with_index(1) do |line, number|
|
|
70
|
+
next line if breaks.include?(number)
|
|
71
|
+
|
|
72
|
+
copied, unused = inline(definition(line, rewrites), number, unused, breaks)
|
|
73
|
+
copied
|
|
74
|
+
end.join
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @rbs lines: Array[String]
|
|
78
|
+
# @rbs document: Bearings::Document
|
|
79
|
+
# @rbs return: Set[Integer]
|
|
80
|
+
def breaks(lines, document)
|
|
81
|
+
blank = lines.filter_map.with_index(1) { |line, number| number if line.strip.empty? }
|
|
82
|
+
Set.new(fenced(document) + blank)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @rbs from: Integer
|
|
86
|
+
# @rbs to: Integer
|
|
87
|
+
# @rbs breaks: Set[Integer]
|
|
88
|
+
# @rbs return: bool
|
|
89
|
+
def same_paragraph?(from, to, breaks)
|
|
90
|
+
from <= to && breaks.disjoint?(from..to)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# @rbs line: String
|
|
94
|
+
# @rbs rewrites: Array[rewrite]
|
|
95
|
+
# @rbs return: String
|
|
96
|
+
def definition(line, rewrites)
|
|
97
|
+
line.sub(DEFINITION) do
|
|
98
|
+
match = Regexp.last_match #: MatchData
|
|
99
|
+
url = rewrites.find { |destination, _, _| destination == match[:destination] }&.last
|
|
100
|
+
url ? "#{match[:lead]}#{url}" : match.to_s
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# @rbs line: String
|
|
105
|
+
# @rbs number: Integer
|
|
106
|
+
# @rbs unused: Array[rewrite]
|
|
107
|
+
# @rbs breaks: Set[Integer]
|
|
108
|
+
# @rbs return: [String, Array[rewrite]]
|
|
109
|
+
def inline(line, number, unused, breaks)
|
|
110
|
+
left = unused
|
|
111
|
+
rewritten = line.gsub(INLINE) do
|
|
112
|
+
match = Regexp.last_match #: MatchData
|
|
113
|
+
if match[:attribute]
|
|
114
|
+
pattern = Bearings::Images::SOURCE_URLS.fetch(match[:source].to_s)
|
|
115
|
+
urls = match[:value].to_s.gsub(pattern) do |destination|
|
|
116
|
+
url, left = take(left, destination, number, breaks)
|
|
117
|
+
url || destination
|
|
118
|
+
end
|
|
119
|
+
next "#{match[:attribute]}#{urls}"
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
url, left = take(left, match[:destination], number, breaks)
|
|
123
|
+
url ? "#{match[:lead]}#{url}" : match.to_s
|
|
124
|
+
end
|
|
125
|
+
[rewritten, left]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# @rbs unused: Array[rewrite]
|
|
129
|
+
# @rbs destination: String?
|
|
130
|
+
# @rbs number: Integer
|
|
131
|
+
# @rbs breaks: Set[Integer]
|
|
132
|
+
# @rbs return: [String?, Array[rewrite]]
|
|
133
|
+
def take(unused, destination, number, breaks)
|
|
134
|
+
# Each link or image kramdown found in this paragraph rewrites one destination; a copy in plain text
|
|
135
|
+
# stays as written.
|
|
136
|
+
found = unused.index { |candidate, at, _| candidate == destination && same_paragraph?(at, number, breaks) }
|
|
137
|
+
return [nil, unused] unless found
|
|
138
|
+
|
|
139
|
+
[unused.fetch(found).last, unused.reject.with_index { |_, index| index == found }]
|
|
140
|
+
end
|
|
141
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
|
|
5
|
+
module Bearings::NetHttp
|
|
6
|
+
extend self
|
|
7
|
+
|
|
8
|
+
REDIRECTS = 5 #: Integer
|
|
9
|
+
TIMEOUT = 10 #: Numeric
|
|
10
|
+
|
|
11
|
+
# @rbs url: String
|
|
12
|
+
# @rbs timeout: Numeric
|
|
13
|
+
# @rbs return: Integer | Bearings::Checks::ExternalLinks::Failure
|
|
14
|
+
def status(url, timeout: TIMEOUT)
|
|
15
|
+
follow(URI(url), REDIRECTS, timeout)
|
|
16
|
+
rescue Timeout::Error
|
|
17
|
+
Bearings::Checks::ExternalLinks::Failure.new(reason: "timed out after #{timeout}s", timed_out: true)
|
|
18
|
+
rescue IOError, SocketError, SystemCallError, OpenSSL::SSL::SSLError, Net::ProtocolError, Net::HTTPBadResponse,
|
|
19
|
+
URI::Error => e
|
|
20
|
+
failure(e.message)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
# @rbs uri: URI::Generic
|
|
26
|
+
# @rbs redirects: Integer
|
|
27
|
+
# @rbs timeout: Numeric
|
|
28
|
+
# @rbs return: Integer | Bearings::Checks::ExternalLinks::Failure
|
|
29
|
+
def follow(uri, redirects, timeout)
|
|
30
|
+
host = uri.host
|
|
31
|
+
return failure("#{uri} is not an http or https URL") unless uri.is_a?(URI::HTTP) && host
|
|
32
|
+
|
|
33
|
+
options = { use_ssl: uri.is_a?(URI::HTTPS), open_timeout: timeout, read_timeout: timeout, write_timeout: timeout }
|
|
34
|
+
response = Net::HTTP.start(host, uri.port, **options) { |http| http.request(Net::HTTP::Get.new(uri)) }
|
|
35
|
+
location = response['location']
|
|
36
|
+
return response.code.to_i unless response.is_a?(Net::HTTPRedirection) && location
|
|
37
|
+
return failure("redirected more than #{REDIRECTS} times") if redirects.zero?
|
|
38
|
+
|
|
39
|
+
follow(uri + location, redirects - 1, timeout)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @rbs reason: String
|
|
43
|
+
# @rbs return: Bearings::Checks::ExternalLinks::Failure
|
|
44
|
+
def failure(reason)
|
|
45
|
+
Bearings::Checks::ExternalLinks::Failure.new(reason:, timed_out: false)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bearings::Pipeline
|
|
4
|
+
extend self
|
|
5
|
+
|
|
6
|
+
# @rbs!
|
|
7
|
+
# type result = [Array[Bearings::OutputFile], Array[Bearings::Problem]]
|
|
8
|
+
# type output = ^(Input) -> result
|
|
9
|
+
# type check = ^(Input, Array[Bearings::OutputFile]) -> Array[Bearings::Problem]
|
|
10
|
+
|
|
11
|
+
Input = Data.define(
|
|
12
|
+
:manifest, #: Bearings::Manifest
|
|
13
|
+
:manifest_file, #: String
|
|
14
|
+
:site, #: Bearings::Site
|
|
15
|
+
:sources, #: Hash[String, String]
|
|
16
|
+
:images, #: Hash[String, String]
|
|
17
|
+
:watched, #: Array[String]
|
|
18
|
+
:layout, #: String?
|
|
19
|
+
:landing, #: String?
|
|
20
|
+
:layout_file, #: String
|
|
21
|
+
:landing_file, #: String
|
|
22
|
+
:assets, #: Hash[String, Pathname]
|
|
23
|
+
:declared, #: Set[String]
|
|
24
|
+
:readme, #: String?
|
|
25
|
+
:readme_paths #: Array[String]
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
OUTPUTS = [
|
|
29
|
+
lambda do |input|
|
|
30
|
+
layout = input.layout
|
|
31
|
+
landing = input.landing
|
|
32
|
+
return files_only([]) unless layout && landing
|
|
33
|
+
|
|
34
|
+
Bearings::HtmlPages.call(input.site, layout:, landing:, assets: input.assets, images: input.images)
|
|
35
|
+
end,
|
|
36
|
+
->(input) { files_only(Bearings::Images.call(input.images)) },
|
|
37
|
+
->(input) { files_only(Bearings::MarkdownCopies.call(input.site, input.sources)) },
|
|
38
|
+
lambda do |input|
|
|
39
|
+
copies = Bearings::MarkdownCopies.call(input.site, input.sources)
|
|
40
|
+
full = input.manifest.outputs.llms_full
|
|
41
|
+
Bearings::LlmsTxt.call(input.site, copies, full:, manifest_file: input.manifest_file)
|
|
42
|
+
end,
|
|
43
|
+
->(input) { Bearings::Sitemap.call(input.site, manifest_file: input.manifest_file) },
|
|
44
|
+
->(input) { files_only(Bearings::Redirects.files(input.manifest.redirects, input.site.details.url)) },
|
|
45
|
+
->(input) { files_only([Bearings::SearchIndex.call(input.site)]) }
|
|
46
|
+
].freeze #: Array[output]
|
|
47
|
+
|
|
48
|
+
CHECKS = [
|
|
49
|
+
->(input, _files) { Bearings::Checks::ManifestMatchesDisk.call(manifest: input.manifest, paths: input.watched) },
|
|
50
|
+
->(input, _files) { Bearings::Checks::CodeExamples.call(input.site) },
|
|
51
|
+
->(input, _files) { Bearings::Checks::Diagrams.call(input.site) },
|
|
52
|
+
->(input, _files) { Bearings::Checks::ConstantNames.call(site: input.site, declared: input.declared) },
|
|
53
|
+
lambda do |input, _files|
|
|
54
|
+
limit_kb = input.manifest.checks.image_limit_kb
|
|
55
|
+
Bearings::Checks::Images.call(site: input.site, images: input.images, watched: input.watched, limit_kb:)
|
|
56
|
+
end,
|
|
57
|
+
lambda do |input, files|
|
|
58
|
+
redirects = input.manifest.redirects
|
|
59
|
+
# Redirects.problems knows the stubs; passed in as pages, each would replace itself.
|
|
60
|
+
pages = files - Bearings::Redirects.files(redirects, input.site.details.url)
|
|
61
|
+
Bearings::Redirects.problems(redirects, input.site, paths: pages.map(&:path), file: input.manifest_file)
|
|
62
|
+
end,
|
|
63
|
+
lambda do |input, files|
|
|
64
|
+
Bearings::Checks::Links.call(
|
|
65
|
+
input.site,
|
|
66
|
+
files,
|
|
67
|
+
sources: input.sources,
|
|
68
|
+
markdown: Bearings::Kramdown,
|
|
69
|
+
layout_file: input.layout_file,
|
|
70
|
+
landing_file: input.landing_file,
|
|
71
|
+
manifest_file: input.manifest_file
|
|
72
|
+
)
|
|
73
|
+
end,
|
|
74
|
+
lambda do |input, files|
|
|
75
|
+
readme = input.readme
|
|
76
|
+
next [] unless readme
|
|
77
|
+
|
|
78
|
+
Bearings::Checks::ReadmeLinks.call(
|
|
79
|
+
input.site, files, readme:, paths: input.readme_paths, markdown: Bearings::Kramdown
|
|
80
|
+
)
|
|
81
|
+
end
|
|
82
|
+
].freeze #: Array[check]
|
|
83
|
+
|
|
84
|
+
# @rbs input: Input
|
|
85
|
+
# @rbs outputs: Array[output]
|
|
86
|
+
# @rbs checks: Array[check]
|
|
87
|
+
# @rbs return: result
|
|
88
|
+
def call(input, outputs:, checks:)
|
|
89
|
+
results = outputs.map { |output| output.call(input) }
|
|
90
|
+
files = results.flat_map(&:first)
|
|
91
|
+
[files, [*results.flat_map(&:last), *checks.flat_map { |check| check.call(input, files) }]]
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
# @rbs files: Array[Bearings::OutputFile]
|
|
97
|
+
# @rbs return: result
|
|
98
|
+
def files_only(files)
|
|
99
|
+
[files, []]
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Zeitwerk ignores this file, so requiring it on its own loads the gem first.
|
|
4
|
+
require 'bearings'
|
|
5
|
+
require 'pathname'
|
|
6
|
+
require 'rake'
|
|
7
|
+
require 'socket'
|
|
8
|
+
|
|
9
|
+
module Bearings::Rake
|
|
10
|
+
extend self
|
|
11
|
+
|
|
12
|
+
MANIFEST = 'docs-site/manifest.yml' #: String
|
|
13
|
+
SITE = '_site' #: String
|
|
14
|
+
SIG = 'sig' #: String
|
|
15
|
+
LAYOUT = 'layout.html.erb' #: String
|
|
16
|
+
LANDING = 'index.html.erb' #: String
|
|
17
|
+
HOST = '127.0.0.1' #: String
|
|
18
|
+
PORT = 8000 #: Integer
|
|
19
|
+
URL = "http://localhost:#{PORT}".freeze #: String
|
|
20
|
+
INTERVAL = 0.5 #: Float
|
|
21
|
+
FAILED = "Build failed; #{SITE}/ keeps the last good build".freeze #: String
|
|
22
|
+
|
|
23
|
+
# @rbs!
|
|
24
|
+
# type server = ^(String root) -> void
|
|
25
|
+
# type watch = ^(Bearings::Watch::listing) -> Enumerable[Array[String]]
|
|
26
|
+
|
|
27
|
+
SERVER = lambda do |root|
|
|
28
|
+
server = TCPServer.new(HOST, PORT)
|
|
29
|
+
Thread.new { Bearings::Server.call(server, root) }
|
|
30
|
+
end #: server
|
|
31
|
+
WATCH = ->(listing) { Bearings::Watch.changes(listing:, wait: -> { sleep(INTERVAL) }) } #: watch
|
|
32
|
+
|
|
33
|
+
# @rbs manifest: String
|
|
34
|
+
# @rbs http: Bearings::_HttpClient
|
|
35
|
+
# @rbs outputs: Array[Bearings::Pipeline::output]
|
|
36
|
+
# @rbs checks: Array[Bearings::Pipeline::check]
|
|
37
|
+
# @rbs server: server
|
|
38
|
+
# @rbs watch: watch
|
|
39
|
+
# @rbs return: void
|
|
40
|
+
def install(
|
|
41
|
+
manifest: MANIFEST,
|
|
42
|
+
http: Bearings::NetHttp,
|
|
43
|
+
outputs: Bearings::Pipeline::OUTPUTS,
|
|
44
|
+
checks: Bearings::Pipeline::CHECKS,
|
|
45
|
+
server: SERVER,
|
|
46
|
+
watch: WATCH
|
|
47
|
+
)
|
|
48
|
+
define('docs', "Build the docs site into #{SITE}/") { build(manifest, outputs, []) }
|
|
49
|
+
define('docs:check', 'Build the docs site and check it, failing on any error') { build(manifest, outputs, checks) }
|
|
50
|
+
task('docs:serve', "Build the docs site, serve it at #{URL} and rebuild it when its files change") do
|
|
51
|
+
serve(manifest, outputs, server, watch)
|
|
52
|
+
end
|
|
53
|
+
define('docs:check:external', 'Request every external link in the guides and report the ones that fail') do
|
|
54
|
+
external(manifest, http)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
# @rbs name: String
|
|
61
|
+
# @rbs description: String
|
|
62
|
+
# @rbs &problems: () -> Array[Bearings::Problem]
|
|
63
|
+
# @rbs return: void
|
|
64
|
+
def define(name, description, &)
|
|
65
|
+
task(name, description) { finish(yield, ENV.fetch('FORMAT', nil)) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @rbs name: String
|
|
69
|
+
# @rbs description: String
|
|
70
|
+
# @rbs &action: () -> void
|
|
71
|
+
# @rbs return: void
|
|
72
|
+
def task(name, description, &)
|
|
73
|
+
::Rake.application.last_description = description
|
|
74
|
+
::Rake::Task.define_task(name, &)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @rbs path: String
|
|
78
|
+
# @rbs outputs: Array[Bearings::Pipeline::output]
|
|
79
|
+
# @rbs checks: Array[Bearings::Pipeline::check]
|
|
80
|
+
# @rbs return: Array[Bearings::Problem]
|
|
81
|
+
def build(path, outputs, checks)
|
|
82
|
+
files, problems = render(path, outputs, checks)
|
|
83
|
+
files ? [*problems, *Bearings::Writer.call(files, SITE)] : problems
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# @rbs path: String
|
|
87
|
+
# @rbs outputs: Array[Bearings::Pipeline::output]
|
|
88
|
+
# @rbs server: server
|
|
89
|
+
# @rbs watch: watch
|
|
90
|
+
# @rbs return: void
|
|
91
|
+
def serve(path, outputs, server, watch)
|
|
92
|
+
# Each rebuild's report has to show as it happens, even when stdout is a pipe that would buffer it.
|
|
93
|
+
$stdout.sync = true
|
|
94
|
+
# Listed before the first build, so an edit saved while it runs still counts as a change.
|
|
95
|
+
changes = watch.call(->(before) { listing(path, before) })
|
|
96
|
+
rebuild(path, outputs)
|
|
97
|
+
server.call(SITE)
|
|
98
|
+
puts "Serving #{SITE}/ at #{URL}; press Ctrl-C to stop"
|
|
99
|
+
changes.each do |changed|
|
|
100
|
+
puts "#{changed.join(', ')} changed; rebuilding"
|
|
101
|
+
rebuild(path, outputs)
|
|
102
|
+
end
|
|
103
|
+
rescue Interrupt
|
|
104
|
+
puts "Stopped serving #{SITE}/"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# @rbs path: String
|
|
108
|
+
# @rbs outputs: Array[Bearings::Pipeline::output]
|
|
109
|
+
# @rbs return: void
|
|
110
|
+
def rebuild(path, outputs)
|
|
111
|
+
files, found = render(path, outputs, [])
|
|
112
|
+
problems = files && !Bearings::Report.failed?(found) ? [*found, *Bearings::Writer.call(files, SITE)] : found
|
|
113
|
+
puts Bearings::Report.text(problems) if problems.any?
|
|
114
|
+
puts Bearings::Report.failed?(problems) ? FAILED : "Built #{SITE}/"
|
|
115
|
+
# The layout and landing page are the repo's ERB, run as Ruby: a typo saved in one raises, as a SyntaxError (a
|
|
116
|
+
# ScriptError) for some, and the server has to outlive it.
|
|
117
|
+
rescue StandardError, ScriptError => e
|
|
118
|
+
puts "#{e.class}: #{e.message}", FAILED
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# @rbs path: String
|
|
122
|
+
# @rbs outputs: Array[Bearings::Pipeline::output]
|
|
123
|
+
# @rbs checks: Array[Bearings::Pipeline::check]
|
|
124
|
+
# @rbs return: [Array[Bearings::OutputFile]?, Array[Bearings::Problem]]
|
|
125
|
+
def render(path, outputs, checks)
|
|
126
|
+
input, problems = input(path)
|
|
127
|
+
return [nil, problems] unless input
|
|
128
|
+
|
|
129
|
+
files, found = Bearings::Pipeline.call(input, outputs:, checks:)
|
|
130
|
+
[files, [*problems, *found]]
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# @rbs path: String
|
|
134
|
+
# @rbs before: Hash[String, Time?]
|
|
135
|
+
# @rbs return: Hash[String, Time?]
|
|
136
|
+
def listing(path, before)
|
|
137
|
+
manifest, = manifest(path)
|
|
138
|
+
named = manifest ? [*source_paths(manifest), *watched(manifest.watch)] : [] #: Array[String]
|
|
139
|
+
listed = [*named, *assets(File.dirname(path), []).values.map(&:to_s)]
|
|
140
|
+
# The last listing's paths carry the images the guides use, which only a render finds, and while the manifest does
|
|
141
|
+
# not parse they stand in for the paths it names; without them each would read as changed.
|
|
142
|
+
known = mtimes([*before.keys, *listed])
|
|
143
|
+
# Finding the images the guides use renders every guide, so it waits for a change to a listed file, the only
|
|
144
|
+
# thing that can change them.
|
|
145
|
+
return known if !manifest || known == before
|
|
146
|
+
|
|
147
|
+
site, = site(manifest, sources(manifest))
|
|
148
|
+
mtimes([*listed, *Bearings::Images.paths(site)])
|
|
149
|
+
# A guide that fails to render still reads as changed, so the rebuild runs and prints why; a file deleted between
|
|
150
|
+
# its stat calls falls back to the paths already stat'd.
|
|
151
|
+
rescue StandardError
|
|
152
|
+
known || before
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# @rbs paths: Array[String]
|
|
156
|
+
# @rbs return: Hash[String, Time?]
|
|
157
|
+
def mtimes(paths)
|
|
158
|
+
files = paths.map { |file| Pathname(file).cleanpath.to_s }.uniq
|
|
159
|
+
files.to_h { |file| [file, (File.mtime(file) if File.file?(file))] }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# @rbs path: String
|
|
163
|
+
# @rbs http: Bearings::_HttpClient
|
|
164
|
+
# @rbs return: Array[Bearings::Problem]
|
|
165
|
+
def external(path, http)
|
|
166
|
+
manifest, problems = manifest(path)
|
|
167
|
+
return problems unless manifest
|
|
168
|
+
|
|
169
|
+
site, built = site(manifest, sources(manifest))
|
|
170
|
+
[*built, *Bearings::Checks::ExternalLinks.call(site, http)]
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# @rbs path: String
|
|
174
|
+
# @rbs return: [Bearings::Pipeline::Input?, Array[Bearings::Problem]]
|
|
175
|
+
def input(path)
|
|
176
|
+
manifest, problems = manifest(path)
|
|
177
|
+
return [nil, problems] unless manifest
|
|
178
|
+
|
|
179
|
+
sources = sources(manifest)
|
|
180
|
+
site, built = site(manifest, sources)
|
|
181
|
+
dir = File.dirname(path)
|
|
182
|
+
layout_file = File.join(dir, LAYOUT)
|
|
183
|
+
landing_file = File.join(dir, LANDING)
|
|
184
|
+
templates = [layout_file, landing_file]
|
|
185
|
+
layout, landing = templates.map { |template| File.read(template) if File.file?(template) }
|
|
186
|
+
readme = read_readme
|
|
187
|
+
input = Bearings::Pipeline::Input.new(
|
|
188
|
+
manifest:,
|
|
189
|
+
manifest_file: path,
|
|
190
|
+
site:,
|
|
191
|
+
sources:,
|
|
192
|
+
images: images(site),
|
|
193
|
+
watched: watched(manifest.watch),
|
|
194
|
+
layout:,
|
|
195
|
+
landing:,
|
|
196
|
+
layout_file:,
|
|
197
|
+
landing_file:,
|
|
198
|
+
assets: assets(dir, [File.basename(path), LAYOUT, LANDING]),
|
|
199
|
+
declared: declared(manifest.site.namespaces),
|
|
200
|
+
readme:,
|
|
201
|
+
readme_paths: readme_paths(readme)
|
|
202
|
+
)
|
|
203
|
+
[input, [*built, *templates.reject { |template| File.file?(template) }.map { |template| no_template(template) }]]
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# @rbs path: String
|
|
207
|
+
# @rbs return: [Bearings::Manifest?, Array[Bearings::Problem]]
|
|
208
|
+
def manifest(path)
|
|
209
|
+
return [nil, [no_manifest(path)]] unless File.file?(path)
|
|
210
|
+
|
|
211
|
+
manifest = Bearings::Manifest::Parser.parse(File.read(path), file: path)
|
|
212
|
+
manifest.is_a?(Array) ? [nil, manifest] : [manifest, []]
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# @rbs manifest: Bearings::Manifest
|
|
216
|
+
# @rbs return: Hash[String, String]
|
|
217
|
+
def sources(manifest)
|
|
218
|
+
source_paths(manifest).select { |path| File.file?(path) }.to_h { |path| [path, File.read(path)] }
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# @rbs manifest: Bearings::Manifest
|
|
222
|
+
# @rbs return: Array[String]
|
|
223
|
+
def source_paths(manifest)
|
|
224
|
+
[*manifest.groups.flat_map(&:pages).map(&:source), manifest.site.version].compact
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# @rbs site: Bearings::Site
|
|
228
|
+
# @rbs return: Hash[String, String]
|
|
229
|
+
def images(site)
|
|
230
|
+
Bearings::Images.paths(site).select { |path| File.file?(path) }.to_h { |path| [path, File.binread(path)] }
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# @rbs manifest: Bearings::Manifest
|
|
234
|
+
# @rbs sources: Hash[String, String]
|
|
235
|
+
# @rbs return: [Bearings::Site, Array[Bearings::Problem]]
|
|
236
|
+
def site(manifest, sources)
|
|
237
|
+
Bearings::Site.build(manifest:, files: sources, markdown: Bearings::Kramdown)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# @rbs folders: Array[String]
|
|
241
|
+
# @rbs return: Array[String]
|
|
242
|
+
def watched(folders)
|
|
243
|
+
Dir.glob(folders.map { |folder| File.join(folder, '**', '*') }).select { |path| File.file?(path) }
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# @rbs dir: String
|
|
247
|
+
# @rbs read: Array[String]
|
|
248
|
+
# @rbs return: Hash[String, Pathname]
|
|
249
|
+
def assets(dir, read)
|
|
250
|
+
# A manifest at the repo root would otherwise copy the last build into the next one.
|
|
251
|
+
site = "#{File.expand_path(SITE)}/"
|
|
252
|
+
files = (Dir.glob('**/*', base: dir) - read).map { |path| File.join(dir, path) }.select do |file|
|
|
253
|
+
File.file?(file) && !File.expand_path(file).start_with?(site)
|
|
254
|
+
end
|
|
255
|
+
files.to_h { |file| [file.delete_prefix("#{dir}/"), Pathname(file)] }
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# @rbs return: String?
|
|
259
|
+
def read_readme
|
|
260
|
+
file = Bearings::Checks::ReadmeLinks::FILE
|
|
261
|
+
File.read(file) if File.file?(file)
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# @rbs readme: String?
|
|
265
|
+
# @rbs return: Array[String]
|
|
266
|
+
def readme_paths(readme)
|
|
267
|
+
return [] unless readme
|
|
268
|
+
|
|
269
|
+
Bearings::Checks::ReadmeLinks.paths(readme, markdown: Bearings::Kramdown).select { |path| File.exist?(path) }
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# @rbs namespaces: Array[String]
|
|
273
|
+
# @rbs return: Set[String]
|
|
274
|
+
def declared(namespaces)
|
|
275
|
+
# Only a site that names its namespaces checks constants, so only it needs the rbs gem, and bearings/rbs is
|
|
276
|
+
# required here, by name, because Zeitwerk ignores it.
|
|
277
|
+
return Set.new if namespaces.empty?
|
|
278
|
+
|
|
279
|
+
require 'bearings/rbs'
|
|
280
|
+
Bearings::Rbs.declared(SIG)
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# @rbs path: String
|
|
284
|
+
# @rbs return: Bearings::Problem
|
|
285
|
+
def no_manifest(path)
|
|
286
|
+
not_found(path, 'manifest', 'add the manifest, or pass its path to Bearings::Rake.install(manifest:)')
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# @rbs path: String
|
|
290
|
+
# @rbs return: Bearings::Problem
|
|
291
|
+
def no_template(path)
|
|
292
|
+
not_found(
|
|
293
|
+
path,
|
|
294
|
+
'layout',
|
|
295
|
+
"add it beside the manifest; #{LAYOUT} renders each guide and #{LANDING} the landing page"
|
|
296
|
+
)
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# @rbs path: String
|
|
300
|
+
# @rbs check: String
|
|
301
|
+
# @rbs hint: String
|
|
302
|
+
# @rbs return: Bearings::Problem
|
|
303
|
+
def not_found(path, check, hint)
|
|
304
|
+
Bearings::Problem.new(check:, level: :error, file: path, line: 1, message: 'was not found', hint:)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# @rbs problems: Array[Bearings::Problem]
|
|
308
|
+
# @rbs format: String?
|
|
309
|
+
# @rbs return: void
|
|
310
|
+
def finish(problems, format)
|
|
311
|
+
if format == 'json'
|
|
312
|
+
puts Bearings::Report.json(problems)
|
|
313
|
+
elsif problems.any?
|
|
314
|
+
puts Bearings::Report.text(problems)
|
|
315
|
+
end
|
|
316
|
+
exit 1 if Bearings::Report.failed?(problems)
|
|
317
|
+
end
|
|
318
|
+
end
|
data/lib/bearings/rbs.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Zeitwerk ignores this file, so requiring it on its own loads the gem first.
|
|
4
|
+
require 'bearings'
|
|
5
|
+
require 'pathname'
|
|
6
|
+
require 'rbs'
|
|
7
|
+
|
|
8
|
+
module Bearings::Rbs
|
|
9
|
+
extend self
|
|
10
|
+
|
|
11
|
+
# @rbs dir: String
|
|
12
|
+
# @rbs return: Set[String]
|
|
13
|
+
def declared(dir)
|
|
14
|
+
loader = RBS::EnvironmentLoader.new(core_root: nil)
|
|
15
|
+
loader.add(path: Pathname.new(dir))
|
|
16
|
+
env = RBS::Environment.from_loader(loader)
|
|
17
|
+
[*env.class_decls.keys, *env.class_alias_decls.keys, *env.constant_decls.keys]
|
|
18
|
+
.to_set { |name| name.to_s.delete_prefix('::') }
|
|
19
|
+
end
|
|
20
|
+
end
|