1 个不稳定版本

0.2.0 2022年1月10日

44#convert-json

MIT 许可证

21KB
433

Github.com Crates.io Github Actions

简单的 AIDL 命令行工具

命令行工具,用于解析 AIDL 文件并提取信息。

功能

  • 显示诊断信息
  • 显示项目
  • 转换为 JSON 或 YAML

有关特定语言的特性,请参阅 rust-aidl-parser

用法

USAGE:
    aidl-cli [FLAGS] [OPTIONS] <dir>

FLAGS:
    -i, --items               Display items
    -h, --help                Prints help information
    -q, --hide-diagnostics    Do not show diagnostics
        --pretty              Make pretty (but longer) output
    -j, --to-json             Convert the whole AST to JSON
    -y, --to-yaml             Convert the whole AST to YAML
    -V, --version             Prints version information

OPTIONS:
    -o, --output-path <output-path>    Output file

ARGS:
    <dir>    The directory where the AIDL files are located

仅显示诊断信息

> aidl-cli /path/to/project

列出项目和文件

> aidl-cli -i /path/to/project

转换为 JSON

格式

JSON 结构

{
  "root": <path_to_root_dir>,
  "items": {
    <item_name>: {
        "path": <relative_path_to_item.aidl>,
        "itemType": <interface|parcelable|enum>,
        "elements": {
          <element_name>: {
            "elementType": <method|const|field|enumElement>,
            "name": <element_name>,
            ... (element-specific info, e.g. field type, method args, ...) ...
          },
          ...
        }
    },
    ...
  }
}

示例

> aidl-cli --to-json ~/path/to/aidl/project --pretty > test.json

/path/to/aidl/project/test/pkg/TestInterface.aidl (输入)

package test.pkg;

import test.pkg.TestParcelable;

interface TestInterface {
  const int VERSION = 12;
  
  /**
   * Say hello
   */
  String hello(boolean loud, in TestParcelable data);
}

/path/to/aidl/project/test/pkg/TestParcelable.aidl (输入)

package test.pkg;

parcelable TestParcelable {
  /**
   * The first field
   */
  Array<String> field1;

  /**
   * The second field
   */
  int field2;
}

test.json (输出)

{
  "root": "/path/to/aidl/project",
  "items": {
    "test.pkg.TestInterface": {
      "path": "test/pkg/TestInterface.aidl",
      "itemType": "interface",
      "name": "TestInterface",
      "elements": {
        "hello": {
          "elementType": "method",
          "oneway": false,
          "name": "hello",
          "returnType": "String",
          "args": [
            {
              "name": "loud",
              "type": "boolean"
            },
            {
              "name": "data",
              "direction": "in",
              "type": "test.pkg.TestParcelable"
            }
          ],
          "doc": "Say hello"
        },
        "VERSION": {
          "elementType": "const",
          "name": "VERSION",
          "type": "int",
          "value": 12,
        }
      }
    },
    "test.pkg.TestParcelable": {
      "path": "test/pkg/TestParcelable.aidl",
      "itemType": "parcelable",
      "name": "TestParcelable",
      "elements": {
        "field1": {
          "elementType": "field",
          "name": "field1",
          "type": "Array<String>",
          "doc": "The first field"
        },
        "field2": {
          "elementType": "field",
          "name": "field2",
          "type": "int",
          "doc": "The second field"
        }
      }
    }
  }
}

提取信息

显示所有项目名称(需要 jq

> aidl-cli -j /path/to/project | jq '.items[] | .name'

显示所有项目作为 [{ <itemType>: <name>, elements: [<name>] }]

> aidl-cli -j /path/to/project | jq '.items[] | { (.itemType): .name, elements: [.elements[] | .name] }

通过名称(使用正则表达式)过滤项目并显示它们作为 <itemType> <name>

> aidl-cli -j /path/to/project | jq '.items[] | select(.name | test("^I")) | "\(.itemType) \(.name)"'

显示项目之间的差异(需要 jd

> aidl-cli -j /path/to/project1 > project1.json
> aidl-cli -j /path/to/project2 > project2.json
> jd project1.json project2.json

转换为 YAML

示例

> aidl-cli --to-yaml ~/path/to/aidl/project

依赖关系

~6–15MB
~191K SLoC