1 /* <lambda>null2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package parser 18 19 import java.io.File 20 import java.nio.file.Files 21 import java.nio.file.Path 22 import java.nio.file.Paths 23 import kotlin.system.exitProcess 24 25 const val LOG_NAME = "[hidl-doc]" 26 27 fun printUsage() { 28 println(""" 29 Usage: hidl-doc [-i path] 30 -i=path Add input HAL file or directory to parse 31 -o=dir Output directory of generated HTML 32 -x=path Exclude file or directory from files to parse 33 -v Verbose mode, print parsing info 34 -h Print this help and exit 35 Error modes: 36 -w Warn on errors instead of exiting 37 -l Lint. Warn-only and do not generate files 38 """.trim()) 39 } 40 41 object config { 42 val files = mutableListOf<File>() 43 lateinit var outDir: Path 44 var verbose = false 45 var lintMode = false 46 var warnOnly = false 47 toStringnull48 override fun toString(): String { 49 return """ 50 verbose: $verbose 51 warnOnly: $warnOnly 52 outDir: $outDir 53 files: $files 54 """ 55 } 56 57 private const val HAL_EXTENSION = ".hal" 58 parseArgsnull59 fun parseArgs(args: Array<String>) { 60 if (args.isEmpty()) { 61 printUsage() 62 exitProcess(1) 63 } 64 65 val dirPathArgs = mutableListOf<Path>() 66 val filePathArgs = mutableListOf<Path>() 67 val excludedPathArgs = mutableListOf<Path>() 68 var maybeOutDir: Path? = null 69 70 val iter = args.iterator() 71 72 //parse command-line arguments 73 while (iter.hasNext()) { 74 var arg = iter.next() 75 76 when (arg) { 77 "-i" -> { 78 val path = Paths.get(iter.next()) 79 if (Files.isDirectory(path)) dirPathArgs.add(path) else filePathArgs.add(path) 80 } 81 "-x" -> excludedPathArgs.add(Paths.get(iter.next()).toAbsolutePath()) 82 "-o" -> maybeOutDir = Paths.get(iter.next()) 83 "-v" -> verbose = true 84 "-l" -> { lintMode = true; warnOnly = true } 85 "-w" -> warnOnly = true 86 "-h" -> { 87 printUsage() 88 exitProcess(0) 89 } 90 else -> { 91 System.err.println("Unknown option: $arg") 92 printUsage() 93 exitProcess(1) 94 } 95 } 96 } 97 98 if (maybeOutDir == null) { 99 System.err.println("Error: No output directory supplied (-o)") 100 exitProcess(1) 101 } 102 outDir = maybeOutDir 103 104 //collect files (explicitly passed and search directories) 105 val allFiles = mutableListOf<File>() 106 107 //add individual files 108 filePathArgs.filterNot { excludedPathArgs.contains(it.toAbsolutePath()) } 109 .map { it.toFile() }.map { fp -> 110 if (!fp.isFile || !fp.canRead() || !fp.absolutePath.toLowerCase().endsWith(HAL_EXTENSION)) { 111 System.err.println("Error: Invalid $HAL_EXTENSION file: ${fp.path}") 112 exitProcess(1) 113 } 114 fp 115 }.map { allFiles.add(it) } 116 117 //check directory args 118 dirPathArgs.map { it.toFile() } 119 .map { findFiles(it, allFiles, HAL_EXTENSION, excludedPathArgs) } 120 121 //consolidate duplicates 122 allFiles.distinctBy { it.canonicalPath } 123 .forEach { files.add(it) } 124 125 if (files.isEmpty()) { 126 System.err.println("Error: Can't find any $HAL_EXTENSION files") 127 exitProcess(1) 128 } 129 } 130 131 /** 132 * Recursively search for files in a directory matching an extension and add to files. 133 */ findFilesnull134 private fun findFiles(dir: File, files: MutableList<File>, ext: String, excludedPaths: List<Path>) { 135 if (!dir.isDirectory || !dir.canRead()) { 136 System.err.println("Invalid directory: ${dir.path}, aborting") 137 exitProcess(1) 138 } 139 dir.listFiles() 140 .filterNot { excludedPaths.contains(it.toPath().toAbsolutePath()) } 141 .forEach { fp -> 142 if (fp.isDirectory) { 143 findFiles(fp, files, ext, excludedPaths) 144 } else if (fp.absolutePath.toLowerCase().endsWith(ext)) { 145 files.add(fp) 146 } 147 } 148 } 149 }