Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "bytes" |
| 20 | "html/template" |
| 21 | "io/ioutil" |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame^] | 22 | "reflect" |
| 23 | "sort" |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 24 | |
| 25 | "github.com/google/blueprint/bootstrap" |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame^] | 26 | "github.com/google/blueprint/bootstrap/bpdoc" |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func writeDocs(ctx *android.Context, filename string) error { |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame^] | 30 | moduleTypeFactories := android.ModuleTypeFactories() |
| 31 | bpModuleTypeFactories := make(map[string]reflect.Value) |
| 32 | for moduleType, factory := range moduleTypeFactories { |
| 33 | bpModuleTypeFactories[moduleType] = reflect.ValueOf(factory) |
| 34 | } |
| 35 | |
| 36 | packages, err := bootstrap.ModuleTypeDocs(ctx.Context, bpModuleTypeFactories) |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | |
| 41 | buf := &bytes.Buffer{} |
| 42 | |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame^] | 43 | var moduleTypeList []*bpdoc.ModuleType |
| 44 | for _, pkg := range packages { |
| 45 | moduleTypeList = append(moduleTypeList, pkg.ModuleTypes...) |
| 46 | } |
| 47 | sort.Slice(moduleTypeList, func(i, j int) bool { return moduleTypeList[i].Name < moduleTypeList[j].Name }) |
| 48 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 49 | unique := 0 |
| 50 | |
| 51 | tmpl, err := template.New("file").Funcs(map[string]interface{}{ |
| 52 | "unique": func() int { |
| 53 | unique++ |
| 54 | return unique |
| 55 | }}).Parse(fileTemplate) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | err = tmpl.Execute(buf, moduleTypeList) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | err = ioutil.WriteFile(filename, buf.Bytes(), 0666) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | const ( |
| 74 | fileTemplate = ` |
| 75 | <html> |
| 76 | <head> |
| 77 | <title>Build Docs</title> |
| 78 | <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> |
| 79 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> |
| 80 | <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> |
| 81 | </head> |
| 82 | <body> |
| 83 | <h1>Build Docs</h1> |
| 84 | <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> |
| 85 | {{range .}} |
| 86 | {{ $collapseIndex := unique }} |
| 87 | <div class="panel panel-default"> |
| 88 | <div class="panel-heading" role="tab" id="heading{{$collapseIndex}}"> |
| 89 | <h2 class="panel-title"> |
| 90 | <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse{{$collapseIndex}}" aria-expanded="false" aria-controls="collapse{{$collapseIndex}}"> |
| 91 | {{.Name}} |
| 92 | </a> |
| 93 | </h2> |
| 94 | </div> |
| 95 | </div> |
| 96 | <div id="collapse{{$collapseIndex}}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading{{$collapseIndex}}"> |
| 97 | <div class="panel-body"> |
| 98 | <p>{{.Text}}</p> |
| 99 | {{range .PropertyStructs}} |
| 100 | <p>{{.Text}}</p> |
| 101 | {{template "properties" .Properties}} |
| 102 | {{end}} |
| 103 | </div> |
| 104 | </div> |
| 105 | {{end}} |
| 106 | </div> |
| 107 | </body> |
| 108 | </html> |
| 109 | |
| 110 | {{define "properties"}} |
| 111 | <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> |
| 112 | {{range .}} |
| 113 | {{$collapseIndex := unique}} |
| 114 | {{if .Properties}} |
| 115 | <div class="panel panel-default"> |
| 116 | <div class="panel-heading" role="tab" id="heading{{$collapseIndex}}"> |
| 117 | <h4 class="panel-title"> |
| 118 | <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse{{$collapseIndex}}" aria-expanded="false" aria-controls="collapse{{$collapseIndex}}"> |
| 119 | {{.Name}}{{range .OtherNames}}, {{.}}{{end}} |
| 120 | </a> |
| 121 | </h4> |
| 122 | </div> |
| 123 | </div> |
| 124 | <div id="collapse{{$collapseIndex}}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading{{$collapseIndex}}"> |
| 125 | <div class="panel-body"> |
| 126 | <p>{{.Text}}</p> |
| 127 | {{range .OtherTexts}}<p>{{.}}</p>{{end}} |
| 128 | {{template "properties" .Properties}} |
| 129 | </div> |
| 130 | </div> |
| 131 | {{else}} |
| 132 | <div> |
| 133 | <h4>{{.Name}}{{range .OtherNames}}, {{.}}{{end}}</h4> |
| 134 | <p>{{.Text}}</p> |
| 135 | {{range .OtherTexts}}<p>{{.}}</p>{{end}} |
| 136 | <p><i>Type: {{.Type}}</i></p> |
| 137 | {{if .Default}}<p><i>Default: {{.Default}}</i></p>{{end}} |
| 138 | </div> |
| 139 | {{end}} |
| 140 | {{end}} |
| 141 | </div> |
| 142 | {{end}} |
| 143 | ` |
| 144 | ) |