Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support project-level operations #122

Merged
merged 4 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"project_info": {
"project_id": "1234",
"project_number": "1234",
"name": "1234",
"firebase_url": ""
},
"client": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<resources>
<string name="app_name">capacitor-configure-test</string>
<string name="title_activity_main">capacitor-configure-test</string>
<string name="package_name">io.ionic.starter</string>
<string name="custom_url_scheme">io.ionic.starter</string>
</resources>
44 changes: 44 additions & 0 deletions packages/common/test/fixtures/ios-and-android/project-xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8" ?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="io.ionic.starter"
>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}"
/>

<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:name="io.ionic.starter.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBarLaunch"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
/>
</provider>
</application>
</manifest>
12 changes: 12 additions & 0 deletions packages/common/test/fixtures/project.basic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
project:
xml:
- file: project-xml-strings.xml
target: resources/string[@name="app_name"]
replace: |
<string name="app_name">Awesome App</string>

json:
- file: project-json.json
set:
project_info:
project_id: "asdf"
2 changes: 1 addition & 1 deletion packages/configure/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ export type IosPlistOperationValue = {
iosTarget?: string;
iosBuild?: string;
entries: any[];
};
};
28 changes: 24 additions & 4 deletions packages/configure/src/op.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Operation } from './definitions';
import { pluralize } from './util/plural';

// Given the parsed yaml file, generate a set of operations to perform against the project
export function processOperations(yaml: any): Operation[] {
return Object.keys(yaml.platforms || {})
.map(p => createPlatform(p, yaml.platforms[p]))
.flat()
.concat(createPlatform('project', yaml.project))
.flat();
}

Expand All @@ -12,6 +15,8 @@ function createPlatform(platform: string, platformEntry: any) {
return createAndroidPlatform(platform, platformEntry);
} else if (platform === 'ios') {
return createIosPlatform(platform, platformEntry);
} else if (platform === 'project') {
return createProjectPlatform(platform, platformEntry);
}
return [];
}
Expand All @@ -26,6 +31,16 @@ function createAndroidPlatform(platform: string, platformEntry: any) {
.flat();
}

function createProjectPlatform(platform: string, platformEntry: any) {
if (!platformEntry) {
return [];
}

return Object.keys(platformEntry || {})
.map(op => createOperation(platform, op, platformEntry[op]))
.flat();
}

