• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/perl
2#
3#  Copyright (C) 2015 Google, Inc.
4#
5#  Licensed under the Apache License, Version 2.0 (the "License");
6#  you may not use this file except in compliance with the License.
7#  You may obtain a copy of the License at:
8#
9#  http://www.apache.org/licenses/LICENSE-2.0
10#
11#  Unless required by applicable law or agreed to in writing, software
12#  distributed under the License is distributed on an "AS IS" BASIS,
13#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#  See the License for the specific language governing permissions and
15#  limitations under the License.
16#
17
18use strict;
19use warnings;
20use Cwd 'abs_path';
21use JSON;
22use File::Find;
23
24my $sl4a_path = $ARGV[0];
25my $md = "";
26my $md_end = "";
27
28if (not defined $sl4a_path) {
29    $sl4a_path = abs_path($0);
30    $sl4a_path =~ s/\/Docs\/generate_api_reference_md\.pl//g;
31}
32
33sub eachFile {
34    my $filename = $_;
35    my $fullpath = $File::Find::name;
36    if (-e $filename && $filename =~ m/Facade\.java/) {
37        open(FILE, $filename);
38        my @lines = <FILE>;
39        close(FILE);
40
41        my $title = $filename;
42        $title =~ s/\.java//;
43        $title = '**' . $title . '**' . "\n";
44        $md = $md . "\n$title";
45        my $description = "";
46        for (my $i = 0; $i < scalar(@lines); $i++) {
47            my $line = $lines[$i];
48            $line =~ s/\n//;
49            $line =~ s/^\s+|\s+$//g;
50
51            if ($line =~ m /^\@Rpc\(description/) {
52                $description = "";
53                for (my $j = $i; $j < scalar(@lines); $j++) {
54                    my $l = $lines[$j];
55                    $l =~ s/^\s+|\s+$//g;
56                    $description = $description . $l;
57                    if ($l =~ m/\)$/) {
58                        $i = $j;
59                        last;
60                    }
61                }
62                $description = _format_description($description);
63
64            }
65            if ($line =~ m /^public/ && $description ne "") {
66                my @words = split(/\s/, $line);
67                my $func_name = $words[2];
68                my $func_names_and_params = "";
69                if ($func_name =~ /void/) {
70                    $func_name = $words[3];
71                    if ($func_name =~ /void/) {
72                        $description = "";
73                        $func_names_and_params = "";
74                        next;
75                    }
76                }
77                if ($func_name =~ /\(/) {
78                    $func_name =~ s/\(.*//;
79                }
80                $func_name =~ s/\(//g;
81                $func_name =~ s/\)//g;
82                for (my $j = $i; $j < scalar(@lines); $j++) {
83                    $func_names_and_params = $func_names_and_params . $lines[$j];
84                    if ($lines[$j] =~ m/{$/) {
85                        last;
86                    }
87                }
88                $func_names_and_params = _format_func_names_and_params($func_names_and_params);
89                if ($func_names_and_params eq "") {
90                    $func_names_and_params = ")\n";
91                } else {
92                    $func_names_and_params = "\n" . $func_names_and_params;
93                }
94                $md_end = $md_end . "# $func_name\n```\n" .
95                    "$func_name(" . $func_names_and_params . "\n$description\n```\n\n" ;
96                $description = "";
97                $func_names_and_params = "";
98                my $lc_name = lc $func_name;
99                $md = $md . "  * [$func_name](\#$lc_name)\n";
100            }
101        }
102
103    }
104}
105
106sub _format_func_names_and_params {
107    my $fn = shift;
108    $fn =~ s/^\s+|\s+$//g;
109    my @words = split(/\n/,$fn);
110    my $format = "";
111    my $description = "";
112    my $name = "";
113    my $params = "";
114    for my $w (@words) {
115        if ($w =~ /\@RpcParameter\(name = "(.+?)", description = "(.+?)"/) {
116           $name = $1;
117           $description = $2;
118        }
119        elsif ($w =~ /\@RpcParameter\(name = "(.+?)"/) {
120           $name = $1;
121        }
122        if ($w =~ m/,$/) {
123            my @split = split(/\s/, $w);
124            $params = "$split[$#split-1] $split[$#split]";
125            if ($description eq "") {
126                $format = $params;
127            } elsif ($description ne "") {
128                $params =~ s/,//;
129                $format = $format . "  $params: $description,\n"
130            }
131            $description = "";
132            $name = "";
133            $params = "";
134        }
135    }
136    $format =~ s/,$/)/;
137    return $format;
138}
139
140sub _format_description {
141    my $description = shift;
142    $description =~ s/\@Rpc\(//;
143    $description =~ s/^\s+|\s+$//g;
144    $description =~ s/\n//g;
145    $description =~ s/description = \"//g;
146    $description =~ s/\"\)//g;
147    if ($description =~ m/returns(\s*)=/) {
148        $description =~ s/\",//;
149        my @words = split(/returns(\s*)=/, $description);
150        my $des = $words[0];
151        my $ret = $words[1];
152        $ret =~ s/^\s+|\s+$//g;
153        $ret =~ s/^"//;
154        $description = $des . "\n\n" . "Returns:\n" . "  $ret";
155    }
156    return $description;
157}
158
159find (\&eachFile, $sl4a_path);
160open(FILE, ">$sl4a_path/Docs/ApiReference.md");
161print FILE $md . "\n";
162print FILE $md_end . "\n";
163close(FILE);
164