-
Notifications
You must be signed in to change notification settings - Fork 30
feat(cli): generate bindings.json from resources referenced in code [PC-5017] #1908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tudormatei1
wants to merge
8
commits into
main
Choose a base branch
from
feat/cli-generate-bindings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,741
−8
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
51d7807
chore(cli): regenerate CLI_REFERENCE.md
tudormatei1 9131eb0
feat(cli): generate bindings.json from resources referenced in code
tudormatei1 3ec738c
fix(cli): drop debug prints and stop folding rebound constants
tudormatei1 569671c
chore(deps): bump uipath and uipath-platform, fix test type errors
tudormatei1 edd399e
feat(cli): discover resources reached through interrupt models
tudormatei1 d57fa05
fix(cli): skip resource names computed at runtime instead of guessing
tudormatei1 50d099d
chore(deps): bump uipath to 2.14.26
tudormatei1 8684fc2
test(cli): trim the bindings suite to what actually catches bugs
tudormatei1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """Best-effort discovery of UiPath resource bindings from project source code.""" | ||
|
|
||
| from ._apply import ( | ||
| InferOutcome, | ||
| infer_bindings, | ||
| infer_bindings_into_file, | ||
| load_bindings, | ||
| report_outcome, | ||
| serialize_bindings, | ||
| ) | ||
| from ._emitter import MergeReport, binding_key, build_binding, merge_bindings | ||
| from ._registry import BINDABLE_RESOURCE_TYPES, BindingSpec, build_registry | ||
| from ._scanner import ( | ||
| ResourceReference, | ||
| ScanResult, | ||
| SkippedReference, | ||
| scan_project, | ||
| scan_source, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "BINDABLE_RESOURCE_TYPES", | ||
| "InferOutcome", | ||
| "BindingSpec", | ||
| "MergeReport", | ||
| "ResourceReference", | ||
| "ScanResult", | ||
| "SkippedReference", | ||
| "binding_key", | ||
| "build_binding", | ||
| "build_registry", | ||
| "infer_bindings", | ||
| "infer_bindings_into_file", | ||
| "load_bindings", | ||
| "merge_bindings", | ||
| "report_outcome", | ||
| "serialize_bindings", | ||
| "scan_project", | ||
| "scan_source", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Shared scan-merge-write step behind `bindings generate` and `init`.""" | ||
|
|
||
| import json | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| from .._utils._console import ConsoleLogger | ||
| from ..models.runtime_schema import Bindings | ||
| from ._emitter import MergeReport, merge_bindings | ||
| from ._registry import build_registry | ||
| from ._scanner import SkippedReference, scan_project | ||
|
|
||
| console = ConsoleLogger() | ||
|
|
||
|
|
||
| @dataclass | ||
| class InferOutcome: | ||
| """What a scan would change about a bindings file.""" | ||
|
|
||
| merged: Bindings | ||
| report: MergeReport | ||
| skipped: list[SkippedReference] | ||
|
|
||
| @property | ||
| def has_changes(self) -> bool: | ||
| return bool(self.report.added) | ||
|
|
||
|
|
||
| def load_bindings(path: Path) -> Optional[Bindings]: | ||
| """Read an existing bindings file, or None when there isn't one.""" | ||
| if not path.exists(): | ||
| return None | ||
| try: | ||
| return Bindings.model_validate_json(path.read_text()) | ||
| except ValueError as exc: | ||
| console.error(f"Could not read '{path}': {exc}") | ||
|
|
||
|
|
||
| def serialize_bindings(bindings: Bindings) -> str: | ||
| payload = bindings.model_dump(by_alias=True, exclude_none=True) | ||
| return json.dumps(payload, indent=4) | ||
|
|
||
|
|
||
| def infer_bindings(root: Path, existing: Optional[Bindings]) -> InferOutcome: | ||
| """Scan ``root`` and merge what it finds into ``existing``.""" | ||
| result = scan_project(root, build_registry()) | ||
| merged, report = merge_bindings(existing, result.references) | ||
| return InferOutcome(merged=merged, report=report, skipped=result.skipped) | ||
|
|
||
|
|
||
| def report_outcome(outcome: InferOutcome) -> None: | ||
| """Say what was found and, just as importantly, what was not.""" | ||
| for skipped in outcome.skipped: | ||
| console.warning(f"{skipped.source}: {skipped.resource_type} — {skipped.reason}") | ||
| for label in outcome.report.added: | ||
| console.info(f"Discovered {label}") | ||
| if outcome.report.preserved: | ||
| console.info( | ||
| f"Kept {len(outcome.report.preserved)} existing binding(s) " | ||
| "not found in code." | ||
| ) | ||
|
|
||
|
|
||
| def infer_bindings_into_file(root: Path, bindings_path: Path) -> InferOutcome: | ||
| """Scan, merge and write. Existing entries are never rewritten.""" | ||
| outcome = infer_bindings(root, load_bindings(bindings_path)) | ||
| report_outcome(outcome) | ||
| if outcome.has_changes: | ||
| bindings_path.write_text(serialize_bindings(outcome.merged)) | ||
| console.success( | ||
| f"Recorded {len(outcome.report.added)} binding(s) in '{bindings_path}'." | ||
| ) | ||
| else: | ||
| console.info(f"No new bindings to record in '{bindings_path}'.") | ||
| return outcome |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| """Turns discovered resource references into ``bindings.json`` entries. | ||
|
|
||
| Merging never rewrites an entry that is already in the file. Existing bindings | ||
| carry things a static scan cannot reproduce — expressions, connector metadata, | ||
| display names edited by hand — so a known key is left exactly as it is. | ||
| """ | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Optional | ||
|
|
||
| from ..models.runtime_schema import BindingResource, BindingResourceValue, Bindings | ||
| from ._scanner import ResourceReference | ||
|
|
||
| BINDINGS_VERSION = "2.0" | ||
| BINDINGS_METADATA_VERSION = "2.2" | ||
|
|
||
| _DISPLAY_NAMES = { | ||
| "app": ("App Name", "App Folder Path"), | ||
| } | ||
| _DEFAULT_DISPLAY_NAMES = ("Name", "Folder Path") | ||
|
|
||
|
|
||
| @dataclass | ||
| class MergeReport: | ||
| added: list[str] = field(default_factory=list) | ||
| unchanged: list[str] = field(default_factory=list) | ||
| preserved: list[str] = field(default_factory=list) | ||
|
|
||
|
|
||
| def binding_key(reference: ResourceReference) -> str: | ||
| """The ``key`` field, which the platform prefixes with the resource type.""" | ||
| if reference.resource_type == "connection" or not reference.folder_path: | ||
| return reference.name | ||
| return f"{reference.name}.{reference.folder_path}" | ||
|
|
||
|
|
||
| def _value(default_value: str, display_name: str) -> BindingResourceValue: | ||
| """Generated values are always literal; see _scanner._record.""" | ||
| return BindingResourceValue( | ||
| default_value=default_value, | ||
| is_expression=False, | ||
| display_name=display_name, | ||
| ) | ||
|
|
||
|
|
||
| def _connection_binding(reference: ResourceReference) -> BindingResource: | ||
| return BindingResource( | ||
| resource="connection", | ||
| key=binding_key(reference), | ||
| value={"ConnectionId": _value(reference.name, "Connection")}, | ||
| metadata={ | ||
| "BindingsVersion": BINDINGS_METADATA_VERSION, | ||
| "Connector": "", | ||
| "UseConnectionService": "True", | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def build_binding(reference: ResourceReference) -> BindingResource: | ||
| """Build a single binding entry for a discovered reference.""" | ||
| if reference.resource_type == "connection": | ||
| return _connection_binding(reference) | ||
|
|
||
| name_label, folder_label = _DISPLAY_NAMES.get( | ||
| reference.resource_type, _DEFAULT_DISPLAY_NAMES | ||
| ) | ||
| display_label = reference.name if reference.resource_type == "app" else "FullName" | ||
|
|
||
| return BindingResource( | ||
| resource=reference.resource_type, | ||
| key=binding_key(reference), | ||
| value={ | ||
| "name": _value(reference.name, name_label), | ||
| "folderPath": _value(reference.folder_path or "", folder_label), | ||
| }, | ||
| metadata={ | ||
| "ActivityName": reference.activity_name, | ||
| "BindingsVersion": BINDINGS_METADATA_VERSION, | ||
| "DisplayLabel": display_label, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def merge_bindings( | ||
| existing: Optional[Bindings], references: list[ResourceReference] | ||
| ) -> tuple[Bindings, MergeReport]: | ||
| """Add newly discovered bindings without disturbing the ones already there.""" | ||
| resources = list(existing.resources) if existing else [] | ||
| known = {(entry.resource, entry.key) for entry in resources} | ||
| report = MergeReport() | ||
| discovered: set[tuple[str, str]] = set() | ||
|
|
||
| ordered = sorted(references, key=lambda ref: (ref.resource_type, binding_key(ref))) | ||
| for reference in ordered: | ||
| identity = (reference.resource_type, binding_key(reference)) | ||
| if identity in discovered: | ||
| continue | ||
| discovered.add(identity) | ||
| label = f"{identity[0]}:{identity[1]}" | ||
| if identity in known: | ||
| report.unchanged.append(label) | ||
| continue | ||
| resources.append(build_binding(reference)) | ||
| known.add(identity) | ||
| report.added.append(label) | ||
|
|
||
| for entry in resources: | ||
| identity = (entry.resource, entry.key) | ||
| if identity not in discovered: | ||
| report.preserved.append(f"{entry.resource}:{entry.key}") | ||
|
|
||
| version = existing.version if existing else BINDINGS_VERSION | ||
| return Bindings(version=version, resources=resources), report | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.