@@ -28,32 +28,34 @@ module Google
28
28
29
29
desc 'gen OUTDIR' , 'Generate ruby API from an API description'
30
30
method_options url : :array , file : :array , from_discovery : :boolean , preferred_only : :boolean ,
31
- verbose : :boolean , names : :string , names_out : :string
31
+ verbose : :boolean , names : :string , names_out : :string , api : :string , clean : :boolean
32
32
def gen ( dir )
33
33
ensure_active_support
34
34
require 'google/apis/generator'
35
35
36
36
self . destination_root = dir
37
37
Google ::Apis . logger . level = Logger ::DEBUG if options [ :verbose ]
38
- generate_from_url ( options [ :url ] ) if options [ :url ]
39
- generate_from_file ( options [ :file ] ) if options [ :file ]
40
- generate_from_discovery ( preferred_only : options [ :preferred_only ] ) if options [ :from_discovery ]
41
- create_file ( options [ :names_out ] ) { |*| generator . dump_api_names } if options [ :names_out ]
38
+ count = 0
39
+ count += generate_from_url ( options [ :url ] ) if options [ :url ]
40
+ count += generate_from_file ( options [ :file ] ) if options [ :file ]
41
+ count += generate_specific_apis ( options [ :api ] ) if options [ :api ]
42
+ count += generate_from_discovery ( preferred_only : options [ :preferred_only ] ) if options [ :from_discovery ]
43
+ count += clean_from_discovery if options [ :clean ]
44
+ create_file ( options [ :names_out ] ) { |*| generator . dump_api_names } if count > 0 && options [ :names_out ]
42
45
end
43
46
44
47
desc 'list' , 'List public APIs'
45
48
method_options verbose : :boolean , preferred_only : :boolean
46
49
def list
47
50
Google ::Apis . logger . level = Logger ::DEBUG if options [ :verbose ]
48
- discovery = Discovery ::DiscoveryService . new
49
- apis = discovery . list_apis
50
- apis . items . each do |api |
51
+ discovery_api_list . each do |api |
51
52
say sprintf ( '%s - %s' , api . id , api . description ) . strip unless options [ :preferred_only ] && !api . preferred?
52
53
end
53
54
end
54
55
55
56
no_commands do
56
- def generate_from_url ( urls )
57
+ def generate_from_url ( urls , first_only : false )
58
+ count = 0
57
59
Array ( urls ) . each do |url |
58
60
begin
59
61
json = discovery . http ( :get , url )
@@ -62,7 +64,10 @@ module Google
62
64
next
63
65
end
64
66
generate_api ( json )
67
+ return 1 if first_only
68
+ count += 1
65
69
end
70
+ count
66
71
end
67
72
68
73
def generate_from_file ( files )
@@ -71,15 +76,38 @@ module Google
71
76
generate_api ( f . read )
72
77
end
73
78
end
79
+ Array ( files ) . size
80
+ end
81
+
82
+ def generate_specific_apis ( apis )
83
+ discovery_apis = discovery_api_list . each_with_object ( { } ) do |api , hash |
84
+ hash [ "#{ api . name } .#{ api . version } " ] = api
85
+ end
86
+ paused_apis = Array ( api_list_config [ "pause" ] )
87
+ count = 0
88
+ Array ( apis ) . each do |name_version |
89
+ api = discovery_apis [ name_version ]
90
+ if api . nil?
91
+ say "API #{ api } is not in the discovery list."
92
+ elsif paused_apis . include? name_version
93
+ say "Ignoring paused API #{ api . name } #{ api . version } "
94
+ else
95
+ discovery_rest_url = "https://raw.githubusercontent.com/googleapis/discovery-artifact-manager/master/discoveries/#{ api . name } .#{ api . version } .json"
96
+ say sprintf ( 'Loading %s, version %s from %s' , api . name , api . version , discovery_rest_url )
97
+ generate_from_url ( [ discovery_rest_url , api . discovery_rest_url ] , first_only : true )
98
+ count += 1
99
+ end
100
+ end
101
+ count
74
102
end
75
103
76
104
def generate_from_discovery ( preferred_only : false )
77
105
say 'Fetching API list'
78
- apis = discovery . list_apis
79
- exclude_apis = api_list_config [ "exclude" ] || [ ]
80
- apis . items . each do |api |
81
- if exclude_apis . include? "#{ api . name } .#{ api . version } "
82
- say "Ignoring excluded API #{ api . name } #{ api . version } "
106
+ paused_apis = Array ( api_list_config [ "pause" ] )
107
+ count = 0
108
+ discovery_api_list . each do |api |
109
+ if paused_apis . include? "#{ api . name } .#{ api . version } "
110
+ say "Ignoring paused API #{ api . name } #{ api . version } "
83
111
elsif ( preferred_only && !api . preferred? )
84
112
say sprintf ( 'Ignoring disoverable API %s' , api . id )
85
113
else
@@ -91,9 +119,27 @@ module Google
91
119
# by the Discovery index.
92
120
discovery_rest_url = "https://raw.githubusercontent.com/googleapis/discovery-artifact-manager/master/discoveries/#{ api . name } .#{ api . version } .json"
93
121
say sprintf ( 'Loading %s, version %s from %s' , api . name , api . version , discovery_rest_url )
94
- generate_from_url ( discovery_rest_url )
122
+ generate_from_url ( [ discovery_rest_url , api . discovery_rest_url ] , first_only : true )
123
+ count += 1
95
124
end
96
125
end
126
+ count
127
+ end
128
+
129
+ def clean_from_discovery
130
+ count = 0
131
+ apis = discovery_api_list . map { |api | "#{ api . name . underscore } _#{ api . version . tr '.' , '_' } " }
132
+ Dir . chdir ( "#{ destination_root } /google/apis" ) do
133
+ Dir . glob ( "*.rb" ) . each do |filename |
134
+ filename = File . basename ( filename , ".rb" )
135
+ unless apis . include? filename
136
+ FileUtils . rm_r filename
137
+ FileUtils . rm "#{ filename } .rb"
138
+ count += 1
139
+ end
140
+ end
141
+ end
142
+ count
97
143
end
98
144
99
145
def generate_api ( json )
@@ -107,6 +153,20 @@ module Google
107
153
@discovery ||= Discovery ::DiscoveryService . new
108
154
end
109
155
156
+ def discovery_api_list
157
+ @discovery_api_list ||= begin
158
+ items = discovery . list_apis . items
159
+ excluded = Array ( api_list_config [ "exclude" ] )
160
+ items . delete_if { |item | excluded . include? "#{ item . name } .#{ item . version } " }
161
+ Array ( api_list_config [ "include" ] ) . each do |include_hash |
162
+ include_hash . symbolize_keys!
163
+ include_item = Discovery ::DirectoryList ::Item . new ( **include_hash )
164
+ items << include_item unless items . any? { |item | item . name == include_item . name && item . version == include_item . version }
165
+ end
166
+ items
167
+ end
168
+ end
169
+
110
170
def generator
111
171
@generator ||= Google ::Apis ::Generator . new ( api_names : options [ :names ] , api_names_out : options [ :names_out ] )
112
172
end
@@ -117,7 +177,9 @@ module Google
117
177
118
178
def ensure_active_support
119
179
begin
180
+ require 'active_support'
120
181
require 'active_support/inflector'
182
+ require 'active_support/core_ext'
121
183
rescue LoadError => e
122
184
error 'ActiveSupport is required, please run:'
123
185
error 'gem install activesupport'
0 commit comments