-
-
Notifications
You must be signed in to change notification settings - Fork 656
Expand file tree
/
Copy pathgenerate-api-samples.rb
More file actions
executable file
Β·128 lines (105 loc) Β· 3.39 KB
/
Copy pathgenerate-api-samples.rb
File metadata and controls
executable file
Β·128 lines (105 loc) Β· 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env ruby
# frozen_string_literal: true
require "fileutils"
require "json"
require "open3"
require "pathname"
ROOT = Pathname(__dir__).parent.freeze
SAMPLES = {
analytics_cask_install_homebrew_cask_30d: "analytics/cask-install/homebrew-cask/30d.json",
analytics_install_30d: "analytics/install/30d.json",
analytics_install_homebrew_core_30d: "analytics/install/homebrew-core/30d.json",
cask_docker_desktop: "cask/docker-desktop.json",
formula_wget: "formula/wget.json",
formula: "formula/wget.json",
}.freeze
# Use template values for the API contents to allow testing without generating all API files
USE_TEMPLATES = ARGV.include? "--template"
TEMPLATE = <<~TEMPLATE
{
"name": "@@TEMPLATE@@",
"formulae": [],
"items": []
}
TEMPLATE
def failed!
@failed ||= true
end
def failed?
@failed
end
def generate_api_samples
includes_dir = "_includes/api-samples"
FileUtils.rm_rf includes_dir
FileUtils.mkdir_p includes_dir
SAMPLES.each do |name, api_path|
contents = if File.extname(api_path) == ".json"
format_json_contents name, api_path
else
codify api_file_contents(api_path), language: "rb"
end
File.write "#{includes_dir}/#{name}.md", contents
end
end
def api_file_contents(api_path)
return TEMPLATE if USE_TEMPLATES
api_file = ROOT/"_data/#{api_path}"
return api_file.read.strip if api_file.exist?
warn "Skipping missing API file: #{api_file}"
failed!
end
def codify(contents, language:)
"```#{language}\n#{contents}\n```"
end
def format_json_contents(name, api_path)
contents = api_file_contents(api_path)
return if failed?
contents = JSON.parse contents
# Only include a select group of items to reduce the length of the sample
case name
when :analytics_cask_install_homebrew_cask_30d
contents["formulae"].select! do |token, _|
%w[docker docker-edge docker-toolbox].include? token
end
when :analytics_install_30d
contents["items"].select! do |obj|
["wget", "wget --HEAD"].include? obj["formula"]
end
when :analytics_install_homebrew_core_30d
contents["formulae"].select! do |formula_name, _|
formula_name == "wget"
end
end
contents = JSON.pretty_generate contents
# Consolidate empty [] and {} into one-liners
contents.gsub!(/: \[\n+\s*\]/, ": []")
contents.gsub!(/: {\n+\s*}/, ": {}")
# Add "..." where needed
case name
when :analytics_cask_install_homebrew_cask_30d
contents.sub!(/("formulae": {)/, "\\1\n ...")
contents.sub!(/\](?=\n }\n})/, "],\n ...")
when :analytics_install_30d
contents.sub!(/("items": \[)/, "\\1\n ...")
contents.sub!(/( },)(?=\n {)/, "\\1\n ...")
contents.sub!(/}(?=\n \]\n})/, "},\n ...")
when :analytics_install_homebrew_core_30d
contents.sub!(/("formulae": {)/, "\\1\n ...")
contents.sub!(/}(?=\n \])/, "},\n ...")
contents.sub!(/\](?=\n }\n})/, "],\n ...")
when :formula
# Each entry in the formula list is a full formula without the analytics or generated date
contents.sub!(/(},\n "analytics":*)$/, "}\n}")
contents = contents.lines.map { |line| " #{line}" }.join
contents = <<~RESPONSE.strip
[
...
#{contents},
...
]
RESPONSE
end
codify contents, language: "json"
end
generate_api_samples
abort if failed?