function createIosPlatform(platform: string, platformEntry: any) {
if (!platformEntry) {
return [];
Expand Down Expand Up @@ -172,6 +187,11 @@ function getOpIdAlias(op: Partial<Operation>) {
// TODO: Move this to per-operation for more powerful display
function createOpDisplayText(op: Partial<Operation>) {
switch (op.id) {
// project
case 'project.xml':
return `${op.value.length} ${pluralize(op.value.length, 'modification')}`;
case 'project.json':
return `${op.value.length} ${pluralize(op.value.length, 'modification')}`;
// ios
case 'ios.bundleId':
return op.value;
Expand All @@ -194,9 +214,9 @@ function createOpDisplayText(op: Partial<Operation>) {
case 'ios.plist':
return `${op.value.entries.length} modifications`;
case 'ios.xml':
return `${op.value.entries.length} modifications`;
return `${op.value.length} ${pluralize(op.value.length, 'modification')}`;
case 'ios.json':
return `${op.value.entries.length} modifications`;
return `${op.value.length} ${pluralize(op.value.length, 'modification')}`;
case 'ios.copy':
return op.value.map((r: any) => r.dest).join(', ');
// android
Expand All @@ -209,9 +229,9 @@ function createOpDisplayText(op: Partial<Operation>) {
case 'android.manifest':
return `${op.value.length} modifications`;
case 'android.json':
return `${op.value.entries.length} modifications`;
return `${op.value.length} ${pluralize(op.value.length, 'modification')}`;
case 'android.xml':
return `${op.value.entries.length} modifications`;
return `${op.value.length} ${pluralize(op.value.length, 'modification')}`;
case 'android.build.gradle':
return '';
case 'android.app.build.gradle':
Expand Down
5 changes: 5 additions & 0 deletions packages/configure/src/operations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import executeIosXml from './ios/xml';
import executeIosJson from './ios/json';
import executeIosCopy from './ios/copy';

import executeProjectJson from './project/json';
import executeProjectXml from './project/xml';

import { Context } from '../ctx';
import { Operation } from '../definitions';

Expand All @@ -27,6 +30,8 @@ interface OperationHandlers {
}

const operations: OperationHandlers = {
'project.json': executeProjectJson,
'project.xml': executeProjectXml,
'ios.plist': executeIosPlist,
'ios.bundleId': executeIosProject,
'ios.displayName': executeIosProject,
Expand Down
37 changes: 37 additions & 0 deletions packages/configure/src/operations/project/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { join } from 'path';

import { JsonFile, VFS } from '@trapezedev/project';
import { Context } from '../../ctx';
import { JsonOperation, Operation } from '../../definitions';

function getJsonFile(path: string, vfs: VFS) {
const existing = vfs.get(path);

if (existing) {
return existing.getData() as JsonFile;
}
return new JsonFile(path, vfs);
}

export default async function execute(ctx: Context, op: Operation) {
const jsonOp = op as JsonOperation;
const entries = jsonOp.value;

for (const entry of entries) {
let filename = entry.file;
if (!filename) {
continue;
}

let jsonFile: JsonFile = getJsonFile(join(ctx.project.projectRoot, filename), ctx.project.vfs);

await jsonFile.load();

if (entry.set) {
jsonFile.set(entry.set);
}
if (entry.merge) {
jsonFile.merge(entry.merge);
}
}
}
48 changes: 48 additions & 0 deletions packages/configure/src/operations/project/xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import { VFS, XmlFile } from '@trapezedev/project';
import { join } from 'path';

import { Context } from "../../ctx";
import { XmlOperation, Operation } from "../../definitions";

function getXmlFile(path: string, vfs: VFS) {
const existing = vfs.get(path);

if (existing) {
return existing.getData() as XmlFile;
}
return new XmlFile(path, vfs);
}

export default async function execute(ctx: Context, op: Operation) {
const xmlOp = op as XmlOperation;
const entries = xmlOp.value;

for (const entry of entries) {
let filename = entry.file;
if (!filename) {
continue;
}
let xmlFile: XmlFile = getXmlFile(join(ctx.project.projectRoot, filename), ctx.project.vfs);

try {
await xmlFile.load();
} catch (e) {
console.log('Unable to load the XML file here', e);
}

if (entry.attrs) {
await xmlFile.setAttrs(entry.target, entry.merge);
} else if (entry.inject) {
await xmlFile.injectFragment(entry.target, entry.inject);
} else if (entry.merge) {
await xmlFile.mergeFragment(entry.target, entry.merge);
} else if (entry.replace) {
await xmlFile.replaceFragment(entry.target, entry.replace);
} else if (entry.delete) {
await xmlFile.deleteNodes(entry.delete);
} else if (entry.deleteAttributes) {
await xmlFile.deleteAttributes(entry.target, entry.deleteAttributes);
}
}
}
2 changes: 1 addition & 1 deletion packages/configure/src/tasks/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function executeOperations(ctx: Context, operations: Operation[]) {
printOp(ctx, op);
}

const skipped = op.platform === 'ios' ? !ctx.project.ios : !ctx.project.android;
const skipped = op.platform !== 'project' && (op.platform === 'ios' ? !ctx.project.ios : !ctx.project.android);
if (skipped) {
Logger.debug(`Skipping ${op.id} because ${op.platform} project does not exist`);
continue;
Expand Down
6 changes: 6 additions & 0 deletions packages/configure/src/util/plural.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function pluralize(v: number, msg: string) {
if (v === 0 || v > 1) {
return `${msg}s`;
}
return msg;
}
36 changes: 36 additions & 0 deletions packages/configure/test/op.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@ describe('operation processing', () => {
ctx = await loadContext('../common/test/fixtures');
});

describe('Project', () => {
it('should process project operations', async () => {
const makeOp = (name: string, value: any): Operation => ({
id: `project.${name}`,
platform: 'project',
name,
value,
iosTarget: null,
iosBuild: null,
displayText: expect.anything(),
});
const parsed = await loadYamlConfig(
ctx,
'../common/test/fixtures/project.basic.yml',
);

const processed = processOperations(parsed);

expect(processed).toMatchObject([
makeOp('xml', [{
file: 'project-xml-strings.xml',
target: 'resources/string[@name="app_name"]',
replace: '<string name="app_name">Awesome App</string>\n'
}]),
makeOp('json', [{
file: 'project-json.json',
set: {
project_info: {
project_id: 'asdf'
}
}
}])
] as Operation[]);
});
});

describe('Android', () => {
it('should process android operations', async () => {
const makeOp = (name: string, value: any): Operation => ({
Expand Down
1 change: 1 addition & 0 deletions packages/configure/test/ops/android.xml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('op: android.xml', () => {

const file = await readFile(join(dir, 'android/app/src/main/AndroidManifest.xml'), { encoding: 'utf-8' });
//console.log(file);
console.log(file.trim());
expect(file.trim()).toBe(`
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="io.ionic.starter">
Expand Down
Loading