@supersdk/supercli 0.1.0-dev.1
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.
- package/dist/api/consoleClient.js +132 -0
- package/dist/api/consoleClient.js.map +1 -0
- package/dist/auth/browserAuth.js +163 -0
- package/dist/auth/browserAuth.js.map +1 -0
- package/dist/auth/refresh.js +32 -0
- package/dist/auth/refresh.js.map +1 -0
- package/dist/cli.js +46 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/accounts.js +46 -0
- package/dist/commands/accounts.js.map +1 -0
- package/dist/commands/createApp.js +225 -0
- package/dist/commands/createApp.js.map +1 -0
- package/dist/commands/login.js +54 -0
- package/dist/commands/login.js.map +1 -0
- package/dist/commands/logout.js +99 -0
- package/dist/commands/logout.js.map +1 -0
- package/dist/commands/publishSite.js +361 -0
- package/dist/commands/publishSite.js.map +1 -0
- package/dist/commands/switchaccount.js +35 -0
- package/dist/commands/switchaccount.js.map +1 -0
- package/dist/commands/update.js +95 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/config/environments.js +53 -0
- package/dist/config/environments.js.map +1 -0
- package/dist/createApp/copyTemplate.js +237 -0
- package/dist/createApp/copyTemplate.js.map +1 -0
- package/dist/publish/orgResolve.js +30 -0
- package/dist/publish/orgResolve.js.map +1 -0
- package/dist/publish/responseClassifier.js +111 -0
- package/dist/publish/responseClassifier.js.map +1 -0
- package/dist/publish/siteClient.js +113 -0
- package/dist/publish/siteClient.js.map +1 -0
- package/dist/publish/siteConfig.js +190 -0
- package/dist/publish/siteConfig.js.map +1 -0
- package/dist/publish/siteZip.js +172 -0
- package/dist/publish/siteZip.js.map +1 -0
- package/dist/store/accountStore.js +236 -0
- package/dist/store/accountStore.js.map +1 -0
- package/dist/store/backends/encryptedFile.js +162 -0
- package/dist/store/backends/encryptedFile.js.map +1 -0
- package/dist/store/backends/osKeyring.js +283 -0
- package/dist/store/backends/osKeyring.js.map +1 -0
- package/dist/store/credentialStore.js +65 -0
- package/dist/store/credentialStore.js.map +1 -0
- package/dist/template/README.md +144 -0
- package/dist/template/android/.gitkeep +1 -0
- package/dist/template/android/app/build.gradle.kts +122 -0
- package/dist/template/android/app/src/main/AndroidManifest.xml +19 -0
- package/dist/template/android/app/src/main/java/com/supersdk/simple/MainActivity.java +17 -0
- package/dist/template/android/app/src/main/java/com/supersdk/simple/SimpleApplication.java +22 -0
- package/dist/template/android/app/src/test/java/com/supersdk/simple/AppNameResourceTest.java +19 -0
- package/dist/template/android/build.gradle.kts +3 -0
- package/dist/template/android/gradle/supersdk-config-reader.gradle +61 -0
- package/dist/template/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/dist/template/android/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/dist/template/android/gradle.properties +3 -0
- package/dist/template/android/gradlew +251 -0
- package/dist/template/android/settings.gradle.kts +25 -0
- package/dist/template/android/supersdk-default.config.json +17 -0
- package/dist/template/ios/MockSuperSDKPackage/Package.swift +15 -0
- package/dist/template/ios/MockSuperSDKPackage/Sources/SuperSDK/SuperSDK.swift +25 -0
- package/dist/template/ios/Resources/Info.plist +43 -0
- package/dist/template/ios/Sources/App/AppDelegate.swift +20 -0
- package/dist/template/ios/Sources/App/SceneDelegate.swift +14 -0
- package/dist/template/ios/Sources/App/SimpleSDKBootstrap.swift +7 -0
- package/dist/template/ios/Sources/App/SimpleSDKProbe.swift +26 -0
- package/dist/template/ios/Sources/App/SimpleSDKProvider.swift +4 -0
- package/dist/template/ios/Sources/App/SimpleViewController.swift +20 -0
- package/dist/template/ios/Tests/SimpleAppTests/SimpleSDKInitializationTests.swift +17 -0
- package/dist/template/ios/project.yml +67 -0
- package/dist/template/scripts/android_snapshot_canary.py +136 -0
- package/dist/template/scripts/build_android.sh +240 -0
- package/dist/template/scripts/build_ios.sh +261 -0
- package/dist/template/scripts/build_web.sh +12 -0
- package/dist/template/scripts/serve_web.py +51 -0
- package/dist/template/scripts/super_build_orchestrator.py +400 -0
- package/dist/template/scripts/supersdk_template.py +611 -0
- package/dist/template/scripts/web_host_validator.py +103 -0
- package/dist/template/scripts/web_package.py +202 -0
- package/dist/template/scripts/ws03_shared_schema_merge_guard.py +73 -0
- package/dist/template/super_build.sh +116 -0
- package/dist/template/supersdk.yaml +15 -0
- package/dist/template/web/src/css/style.css +83 -0
- package/dist/template/web/src/index.html +37 -0
- package/dist/template/web/src/js/app.js +99 -0
- package/dist/template/web/src/js/supersdk.js +1848 -0
- package/dist/template/web/src/js/supersdk.provenance.json +6 -0
- package/dist/update/channelResolver.js +183 -0
- package/dist/update/channelResolver.js.map +1 -0
- package/dist/update/installSafety.js +135 -0
- package/dist/update/installSafety.js.map +1 -0
- package/dist/util/fsAtomic.js +294 -0
- package/dist/util/fsAtomic.js.map +1 -0
- package/dist/util/log.js +55 -0
- package/dist/util/log.js.map +1 -0
- package/dist/util/origin.js +66 -0
- package/dist/util/origin.js.map +1 -0
- package/dist/util/prompt.js +113 -0
- package/dist/util/prompt.js.map +1 -0
- package/package.json +43 -0
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import argparse
|
|
3
|
+
import ipaddress
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import sys
|
|
8
|
+
import uuid
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from urllib.parse import urlparse
|
|
11
|
+
|
|
12
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
13
|
+
from web_host_validator import HostValidationError, normalize_host
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
MANAGED_BY = "supersdk-template"
|
|
17
|
+
MANAGED_CONFIG_SCHEMA_VERSION = 2
|
|
18
|
+
MANAGED_SCHEMA_VERSION = MANAGED_CONFIG_SCHEMA_VERSION
|
|
19
|
+
MANAGED_CONFIG_KEYS = ("managed_by", "schema_version", "app_id", "name", "platform", "version", "sdk", "platform_config")
|
|
20
|
+
ANDROID_NAMESPACE = "com.supersdk.simple"
|
|
21
|
+
ANDROID_VERSION_RE = r"^[0-9]+\.[0-9]+\.[0-9]+(?:[-.][A-Za-z0-9]+)*$"
|
|
22
|
+
PLATFORMS = ("android", "ios", "web")
|
|
23
|
+
ROOT_KEYS = {"schema_version", "app_id", "name", "android", "ios", "web", "version", "sdk"}
|
|
24
|
+
NAMESPACE_KEYS = {
|
|
25
|
+
"android": {"package"},
|
|
26
|
+
"ios": {"bundleId", "spmMode"},
|
|
27
|
+
"web": {"host"},
|
|
28
|
+
"version": {"name", "code"},
|
|
29
|
+
}
|
|
30
|
+
SDK_KEYS = {"androidVersion", "iosPackageUrl", "iosVersion"}
|
|
31
|
+
IOS_VERSION_PATTERN = r"^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$"
|
|
32
|
+
APP_ID_PATTERN = r"[A-Za-z][A-Za-z0-9_-]{2,63}"
|
|
33
|
+
BUNDLE_ID_PATTERN = r"[A-Za-z0-9][A-Za-z0-9-]*(\.[A-Za-z0-9][A-Za-z0-9-]*)+"
|
|
34
|
+
SDK_PROJECTION = {
|
|
35
|
+
"android": {"mock": ("androidVersion",), "real": ("androidVersion",)},
|
|
36
|
+
"ios": {"mock": (), "real": ("iosPackageUrl", "iosVersion")},
|
|
37
|
+
"web": {"mock": (), "real": ()},
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class ConfigError(Exception):
|
|
42
|
+
def __init__(self, field, message):
|
|
43
|
+
super().__init__(f"{field}: {message}")
|
|
44
|
+
self.field = field
|
|
45
|
+
self.message = message
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def strip_comment(line):
|
|
49
|
+
in_single = False
|
|
50
|
+
in_double = False
|
|
51
|
+
escaped = False
|
|
52
|
+
for index, char in enumerate(line):
|
|
53
|
+
if in_double and escaped:
|
|
54
|
+
escaped = False
|
|
55
|
+
continue
|
|
56
|
+
if in_double and char == "\\":
|
|
57
|
+
escaped = True
|
|
58
|
+
continue
|
|
59
|
+
if char == "'" and not in_double:
|
|
60
|
+
in_single = not in_single
|
|
61
|
+
elif char == '"' and not in_single:
|
|
62
|
+
in_double = not in_double
|
|
63
|
+
elif char == "#" and not in_single and not in_double and (index == 0 or line[index - 1].isspace()):
|
|
64
|
+
return line[:index]
|
|
65
|
+
return line
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def parse_scalar(raw):
|
|
69
|
+
value = raw.strip()
|
|
70
|
+
if value == "":
|
|
71
|
+
return None
|
|
72
|
+
if value[0:1] in ("[", "{", "&", "*") or value.startswith("|") or value.startswith(">"):
|
|
73
|
+
raise ConfigError("yaml", "unsupported YAML feature in schema v1")
|
|
74
|
+
if value.startswith('"') and value.endswith('"'):
|
|
75
|
+
inner = value[1:-1]
|
|
76
|
+
if "\\" in inner:
|
|
77
|
+
raise ConfigError("yaml", "unsupported escape in schema v1 scalar; use --parser pyyaml")
|
|
78
|
+
return inner
|
|
79
|
+
if value.startswith("'") and value.endswith("'"):
|
|
80
|
+
inner = value[1:-1]
|
|
81
|
+
if "''" in inner:
|
|
82
|
+
raise ConfigError("yaml", "unsupported escape in schema v1 scalar; use --parser pyyaml")
|
|
83
|
+
return value[1:-1]
|
|
84
|
+
if re.fullmatch(r"-?\d+", value):
|
|
85
|
+
return int(value)
|
|
86
|
+
if re.fullmatch(r"-?(\d+\.\d*|\d*\.\d+)", value):
|
|
87
|
+
return float(value)
|
|
88
|
+
if value == "true":
|
|
89
|
+
return True
|
|
90
|
+
if value == "false":
|
|
91
|
+
return False
|
|
92
|
+
if value in ("null", "~"):
|
|
93
|
+
return None
|
|
94
|
+
return value
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def parse_fallback(text):
|
|
98
|
+
data = {}
|
|
99
|
+
current_key = None
|
|
100
|
+
current_map = None
|
|
101
|
+
for raw_line in text.splitlines():
|
|
102
|
+
line = strip_comment(raw_line).rstrip()
|
|
103
|
+
if not line.strip():
|
|
104
|
+
continue
|
|
105
|
+
if line.lstrip().startswith("-"):
|
|
106
|
+
raise ConfigError("yaml", "lists are not supported in schema v1")
|
|
107
|
+
indent = len(line) - len(line.lstrip(" "))
|
|
108
|
+
if indent not in (0, 2):
|
|
109
|
+
raise ConfigError("yaml", "only two-space nested mappings are supported")
|
|
110
|
+
stripped = line.strip()
|
|
111
|
+
if ":" not in stripped:
|
|
112
|
+
raise ConfigError("yaml", "expected key: value")
|
|
113
|
+
key, raw_value = stripped.split(":", 1)
|
|
114
|
+
key = key.strip()
|
|
115
|
+
if not key:
|
|
116
|
+
raise ConfigError("yaml", "empty key")
|
|
117
|
+
if indent == 0:
|
|
118
|
+
if key in data:
|
|
119
|
+
raise ConfigError(key, "duplicate key")
|
|
120
|
+
if raw_value.strip() == "" and key in {"android", "ios", "web", "version", "sdk"}:
|
|
121
|
+
value = {}
|
|
122
|
+
current_key = key
|
|
123
|
+
current_map = value
|
|
124
|
+
else:
|
|
125
|
+
value = parse_scalar(raw_value)
|
|
126
|
+
current_key = None
|
|
127
|
+
current_map = None
|
|
128
|
+
data[key] = value
|
|
129
|
+
else:
|
|
130
|
+
if current_map is None or current_key is None:
|
|
131
|
+
raise ConfigError("yaml", f"nested key {key} has no parent")
|
|
132
|
+
if key in current_map:
|
|
133
|
+
raise ConfigError(f"{current_key}.{key}", "duplicate key")
|
|
134
|
+
current_map[key] = parse_scalar(raw_value)
|
|
135
|
+
return data
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def parse_pyyaml(text):
|
|
139
|
+
try:
|
|
140
|
+
import yaml
|
|
141
|
+
except ImportError as exc:
|
|
142
|
+
raise ConfigError("yaml", "PyYAML is not installed") from exc
|
|
143
|
+
class UniqueKeyLoader(yaml.SafeLoader):
|
|
144
|
+
pass
|
|
145
|
+
|
|
146
|
+
def construct_unique_mapping(loader, node, deep=False):
|
|
147
|
+
mapping = {}
|
|
148
|
+
for key_node, value_node in node.value:
|
|
149
|
+
key = loader.construct_object(key_node, deep=deep)
|
|
150
|
+
if key in mapping:
|
|
151
|
+
raise ConfigError("yaml", f"duplicate key {key}")
|
|
152
|
+
mapping[key] = loader.construct_object(value_node, deep=deep)
|
|
153
|
+
return mapping
|
|
154
|
+
|
|
155
|
+
UniqueKeyLoader.add_constructor(
|
|
156
|
+
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
|
157
|
+
construct_unique_mapping,
|
|
158
|
+
)
|
|
159
|
+
try:
|
|
160
|
+
loaded = yaml.load(text, Loader=UniqueKeyLoader)
|
|
161
|
+
except ConfigError:
|
|
162
|
+
raise
|
|
163
|
+
except Exception as exc:
|
|
164
|
+
raise ConfigError("yaml", str(exc)) from exc
|
|
165
|
+
return {} if loaded is None else loaded
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def load_yaml(path, parser):
|
|
169
|
+
config_path = Path(path)
|
|
170
|
+
if not config_path.is_file():
|
|
171
|
+
raise ConfigError("supersdk.yaml", f"not found: {config_path}")
|
|
172
|
+
text = config_path.read_text(encoding="utf-8")
|
|
173
|
+
if parser == "pyyaml":
|
|
174
|
+
return parse_pyyaml(text)
|
|
175
|
+
if parser in ("auto", "fallback"):
|
|
176
|
+
return parse_fallback(text)
|
|
177
|
+
raise ConfigError("parser", f"unknown parser: {parser}")
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def require_mapping(value, field):
|
|
181
|
+
if value is None:
|
|
182
|
+
return {}
|
|
183
|
+
if not isinstance(value, dict):
|
|
184
|
+
raise ConfigError(field, "must be a mapping")
|
|
185
|
+
return value
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def require_string(value, field, allow_empty=False):
|
|
189
|
+
if value is None:
|
|
190
|
+
raise ConfigError(field, "must not be empty")
|
|
191
|
+
if not isinstance(value, str):
|
|
192
|
+
raise ConfigError(field, "must be a string")
|
|
193
|
+
if not allow_empty and value.strip() == "":
|
|
194
|
+
raise ConfigError(field, "must not be empty")
|
|
195
|
+
return value
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def validate_android_version(value):
|
|
199
|
+
if not isinstance(value, str) or not re.fullmatch(ANDROID_VERSION_RE, value.strip()):
|
|
200
|
+
raise ConfigError("sdk.androidVersion", f"must match {ANDROID_VERSION_RE}")
|
|
201
|
+
return value.strip()
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def validate_version_code(value):
|
|
205
|
+
if isinstance(value, bool) or not isinstance(value, int) or value <= 0 or value > 2147483647:
|
|
206
|
+
raise ConfigError("version.code", "must be a positive 32-bit integer")
|
|
207
|
+
return value
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def is_positive_int(value):
|
|
211
|
+
return not isinstance(value, bool) and isinstance(value, int) and value > 0
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def validate_known_namespace(data, namespace):
|
|
215
|
+
value = require_mapping(data.get(namespace), namespace)
|
|
216
|
+
allowed = NAMESPACE_KEYS[namespace]
|
|
217
|
+
for key in value:
|
|
218
|
+
if namespace == "ios" and key in {"bundle_id", "bundleID", "bundle-id"}:
|
|
219
|
+
raise ConfigError("ios.bundleId", "use canonical ios.bundleId")
|
|
220
|
+
if key not in allowed:
|
|
221
|
+
raise ConfigError(f"{namespace}.{key}", "unknown key")
|
|
222
|
+
return value
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def validate_ios_spm_mode(ios):
|
|
226
|
+
if "spmMode" not in ios:
|
|
227
|
+
return "mock"
|
|
228
|
+
value = ios.get("spmMode")
|
|
229
|
+
if not isinstance(value, str) or value.strip() == "" or value not in {"mock", "real"}:
|
|
230
|
+
raise ConfigError("ios.spmMode", "must be mock or real")
|
|
231
|
+
return value
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def validate_ios_package_url(value):
|
|
235
|
+
if value is None or not isinstance(value, str) or value.strip() == "":
|
|
236
|
+
raise ConfigError("sdk.iosPackageUrl", "must be an HTTPS URL or ios.spmMode must be mock")
|
|
237
|
+
parsed = urlparse(value)
|
|
238
|
+
if parsed.scheme != "https" or not parsed.netloc:
|
|
239
|
+
raise ConfigError("sdk.iosPackageUrl", "must be an HTTPS URL")
|
|
240
|
+
host = (parsed.hostname or "").lower()
|
|
241
|
+
if (
|
|
242
|
+
host in {"localhost", "127.0.0.1", "0.0.0.0", "::1", "example.com", "example.org"}
|
|
243
|
+
or host.endswith(".localhost")
|
|
244
|
+
or host.endswith(".local")
|
|
245
|
+
or host.endswith(".example.com")
|
|
246
|
+
or host.endswith(".example.org")
|
|
247
|
+
or "example.invalid" in host
|
|
248
|
+
):
|
|
249
|
+
raise ConfigError("sdk.iosPackageUrl", "must not be a placeholder or local URL")
|
|
250
|
+
try:
|
|
251
|
+
address = ipaddress.ip_address(host)
|
|
252
|
+
except ValueError:
|
|
253
|
+
address = None
|
|
254
|
+
if address and (address.is_private or address.is_loopback or address.is_link_local or address.is_unspecified):
|
|
255
|
+
raise ConfigError("sdk.iosPackageUrl", "must not be a placeholder or local URL")
|
|
256
|
+
return value
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def validate_ios_version(value):
|
|
260
|
+
if not isinstance(value, str) or not re.fullmatch(IOS_VERSION_PATTERN, value):
|
|
261
|
+
raise ConfigError("sdk.iosVersion", f"must match {IOS_VERSION_PATTERN}")
|
|
262
|
+
return value
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def validate_config(raw, selected=None):
|
|
266
|
+
selected = set(selected or PLATFORMS)
|
|
267
|
+
if not isinstance(raw, dict):
|
|
268
|
+
raise ConfigError("yaml", "top-level document must be a mapping")
|
|
269
|
+
for key in raw:
|
|
270
|
+
if key not in ROOT_KEYS:
|
|
271
|
+
raise ConfigError(key, "unknown root key")
|
|
272
|
+
|
|
273
|
+
if type(raw.get("schema_version")) is not int or raw.get("schema_version") != 1:
|
|
274
|
+
raise ConfigError("schema_version", "must be 1")
|
|
275
|
+
app_id = require_string(raw.get("app_id"), "app_id")
|
|
276
|
+
if not re.fullmatch(APP_ID_PATTERN, app_id):
|
|
277
|
+
raise ConfigError("app_id", f"must match ^{APP_ID_PATTERN}$")
|
|
278
|
+
|
|
279
|
+
name = raw.get("name", "SuperSDK Simple")
|
|
280
|
+
name = require_string(name, "name")
|
|
281
|
+
|
|
282
|
+
android = validate_known_namespace(raw, "android")
|
|
283
|
+
ios = validate_known_namespace(raw, "ios")
|
|
284
|
+
web = validate_known_namespace(raw, "web")
|
|
285
|
+
version = validate_known_namespace(raw, "version")
|
|
286
|
+
sdk = require_mapping(raw.get("sdk", {}), "sdk")
|
|
287
|
+
for key, value in sdk.items():
|
|
288
|
+
if not isinstance(key, str):
|
|
289
|
+
raise ConfigError("sdk", "keys must be strings")
|
|
290
|
+
if key not in SDK_KEYS:
|
|
291
|
+
raise ConfigError(f"sdk.{key}", "unknown key")
|
|
292
|
+
if isinstance(value, (dict, list)):
|
|
293
|
+
raise ConfigError(f"sdk.{key}", "must be a scalar")
|
|
294
|
+
if key == "androidVersion" and value is not None:
|
|
295
|
+
validate_android_version(value)
|
|
296
|
+
|
|
297
|
+
android_package = android.get("package")
|
|
298
|
+
if android_package is not None:
|
|
299
|
+
android_package = require_string(android_package, "android.package")
|
|
300
|
+
if not re.fullmatch(r"[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+", android_package):
|
|
301
|
+
raise ConfigError("android.package", "must be a Java package")
|
|
302
|
+
|
|
303
|
+
bundle_id = ios.get("bundleId")
|
|
304
|
+
if bundle_id is not None:
|
|
305
|
+
bundle_id = require_string(bundle_id, "ios.bundleId")
|
|
306
|
+
if not re.fullmatch(BUNDLE_ID_PATTERN, bundle_id):
|
|
307
|
+
raise ConfigError("ios.bundleId", "must be a bundle identifier")
|
|
308
|
+
spm_mode = validate_ios_spm_mode(ios)
|
|
309
|
+
if spm_mode == "mock":
|
|
310
|
+
for key in ("iosPackageUrl", "iosVersion"):
|
|
311
|
+
if key in sdk:
|
|
312
|
+
raise ConfigError(f"sdk.{key}", "not allowed when ios.spmMode is mock")
|
|
313
|
+
else:
|
|
314
|
+
validate_ios_package_url(sdk.get("iosPackageUrl"))
|
|
315
|
+
validate_ios_version(sdk.get("iosVersion"))
|
|
316
|
+
|
|
317
|
+
web_host = web.get("host")
|
|
318
|
+
if web_host is not None and "web" in selected:
|
|
319
|
+
web_host = require_string(web_host, "web.host")
|
|
320
|
+
try:
|
|
321
|
+
web_host = normalize_host(web_host)
|
|
322
|
+
except HostValidationError as exc:
|
|
323
|
+
raise ConfigError("web.host", str(exc)) from exc
|
|
324
|
+
|
|
325
|
+
version_name = version.get("name", "1.0.0")
|
|
326
|
+
if isinstance(version_name, (int, float)) and not isinstance(version_name, bool):
|
|
327
|
+
version_name = str(version_name)
|
|
328
|
+
version_name = require_string(version_name, "version.name")
|
|
329
|
+
version_code = validate_version_code(version.get("code", 1))
|
|
330
|
+
|
|
331
|
+
return {
|
|
332
|
+
"schema_version": 1,
|
|
333
|
+
"app_id": app_id,
|
|
334
|
+
"name": name,
|
|
335
|
+
"platforms": {
|
|
336
|
+
"android": {"package": android_package},
|
|
337
|
+
"ios": {"bundleId": bundle_id, "spmMode": spm_mode},
|
|
338
|
+
"web": {"host": web_host},
|
|
339
|
+
},
|
|
340
|
+
"version": {"name": version_name, "code": version_code},
|
|
341
|
+
"sdk": sdk,
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def require_android_config(normalized):
|
|
346
|
+
android = normalized["platforms"]["android"]
|
|
347
|
+
package_name = android.get("package")
|
|
348
|
+
if package_name is None or not isinstance(package_name, str) or package_name.strip() == "":
|
|
349
|
+
raise ConfigError("android.package", "required for android platform")
|
|
350
|
+
if not re.fullmatch(r"[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+", package_name):
|
|
351
|
+
raise ConfigError("android.package", "must be a valid Java package")
|
|
352
|
+
if package_name == ANDROID_NAMESPACE:
|
|
353
|
+
raise ConfigError("android.package", f"must differ from fixed namespace {ANDROID_NAMESPACE}")
|
|
354
|
+
|
|
355
|
+
name = normalized["name"]
|
|
356
|
+
if name.strip() == "" or any(ord(char) < 32 for char in name):
|
|
357
|
+
raise ConfigError("name", "must be a non-empty Android app label without control characters")
|
|
358
|
+
if name != name.strip():
|
|
359
|
+
raise ConfigError("name", "must not have leading or trailing whitespace")
|
|
360
|
+
|
|
361
|
+
version_name = normalized["version"]["name"]
|
|
362
|
+
if not isinstance(version_name, str) or version_name.strip() == "":
|
|
363
|
+
raise ConfigError("version.name", "must be a non-empty Android versionName")
|
|
364
|
+
|
|
365
|
+
version_code = normalized["version"]["code"]
|
|
366
|
+
validate_version_code(version_code)
|
|
367
|
+
|
|
368
|
+
android_version = normalized["sdk"].get("androidVersion")
|
|
369
|
+
validate_android_version(android_version)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def require_ios_config(normalized):
|
|
373
|
+
ios = normalized["platforms"]["ios"]
|
|
374
|
+
if ios.get("spmMode", "mock") != "real":
|
|
375
|
+
return
|
|
376
|
+
for key in SDK_PROJECTION["ios"]["real"]:
|
|
377
|
+
require_string(normalized["sdk"].get(key), f"sdk.{key}")
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def enforce_platform_contracts(normalized, platform):
|
|
381
|
+
for item in selected_platforms(platform):
|
|
382
|
+
if item == "android":
|
|
383
|
+
require_android_config(normalized)
|
|
384
|
+
if item == "ios":
|
|
385
|
+
require_ios_config(normalized)
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def load_and_validate(config_path, parser, selected=None):
|
|
389
|
+
return validate_config(load_yaml(config_path, parser), selected)
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def selected_platforms(platform):
|
|
393
|
+
return list(PLATFORMS) if platform == "all" else [platform]
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def target_path(root, platform):
|
|
397
|
+
return Path(root) / platform / ".supersdk" / "managed" / "config.json"
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def project_sdk(platform, mode="mock"):
|
|
401
|
+
if platform not in SDK_PROJECTION:
|
|
402
|
+
raise ConfigError("platform", f"unknown platform: {platform}")
|
|
403
|
+
if mode not in SDK_PROJECTION[platform]:
|
|
404
|
+
raise ConfigError("sdk", f"unknown projection mode: {mode}")
|
|
405
|
+
return list(SDK_PROJECTION[platform][mode])
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
def project_sdk_values(normalized, platform):
|
|
409
|
+
sdk = normalized["sdk"]
|
|
410
|
+
mode = normalized["platforms"][platform].get("spmMode", "mock")
|
|
411
|
+
return {key: sdk[key] for key in project_sdk(platform, mode) if key in sdk}
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def target_content(normalized, platform):
|
|
415
|
+
return {
|
|
416
|
+
"managed_by": MANAGED_BY,
|
|
417
|
+
"schema_version": MANAGED_CONFIG_SCHEMA_VERSION,
|
|
418
|
+
"app_id": normalized["app_id"],
|
|
419
|
+
"name": normalized["name"],
|
|
420
|
+
"platform": platform,
|
|
421
|
+
"version": normalized["version"],
|
|
422
|
+
"sdk": project_sdk_values(normalized, platform),
|
|
423
|
+
"platform_config": normalized["platforms"][platform],
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def test_fail_point(name):
|
|
428
|
+
# Internal regression-test hook for atomic-write rollback; not a public template feature.
|
|
429
|
+
if os.environ.get("SUPERSDK_TEMPLATE_TEST_MODE") != "1":
|
|
430
|
+
return 0
|
|
431
|
+
raw = os.environ.get(name, "0") or "0"
|
|
432
|
+
try:
|
|
433
|
+
value = int(raw)
|
|
434
|
+
except ValueError as exc:
|
|
435
|
+
raise ConfigError(name, "must be an integer") from exc
|
|
436
|
+
if value < 0:
|
|
437
|
+
raise ConfigError(name, "must be non-negative")
|
|
438
|
+
return value
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def is_managed_or_empty(path):
|
|
442
|
+
if not path.exists():
|
|
443
|
+
return True
|
|
444
|
+
if path.stat().st_size == 0:
|
|
445
|
+
return True
|
|
446
|
+
try:
|
|
447
|
+
return json.loads(path.read_text(encoding="utf-8")).get("managed_by") == MANAGED_BY
|
|
448
|
+
except OSError as exc:
|
|
449
|
+
raise ConfigError(str(path), f"cannot inspect target: {exc}") from exc
|
|
450
|
+
except (json.JSONDecodeError, UnicodeDecodeError, AttributeError):
|
|
451
|
+
return False
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def build_write_plan(root, normalized, platform):
|
|
455
|
+
enforce_platform_contracts(normalized, platform)
|
|
456
|
+
return [
|
|
457
|
+
(target_path(root, item), json.dumps(target_content(normalized, item), indent=2, sort_keys=True) + "\n")
|
|
458
|
+
for item in selected_platforms(platform)
|
|
459
|
+
]
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def inject(root, config, platform, parser, force, dry_run):
|
|
463
|
+
root_path = Path(root)
|
|
464
|
+
normalized = load_and_validate(config, parser, selected_platforms(platform))
|
|
465
|
+
plan = build_write_plan(root, normalized, platform)
|
|
466
|
+
if dry_run:
|
|
467
|
+
refused = [str(path) for path, _ in plan if not force and not is_managed_or_empty(path)]
|
|
468
|
+
print(
|
|
469
|
+
json.dumps(
|
|
470
|
+
{
|
|
471
|
+
"would_abort": bool(refused),
|
|
472
|
+
"would_write": [] if refused else [str(path) for path, _ in plan],
|
|
473
|
+
"would_refuse": refused,
|
|
474
|
+
},
|
|
475
|
+
indent=2,
|
|
476
|
+
sort_keys=True,
|
|
477
|
+
)
|
|
478
|
+
)
|
|
479
|
+
return
|
|
480
|
+
refused = [str(path) for path, _ in plan if not force and not is_managed_or_empty(path)]
|
|
481
|
+
if refused:
|
|
482
|
+
raise ConfigError("refuse", "refuse overwrite unmanaged file(s): " + ", ".join(refused))
|
|
483
|
+
|
|
484
|
+
temp_files = []
|
|
485
|
+
backups = {}
|
|
486
|
+
replaced = []
|
|
487
|
+
created_dirs = set()
|
|
488
|
+
run_id = f"{os.getpid()}.{uuid.uuid4().hex}"
|
|
489
|
+
fail_after = test_fail_point("SUPERSDK_TEMPLATE_TEST_FAIL_REPLACE_AFTER")
|
|
490
|
+
fail_at = test_fail_point("SUPERSDK_TEMPLATE_TEST_FAIL_REPLACE_AT")
|
|
491
|
+
try:
|
|
492
|
+
for path, content in plan:
|
|
493
|
+
directory = path.parent
|
|
494
|
+
while directory != root_path:
|
|
495
|
+
if not directory.exists():
|
|
496
|
+
created_dirs.add(directory)
|
|
497
|
+
directory = directory.parent
|
|
498
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
499
|
+
temp = path.with_name(f".{path.name}.tmp.{run_id}")
|
|
500
|
+
if temp.exists():
|
|
501
|
+
raise OSError(f"sidecar already exists: {temp}")
|
|
502
|
+
temp.write_text(content, encoding="utf-8")
|
|
503
|
+
temp_files.append(temp)
|
|
504
|
+
|
|
505
|
+
for index, (path, _) in enumerate(plan, start=1):
|
|
506
|
+
backup = path.with_name(f".{path.name}.bak.{run_id}")
|
|
507
|
+
if backup.exists():
|
|
508
|
+
raise OSError(f"sidecar already exists: {backup}")
|
|
509
|
+
if path.exists():
|
|
510
|
+
os.replace(path, backup)
|
|
511
|
+
backups[path] = backup
|
|
512
|
+
else:
|
|
513
|
+
backups[path] = None
|
|
514
|
+
if fail_at and index >= fail_at:
|
|
515
|
+
raise OSError("simulated replace syscall failure")
|
|
516
|
+
os.replace(temp_files[index - 1], path)
|
|
517
|
+
replaced.append(path)
|
|
518
|
+
if fail_after and index >= fail_after:
|
|
519
|
+
raise OSError("simulated replace failure")
|
|
520
|
+
except Exception as exc:
|
|
521
|
+
rollback_errors = []
|
|
522
|
+
|
|
523
|
+
def remember(action, target, error):
|
|
524
|
+
rollback_errors.append(f"{action} {target}: {error}")
|
|
525
|
+
|
|
526
|
+
for path in replaced:
|
|
527
|
+
try:
|
|
528
|
+
if path.exists():
|
|
529
|
+
path.unlink()
|
|
530
|
+
except Exception as rollback_exc:
|
|
531
|
+
remember("remove", path, rollback_exc)
|
|
532
|
+
for path, backup in backups.items():
|
|
533
|
+
try:
|
|
534
|
+
if backup is not None and backup.exists():
|
|
535
|
+
os.replace(backup, path)
|
|
536
|
+
except Exception as rollback_exc:
|
|
537
|
+
remember("restore", path, rollback_exc)
|
|
538
|
+
for temp in temp_files:
|
|
539
|
+
try:
|
|
540
|
+
if temp.exists():
|
|
541
|
+
temp.unlink()
|
|
542
|
+
except Exception as rollback_exc:
|
|
543
|
+
remember("remove", temp, rollback_exc)
|
|
544
|
+
for directory in sorted(created_dirs, key=lambda item: len(str(item)), reverse=True):
|
|
545
|
+
try:
|
|
546
|
+
directory.rmdir()
|
|
547
|
+
except OSError:
|
|
548
|
+
pass
|
|
549
|
+
if rollback_errors:
|
|
550
|
+
print(f"write failed; rollback incomplete: {exc}; {'; '.join(rollback_errors)}", file=sys.stderr)
|
|
551
|
+
else:
|
|
552
|
+
print(f"write failed; rolled back: {exc}", file=sys.stderr)
|
|
553
|
+
raise SystemExit(1)
|
|
554
|
+
|
|
555
|
+
cleanup_errors = []
|
|
556
|
+
for backup in backups.values():
|
|
557
|
+
if backup is not None and backup.exists():
|
|
558
|
+
try:
|
|
559
|
+
backup.unlink()
|
|
560
|
+
except Exception as cleanup_exc:
|
|
561
|
+
cleanup_errors.append(f"remove {backup}: {cleanup_exc}")
|
|
562
|
+
if cleanup_errors:
|
|
563
|
+
print(f"warning: backup cleanup incomplete: {'; '.join(cleanup_errors)}", file=sys.stderr)
|
|
564
|
+
print(json.dumps({"written": [str(path) for path, _ in plan], "skipped": [], "refused": []}, indent=2, sort_keys=True))
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def print_plan(root, config, platform, parser):
|
|
568
|
+
normalized = load_and_validate(config, parser, selected_platforms(platform))
|
|
569
|
+
enforce_platform_contracts(normalized, platform)
|
|
570
|
+
for item in selected_platforms(platform):
|
|
571
|
+
print(f"platform={item} helper={Path(root) / 'scripts' / ('build_' + item + '.sh')} mode=plan")
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
def main(argv):
|
|
575
|
+
parser = argparse.ArgumentParser(description="SuperSDK template schema and injection helper")
|
|
576
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
577
|
+
|
|
578
|
+
validate = sub.add_parser("validate")
|
|
579
|
+
validate.add_argument("--config", required=True)
|
|
580
|
+
validate.add_argument("--parser", choices=["auto", "pyyaml", "fallback"], default="auto")
|
|
581
|
+
|
|
582
|
+
plan = sub.add_parser("plan")
|
|
583
|
+
plan.add_argument("--root", required=True)
|
|
584
|
+
plan.add_argument("--config", required=True)
|
|
585
|
+
plan.add_argument("--platform", choices=["all", *PLATFORMS], default="all")
|
|
586
|
+
plan.add_argument("--parser", choices=["auto", "pyyaml", "fallback"], default="auto")
|
|
587
|
+
|
|
588
|
+
inject_cmd = sub.add_parser("inject")
|
|
589
|
+
inject_cmd.add_argument("--root", required=True)
|
|
590
|
+
inject_cmd.add_argument("--config", required=True)
|
|
591
|
+
inject_cmd.add_argument("--platform", choices=["all", *PLATFORMS], default="all")
|
|
592
|
+
inject_cmd.add_argument("--parser", choices=["auto", "pyyaml", "fallback"], default="auto")
|
|
593
|
+
inject_cmd.add_argument("--force", action="store_true")
|
|
594
|
+
inject_cmd.add_argument("--dry-run", action="store_true")
|
|
595
|
+
|
|
596
|
+
args = parser.parse_args(argv)
|
|
597
|
+
try:
|
|
598
|
+
if args.command == "validate":
|
|
599
|
+
print(json.dumps(load_and_validate(args.config, args.parser), indent=2, sort_keys=True))
|
|
600
|
+
elif args.command == "plan":
|
|
601
|
+
print_plan(args.root, args.config, args.platform, args.parser)
|
|
602
|
+
elif args.command == "inject":
|
|
603
|
+
inject(args.root, args.config, args.platform, args.parser, args.force, args.dry_run)
|
|
604
|
+
except ConfigError as exc:
|
|
605
|
+
print(str(exc), file=sys.stderr)
|
|
606
|
+
return 1
|
|
607
|
+
return 0
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
if __name__ == "__main__":
|
|
611
|
+
raise SystemExit(main(sys.argv[1:]))
|