|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package mage |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "regexp" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/pkg/errors" |
| 29 | + |
| 30 | + "github.com/elastic/beats/dev-tools/mage" |
| 31 | +) |
| 32 | + |
| 33 | +const moduleDocsGlob = "module/*/_meta/docs.asciidoc" |
| 34 | + |
| 35 | +var moduleNameRegex = regexp.MustCompile(`module\/(.*)\/_meta\/docs.asciidoc`) |
| 36 | + |
| 37 | +var modulesListTmpl = ` |
| 38 | +//// |
| 39 | +This file is generated! See scripts/mage/docs.go or run 'mage docs'. |
| 40 | +//// |
| 41 | +{{range $module := .Modules}} |
| 42 | + * <<{beatname_lc}-module-{{$module}},{{$module | title}}>> |
| 43 | +{{- end}} |
| 44 | +
|
| 45 | +-- |
| 46 | +
|
| 47 | +{{range $module := .Modules}} |
| 48 | +include::./modules/{{$module}}.asciidoc[] |
| 49 | +{{- end}} |
| 50 | +`[1:] |
| 51 | + |
| 52 | +func moduleDocs() error { |
| 53 | + searchPath := filepath.Join(mage.XPackBeatDir(moduleDocsGlob)) |
| 54 | + |
| 55 | + // Find module docs. |
| 56 | + files, err := mage.FindFiles(searchPath) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + if len(files) == 0 { |
| 61 | + return errors.Errorf("No modules found matching %v", searchPath) |
| 62 | + } |
| 63 | + |
| 64 | + // Clean existing files. |
| 65 | + if err := os.RemoveAll(mage.OSSBeatDir("docs/modules")); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + // Extract module name from path and copy the file. |
| 70 | + var names []string |
| 71 | + for _, f := range files { |
| 72 | + matches := moduleNameRegex.FindStringSubmatch(filepath.ToSlash(f)) |
| 73 | + if len(matches) != 2 { |
| 74 | + return errors.Errorf("module path %v does not match regexp", f) |
| 75 | + } |
| 76 | + name := matches[1] |
| 77 | + names = append(names, name) |
| 78 | + |
| 79 | + // Copy to the docs dirs. |
| 80 | + if err = mage.Copy(f, mage.CreateDir(mage.OSSBeatDir("docs/modules", name+".asciidoc"))); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Generate and write the docs/modules_list.asciidoc file. |
| 86 | + content, err := mage.Expand(modulesListTmpl, map[string]interface{}{ |
| 87 | + "Modules": names, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + fmt.Printf(">> update:moduleDocs: Collecting module documentation for %v.\n", strings.Join(names, ", ")) |
| 94 | + return ioutil.WriteFile(mage.OSSBeatDir("docs/modules_list.asciidoc"), []byte(content), 0644) |
| 95 | +} |
0 commit comments