summaryrefslogtreecommitdiff
path: root/switcher4.py
blob: 6cd773920eb380749f8703454a9ab2cbf7ba2512 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# Copyright 2019 - The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

###############################################################################
# This script adds a HTML snippet to the generated reference docs located at
# developer.android.com/reference.  The snippet renders HTML that adds links to
# toggle between the Java and Kotlin versions of the page.
###############################################################################

import getopt
import os
import sys


# GLOBAL FLAGS

global stubs
global java_stubs, kotlin_stubs
global work, verbose, show_solo, max_stubs
global java_source_abs_path
global kotlin_source_abs_path

verbose = False  # set True to list all files as they are stubbed (--verbose)
work = False  # set True to insert stubs, False to do a dry run for stats (--work)
show_solo = False  # set True to list files that only appear in one language, rather than both (--solo)
max_stubs = 0  # set positive to create a limited number of stubs (--max 12)


# You must run the script from the refodcs reference/ root directory

java_ref_root = os.getcwd()
kotlin_ref_root = os.path.join(java_ref_root, "kotlin")
root = os.path.split(java_ref_root)[1]
if root != "reference":
  print ("You must cd to the refocs reference/ root directoy")
  sys.exit()


# This method inserts the language switcher into the two top-level Android
# Platform pages: packages.html and classes.html
# For both Java and Kotlin
def insert_platform_summaries():
  global stubs
  global java_stubs, kotlin_stubs
  global verbose, work, show_solo
  global java_source_abs_path
  global kotlin_source_abs_path

  stubs = 0
  java_stubs = 0
  kotlin_stubs = 0

  java_source_abs_path = java_ref_root
  kotlin_source_abs_path = kotlin_ref_root
  insert_stub(os.path.join(java_ref_root, "packages.html"), True, True)
  insert_stub(os.path.join(kotlin_ref_root, "packages.html"), False, True)

  insert_stub(os.path.join(java_ref_root, "classes.html"), True, True)
  insert_stub(os.path.join(kotlin_ref_root, "classes.html"), False, True)

# This method uses switcher2, which assumes the refdocs stay in their current
# assymetrical dirs (ref/android and ref/kotlin/android)
# And just puts the switcher in the existing docs
def insert_stub(doc, java, both):
  global stubs
  global java_stubs, kotlin_stubs
  global verbose, work, show_solo
  global java_source_abs_path
  global kotlin_source_abs_path

  stubs = stubs+1

  if verbose:
    print "File: ", stubs, doc
  else:
    fn  = os.path.split(doc)
    print "File: ", stubs, fn[1], "\r",

  if (java):
      java_stubs = java_stubs + 1
  else:
      kotlin_stubs = kotlin_stubs + 1


  if (work):
    if (java):
      file_path = doc[len(java_ref_root)+1:]
      stub = doc.replace(java_source_abs_path, kotlin_source_abs_path)
      if (both):
        slug1 = "sed -i 's/<\/h1>/{}/' {}".format("<\/h1>\\n{% setvar page_path %}_page_path_{% endsetvar %}\\n{% setvar can_switch %}1{% endsetvar %}\\n{% include \"reference\/_java_switcher2.md\" %}",doc)
      else:
        slug1 = "sed -i 's/<\/h1>/{}/' {}".format("<\/h1>\\n{% include \"reference\/_java_switcher2.md\" %}",doc)
    else:
      file_path = doc[len(kotlin_ref_root)+1:]
      stub = doc.replace(kotlin_source_abs_path, java_source_abs_path)
      if (both):
        slug1 = "sed -i 's/<\/h1>/{}/' {}".format("<\/h1>\\n{% setvar page_path %}_page_path_{% endsetvar %}\\n{% setvar can_switch %}1{% endsetvar %}\\n{% include \"reference\/_kotlin_switcher2.md\" %}",doc)
      else:
        slug1 = "sed -i 's/<\/h1>/{}/' {}".format("<\/h1>\\n{% include \"reference\/_kotlin_switcher2.md\" %}",doc)

    os.system(slug1)
    if (both):
      page_path_slug = "sed -i 's/_page_path_/{}/' {}".format(file_path.replace("/","\/"),doc)
      os.system(page_path_slug)


