@blotch/angular-lynx-native 22.3.2 → 22.3.3
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/README.md +8 -2
- package/dist/android/project.d.ts +27 -3
- package/dist/android/project.d.ts.map +1 -1
- package/dist/android/project.js +80 -9
- package/dist/eject.d.ts.map +1 -1
- package/dist/eject.js +1 -0
- package/dist/files.d.ts +24 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +41 -0
- package/dist/generate.d.ts +17 -2
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +68 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/ios/project.d.ts +21 -2
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +58 -2
- package/dist/options.d.ts +68 -0
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +27 -0
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +18 -3
- package/dist/plugins.d.ts +245 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +1244 -0
- package/package.json +1 -1
- package/templates/android/README.md +12 -4
- package/templates/android/app/build.gradle.kts +27 -9
- package/templates/android/app/src/main/AndroidManifest.xml +5 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AngularLynxPlugins.kt +13 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AudioRecorderModule.kt +520 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/EdgeToEdgeOverlay.kt +24 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/EngineThreadHint.kt +156 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/GeolocationModule.kt +557 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/HostElementUI.kt +331 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +143 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePipeline.kt +83 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +153 -9
- package/templates/android/app/src/main/java/dev/angularlynx/shell/NetworkStatus.kt +121 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +10 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +203 -16
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +1 -8
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +11 -1
- package/templates/android/app/src/main/res/xml/share_paths.xml +7 -2
- package/templates/ios/App/AngularLynxPlugins.swift +22 -0
- package/templates/ios/App/AnimationFrameRate.swift +66 -0
- package/templates/ios/App/AppDelegate.swift +3 -1
- package/templates/ios/App/AudioRecorderModule.swift +433 -0
- package/templates/ios/App/GeolocationModule.swift +455 -0
- package/templates/ios/App/HostElementUI.swift +270 -0
- package/templates/ios/App/ImagePickerModule.swift +160 -1
- package/templates/ios/App/LynxViewController.swift +66 -5
- package/templates/ios/App/NetworkStatus.swift +41 -0
package/dist/plugins.js
ADDED
|
@@ -0,0 +1,1244 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { listFiles } from "./files.js";
|
|
4
|
+
import { NativeConfigError } from "./options.js";
|
|
5
|
+
/**
|
|
6
|
+
* The file that described a native plugin, in its folder, before each
|
|
7
|
+
* plugin was described in angular.json's `nativePlugins` (issue #1051). A
|
|
8
|
+
* folder that still has one is refused, so what it says isn't left unread
|
|
9
|
+
* without a word: the build would leave out the modules and elements it
|
|
10
|
+
* names, which the app would find missing only when it runs.
|
|
11
|
+
*/
|
|
12
|
+
export const LEGACY_PLUGIN_MANIFEST = 'angular-lynx-plugin.json';
|
|
13
|
+
const PLATFORM_LABEL = {
|
|
14
|
+
ios: 'iOS',
|
|
15
|
+
android: 'Android',
|
|
16
|
+
};
|
|
17
|
+
const PLATFORM_KEYS = {
|
|
18
|
+
ios: ['sourceDir', 'modules', 'elements'],
|
|
19
|
+
android: ['packageName', 'sourceDir', 'resDir', 'modules', 'elements'],
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* A custom element's name, as HTML has them: lowercase, starting with a
|
|
23
|
+
* letter, with a hyphen anywhere after it (`native--switch`, `native-`),
|
|
24
|
+
* which keeps a plugin's tags apart from Lynx's elements without one
|
|
25
|
+
* (`view`, `image`). Letters, digits and hyphens only, as the runtime's
|
|
26
|
+
* `registerNativeElement()` takes.
|
|
27
|
+
*/
|
|
28
|
+
const TAG = /^[a-z][a-z0-9-]*-[a-z0-9-]*$/;
|
|
29
|
+
/**
|
|
30
|
+
* The tags something else already draws, which a plugin's element would
|
|
31
|
+
* replace without a word: a behavior added later wins on Android.
|
|
32
|
+
*
|
|
33
|
+
* - The runtime's `LYNX_ELEMENT_TAGS` (`packages/runtime/src/lib/lynx-document/element-tags.ts`),
|
|
34
|
+
* which a test keeps this copy of.
|
|
35
|
+
* - Lynx's own on Android (`BuiltInUIRegistry` in the 4.1.0 AAR) that the
|
|
36
|
+
* runtime doesn't create: `component`, `list-container`, `inline-text`,
|
|
37
|
+
* `inline-image` and `bounce-view`.
|
|
38
|
+
* - XElement's `animax-view`, which the shells leave out but the name is
|
|
39
|
+
* still XElement's.
|
|
40
|
+
*
|
|
41
|
+
* Tags starting with {@link RESERVED_TAG_PREFIXES} are refused too.
|
|
42
|
+
*/
|
|
43
|
+
export const RESERVED_TAGS = new Set([
|
|
44
|
+
// The runtime's LYNX_ELEMENT_TAGS, in its order.
|
|
45
|
+
'page',
|
|
46
|
+
'view',
|
|
47
|
+
'text',
|
|
48
|
+
'raw-text',
|
|
49
|
+
'inline-truncation',
|
|
50
|
+
'image',
|
|
51
|
+
'scroll-view',
|
|
52
|
+
'list',
|
|
53
|
+
'list-item',
|
|
54
|
+
'block',
|
|
55
|
+
'if',
|
|
56
|
+
'for',
|
|
57
|
+
'frame',
|
|
58
|
+
'input',
|
|
59
|
+
'textarea',
|
|
60
|
+
'overlay',
|
|
61
|
+
'svg',
|
|
62
|
+
'viewpager',
|
|
63
|
+
'viewpager-item',
|
|
64
|
+
'refresh',
|
|
65
|
+
'refresh-header',
|
|
66
|
+
'title-bar-view',
|
|
67
|
+
'scroll-coordinator',
|
|
68
|
+
'scroll-coordinator-header',
|
|
69
|
+
'scroll-coordinator-toolbar',
|
|
70
|
+
'scroll-coordinator-slot',
|
|
71
|
+
'scroll-coordinator-slot-drag',
|
|
72
|
+
'blur-view',
|
|
73
|
+
'webview',
|
|
74
|
+
'video',
|
|
75
|
+
'cover-view',
|
|
76
|
+
'canvas',
|
|
77
|
+
'apple-sign-in-button',
|
|
78
|
+
'comment',
|
|
79
|
+
// Lynx's on Android, besides those.
|
|
80
|
+
'component',
|
|
81
|
+
'list-container',
|
|
82
|
+
'inline-text',
|
|
83
|
+
'inline-image',
|
|
84
|
+
'bounce-view',
|
|
85
|
+
// XElement's.
|
|
86
|
+
'animax-view',
|
|
87
|
+
]);
|
|
88
|
+
/**
|
|
89
|
+
* `x-` is XElement's (`x-svg`) and Lynx for Web's (`x-view`, `x-text`), and
|
|
90
|
+
* `lynx-` Lynx for Web's own (`lynx-view`, `lynx-wrapper`).
|
|
91
|
+
*/
|
|
92
|
+
const RESERVED_TAG_PREFIXES = ['x-', 'lynx-'];
|
|
93
|
+
/** What `getNativeModule()` takes: a JavaScript identifier. */
|
|
94
|
+
const MODULE_NAME = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
95
|
+
/**
|
|
96
|
+
* The shells' own modules are `AngularLynx…`, and their development
|
|
97
|
+
* modules, in place of Lynx DevTool's, `Lynx…` (`LynxWebSocketModule`,
|
|
98
|
+
* `LynxDevToolSetModule`), as Lynx's own are. A plugin's module of such a name
|
|
99
|
+
* would replace one the runtime, the dev server or Lynx calls.
|
|
100
|
+
*/
|
|
101
|
+
const RESERVED_MODULE_PREFIXES = ['AngularLynx', 'Lynx'];
|
|
102
|
+
/**
|
|
103
|
+
* A Swift class in the app's module, which the registration names as it is:
|
|
104
|
+
* the sources are compiled into the app, so no module prefix.
|
|
105
|
+
*/
|
|
106
|
+
const SWIFT_CLASS = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
107
|
+
/** A Java package: `com.example.greeter`. */
|
|
108
|
+
const JVM_PACKAGE = /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*$/;
|
|
109
|
+
/** A fully qualified JVM class: `com.example.greeter.GreeterModule`. */
|
|
110
|
+
const JVM_CLASS = /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*\.[A-Za-z_][A-Za-z0-9_]*$/;
|
|
111
|
+
/** The package of the Android shell's own code, which a plugin keeps out of. */
|
|
112
|
+
const SHELL_PACKAGE = 'dev.angularlynx.shell';
|
|
113
|
+
/**
|
|
114
|
+
* Reads and checks the plugins angular.json's `nativePlugins` lists, each a
|
|
115
|
+
* folder relative to the project root and what each platform registers from
|
|
116
|
+
* it. What's wrong is caught here, with the entry's folder in the message:
|
|
117
|
+
* the Swift and Kotlin the generators write from an entry would otherwise
|
|
118
|
+
* fail in Xcode or Gradle, far from it.
|
|
119
|
+
*
|
|
120
|
+
* @param entries - `nativePlugins` from the builder options.
|
|
121
|
+
* @param projectRoot - The Angular project's root folder.
|
|
122
|
+
* @throws NativeConfigError for anything a build would fail on, or a name
|
|
123
|
+
* that would replace one of Lynx's or the shell's.
|
|
124
|
+
*/
|
|
125
|
+
export const readNativePlugins = (entries, projectRoot) => {
|
|
126
|
+
const plugins = entries.map((entry) => readPlugin(entry, projectRoot));
|
|
127
|
+
// Case-insensitively: the folders on a Mac's disk are.
|
|
128
|
+
const names = new Map();
|
|
129
|
+
for (const [index, plugin] of plugins.entries()) {
|
|
130
|
+
const entry = entries[index].path;
|
|
131
|
+
const other = names.get(plugin.name.toLowerCase());
|
|
132
|
+
if (other !== undefined) {
|
|
133
|
+
throw new NativeConfigError(`nativePlugins entries ${other} and ${entry} are both named "${plugin.name}". The generated projects keep each plugin's sources in a folder of its name (capitals aside): rename one of the folders.`);
|
|
134
|
+
}
|
|
135
|
+
names.set(plugin.name.toLowerCase(), entry);
|
|
136
|
+
}
|
|
137
|
+
for (const platform of ['ios', 'android']) {
|
|
138
|
+
const modules = new Map();
|
|
139
|
+
const elements = new Map();
|
|
140
|
+
for (const [index, plugin] of plugins.entries()) {
|
|
141
|
+
const entry = entries[index].path;
|
|
142
|
+
for (const { name } of plugin[platform].modules) {
|
|
143
|
+
const other = modules.get(name);
|
|
144
|
+
if (other !== undefined) {
|
|
145
|
+
throw new NativeConfigError(`Both ${other} and ${entry} register the ${PLATFORM_LABEL[platform]} module "${name}". A module name can be registered once.`);
|
|
146
|
+
}
|
|
147
|
+
modules.set(name, entry);
|
|
148
|
+
}
|
|
149
|
+
for (const { tag } of plugin[platform].elements) {
|
|
150
|
+
const other = elements.get(tag);
|
|
151
|
+
if (other !== undefined) {
|
|
152
|
+
throw new NativeConfigError(`Both ${other} and ${entry} register the ${PLATFORM_LABEL[platform]} element <${tag}>. A tag can be registered once.`);
|
|
153
|
+
}
|
|
154
|
+
elements.set(tag, entry);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return plugins;
|
|
159
|
+
};
|
|
160
|
+
/** The keys a `nativePlugins` entry takes. */
|
|
161
|
+
const ENTRY_KEYS = ['path', 'ios', 'android'];
|
|
162
|
+
const readPlugin = (entry, projectRoot) => {
|
|
163
|
+
const folder = entry.path;
|
|
164
|
+
// In messages, the entry as angular.json lists it, by its folder: an index
|
|
165
|
+
// would change as the list does.
|
|
166
|
+
const label = `nativePlugins entry "${folder}"`;
|
|
167
|
+
if (path.isAbsolute(folder)) {
|
|
168
|
+
throw new NativeConfigError(`${label} is not a folder relative to the project root, e.g. "native/greeter".`);
|
|
169
|
+
}
|
|
170
|
+
const dir = path.resolve(projectRoot, folder);
|
|
171
|
+
// The generated projects are in Angular's cache, under the project folder:
|
|
172
|
+
// a plugin around it would hash and copy them, and node_modules.
|
|
173
|
+
if (!path.relative(dir, projectRoot).startsWith('..')) {
|
|
174
|
+
throw new NativeConfigError(`${label} is the project's folder or one it is in. A native plugin is a folder of its own, e.g. "native/greeter": the project's holds its build output, which a plugin's files can't include.`);
|
|
175
|
+
}
|
|
176
|
+
if (!isFolder(dir)) {
|
|
177
|
+
throw new NativeConfigError(`${label} is not a folder. path is the plugin's folder, relative to the project root, e.g. "native/greeter".`);
|
|
178
|
+
}
|
|
179
|
+
if (existsSync(path.join(dir, LEGACY_PLUGIN_MANIFEST))) {
|
|
180
|
+
throw new NativeConfigError(`${label} has an ${LEGACY_PLUGIN_MANIFEST}, which is no longer read: a plugin is described in angular.json. Move the "ios" and "android" its "platforms" holds into this entry, beside "path", and delete the file.`);
|
|
181
|
+
}
|
|
182
|
+
for (const key of Object.keys(entry)) {
|
|
183
|
+
if (!ENTRY_KEYS.includes(key)) {
|
|
184
|
+
throw new NativeConfigError(`${label} has "${key}". An entry takes ${ENTRY_KEYS.map((name) => `"${name}"`).join(', ')}.`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
name: path.basename(dir),
|
|
189
|
+
dir,
|
|
190
|
+
ios: readPlatform('ios', entry.ios, dir, label),
|
|
191
|
+
android: readPlatform('android', entry.android, dir, label),
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
const readPlatform = (platform, value, dir, label) => {
|
|
195
|
+
if (value === undefined)
|
|
196
|
+
return { modules: [], elements: [] };
|
|
197
|
+
const field = platform;
|
|
198
|
+
if (!isObject(value)) {
|
|
199
|
+
throw new NativeConfigError(`${label}: ${field} must be an object.`);
|
|
200
|
+
}
|
|
201
|
+
for (const key of Object.keys(value)) {
|
|
202
|
+
if (!PLATFORM_KEYS[platform].includes(key)) {
|
|
203
|
+
throw new NativeConfigError(`${label}: ${field} has no "${key}". It takes ${PLATFORM_KEYS[platform].map((name) => `"${name}"`).join(', ')}.`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const sourceDir = readSourceDir(value['sourceDir'], platform, dir, label);
|
|
207
|
+
const resDir = platform === 'android'
|
|
208
|
+
? readResDir(value['resDir'], sourceDir, dir, label)
|
|
209
|
+
: undefined;
|
|
210
|
+
const packageName = value['packageName'];
|
|
211
|
+
if (packageName !== undefined &&
|
|
212
|
+
(typeof packageName !== 'string' ||
|
|
213
|
+
!JVM_PACKAGE.test(packageName) ||
|
|
214
|
+
isShellPackage(packageName))) {
|
|
215
|
+
throw new NativeConfigError(`${label}: ${field}.packageName "${String(packageName)}" is not a Java package of the plugin's own, e.g. "com.example.greeter".`);
|
|
216
|
+
}
|
|
217
|
+
const classOf = (kind, key, name) => {
|
|
218
|
+
if (platform === 'ios') {
|
|
219
|
+
if (!SWIFT_CLASS.test(name)) {
|
|
220
|
+
throw new NativeConfigError(`${label}: ${field}.${kind}.${key} "${name}" is not a Swift class name. Give the class's name alone, e.g. "GreeterModule": the sources are compiled into the app.`);
|
|
221
|
+
}
|
|
222
|
+
return name;
|
|
223
|
+
}
|
|
224
|
+
const qualified = SWIFT_CLASS.test(name) && packageName !== undefined
|
|
225
|
+
? `${packageName}.${name}`
|
|
226
|
+
: name;
|
|
227
|
+
if (!JVM_CLASS.test(qualified)) {
|
|
228
|
+
throw new NativeConfigError(`${label}: ${field}.${kind}.${key} "${name}" is not a fully qualified class name, e.g. "com.example.greeter.GreeterModule", or a class in ${field}.packageName.`);
|
|
229
|
+
}
|
|
230
|
+
if (isShellPackage(qualified.slice(0, qualified.lastIndexOf('.')))) {
|
|
231
|
+
throw new NativeConfigError(`${label}: ${field}.${kind}.${key} "${name}" is in the shell's package, ${SHELL_PACKAGE}. Put the plugin's classes in a package of its own.`);
|
|
232
|
+
}
|
|
233
|
+
return qualified;
|
|
234
|
+
};
|
|
235
|
+
const modules = readMap(value['modules'], `${field}.modules`, label).map(([name, moduleClass]) => {
|
|
236
|
+
if (!MODULE_NAME.test(name)) {
|
|
237
|
+
throw new NativeConfigError(`${label}: ${field}.modules "${name}" is not a valid module name. Use letters, digits and underscores, as a JavaScript identifier, e.g. "Greeter".`);
|
|
238
|
+
}
|
|
239
|
+
const prefix = RESERVED_MODULE_PREFIXES.find((reserved) => name.startsWith(reserved));
|
|
240
|
+
if (prefix !== undefined) {
|
|
241
|
+
throw new NativeConfigError(`${label}: ${field}.modules "${name}": ${prefix} is the prefix of the shell's and Lynx's own modules. Name the module after your app or library.`);
|
|
242
|
+
}
|
|
243
|
+
return { name, className: classOf('modules', name, moduleClass) };
|
|
244
|
+
});
|
|
245
|
+
const elements = readMap(value['elements'], `${field}.elements`, label).map(([tag, uiClass]) => {
|
|
246
|
+
if (!TAG.test(tag)) {
|
|
247
|
+
throw new NativeConfigError(`${label}: ${field}.elements "${tag}" is not a custom element's name. Use lowercase letters and digits with a hyphen, e.g. "native-switch".`);
|
|
248
|
+
}
|
|
249
|
+
const prefix = RESERVED_TAG_PREFIXES.find((reserved) => tag.startsWith(reserved));
|
|
250
|
+
if (RESERVED_TAGS.has(tag) || prefix !== undefined) {
|
|
251
|
+
throw new NativeConfigError(`${label}: ${field}.elements "${tag}" ${prefix === undefined ? 'is an element Lynx, XElement or the shell has already' : `starts with ${prefix}, which Lynx's and XElement's elements do`}. Give the element a name of its own.`);
|
|
252
|
+
}
|
|
253
|
+
return { tag, className: classOf('elements', tag, uiClass) };
|
|
254
|
+
});
|
|
255
|
+
return {
|
|
256
|
+
...(sourceDir !== undefined && { sourceDir }),
|
|
257
|
+
...(resDir !== undefined && { resDir }),
|
|
258
|
+
modules,
|
|
259
|
+
elements,
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* The platform's source folder: the one `sourceDir` names, which must be a
|
|
264
|
+
* folder inside the plugin, or else `ios` or `android` when it's there, as
|
|
265
|
+
* Autolink's default is.
|
|
266
|
+
*/
|
|
267
|
+
const readSourceDir = (sourceDir, platform, dir, label) => {
|
|
268
|
+
if (sourceDir === undefined) {
|
|
269
|
+
const fallback = path.join(dir, platform);
|
|
270
|
+
return isFolder(fallback) ? fallback : undefined;
|
|
271
|
+
}
|
|
272
|
+
return readFolder(sourceDir, `${platform}.sourceDir`, dir, label);
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* The Android resource folder: the one `resDir` names, or else `res` in the
|
|
276
|
+
* source folder when it's there, beside the Kotlin, as the iOS catalogs are
|
|
277
|
+
* beside the Swift.
|
|
278
|
+
*/
|
|
279
|
+
const readResDir = (resDir, sourceDir, dir, label) => {
|
|
280
|
+
if (resDir === undefined) {
|
|
281
|
+
const fallback = sourceDir && path.join(sourceDir, 'res');
|
|
282
|
+
return fallback && isFolder(fallback) ? fallback : undefined;
|
|
283
|
+
}
|
|
284
|
+
return readFolder(resDir, 'android.resDir', dir, label);
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* A folder an entry names, which must be inside the plugin.
|
|
288
|
+
*/
|
|
289
|
+
const readFolder = (value, field, dir, label) => {
|
|
290
|
+
const resolved = typeof value === 'string' && !path.isAbsolute(value)
|
|
291
|
+
? path.resolve(dir, value)
|
|
292
|
+
: undefined;
|
|
293
|
+
const inside = resolved && path.relative(dir, resolved);
|
|
294
|
+
if (!resolved ||
|
|
295
|
+
!inside ||
|
|
296
|
+
// By its first component, so a folder named "..shared" is inside, and
|
|
297
|
+
// absolute, as Windows gives for a folder on another drive.
|
|
298
|
+
path.isAbsolute(inside) ||
|
|
299
|
+
inside.split(path.sep)[0] === '..' ||
|
|
300
|
+
!isFolder(resolved)) {
|
|
301
|
+
throw new NativeConfigError(`${label}: ${field} "${String(value)}" is not a folder inside the plugin.`);
|
|
302
|
+
}
|
|
303
|
+
return resolved;
|
|
304
|
+
};
|
|
305
|
+
const isFolder = (file) => existsSync(file) && statSync(file).isDirectory();
|
|
306
|
+
const readMap = (value, field, label) => {
|
|
307
|
+
if (value === undefined)
|
|
308
|
+
return [];
|
|
309
|
+
if (!isObject(value) ||
|
|
310
|
+
!Object.values(value).every((entry) => typeof entry === 'string')) {
|
|
311
|
+
throw new NativeConfigError(`${label}: ${field} must map each name to a class name, e.g. { "Greeter": "GreeterModule" }.`);
|
|
312
|
+
}
|
|
313
|
+
return Object.entries(value);
|
|
314
|
+
};
|
|
315
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
316
|
+
const isShellPackage = (name) => name === SHELL_PACKAGE || name.startsWith(`${SHELL_PACKAGE}.`);
|
|
317
|
+
/**
|
|
318
|
+
* Every file and folder the plugins' generated code comes from, sorted:
|
|
319
|
+
* everything under each plugin's folder. The folders too, empty ones
|
|
320
|
+
* included, as the resource checks refuse a folder Android reads nothing
|
|
321
|
+
* from. Call it once readNativePlugins has found each folder. What each entry
|
|
322
|
+
* registers is in angular.json, which the caller hashes with these.
|
|
323
|
+
*
|
|
324
|
+
* @param entries - `nativePlugins` from the builder options.
|
|
325
|
+
* @param projectRoot - The Angular project's root folder.
|
|
326
|
+
*/
|
|
327
|
+
export const pluginInputFiles = (entries, projectRoot) => entries.flatMap((entry) => listFiles(path.resolve(projectRoot, entry.path), { folders: true }));
|
|
328
|
+
/**
|
|
329
|
+
* Copies the files under `from` to `to`, keeping their folders, but for those
|
|
330
|
+
* `skip` names. Links are copied as the files they point at, so the generated
|
|
331
|
+
* project holds no link into the app: file by file, from listFiles, as
|
|
332
|
+
* cpSync's `dereference` follows a link only at the top of the copy and
|
|
333
|
+
* copies one below it as a link.
|
|
334
|
+
*
|
|
335
|
+
* @param from - The folder to copy.
|
|
336
|
+
* @param to - Where its files go.
|
|
337
|
+
* @param skip - Whether to leave out a file, by its absolute path.
|
|
338
|
+
*/
|
|
339
|
+
const copyFiles = (from, to, skip = () => false) => {
|
|
340
|
+
for (const { relative, absolute } of listFiles(from)) {
|
|
341
|
+
if (skip(absolute))
|
|
342
|
+
continue;
|
|
343
|
+
const copy = path.join(to, relative);
|
|
344
|
+
mkdirSync(path.dirname(copy), { recursive: true });
|
|
345
|
+
copyFileSync(absolute, copy);
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
/**
|
|
349
|
+
* Copies each plugin's sources for `platform` into `<into>/<name>`, after
|
|
350
|
+
* deleting what an earlier copy left there, with copyFiles. On Android, the
|
|
351
|
+
* resource folder is left out, as copyPluginResources copies it.
|
|
352
|
+
*
|
|
353
|
+
* @param plugins - From readNativePlugins.
|
|
354
|
+
* @param platform - Which half of each plugin to copy.
|
|
355
|
+
* @param into - The folder, inside the generated project, the build compiles
|
|
356
|
+
* every source file under.
|
|
357
|
+
*/
|
|
358
|
+
export const copyPluginSources = (plugins, platform, into) => {
|
|
359
|
+
rmSync(into, { recursive: true, force: true });
|
|
360
|
+
for (const plugin of plugins) {
|
|
361
|
+
const { sourceDir } = plugin[platform];
|
|
362
|
+
if (!sourceDir)
|
|
363
|
+
continue;
|
|
364
|
+
// Android's resources go to a folder of their own (copyPluginResources):
|
|
365
|
+
// under src/main/java, Gradle reads none of them.
|
|
366
|
+
const { resDir } = plugin.android;
|
|
367
|
+
copyFiles(sourceDir, path.join(into, plugin.name), (file) => {
|
|
368
|
+
if (platform !== 'android' || !resDir)
|
|
369
|
+
return false;
|
|
370
|
+
const inside = path.relative(resDir, file);
|
|
371
|
+
return inside !== '..' && !inside.startsWith(`..${path.sep}`);
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
/**
|
|
376
|
+
* The types a Swift or Objective-C file declares at its top level: `class`,
|
|
377
|
+
* `struct`, `enum`, `protocol`, `actor` and `typealias` at the start of a
|
|
378
|
+
* line, after any attributes and modifiers, and a class's primary
|
|
379
|
+
* `@interface`. Swift compiles every file of the app into one module, so two
|
|
380
|
+
* such types of one name fail the build, unless both are file-local. An
|
|
381
|
+
* Objective-C class extension (`@interface Helper ()`) or category
|
|
382
|
+
* (`@interface Helper (Extra)`) adds to a class declared elsewhere, as a
|
|
383
|
+
* `.m` does to its header's, so it declares nothing.
|
|
384
|
+
*
|
|
385
|
+
* A name the file declares twice is one type: the file is either declaring it
|
|
386
|
+
* in each branch of an `#if`, of which only one compiles, or failing in the
|
|
387
|
+
* compiler on its own, which names the file.
|
|
388
|
+
*
|
|
389
|
+
* @param source - The file's text.
|
|
390
|
+
* @param options - Whether the language's block comments nest: Swift's do,
|
|
391
|
+
* and Objective-C's, which are C's, don't.
|
|
392
|
+
*/
|
|
393
|
+
export const swiftTypes = (source, { nested = true } = {}) => {
|
|
394
|
+
const types = new Map();
|
|
395
|
+
const add = (name, fileLocal) => types.set(name, (types.get(name) ?? true) && fileLocal);
|
|
396
|
+
const code = withoutCommentsAndStrings(source, { nested });
|
|
397
|
+
for (const [, modifiers, name] of code.matchAll(/^((?:@\w+(?:\([^)]*\))?\s+|(?:public|internal|private|fileprivate|open|final)\s+)*)(?:class|struct|enum|protocol|actor|typealias)\s+([A-Za-z_]\w*)/gm)) {
|
|
398
|
+
add(name, /\b(?:private|fileprivate)\s/.test(modifiers));
|
|
399
|
+
}
|
|
400
|
+
for (const [, name] of code.matchAll(/^@interface\s+([A-Za-z_]\w*)\b(?!\s*\()/gm)) {
|
|
401
|
+
add(name, false);
|
|
402
|
+
}
|
|
403
|
+
return [...types].map(([name, fileLocal]) => ({ name, fileLocal }));
|
|
404
|
+
};
|
|
405
|
+
/**
|
|
406
|
+
* The source without its comments and string literals, each replaced by the
|
|
407
|
+
* line breaks in it (and a block comment after a token by a space), so a declaration in an example in a comment or a string
|
|
408
|
+
* isn't read as one, and a `/*` in a line comment or a string doesn't start
|
|
409
|
+
* a block comment. A nested block comment (`nested`, as Swift's and Kotlin's
|
|
410
|
+
* are) ends at the `*\/` that matches its own `/*`; Java's and C's end at the
|
|
411
|
+
* first. A `"""` string runs to the next `"""`, as Swift's, Kotlin's and
|
|
412
|
+
* Java's multiline strings do; a string in `"` or a character in `'` ends at
|
|
413
|
+
* its unescaped quote or the end of the line, so one this misreads, such as a
|
|
414
|
+
* raw string's backslash, hides no more than its line.
|
|
415
|
+
*
|
|
416
|
+
* @param source - The file's text.
|
|
417
|
+
* @param options - Whether the language's block comments nest.
|
|
418
|
+
*/
|
|
419
|
+
const withoutCommentsAndStrings = (source, { nested }) => {
|
|
420
|
+
const lineBreaks = (text) => text.replaceAll(/[^\n]/g, '');
|
|
421
|
+
let code = '';
|
|
422
|
+
let depth = 0;
|
|
423
|
+
let index = 0;
|
|
424
|
+
while (index < source.length) {
|
|
425
|
+
if (depth > 0) {
|
|
426
|
+
if (nested && source.startsWith('/*', index)) {
|
|
427
|
+
depth += 1;
|
|
428
|
+
index += 2;
|
|
429
|
+
}
|
|
430
|
+
else if (source.startsWith('*/', index)) {
|
|
431
|
+
depth -= 1;
|
|
432
|
+
index += 2;
|
|
433
|
+
// A comment separates tokens, as whitespace does, so
|
|
434
|
+
// `package/* note */dev.angularlynx.shell` still reads as a package.
|
|
435
|
+
// One at the start of a line leaves none, as the type scan reads only
|
|
436
|
+
// declarations that start their line (top-level ones).
|
|
437
|
+
if (depth === 0 && code !== '' && !code.endsWith('\n'))
|
|
438
|
+
code += ' ';
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
if (source.charAt(index) === '\n')
|
|
442
|
+
code += '\n';
|
|
443
|
+
index += 1;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
else if (source.startsWith('/*', index)) {
|
|
447
|
+
depth = 1;
|
|
448
|
+
index += 2;
|
|
449
|
+
}
|
|
450
|
+
else if (source.startsWith('//', index)) {
|
|
451
|
+
const end = source.indexOf('\n', index);
|
|
452
|
+
index = end === -1 ? source.length : end;
|
|
453
|
+
}
|
|
454
|
+
else if (source.startsWith('"""', index)) {
|
|
455
|
+
const close = source.indexOf('"""', index + 3);
|
|
456
|
+
const end = close === -1 ? source.length : close + 3;
|
|
457
|
+
code += lineBreaks(source.slice(index, end));
|
|
458
|
+
index = end;
|
|
459
|
+
}
|
|
460
|
+
else if (source.charAt(index) === '"' || source.charAt(index) === "'") {
|
|
461
|
+
const quote = source.charAt(index);
|
|
462
|
+
index += 1;
|
|
463
|
+
while (index < source.length) {
|
|
464
|
+
const char = source.charAt(index);
|
|
465
|
+
if (char === '\n')
|
|
466
|
+
break;
|
|
467
|
+
index += char === '\\' ? 2 : 1;
|
|
468
|
+
if (char === quote)
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
code += source.charAt(index);
|
|
474
|
+
index += 1;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return code;
|
|
478
|
+
};
|
|
479
|
+
/**
|
|
480
|
+
* Checks that no plugin's Swift declares a type the iOS shell or another
|
|
481
|
+
* plugin declares: the app's sources are one Swift module, and Xcode's
|
|
482
|
+
* "invalid redeclaration" names neither the plugin nor the fix. File-local
|
|
483
|
+
* types of one name in different files pass, as they do in Swift.
|
|
484
|
+
*
|
|
485
|
+
* @param plugins - From readNativePlugins.
|
|
486
|
+
* @param appDir - The generated project's App/ folder, with the shell's
|
|
487
|
+
* sources and not yet the plugins'.
|
|
488
|
+
* @throws NativeConfigError naming the plugin's file and the type.
|
|
489
|
+
*/
|
|
490
|
+
export const checkSwiftTypes = (plugins, appDir) => {
|
|
491
|
+
const declared = new Map();
|
|
492
|
+
const declare = ({ name, fileLocal }, owner) => {
|
|
493
|
+
const other = declared.get(name);
|
|
494
|
+
if (other === undefined) {
|
|
495
|
+
declared.set(name, { owner, fileLocal });
|
|
496
|
+
return undefined;
|
|
497
|
+
}
|
|
498
|
+
return other.fileLocal && fileLocal ? undefined : other.owner;
|
|
499
|
+
};
|
|
500
|
+
for (const file of readdirSync(appDir)) {
|
|
501
|
+
if (/\.(swift|h|m)$/.test(file)) {
|
|
502
|
+
for (const type of swiftTypes(readFileSync(path.join(appDir, file), 'utf8'), { nested: file.endsWith('.swift') })) {
|
|
503
|
+
declare(type, 'the shell');
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
for (const plugin of plugins) {
|
|
508
|
+
const { sourceDir } = plugin.ios;
|
|
509
|
+
if (!sourceDir)
|
|
510
|
+
continue;
|
|
511
|
+
for (const { relative, absolute } of listFiles(sourceDir)) {
|
|
512
|
+
if (!/\.(swift|h|m)$/.test(relative))
|
|
513
|
+
continue;
|
|
514
|
+
const file = path.posix.join(plugin.name, relative);
|
|
515
|
+
for (const type of swiftTypes(readFileSync(absolute, 'utf8'), {
|
|
516
|
+
nested: relative.endsWith('.swift'),
|
|
517
|
+
})) {
|
|
518
|
+
const other = declare(type, `the plugin file ${file}`);
|
|
519
|
+
if (other !== undefined) {
|
|
520
|
+
throw new NativeConfigError(`The native plugin file ${file} declares ${type.name}, which ${other} declares too, and the app's Swift is one module. Give the plugin's types a prefix of its own, e.g. ${pascal(plugin.name)}${type.name}.`);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
/**
|
|
527
|
+
* Checks that no two Swift files of the app, the shell's and the plugins',
|
|
528
|
+
* have one name: the compiler takes a file name once in a module ("filename
|
|
529
|
+
* used twice"), wherever the files are, and, with names that differ only in
|
|
530
|
+
* capitals, writes their objects over each other on a Mac's disk. Objective-C
|
|
531
|
+
* files may share a name, as Xcode gives each object a name of its own.
|
|
532
|
+
*
|
|
533
|
+
* @param plugins - From readNativePlugins.
|
|
534
|
+
* @param appDir - The generated project's App/ folder, with the shell's
|
|
535
|
+
* sources at its top.
|
|
536
|
+
* @throws NativeConfigError naming the plugin's file and the other.
|
|
537
|
+
*/
|
|
538
|
+
export const checkSwiftFileNames = (plugins, appDir) => {
|
|
539
|
+
// Each file name, in lowercase, to the file and whether it's the shell's.
|
|
540
|
+
const named = new Map();
|
|
541
|
+
for (const file of readdirSync(appDir)) {
|
|
542
|
+
if (file.endsWith('.swift')) {
|
|
543
|
+
named.set(file.toLowerCase(), { file: `App/${file}`, shell: true });
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
for (const plugin of plugins) {
|
|
547
|
+
const { sourceDir } = plugin.ios;
|
|
548
|
+
if (!sourceDir)
|
|
549
|
+
continue;
|
|
550
|
+
for (const { relative } of listFiles(sourceDir)) {
|
|
551
|
+
if (!relative.endsWith('.swift'))
|
|
552
|
+
continue;
|
|
553
|
+
const file = path.posix.join(plugin.name, relative);
|
|
554
|
+
const base = path.posix.basename(relative);
|
|
555
|
+
const other = named.get(base.toLowerCase());
|
|
556
|
+
if (other !== undefined) {
|
|
557
|
+
const rename = `${pascal(plugin.name)}${base.charAt(0).toUpperCase()}${base.slice(1)}`;
|
|
558
|
+
throw new NativeConfigError(other.shell
|
|
559
|
+
? `The native plugin file ${file} has the name of the shell's ${other.file}, which Swift takes once in the app's module. Rename it, e.g. to ${rename}.`
|
|
560
|
+
: `The native plugin files ${other.file} and ${file} have one name (capitals aside), which Swift takes once in the app's module. Rename one, e.g. to ${rename}.`);
|
|
561
|
+
}
|
|
562
|
+
named.set(base.toLowerCase(), { file, shell: false });
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
/**
|
|
567
|
+
* Checks that no plugin's Kotlin or Java is in the Android shell's package,
|
|
568
|
+
* where a class of the same name as one of the shell's would fail the build.
|
|
569
|
+
*
|
|
570
|
+
* @param plugins - From readNativePlugins.
|
|
571
|
+
* @throws NativeConfigError naming the plugin's file.
|
|
572
|
+
*/
|
|
573
|
+
export const checkJvmPackages = (plugins) => {
|
|
574
|
+
for (const plugin of plugins) {
|
|
575
|
+
const { sourceDir } = plugin.android;
|
|
576
|
+
if (!sourceDir)
|
|
577
|
+
continue;
|
|
578
|
+
for (const { relative, absolute } of listFiles(sourceDir)) {
|
|
579
|
+
if (!/\.(kt|java)$/.test(relative))
|
|
580
|
+
continue;
|
|
581
|
+
// Kotlin's block comments nest, as Swift's do; Java's end at the
|
|
582
|
+
// first */. Space may surround the dots, as a comment the scanner
|
|
583
|
+
// reads as a space leaves there.
|
|
584
|
+
const declared = /^\s*package\s+(\w+(?:\s*\.\s*\w+)*)/m
|
|
585
|
+
.exec(withoutCommentsAndStrings(readFileSync(absolute, 'utf8'), {
|
|
586
|
+
nested: relative.endsWith('.kt'),
|
|
587
|
+
}))?.[1]
|
|
588
|
+
?.replaceAll(/\s/g, '');
|
|
589
|
+
if (declared !== undefined && isShellPackage(declared)) {
|
|
590
|
+
throw new NativeConfigError(`The native plugin file ${path.posix.join(plugin.name, relative)} is in the shell's package, ${SHELL_PACKAGE}. Put the plugin's classes in a package of its own.`);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
/**
|
|
596
|
+
* The folders Android reads resources from, before any qualifier
|
|
597
|
+
* (`values-night`, `drawable-hdpi`). A folder of any other name is skipped
|
|
598
|
+
* by Gradle without a word.
|
|
599
|
+
*/
|
|
600
|
+
const ANDROID_RESOURCE_FOLDERS = new Set([
|
|
601
|
+
'anim',
|
|
602
|
+
'animator',
|
|
603
|
+
'color',
|
|
604
|
+
'drawable',
|
|
605
|
+
'font',
|
|
606
|
+
'interpolator',
|
|
607
|
+
'layout',
|
|
608
|
+
'menu',
|
|
609
|
+
'mipmap',
|
|
610
|
+
'navigation',
|
|
611
|
+
'raw',
|
|
612
|
+
'transition',
|
|
613
|
+
'values',
|
|
614
|
+
'xml',
|
|
615
|
+
]);
|
|
616
|
+
/** The values elements whose `R` class has another name than the element's. */
|
|
617
|
+
const VALUE_TYPES = {
|
|
618
|
+
'declare-styleable': 'styleable',
|
|
619
|
+
'string-array': 'array',
|
|
620
|
+
'integer-array': 'array',
|
|
621
|
+
};
|
|
622
|
+
/**
|
|
623
|
+
* Values elements that define no resource of their own, and ids, which
|
|
624
|
+
* Android merges rather than refuses: two layouts that both say `@+id/title`
|
|
625
|
+
* share one.
|
|
626
|
+
*/
|
|
627
|
+
const NOT_DEFINED = new Set([
|
|
628
|
+
'eat-comment',
|
|
629
|
+
'skip',
|
|
630
|
+
'public',
|
|
631
|
+
'public-group',
|
|
632
|
+
'staging-public-group',
|
|
633
|
+
'overlayable',
|
|
634
|
+
'id',
|
|
635
|
+
]);
|
|
636
|
+
/**
|
|
637
|
+
* Every resource in an Android resource folder, as Gradle's merger and
|
|
638
|
+
* `R` see them: a file's by its folder's type and its name before the
|
|
639
|
+
* first dot (`drawable-hdpi/photo.9.png` is `drawable/photo`), and each
|
|
640
|
+
* value in a values file by its element. What Gradle would skip or refuse is
|
|
641
|
+
* refused here, with the file's name.
|
|
642
|
+
*
|
|
643
|
+
* @param resDir - The resource folder, laid out as `src/main/res`.
|
|
644
|
+
* @param label - The folder as messages name it.
|
|
645
|
+
* @throws NativeConfigError for a file or folder Android reads no
|
|
646
|
+
* resources from.
|
|
647
|
+
*/
|
|
648
|
+
export const androidResources = (resDir, label) => mergedEntries(resDir).flatMap((folderName) => {
|
|
649
|
+
const folder = path.join(resDir, folderName);
|
|
650
|
+
const folderLabel = path.posix.join(label, folderName);
|
|
651
|
+
if (!isFolder(folder)) {
|
|
652
|
+
throw new NativeConfigError(`${folderLabel} is not in a resource folder, such as values/ or drawable/.`);
|
|
653
|
+
}
|
|
654
|
+
const type = folderName.split('-')[0];
|
|
655
|
+
if (!ANDROID_RESOURCE_FOLDERS.has(type)) {
|
|
656
|
+
throw new NativeConfigError(`${folderLabel} is not a folder Android reads resources from, such as values/, values-night/, drawable/ or font/.`);
|
|
657
|
+
}
|
|
658
|
+
return mergedEntries(folder).flatMap((fileName) => {
|
|
659
|
+
const file = path.join(folder, fileName);
|
|
660
|
+
const fileLabel = path.posix.join(folderLabel, fileName);
|
|
661
|
+
if (isFolder(file)) {
|
|
662
|
+
throw new NativeConfigError(`${fileLabel}: Android reads no folders inside a resource folder. Put its files in ${type}/ itself.`);
|
|
663
|
+
}
|
|
664
|
+
if (type !== 'values') {
|
|
665
|
+
const name = fileName.split('.')[0];
|
|
666
|
+
// As Android's resource compiler requires, since R names the file by
|
|
667
|
+
// it: rejected here rather than by Gradle.
|
|
668
|
+
if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
|
|
669
|
+
throw new NativeConfigError(`${fileLabel}: a resource file is named in lowercase a-z, 0-9 and _, starting with a letter or _, e.g. badge.xml.`);
|
|
670
|
+
}
|
|
671
|
+
return [{ type, name, file: fileLabel }];
|
|
672
|
+
}
|
|
673
|
+
if (!fileName.endsWith('.xml')) {
|
|
674
|
+
throw new NativeConfigError(`${fileLabel}: a values folder holds XML files.`);
|
|
675
|
+
}
|
|
676
|
+
return valueResources(readFileSync(file, 'utf8'), fileLabel);
|
|
677
|
+
});
|
|
678
|
+
});
|
|
679
|
+
/**
|
|
680
|
+
* A folder's entries, sorted, without the hidden ones (`.DS_Store`), which
|
|
681
|
+
* Gradle skips too.
|
|
682
|
+
*/
|
|
683
|
+
/**
|
|
684
|
+
* A resource folder's entries that Gradle's resource merger reads: without
|
|
685
|
+
* those its default filter skips, `!.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:
|
|
686
|
+
* !CVS:!thumbs.db:!picasa.ini:!*~` (`PatternBasedFileFilter`), whatever their
|
|
687
|
+
* capitals, such as the `Thumbs.db` Windows leaves beside images.
|
|
688
|
+
*/
|
|
689
|
+
const mergedEntries = (dir) => visibleEntries(dir).filter((name) => !/\.scc$|~$|^(?:cvs|thumbs\.db|picasa\.ini)$/i.test(name) &&
|
|
690
|
+
!(name.startsWith('_') && isFolder(path.join(dir, name))));
|
|
691
|
+
const visibleEntries = (dir) => readdirSync(dir)
|
|
692
|
+
.filter((name) => !name.startsWith('.'))
|
|
693
|
+
.sort((a, b) => a.localeCompare(b));
|
|
694
|
+
/**
|
|
695
|
+
* The resources a values file defines: each element of `<resources>`, and
|
|
696
|
+
* each attribute a `<declare-styleable>` declares with its format or its
|
|
697
|
+
* `<enum>` or `<flag>` values. One with neither, `<attr name="tone"/>` or
|
|
698
|
+
* `<attr name="tone"></attr>`, is a use of an attribute defined elsewhere,
|
|
699
|
+
* as Android's resource compiler reads it. A style's items, an array's and
|
|
700
|
+
* a plural's are not resources, so only those depths count. Comments, CDATA
|
|
701
|
+
* and the XML declaration are taken out first, as they may hold anything.
|
|
702
|
+
*/
|
|
703
|
+
const valueResources = (xml, file) => {
|
|
704
|
+
const text = xml.replace(/<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>|<\?[\s\S]*?\?>/g, '');
|
|
705
|
+
const found = [];
|
|
706
|
+
const open = [];
|
|
707
|
+
// The open styleable attribute with no format, until a value of its own
|
|
708
|
+
// shows that it declares the attribute; its closing tag ends it.
|
|
709
|
+
let undeclared;
|
|
710
|
+
for (const [, closing, tag, attributes, selfClosing] of text.matchAll(/<(\/?)([A-Za-z][\w:.-]*)((?:[^>"']|"[^"]*"|'[^']*')*?)(\/?)>/g)) {
|
|
711
|
+
if (closing) {
|
|
712
|
+
open.pop();
|
|
713
|
+
undeclared = undefined;
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
const depth = open.length;
|
|
717
|
+
if (!selfClosing)
|
|
718
|
+
open.push(tag);
|
|
719
|
+
if (undeclared !== undefined && (tag === 'enum' || tag === 'flag')) {
|
|
720
|
+
found.push(undeclared);
|
|
721
|
+
undeclared = undefined;
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
// Each attribute whole, from the first, so a value that holds
|
|
725
|
+
// `name="…"` is read as the value it is.
|
|
726
|
+
const values = new Map([
|
|
727
|
+
...attributes.matchAll(/([A-Za-z_][\w:.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/g),
|
|
728
|
+
].map(([, key, double, single]) => [key, double ?? single]));
|
|
729
|
+
const attribute = (name) => values.get(name);
|
|
730
|
+
const styleableAttr = depth === 2 && open[1] === 'declare-styleable' && tag === 'attr';
|
|
731
|
+
const type = depth === 1
|
|
732
|
+
? tag === 'item'
|
|
733
|
+
? attribute('type')
|
|
734
|
+
: (VALUE_TYPES[tag] ?? tag)
|
|
735
|
+
: styleableAttr
|
|
736
|
+
? 'attr'
|
|
737
|
+
: undefined;
|
|
738
|
+
const name = attribute('name');
|
|
739
|
+
// A name with a package (`android:text`) is that package's resource.
|
|
740
|
+
if (type === undefined ||
|
|
741
|
+
NOT_DEFINED.has(type) ||
|
|
742
|
+
name === undefined ||
|
|
743
|
+
name.includes(':')) {
|
|
744
|
+
continue;
|
|
745
|
+
}
|
|
746
|
+
const resource = { type, name: name.replace(/[.-]/g, '_'), file };
|
|
747
|
+
if (!styleableAttr || attribute('format') !== undefined) {
|
|
748
|
+
found.push(resource);
|
|
749
|
+
}
|
|
750
|
+
else if (!selfClosing) {
|
|
751
|
+
undeclared = resource;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return found;
|
|
755
|
+
};
|
|
756
|
+
/**
|
|
757
|
+
* Checks that no plugin's Android resources define one the shell or another
|
|
758
|
+
* plugin defines. Gradle merges every resource folder of the app into one
|
|
759
|
+
* `R`, so the same name would fail the build as a duplicate, or, across
|
|
760
|
+
* qualifiers (`values` and `values-night`), quietly give the app one
|
|
761
|
+
* plugin's resource in the other's place. A plugin may define a resource
|
|
762
|
+
* again under its own qualifiers, which is how it has a night color.
|
|
763
|
+
*
|
|
764
|
+
* @param plugins - From readNativePlugins.
|
|
765
|
+
* @param appDir - The generated project's `app/` folder, with the shell's
|
|
766
|
+
* resources (`src/main/res`, `src/debug/res`) and the `resValue`s in its
|
|
767
|
+
* `build.gradle.kts`.
|
|
768
|
+
* @throws NativeConfigError naming the plugin's file and the resource.
|
|
769
|
+
*/
|
|
770
|
+
export const checkAndroidResources = (plugins, appDir) => {
|
|
771
|
+
const defined = new Map();
|
|
772
|
+
const key = ({ type, name }) => `${type}/${name}`;
|
|
773
|
+
for (const set of ['main', 'debug', 'release']) {
|
|
774
|
+
const res = path.join(appDir, 'src', set, 'res');
|
|
775
|
+
if (!isFolder(res))
|
|
776
|
+
continue;
|
|
777
|
+
for (const resource of androidResources(res, `src/${set}/res`)) {
|
|
778
|
+
defined.set(key(resource), {
|
|
779
|
+
owner: '',
|
|
780
|
+
by: `the shell defines too (${resource.file})`,
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
const gradle = path.join(appDir, 'build.gradle.kts');
|
|
785
|
+
if (existsSync(gradle)) {
|
|
786
|
+
for (const [, type, name] of readFileSync(gradle, 'utf8').matchAll(/resValue\(\s*"(\w+)",\s*"(\w+)"/g)) {
|
|
787
|
+
defined.set(`${type}/${name}`, {
|
|
788
|
+
owner: '',
|
|
789
|
+
by: 'the shell defines too (build.gradle.kts)',
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
for (const plugin of plugins) {
|
|
794
|
+
const { resDir } = plugin.android;
|
|
795
|
+
if (!resDir)
|
|
796
|
+
continue;
|
|
797
|
+
const label = path.posix.join(plugin.name, path.relative(plugin.dir, resDir).split(path.sep).join('/'));
|
|
798
|
+
for (const resource of androidResources(resDir, label)) {
|
|
799
|
+
const other = defined.get(key(resource));
|
|
800
|
+
if (other === undefined) {
|
|
801
|
+
defined.set(key(resource), {
|
|
802
|
+
owner: plugin.name,
|
|
803
|
+
by: `the plugin file ${resource.file} defines too`,
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
else if (other.owner !== plugin.name) {
|
|
807
|
+
throw new NativeConfigError(`The native plugin file ${resource.file} defines the ${resource.type} ${resource.name}, which ${other.by}, and an app has one R. Give the plugin's resources a prefix of its own, e.g. ${plugin.name.replace(/[^A-Za-z0-9]+/g, '_')}_${resource.name}.`);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
/**
|
|
813
|
+
* Copies each plugin's Android resource folder to `<into>/<name>`, after
|
|
814
|
+
* deleting what an earlier copy left there. Each is a resource folder of the
|
|
815
|
+
* app module of its own (the template's `build.gradle.kts` adds every folder
|
|
816
|
+
* in `into`), so a plugin's `values/colors.xml` sits beside the shell's file
|
|
817
|
+
* of that name rather than over it. Links are copied as the files they
|
|
818
|
+
* point at, with copyFiles.
|
|
819
|
+
*
|
|
820
|
+
* @param plugins - From readNativePlugins and checkAndroidResources.
|
|
821
|
+
* @param into - The folder, inside the generated project, Gradle reads the
|
|
822
|
+
* plugins' resource folders from.
|
|
823
|
+
*/
|
|
824
|
+
export const copyPluginResources = (plugins, into) => {
|
|
825
|
+
rmSync(into, { recursive: true, force: true });
|
|
826
|
+
for (const plugin of plugins) {
|
|
827
|
+
const { resDir } = plugin.android;
|
|
828
|
+
if (resDir)
|
|
829
|
+
copyFiles(resDir, path.join(into, plugin.name));
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
/** The source files Xcode compiles rather than copies into the app. */
|
|
833
|
+
const IOS_SOURCE = /\.(swift|m|mm|c|cc|cpp|cxx|h|hh|hpp|metal)$/;
|
|
834
|
+
/**
|
|
835
|
+
* Checks that no plugin's iOS resources take a name the shell's or another
|
|
836
|
+
* plugin's have. App/ is a synchronized folder, so Xcode builds whatever the
|
|
837
|
+
* plugins' folders hold: their asset catalogs (`.xcassets`) are compiled
|
|
838
|
+
* with the shell's into the app's one `Assets.car`, where two assets of a
|
|
839
|
+
* name leave one, with only a warning; and every other file that isn't a
|
|
840
|
+
* source (a font, a JSON file, a `.lproj` folder's strings) is copied to the
|
|
841
|
+
* top of the app, out of its folders, where two files of a name, or one
|
|
842
|
+
* of the name of a file Xcode writes there (`Assets.car`, `PkgInfo`), fail
|
|
843
|
+
* the build with "Multiple commands produce".
|
|
844
|
+
*
|
|
845
|
+
* @param plugins - From readNativePlugins.
|
|
846
|
+
* @param appDir - The generated project's App/ folder. Only its own catalogs
|
|
847
|
+
* and files are the shell's: a copy of the plugins may be in App/Plugins.
|
|
848
|
+
* @throws NativeConfigError naming the plugin's file and the name.
|
|
849
|
+
*/
|
|
850
|
+
export const checkIosResources = (plugins, appDir) => {
|
|
851
|
+
const assets = new Map();
|
|
852
|
+
// Each name at the top of the app, in lowercase as a Mac's case-insensitive
|
|
853
|
+
// volume compares them, to what has it, and for a `.bundle` the folder it's
|
|
854
|
+
// copied from: a bundle's files share its name.
|
|
855
|
+
const files = new Map();
|
|
856
|
+
// Each strings table by name, in lowercase, to whether it's a string
|
|
857
|
+
// catalog (`.xcstrings`) and what has it: Xcode refuses a catalog beside
|
|
858
|
+
// `.strings` or `.stringsdict` tables of the same name, in any language.
|
|
859
|
+
const tables = new Map();
|
|
860
|
+
// What Xcode writes at the top of every app, beside the copied files:
|
|
861
|
+
// the catalogs compiled into one, and the bundle's type and creator.
|
|
862
|
+
files.set('assets.car', { by: "the app's compiled asset catalog" });
|
|
863
|
+
files.set('pkginfo', { by: 'the PkgInfo Xcode writes for the app' });
|
|
864
|
+
for (const name of visibleEntries(appDir)) {
|
|
865
|
+
const file = path.join(appDir, name);
|
|
866
|
+
if (name.endsWith('.xcassets')) {
|
|
867
|
+
for (const asset of catalogAssets(file, name)) {
|
|
868
|
+
assets.set(asset.name, {
|
|
869
|
+
owner: '',
|
|
870
|
+
by: `the shell names too (${asset.file})`,
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
else if (name === 'Info.plist') {
|
|
875
|
+
// The target leaves it out of its resources (membershipExceptions),
|
|
876
|
+
// and processes it into the app's own Info.plist, which a plugin's
|
|
877
|
+
// file of the name would be copied over: "Multiple commands produce".
|
|
878
|
+
files.set(name.toLowerCase(), { by: "the shell's App/Info.plist" });
|
|
879
|
+
}
|
|
880
|
+
else if (!isFolder(file) && !IOS_SOURCE.test(name)) {
|
|
881
|
+
files.set(name.toLowerCase(), { by: `the shell file ${name}` });
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
for (const plugin of plugins) {
|
|
885
|
+
const { sourceDir } = plugin.ios;
|
|
886
|
+
if (!sourceDir)
|
|
887
|
+
continue;
|
|
888
|
+
const label = path.posix.join(plugin.name, path.relative(plugin.dir, sourceDir).split(path.sep).join('/'));
|
|
889
|
+
const catalogs = new Set();
|
|
890
|
+
for (const { relative } of listFiles(sourceDir)) {
|
|
891
|
+
const parts = relative.split('/');
|
|
892
|
+
const bundle = parts.findIndex((part) => part.endsWith('.bundle'));
|
|
893
|
+
const catalog = parts.findIndex((part) => part.endsWith('.xcassets'));
|
|
894
|
+
// A catalog inside a `.bundle` is part of the bundle, which Xcode
|
|
895
|
+
// copies whole without compiling it, so its assets aren't the app's:
|
|
896
|
+
// it counts only as the bundle's name at the top of the app.
|
|
897
|
+
if (catalog !== -1 && (bundle === -1 || catalog < bundle)) {
|
|
898
|
+
catalogs.add(parts.slice(0, catalog + 1).join('/'));
|
|
899
|
+
continue;
|
|
900
|
+
}
|
|
901
|
+
const name = bundledName(parts);
|
|
902
|
+
if (name === undefined)
|
|
903
|
+
continue;
|
|
904
|
+
const file = path.posix.join(label, relative);
|
|
905
|
+
const root = bundle === -1
|
|
906
|
+
? undefined
|
|
907
|
+
: path.posix.join(plugin.name, ...parts.slice(0, bundle + 1));
|
|
908
|
+
const table = /^(.+)\.(xcstrings|strings|stringsdict)$/.exec(parts.at(-1));
|
|
909
|
+
if (bundle === -1 && table !== null) {
|
|
910
|
+
const catalog = table[2] === 'xcstrings';
|
|
911
|
+
const named = tables.get(table[1].toLowerCase());
|
|
912
|
+
if (named === undefined) {
|
|
913
|
+
tables.set(table[1].toLowerCase(), {
|
|
914
|
+
catalog,
|
|
915
|
+
by: `the plugin file ${file}`,
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
else if (named.catalog !== catalog) {
|
|
919
|
+
throw new NativeConfigError(`The native plugin file ${file} and ${named.by} are a string catalog and a strings table both named ${table[1]}, which Xcode refuses in one app: give the plugin's table a name of its own.`);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
const other = files.get(name.toLowerCase());
|
|
923
|
+
if (other === undefined) {
|
|
924
|
+
files.set(name.toLowerCase(), {
|
|
925
|
+
by: `the plugin file ${file}`,
|
|
926
|
+
...(root !== undefined && { root }),
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
else if (root === undefined || other.root !== root) {
|
|
930
|
+
throw new NativeConfigError(`The native plugin file ${file} is copied into the app as ${name}, as ${other.by} is. Xcode copies a plugin's resources to the top of the app, out of their folders: give the file a name of its own.`);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
for (const catalog of catalogs) {
|
|
934
|
+
for (const asset of catalogAssets(path.join(sourceDir, catalog), path.posix.join(label, catalog))) {
|
|
935
|
+
const other = assets.get(asset.name);
|
|
936
|
+
if (other !== undefined) {
|
|
937
|
+
throw new NativeConfigError(`The native plugin file ${asset.file} names the asset ${asset.name}, which ${other.by}. The app compiles every catalog into one, where Xcode keeps one of the two and only warns: give the plugin's assets a prefix of its own, e.g. ${pascal(plugin.name)}${asset.name}.`);
|
|
938
|
+
}
|
|
939
|
+
assets.set(asset.name, {
|
|
940
|
+
owner: plugin.name,
|
|
941
|
+
by: `the plugin file ${asset.file} names too`,
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
/**
|
|
948
|
+
* What a file in a plugin's iOS folder is called at the top of the app, or
|
|
949
|
+
* undefined for a source file or a hidden one: its name alone, a
|
|
950
|
+
* `.lproj` folder's file inside that folder, the whole of a `.bundle`, and
|
|
951
|
+
* an Interface Builder file under its compiled name.
|
|
952
|
+
*/
|
|
953
|
+
const bundledName = (parts) => {
|
|
954
|
+
const name = parts.at(-1);
|
|
955
|
+
if (parts.some((part) => part.startsWith('.')) || IOS_SOURCE.test(name)) {
|
|
956
|
+
return undefined;
|
|
957
|
+
}
|
|
958
|
+
const bundle = parts.find((part) => part.endsWith('.bundle'));
|
|
959
|
+
if (bundle !== undefined)
|
|
960
|
+
return bundle;
|
|
961
|
+
const compiled = name
|
|
962
|
+
.replace(/\.storyboard$/, '.storyboardc')
|
|
963
|
+
.replace(/\.xib$/, '.nib');
|
|
964
|
+
const localized = parts.findLast((part) => part.endsWith('.lproj'));
|
|
965
|
+
return localized === undefined ? compiled : `${localized}/${compiled}`;
|
|
966
|
+
};
|
|
967
|
+
/**
|
|
968
|
+
* The assets an asset catalog holds, by the name an app loads them with
|
|
969
|
+
* (`UIColor(named:)`, `UIImage(named:)`): each folder with an asset type's
|
|
970
|
+
* extension (`.colorset`, `.imageset`, `.dataset`, …), and a sprite atlas's
|
|
971
|
+
* image sets, after the names of the folders above it that provide a
|
|
972
|
+
* namespace.
|
|
973
|
+
*/
|
|
974
|
+
const catalogAssets = (dir, label, namespace = '') => visibleEntries(dir).flatMap((entry) => {
|
|
975
|
+
const folder = path.join(dir, entry);
|
|
976
|
+
if (!isFolder(folder))
|
|
977
|
+
return [];
|
|
978
|
+
const file = path.posix.join(label, entry);
|
|
979
|
+
const extension = path.extname(entry);
|
|
980
|
+
if (extension === '.spriteatlas') {
|
|
981
|
+
// An atlas is an asset of its own, and holds image sets an app loads
|
|
982
|
+
// by name, after the atlas's if it provides a namespace.
|
|
983
|
+
const name = entry.slice(0, -extension.length);
|
|
984
|
+
return [
|
|
985
|
+
{ name: namespace + name, file },
|
|
986
|
+
...catalogAssets(folder, file, providesNamespace(folder) ? `${namespace}${name}/` : namespace),
|
|
987
|
+
];
|
|
988
|
+
}
|
|
989
|
+
if (extension !== '') {
|
|
990
|
+
return [{ name: namespace + entry.slice(0, -extension.length), file }];
|
|
991
|
+
}
|
|
992
|
+
return catalogAssets(folder, file, providesNamespace(folder) ? `${namespace}${entry}/` : namespace);
|
|
993
|
+
});
|
|
994
|
+
/**
|
|
995
|
+
* Whether a catalog's folder puts its name before its assets'.
|
|
996
|
+
*/
|
|
997
|
+
const providesNamespace = (folder) => {
|
|
998
|
+
const contents = path.join(folder, 'Contents.json');
|
|
999
|
+
if (!existsSync(contents))
|
|
1000
|
+
return false;
|
|
1001
|
+
try {
|
|
1002
|
+
const json = JSON.parse(readFileSync(contents, 'utf8'));
|
|
1003
|
+
return (isObject(json) &&
|
|
1004
|
+
isObject(json['properties']) &&
|
|
1005
|
+
json['properties']['provides-namespace'] === true);
|
|
1006
|
+
}
|
|
1007
|
+
catch {
|
|
1008
|
+
// actool reports a broken Contents.json itself, with the folder's name.
|
|
1009
|
+
return false;
|
|
1010
|
+
}
|
|
1011
|
+
};
|
|
1012
|
+
/**
|
|
1013
|
+
* `greeter-kit` → `GreeterKit`, for a suggested type prefix.
|
|
1014
|
+
*/
|
|
1015
|
+
const pascal = (name) => name
|
|
1016
|
+
.split(/[^A-Za-z0-9]+/)
|
|
1017
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
1018
|
+
.join('');
|
|
1019
|
+
/**
|
|
1020
|
+
* The comment the registration files start with. In a generated project they
|
|
1021
|
+
* are written again on every build; eject writes them once, after which
|
|
1022
|
+
* they're the user's, as the rest of the ejected project is.
|
|
1023
|
+
*/
|
|
1024
|
+
const generatedHeader = (comment) => [
|
|
1025
|
+
"Written by @blotch/angular-lynx-native from the plugins angular.json's",
|
|
1026
|
+
'`nativePlugins` lists: a generated project gets a new copy on every',
|
|
1027
|
+
'build, so change the plugins rather than this file. In an ejected',
|
|
1028
|
+
'project it is yours, and `nativePlugins` no longer applies.',
|
|
1029
|
+
].map((line) => `${comment} ${line}`);
|
|
1030
|
+
/**
|
|
1031
|
+
* App/AngularLynxPlugins.swift: registers the plugins' modules and elements
|
|
1032
|
+
* with the LynxView's config, which LynxViewController calls before it
|
|
1033
|
+
* builds the view, after the shell's own. Per view rather than in
|
|
1034
|
+
* LynxComponentRegistry, as Android's are with the LynxViewBuilder.
|
|
1035
|
+
*
|
|
1036
|
+
* A module's param is an `AngularLynxPluginHost`, which holds the view
|
|
1037
|
+
* controller weakly: Lynx keeps a module's param for as long as the config
|
|
1038
|
+
* (`LynxModuleWrapper.param` is strong), and the controller keeps the config
|
|
1039
|
+
* through its LynxView, so the controller itself would never be freed.
|
|
1040
|
+
*
|
|
1041
|
+
* @param plugins - From readNativePlugins.
|
|
1042
|
+
*/
|
|
1043
|
+
export const iosPluginsSwift = (plugins) => {
|
|
1044
|
+
const lines = plugins.flatMap(({ ios }) => [
|
|
1045
|
+
...ios.modules.map(({ name, className }) => ` config.register(${swiftName(className)}.self, withName: ${JSON.stringify(name)}, param: pluginHost)`),
|
|
1046
|
+
...ios.elements.map(({ tag, className }) => ` config.registerUI(${swiftName(className)}.self, withName: ${JSON.stringify(tag)})`),
|
|
1047
|
+
]);
|
|
1048
|
+
const hasModules = plugins.some(({ ios }) => ios.modules.length > 0);
|
|
1049
|
+
const signature = ' static func register(in config: LynxConfig, host: UIViewController)';
|
|
1050
|
+
return [
|
|
1051
|
+
...generatedHeader('//'),
|
|
1052
|
+
'import Lynx',
|
|
1053
|
+
'import UIKit',
|
|
1054
|
+
'',
|
|
1055
|
+
"/// The app's native plugins' modules and elements.",
|
|
1056
|
+
'enum AngularLynxPlugins {',
|
|
1057
|
+
...(lines.length === 0
|
|
1058
|
+
? [`${signature} {}`]
|
|
1059
|
+
: [
|
|
1060
|
+
`${signature} {`,
|
|
1061
|
+
...(hasModules
|
|
1062
|
+
? [' let pluginHost = AngularLynxPluginHost(host)']
|
|
1063
|
+
: []),
|
|
1064
|
+
...lines,
|
|
1065
|
+
' }',
|
|
1066
|
+
]),
|
|
1067
|
+
'}',
|
|
1068
|
+
'',
|
|
1069
|
+
"/// A plugin module's param: the view controller showing the app, for a",
|
|
1070
|
+
'/// module that presents a screen of its own. Weak, as Lynx keeps the',
|
|
1071
|
+
'/// param as long as the config, which the controller keeps.',
|
|
1072
|
+
'final class AngularLynxPluginHost: NSObject {',
|
|
1073
|
+
' weak var viewController: UIViewController?',
|
|
1074
|
+
'',
|
|
1075
|
+
' init(_ viewController: UIViewController) {',
|
|
1076
|
+
' self.viewController = viewController',
|
|
1077
|
+
' }',
|
|
1078
|
+
'}',
|
|
1079
|
+
'',
|
|
1080
|
+
].join('\n');
|
|
1081
|
+
};
|
|
1082
|
+
/**
|
|
1083
|
+
* AngularLynxPlugins.kt: registers the plugins' modules and elements with
|
|
1084
|
+
* the LynxViewBuilder MainActivity builds its LynxView with, after the
|
|
1085
|
+
* shell's own. A module gets the activity as its param, as the shell's own
|
|
1086
|
+
* modules that need one do.
|
|
1087
|
+
*
|
|
1088
|
+
* @param plugins - From readNativePlugins.
|
|
1089
|
+
*/
|
|
1090
|
+
export const androidPluginsKotlin = (plugins) => {
|
|
1091
|
+
const modules = plugins.flatMap(({ android }) => android.modules.map(({ name, className }) => ` builder.registerModule(${kotlinString(name)}, ${kotlinName(className)}::class.java, activity)`));
|
|
1092
|
+
const elements = plugins.flatMap(({ android }) => android.elements.map(({ tag, className }) => [
|
|
1093
|
+
` builder.addBehavior(object : Behavior(${kotlinString(tag)}) {`,
|
|
1094
|
+
' override fun createUI(context: LynxContext): LynxUI<*> =',
|
|
1095
|
+
` ${kotlinName(className)}(context)`,
|
|
1096
|
+
' })',
|
|
1097
|
+
].join('\n')));
|
|
1098
|
+
const signature = ' fun register(builder: LynxViewBuilder, activity: ComponentActivity)';
|
|
1099
|
+
const body = [...modules, ...elements];
|
|
1100
|
+
return [
|
|
1101
|
+
...generatedHeader('//'),
|
|
1102
|
+
'package dev.angularlynx.shell',
|
|
1103
|
+
'',
|
|
1104
|
+
'import androidx.activity.ComponentActivity',
|
|
1105
|
+
'import com.lynx.tasm.LynxViewBuilder',
|
|
1106
|
+
// Only with an element, so a project without one has no unused imports.
|
|
1107
|
+
...(elements.length === 0
|
|
1108
|
+
? []
|
|
1109
|
+
: [
|
|
1110
|
+
'import com.lynx.tasm.behavior.Behavior',
|
|
1111
|
+
'import com.lynx.tasm.behavior.LynxContext',
|
|
1112
|
+
'import com.lynx.tasm.behavior.ui.LynxUI',
|
|
1113
|
+
]),
|
|
1114
|
+
'',
|
|
1115
|
+
"/** The app's native plugins' modules and elements. */",
|
|
1116
|
+
'internal object AngularLynxPlugins {',
|
|
1117
|
+
...(body.length === 0
|
|
1118
|
+
? [`${signature} {}`]
|
|
1119
|
+
: [`${signature} {`, ...body, ' }']),
|
|
1120
|
+
'}',
|
|
1121
|
+
'',
|
|
1122
|
+
].join('\n');
|
|
1123
|
+
};
|
|
1124
|
+
/**
|
|
1125
|
+
* Swift's keywords that can't be an identifier unless it's in backticks: the
|
|
1126
|
+
* ones the language reference lists for declarations, statements, and
|
|
1127
|
+
* expressions and types
|
|
1128
|
+
* (https://docs.swift.org/swift-book/documentation/the-swift-programming-language/lexicalstructure/#Keywords-and-Punctuation).
|
|
1129
|
+
*/
|
|
1130
|
+
const SWIFT_KEYWORDS = new Set([
|
|
1131
|
+
'associatedtype',
|
|
1132
|
+
'borrowing',
|
|
1133
|
+
'class',
|
|
1134
|
+
'consuming',
|
|
1135
|
+
'deinit',
|
|
1136
|
+
'enum',
|
|
1137
|
+
'extension',
|
|
1138
|
+
'fileprivate',
|
|
1139
|
+
'func',
|
|
1140
|
+
'import',
|
|
1141
|
+
'init',
|
|
1142
|
+
'inout',
|
|
1143
|
+
'internal',
|
|
1144
|
+
'let',
|
|
1145
|
+
'nonisolated',
|
|
1146
|
+
'open',
|
|
1147
|
+
'operator',
|
|
1148
|
+
'precedencegroup',
|
|
1149
|
+
'private',
|
|
1150
|
+
'protocol',
|
|
1151
|
+
'public',
|
|
1152
|
+
'rethrows',
|
|
1153
|
+
'static',
|
|
1154
|
+
'struct',
|
|
1155
|
+
'subscript',
|
|
1156
|
+
'typealias',
|
|
1157
|
+
'var',
|
|
1158
|
+
'break',
|
|
1159
|
+
'case',
|
|
1160
|
+
'catch',
|
|
1161
|
+
'continue',
|
|
1162
|
+
'default',
|
|
1163
|
+
'defer',
|
|
1164
|
+
'do',
|
|
1165
|
+
'else',
|
|
1166
|
+
'fallthrough',
|
|
1167
|
+
'for',
|
|
1168
|
+
'guard',
|
|
1169
|
+
'if',
|
|
1170
|
+
'in',
|
|
1171
|
+
'repeat',
|
|
1172
|
+
'return',
|
|
1173
|
+
'throw',
|
|
1174
|
+
'switch',
|
|
1175
|
+
'where',
|
|
1176
|
+
'while',
|
|
1177
|
+
'Any',
|
|
1178
|
+
'as',
|
|
1179
|
+
'await',
|
|
1180
|
+
'false',
|
|
1181
|
+
'is',
|
|
1182
|
+
'nil',
|
|
1183
|
+
'self',
|
|
1184
|
+
'Self',
|
|
1185
|
+
'super',
|
|
1186
|
+
'throws',
|
|
1187
|
+
'true',
|
|
1188
|
+
'try',
|
|
1189
|
+
]);
|
|
1190
|
+
/**
|
|
1191
|
+
* A Swift class name as source spells it, in backticks when it's a keyword,
|
|
1192
|
+
* as its declaration must be: `switch` → `` `switch` ``.
|
|
1193
|
+
*/
|
|
1194
|
+
const swiftName = (name) => SWIFT_KEYWORDS.has(name) ? `\`${name}\`` : name;
|
|
1195
|
+
/**
|
|
1196
|
+
* Kotlin's hard keywords, which can't be an identifier, even a part of a
|
|
1197
|
+
* qualified name, unless it's in backticks
|
|
1198
|
+
* (https://kotlinlang.org/docs/keyword-reference.html). A Java package can
|
|
1199
|
+
* have the ones Java doesn't reserve, such as `in`, `is`, `fun`, `object` and
|
|
1200
|
+
* `when`.
|
|
1201
|
+
*/
|
|
1202
|
+
const KOTLIN_HARD_KEYWORDS = new Set([
|
|
1203
|
+
'as',
|
|
1204
|
+
'break',
|
|
1205
|
+
'class',
|
|
1206
|
+
'continue',
|
|
1207
|
+
'do',
|
|
1208
|
+
'else',
|
|
1209
|
+
'false',
|
|
1210
|
+
'for',
|
|
1211
|
+
'fun',
|
|
1212
|
+
'if',
|
|
1213
|
+
'in',
|
|
1214
|
+
'interface',
|
|
1215
|
+
'is',
|
|
1216
|
+
'null',
|
|
1217
|
+
'object',
|
|
1218
|
+
'package',
|
|
1219
|
+
'return',
|
|
1220
|
+
'super',
|
|
1221
|
+
'this',
|
|
1222
|
+
'throw',
|
|
1223
|
+
'true',
|
|
1224
|
+
'try',
|
|
1225
|
+
'typealias',
|
|
1226
|
+
'typeof',
|
|
1227
|
+
'val',
|
|
1228
|
+
'var',
|
|
1229
|
+
'when',
|
|
1230
|
+
'while',
|
|
1231
|
+
]);
|
|
1232
|
+
/**
|
|
1233
|
+
* A qualified JVM class name as Kotlin source spells it, with the parts that
|
|
1234
|
+
* are hard keywords in backticks: `com.example.in.Greeter` →
|
|
1235
|
+
* ``com.example.`in`.Greeter``.
|
|
1236
|
+
*/
|
|
1237
|
+
const kotlinName = (qualified) => qualified
|
|
1238
|
+
.split('.')
|
|
1239
|
+
.map((part) => (KOTLIN_HARD_KEYWORDS.has(part) ? `\`${part}\`` : part))
|
|
1240
|
+
.join('.');
|
|
1241
|
+
/**
|
|
1242
|
+
* A Kotlin string literal, without templates.
|
|
1243
|
+
*/
|
|
1244
|
+
const kotlinString = (text) => JSON.stringify(text).replace(/\$/g, '\\$');
|