Query types
The PrepareResult
may need to inspect the contents of the files being visited, to understand their dependencies, whether they appear to be a test or executable, or scan for any other syntax you care to detect.
They appear in the documentation: https://docs.aspect.build/cli/starlark#query-types
Regex
Regular expressions work great, until they don’t. It’s a simple way to get started and it’s possible it will work for all cases you ever encounter.
For example, to detect whether a shell file has a shebang line (and therefore should be in sh_binary
) the shell.star
example above has:
"shebang": aspect.RegexQuery(
expression = "#!/.*sh",
),
Tree-sitter
The tree-sitter query language allows you to express AST search and match, on any language tree-sitter can parse. An online playground is very useful for rapid prototyping of queries, and your favorite AI assistant can probably give you a partially working query to start from.
Aspect CLI has a number of tree-sitter grammars built-in, and we’re planning to add essentially all of them, as needed.
https://tree-sitter.github.io/tree-sitter/7-playground.html looks like the following. In this example we’ve pasted some sample Java code where we want to extract the name of any “main” classes. The query is color-coded with highlights showing each matching region. The AST is also shown to help navigate the language structure.