def scan_files(stem):
  global work, verbose, show_solo, max_stubs
  global stubs
  global java_stubs, kotlin_stubs
  global java_source_abs_path
  global kotlin_source_abs_path

  java_source_abs_path = os.path.join(java_ref_root, stem)
  kotlin_source_abs_path = os.path.join(kotlin_ref_root, stem)

  # Pass 1
  # Loop over java content, create stubs for java,
  # and for corresponding Kotlin (when it exsits)

  # solo is java-only classes
  # both is java+kotlin
  stubs = 0
  java_stubs = 0
  kotlin_stubs = 0
  solo = 0
  both = 0

  print "*** PASS1 (Java) ***"
  maxed_out = False
  for root, dirs, files in os.walk(java_source_abs_path):
      if maxed_out:
        break;
      for file_ in files:
        ext = os.path.splitext(file_)
        ext = ext[1]
        if not ext:
          # this catches package-lists with no extension
          print "***", os.path.join(root, file_)
        elif ext != ".html":
          # filter out png, yaml, etc
          continue
        else:
          # we have java content
          doc = os.path.join(root, file_)



          # look for matching kotlin file
          kotlinsource = doc.replace(java_source_abs_path, kotlin_source_abs_path)
          if os.path.isfile(kotlinsource):
             # corresponding kotlin content exists
             insert_stub(doc, True, True)
             insert_stub(kotlinsource, False, True)
             both = both+1
          else:
            # no kotlin content
            if (show_solo):
              print "solo: ", doc
            insert_stub(doc, True, False)
            solo = solo+1

          if max_stubs>0 and stubs>=max_stubs:
            print
            print "max java stubs: ", max_stubs
            maxed_out = True;
            break

  print "Java+Kotlin:", both, "Only Java:", solo
  print


  # PASS 2
  # Loop over kotlin content, create stubs for Kotlin-only APIs
  print "*** PASS2 (Kotlin) ***"
  solo = 0
  both = 0
  maxed_out = False
  stubs = 0
  for root, dirs, files in os.walk(kotlin_source_abs_path):
      if maxed_out:
        break;
      for file_ in files:
        ext = os.path.splitext (file_)
        ext = ext[1]
        if not ext:
          # this catches package-lists with no extension
          print "***", os.path.join(root, file_)
        elif ext != ".html":
          # filter out png, yaml, etc
          continue
        else:
          # we have kotlin content
          doc = os.path.join(root, file_)
          javadoc = doc.replace(kotlin_source_abs_path, java_source_abs_path)
          file_name = os.path.splitext(file_)[0]
          file_path = doc[len(kotlin_source_abs_path)+1:]
          include_path = os.path.join("/reference/_kotlin", file_path)

          if os.path.isfile(javadoc):
             # corresponding java content exists
             # so we already created the kotlin stub file
             # nothing to do
             both = both+1
          else:
            # no java content
            # create the kotlin stub file
            if (show_solo):
              print "solo: ", doc
            insert_stub(doc , False, False)
            solo = solo+1

          if (max_stubs>0 and stubs>=max_stubs):
            print
            print "max koltin stubs: ", max_stubs
            maxed_out = True;
            break


  print "Java+Kotlin:", both, "Only Kotlin:", solo
  print
  print "Java: ", java_stubs, " Kotlin: ", kotlin_stubs, "Total: ", java_stubs + kotlin_stubs


def main(argv):

  global work, verbose, show_solo, max_stubs
  global java_source_abs_path
  global kotlin_source_abs_path
  stem = ""

  try:
    opts, args = getopt.getopt(argv,"",["work","verbose","solo","max="])
  except getopt.GetoptError:
    print 'USAGE: switcher --work --verbose --solo --max=<max_stubs> platform|androidx|support|chrome'
    sys.exit(2)

  for opt, arg in opts:
    if opt == '--work':
       work = True
    elif opt == "--verbose":
       print "verbose"
       verbose = True
    elif opt == "--solo":
       print "verbose"
       show_solo = True
    elif opt == "--max":
       max_stubs = int(arg)
       print "max ", max_stubs

  if len(args)>0:
    source = args[0]
    if source == "platform":
      stem = "android"
      print
      print "*** PLATFORM PAGES ***"
      print "======================"

      # Insert the switchers at the top level first
      insert_platform_summaries()

    elif source == "androidx":
      stem = "androidx"
      print
      print "*** ANDROIDX SUPPORT LIBRARY PAGES ***"
      print "======================================"

    elif source == "support":
      stem = "android/support/v4/media"
      print
      print "*** ANDROIDX SUPPORT LIBRARY PAGES ***"
      print "======================================"

    elif source == "chrome":
      stem = "org/chromium/support_lib_boundary"
      print
      print "*** ANDROIDX CHROMIUM PAGES ***"
      print "==============================="

  if (len(stem)>0):
    scan_files(stem)
    print " *** DONE ***"
  else:
      print 'You must specify one of: platform|androidx|support|chrome'



if __name__ == "__main__":
   main(sys.argv[1:])