MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / extract_powershell

Function extract_powershell

graphify/extractors/powershell.py:11–442  ·  view source on GitHub ↗

Extract functions, classes, methods, and using statements from a .ps1 file.

(path: Path)

Source from the content-addressed store, hash-verified

9
10
11def extract_powershell(path: Path) -> dict:
12 """Extract functions, classes, methods, and using statements from a .ps1 file."""
13 try:
14 import tree_sitter_powershell as tsps
15 from tree_sitter import Language, Parser
16 except ImportError:
17 return {"nodes": [], "edges": [], "error": "tree_sitter_powershell not installed"}
18
19 try:
20 language = Language(tsps.language())
21 parser = Parser(language)
22 source = _read_source_bytes(path)
23 tree = parser.parse(source)
24 root = tree.root_node
25 except Exception as e:
26 return {"nodes": [], "edges": [], "error": str(e)}
27
28 stem = _file_stem(path)
29 str_path = str(path)
30 nodes: list[dict] = []
31 edges: list[dict] = []
32 seen_ids: set[str] = set()
33 function_bodies: list[tuple[str, Any, str | None]] = []
34 class_ids_by_name: dict[str, list[str]] = {}
35 class_names_by_id: dict[str, str] = {}
36 methods_by_class: dict[str, dict[str, str]] = {}
37
38 def add_node(nid: str, label: str, line: int) -> None:
39 if nid not in seen_ids:
40 seen_ids.add(nid)
41 nodes.append({"id": nid, "label": label, "file_type": "code",
42 "source_file": str_path, "source_location": f"L{line}"})
43
44 def add_edge(src: str, tgt: str, relation: str, line: int,
45 confidence: str = "EXTRACTED", weight: float = 1.0,
46 context: str | None = None) -> None:
47 edge = {"source": src, "target": tgt, "relation": relation,
48 "confidence": confidence, "source_file": str_path,
49 "source_location": f"L{line}", "weight": weight}
50 if context:
51 edge["context"] = context
52 edges.append(edge)
53
54 file_nid = _make_id(str(path))
55 add_node(file_nid, path.name, 1)
56
57 _PS_SKIP = frozenset({
58 "using", "return", "if", "else", "elseif", "foreach", "for",
59 "while", "do", "switch", "try", "catch", "finally", "throw",
60 "break", "continue", "exit", "param", "begin", "process", "end",
61 # Import commands — handled as import edges, not function calls
62 "import-module",
63 })
64 # File-scope advanced-function blocks that parse as pseudo-commands rather
65 # than named_block nodes; walk() must descend into them to reach nested
66 # function/class/enum statements (#4270).
67 _PS_NAMED_BLOCK_CMDS = frozenset({"begin", "process", "end", "dynamicparam", "clean"})
68

Calls 6

_read_source_bytesFunction · 0.90
_make_idFunction · 0.90
_file_stemFunction · 0.85
add_nodeFunction · 0.70
walkFunction · 0.70
walk_callsFunction · 0.70