jac-client 0.2.9__py3-none-any.whl → 0.2.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ """Implementation of target registry."""
2
+
3
+ """Register a new build target."""
4
+ impl TargetRegistry.register(target: ClientTarget) -> None {
5
+ self._targets[target.name] = target;
6
+ if target.default {
7
+ self._default_target = target.name;
8
+ }
9
+ }
10
+
11
+ """Get a target by name."""
12
+ impl TargetRegistry.get(name: str) -> Optional[ClientTarget] {
13
+ return self._targets.get(name);
14
+ }
15
+
16
+ """Get the default target."""
17
+ impl TargetRegistry.get_default -> Optional[ClientTarget] {
18
+ if self._default_target {
19
+ return self._targets.get(self._default_target);
20
+ }
21
+ # Find first target marked as default
22
+ for target in self._targets.values() {
23
+ if target.default {
24
+ return target;
25
+ }
26
+ }
27
+ return None;
28
+ }
29
+
30
+ """Get all registered targets."""
31
+ impl TargetRegistry.get_all -> list[ClientTarget] {
32
+ return list(self._targets.values());
33
+ }
34
+
35
+ """Check if a target is registered."""
36
+ impl TargetRegistry.has(name: str) -> bool {
37
+ return name in self._targets;
38
+ }
39
+
40
+ """Set the default target."""
41
+ impl TargetRegistry.set_default(name: str) -> None {
42
+ if name not in self._targets {
43
+ raise ValueError(f"ClientTarget '{name}' is not registered") ;
44
+ }
45
+ # Unset previous default
46
+ if self._default_target {
47
+ prev_target = self._targets.get(self._default_target);
48
+ if prev_target {
49
+ prev_target.default = False;
50
+ }
51
+ }
52
+ # Set new default
53
+ self._default_target = name;
54
+ target = self._targets.get(name);
55
+ if target {
56
+ target.default = True;
57
+ }
58
+ }
59
+
60
+ """Clear all registered targets (for testing)."""
61
+ impl TargetRegistry.clear -> None {
62
+ self._targets.clear();
63
+ self._default_target = None;
64
+ }
@@ -0,0 +1,157 @@
1
+ """Implementation of WebTarget methods."""
2
+ import types;
3
+ import from pathlib { Path }
4
+ import from typing { Optional }
5
+ import from jac_client.plugin.src.vite_bundler { ViteBundler }
6
+
7
+ """Build web bundle using existing Vite pipeline."""
8
+ impl WebTarget.build(
9
+ self: WebTarget,
10
+ entry_file: Path,
11
+ project_dir: Path,
12
+ platform: Optional[str] = None
13
+ ) -> Path {
14
+ import os;
15
+ import shutil;
16
+ import from jaclang.pycore.runtime { JacRuntime as Jac }
17
+ import from jac_client.plugin.client { JacClient }
18
+ bundler = ViteBundler(project_dir=project_dir);
19
+ # Clean dist directory for fresh build
20
+ dist_dir = bundler.output_dir;
21
+ if dist_dir.exists() {
22
+ shutil.rmtree(dist_dir);
23
+ }
24
+ dist_dir.mkdir(parents=True, exist_ok=True);
25
+ # Load the module (same as jac start does via _proc_file + jac_import)
26
+ (base, mod) = os.path.split(str(entry_file));
27
+ base = base or "./";
28
+ if entry_file.name.endswith('.jac') {
29
+ mod = mod[:-4];
30
+ }
31
+ # Import the module (Jac.jac_import handles context creation internally)
32
+ Jac.jac_import(target=mod, base_path=base, lng='jac');
33
+ if Jac.program.errors_had {
34
+ errors = '\n'.join(Jac.program.errors_had);
35
+ raise RuntimeError(f"Failed to compile {entry_file}:\n{errors}") ;
36
+ }
37
+ # Get the loaded module
38
+ loaded_mod = Jac.loaded_modules.get(mod);
39
+ if not loaded_mod {
40
+ raise RuntimeError(f"Module '{mod}' not found after import") ;
41
+ }
42
+ # Type assertion: loaded_mod is guaranteed to be not None here
43
+ if not isinstance(loaded_mod, types.ModuleType) {
44
+ raise RuntimeError(f"Module '{mod}' is not a valid module type") ;
45
+ }
46
+ # Build bundle using ViteClientBundleBuilder (same as jac start's introspector.ensure_bundle)
47
+ builder = JacClient.get_client_bundle_builder();
48
+ bundle = builder.build(loaded_mod, force=True);
49
+ # Find the bundle file
50
+ bundle_path = bundler.find_bundle();
51
+ if not bundle_path {
52
+ raise RuntimeError("Web build failed: bundle not found") ;
53
+ }
54
+ # Always generate static index.html for static serving
55
+ _generate_index_html(
56
+ bundle_path=bundle_path,
57
+ bundler=bundler,
58
+ loaded_mod=loaded_mod,
59
+ project_dir=project_dir
60
+ );
61
+ return bundle_path;
62
+ }
63
+
64
+ """Generate static index.html file for the built bundle."""
65
+ def _generate_index_html(
66
+ bundle_path: Path,
67
+ bundler: ViteBundler,
68
+ loaded_mod: types.ModuleType,
69
+ project_dir: Path
70
+ ) -> None {
71
+ import html;
72
+ import hashlib;
73
+ import from jaclang.project.config { get_config }
74
+ import from jac_client.plugin.client { HeaderBuilder }
75
+ import from jaclang.pycore.runtime { JacRuntime as Jac }
76
+
77
+ # Get the first client export function (or default to 'app')
78
+ mod_path = getattr(loaded_mod, '__file__', None);
79
+ if mod_path {
80
+ mod = Jac.program.mod.hub.get(mod_path);
81
+ if mod and mod.gen.client_manifest {
82
+ client_exports = mod.gen.client_manifest.exports;
83
+ # client_exports is a list[str], not a dict
84
+ if client_exports and len(client_exports) > 0 {
85
+ function_name = client_exports[0];
86
+ } else {
87
+ function_name = 'app';
88
+ }
89
+ } else {
90
+ function_name = 'app';
91
+ }
92
+ } else {
93
+ function_name = 'app';
94
+ }
95
+
96
+ # Get module name
97
+ module_name = loaded_mod.__name__;
98
+
99
+ # Get dist directory (output_dir is already a Path)
100
+ dist_dir = Path(bundler.output_dir);
101
+
102
+ # Check for CSS file
103
+ css_link = '';
104
+ css_file = bundler.find_css();
105
+ if css_file {
106
+ css_hash = hashlib.sha256(css_file.read_bytes()).hexdigest()[:8];
107
+ css_filename = css_file.name;
108
+ css_link = f'<link rel="stylesheet" href="{css_filename}?hash={css_hash}"/>';
109
+ }
110
+
111
+ # Get meta data from config
112
+ config = get_config();
113
+ meta_data = {};
114
+ if config {
115
+ client_cfg = config.get_plugin_config("client");
116
+ if client_cfg {
117
+ meta_data = client_cfg.get("app_meta_data", {});
118
+ }
119
+ }
120
+
121
+ # Build HTML head using HeaderBuilder (same as render_page)
122
+ head_builder = HeaderBuilder(meta_data, function_name);
123
+ head_content = head_builder.build_head();
124
+ if css_link {
125
+ head_content += f"\n {css_link}";
126
+ }
127
+
128
+ # Get bundle filename (relative to dist_dir)
129
+ bundle_filename = bundle_path.name;
130
+
131
+ # Generate HTML with __jac_init__ script tag for client runtime
132
+ init_payload = {
133
+ "module": module_name,
134
+ "function": function_name,
135
+ "args": {},
136
+ "argOrder": [],
137
+ "globals": {}
138
+ };
139
+ import json;
140
+ init_json = json.dumps(init_payload);
141
+
142
+ html_content = f'''<!DOCTYPE html>
143
+ <html lang="en">
144
+ <head>
145
+ {head_content}
146
+ </head>
147
+ <body>
148
+ <div id="root"></div>
149
+ <script id="__jac_init__" type="application/json">{html.escape(init_json)}</script>
150
+ <script src="{bundle_filename}" defer></script>
151
+ </body>
152
+ </html>''';
153
+
154
+ # Write index.html to dist directory
155
+ index_html = dist_dir / 'index.html';
156
+ index_html.write_text(html_content, encoding='utf-8');
157
+ }
@@ -0,0 +1,21 @@
1
+ """Register all build targets.
2
+
3
+ This module registers all available build targets when the plugin loads.
4
+ """
5
+ import from jac_client.plugin.src.targets.registry { get_target_registry }
6
+ import from jac_client.plugin.src.targets.web_target { WebTarget }
7
+ import from jac_client.plugin.src.targets.desktop_target { DesktopTarget }
8
+ # Note: Jac's annex pass should auto-discover .impl.jac files automatically
9
+ # The impl files in impl/ directory should be loaded when the base classes are imported
10
+ """Register all available targets."""
11
+ def register_targets -> None {
12
+ registry = get_target_registry();
13
+
14
+ # Register web target (default)
15
+ web_target = WebTarget();
16
+ registry.register(web_target);
17
+
18
+ # Register desktop target (placeholder for Phase 2)
19
+ desktop_target = DesktopTarget();
20
+ registry.register(desktop_target);
21
+ }
@@ -0,0 +1,87 @@
1
+ """Target registry for build targets (web, desktop, mobile, etc.).
2
+
3
+ This module provides a registry system for managing different build targets.
4
+ Each target is a class that inherits from the base ClientTarget class.
5
+ """
6
+ import from typing { Optional }
7
+ import from pathlib { Path }
8
+
9
+ """Base class for all build targets.
10
+
11
+ All targets must inherit from this class and implement the required methods.
12
+ """
13
+ class ClientTarget {
14
+ has name: str,
15
+ default: bool = False,
16
+ requires_setup: bool = False,
17
+ config_section: str = "",
18
+ required_dependencies: list[str] = [],
19
+ output_dir: Optional[Path] = None;
20
+
21
+ """Setup the target (one-time initialization)."""
22
+ def setup(self: ClientTarget, project_dir: Path) -> None abs;
23
+
24
+ """Build the target."""
25
+ def build(
26
+ self: ClientTarget,
27
+ entry_file: Path,
28
+ project_dir: Path,
29
+ platform: Optional[str] = None
30
+ ) -> Path abs;
31
+
32
+ """Start dev server for the target."""
33
+ def dev(self: ClientTarget, entry_file: Path, project_dir: Path) -> None abs;
34
+
35
+ """Start the target (production mode - build and run)."""
36
+ def start(self: ClientTarget, entry_file: Path, project_dir: Path) -> None abs;
37
+ }
38
+
39
+ """Central registry for build targets.
40
+
41
+ This is a singleton that collects all build targets registered by plugins.
42
+ The CLI uses this registry to delegate build operations to the appropriate target.
43
+ """
44
+ obj TargetRegistry {
45
+ has _targets: dict[str, ClientTarget] = {},
46
+ _default_target: Optional[str] = None;
47
+
48
+ """Register a new build target."""
49
+ def register(target: ClientTarget) -> None;
50
+
51
+ """Get a target by name."""
52
+ def get(name: str) -> Optional[ClientTarget];
53
+
54
+ """Get the default target."""
55
+ def get_default -> Optional[ClientTarget];
56
+
57
+ """Get all registered targets."""
58
+ def get_all -> list[ClientTarget];
59
+
60
+ """Check if a target is registered."""
61
+ def has(name: str) -> bool;
62
+
63
+ """Set the default target."""
64
+ def set_default(name: str) -> None;
65
+
66
+ """Clear all registered targets (for testing)."""
67
+ def clear -> None;
68
+ }
69
+
70
+ # Global singleton instance
71
+ glob _registry: TargetRegistry | None = None;
72
+
73
+ """Get or create the global target registry."""
74
+ def get_target_registry -> TargetRegistry {
75
+ import from jac_client.plugin.src.targets.registry { TargetRegistry }
76
+ global _registry;
77
+ if _registry is None {
78
+ _registry = TargetRegistry();
79
+ }
80
+ return _registry;
81
+ }
82
+ # Target name: "web", "desktop", "android", "ios"
83
+ # Is this the default target?
84
+ # Does this target need setup?
85
+ # Section in jac.toml (e.g., "desktop")
86
+ # Required npm/system dependencies
87
+ # Where build outputs go
@@ -0,0 +1,35 @@
1
+ """Web target implementation.
2
+
3
+ This is the default target for building web applications.
4
+ """
5
+ import from pathlib { Path }
6
+ import from typing { Optional }
7
+ import from jac_client.plugin.src.targets.registry { ClientTarget }
8
+
9
+ """Web build target."""
10
+ class WebTarget(ClientTarget) {
11
+ def init(self: WebTarget) {
12
+ self.name = "web";
13
+ self.default = True;
14
+ self.requires_setup = False;
15
+ self.config_section = "plugin.client";
16
+ self.output_dir = Path(".jac/client/dist");
17
+ }
18
+
19
+ """Setup web target (no-op for web, already set up by default)."""
20
+ override def setup(self: WebTarget, project_dir: Path) -> None;
21
+
22
+ """Build web bundle using existing Vite pipeline."""
23
+ override def build(
24
+ self: WebTarget,
25
+ entry_file: Path,
26
+ project_dir: Path,
27
+ platform: Optional[str] = None
28
+ ) -> Path;
29
+
30
+ """Start web dev server."""
31
+ override def dev(self: WebTarget, entry_file: Path, project_dir: Path) -> None;
32
+
33
+ """Start web target (no-op for web, handled by jac start)."""
34
+ override def start(self: WebTarget, entry_file: Path, project_dir: Path) -> None;
35
+ }