forked from slackapi/node-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_generator.rb
83 lines (72 loc) · 2.94 KB
/
code_generator.rb
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
#!/usr/bin/env ruby
#
# prerequisites
# npm install quicktype
require 'open3'
index_file = __dir__ + '/../packages/web-api/src/response/index.ts'
File.truncate(index_file, 0)
class TsWriter
NOTICE = [
"/////////////////////////////////////////////////////////////////////////////////////////",
"// //",
"// !!! DO NOT EDIT THIS FILE !!! //",
"// //",
"// This file is auto-generated by scripts/generate-web-api-types.sh in the repository. //",
"// Please refer to the script code to learn how to update the source data. //",
"// //",
"/////////////////////////////////////////////////////////////////////////////////////////",
""
].join("\n")
def write(root_class_name, json_path, typedef_filepath, input_json)
cmd = "npx quicktype --just-types --alphabetize-properties --all-properties-optional --acronym-style original -t #{root_class_name} -l ts"
puts "Generating #{root_class_name} from #{json_path}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
stdin.write(input_json)
stdin.close()
source = "/* eslint-disable */\n#{NOTICE}\nimport { WebAPICallResult } from '../WebClient';\n" + stdout.read
source.gsub!(
"export interface #{root_class_name} {",
"export type #{root_class_name} = WebAPICallResult & {"
)
source.gsub!(/^ /, ' ')
source.gsub!('"', "'")
source.sub!(/^}$/, '};')
File.open(typedef_filepath, 'w') { |f|
f.write(source)
}
end
end
def append_to_index(root_class_name, index_file)
File.open(index_file, 'a') do |index_f|
index_f.puts("export { #{root_class_name} } from './#{root_class_name}';")
end
end
end
ts_writer = TsWriter.new
Dir.glob(__dir__ + '/../tmp/java-slack-sdk/json-logs/samples/api/*').sort.each do |json_path|
File.open(json_path) do |json_file|
root_class_name = ''
prev_c = nil
filename = json_path.split('/').last.gsub(/\.json$/, '')
if !filename.include? "admin.analytics.getFile"
filename.split('').each do |c|
if prev_c.nil? || prev_c == '.'
root_class_name << c.upcase
elsif c == '.'
# noop
else
root_class_name << c
end
prev_c = c
end
if root_class_name.start_with? 'Openid'
root_class_name.sub!('Openid', 'OpenID')
end
root_class_name << 'Response'
typedef_filepath = __dir__ + "/../packages/web-api/src/response/#{root_class_name}.ts"
input_json = json_file.read
ts_writer.write(root_class_name, json_path, typedef_filepath, input_json)
ts_writer.append_to_index(root_class_name, index_file)
end
end
end