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

144 pbxproj files #146

Merged
merged 3 commits into from
Dec 20, 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
6 changes: 3 additions & 3 deletions packages/configure/src/tasks/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function checkModifiedFiles(ctx: Context) {
Object.keys(files).map(k => {
const file = files[k];
log(c.log.WARN(c.strong(`updated`)), file.getFilename());
const diff = diffs.find(d => d.file === file);
const diff = diffs.find((d: any) => d.file === file);
if (diff && ctx.args.diff) {
printDiff(diff);
}
Expand All @@ -110,12 +110,12 @@ async function checkModifiedFiles(ctx: Context) {
);

if (answers.apply) {
return ctx.project.vfs.commitAll();
return ctx.project.vfs.commitAll(ctx.project);
} else {
log('Not applying changes. Exiting');
}
} else if (!ctx.args.dryRun && ctx.args.y) {
logger.info('-y provided, automatically applying configuration');
return ctx.project.vfs.commitAll();
return ctx.project.vfs.commitAll(ctx.project);
}
}
Binary file modified packages/gradle-parse/capacitor-gradle-parse.jar
Binary file not shown.
23 changes: 23 additions & 0 deletions packages/project/src/ios/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,29 @@ export class IosProject extends PlatformProject {
return copy(srcPath, destPath);
}

/**
* Add a source file to the project. this attemps to add the file
* to the main "app" target, or adds it to the empty group (i.e. the root of
* the project tree) if the app target can't be found.
*/
async addFile(path: string): Promise<void> {
const groups = this.pbxProject?.hash.project.objects['PBXGroup'] ?? [];
const emptyGroup = Object.entries(groups).find(([key, value]: [string, any]) => {
return value.isa === 'PBXGroup' && typeof value.name === 'undefined'
});

const appTarget = this.getAppTargetName();
const appGroup = Object.entries(groups).find(([key, value]: [string, any]) => {
return value.isa === 'PBXGroup' && (value.name === appTarget || value.path === appTarget);
});

if (appGroup) {
this.pbxProject?.addSourceFile(path, {}, appGroup?.[0]);
} else {
this.pbxProject?.addSourceFile(path, {}, emptyGroup?.[0]);
}
}

private async assertEntitlementsFile(targetName: IosTargetName, buildName: IosBuildName | null) {
let file = this.getEntitlementsFile(targetName, buildName ?? undefined);

Expand Down
24 changes: 20 additions & 4 deletions packages/project/src/plist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import plist, { PlistObject, PlistValue } from "plist";
import { readFile, writeFile } from '@ionic/utils-fs';
import { relative } from 'path';
import { pathExists, readFile, writeFile } from '@ionic/utils-fs';
import { mergeWith, union } from 'lodash';

import { parsePlist } from "./util/plist";
Expand All @@ -23,25 +24,40 @@ export class PlistFile extends VFSStorable {
this.doc = doc;
}

async exists() {
return pathExists(this.path);
}

async load() {
if (this.vfs.isOpen(this.path)) {
return;
}

this.doc = await parsePlist(this.path);
if (await this.exists()) {
this.doc = await parsePlist(this.path);
} else {
this.doc = {};
}

Logger.v('plist', 'read', `Loaded plist file at ${this.path}`, this.doc);
this.vfs.open(this.path, this, this.plistCommitFn, this.plistDiffFn);
}

private plistCommitFn = async (file: VFSFile) => {
private plistCommitFn = async (file: VFSFile, project: MobileProject) => {
const data = file.getData() as PlistFile;
const xml = plist.build(data.getDocument() ?? {}, {
indent: ' ', // Tab character
offset: -1,
newline: '\n'
});
const shouldAdd = !(await pathExists(this.path));
await assertParentDirs(file.getFilename());
return writeFile(file.getFilename(), xml);
await writeFile(file.getFilename(), xml);
// Add the file to the project
if (shouldAdd) {
const rel = relative(project.config.ios?.path ?? '', this.path);
project.ios?.addFile(rel);
}
}

plistDiffFn = async (file: VFSFile) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/project/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class MobileProject {
}

commit(): Promise<void> {
return this.vfs.commitAll();
return this.vfs.commitAll(this);
}

async copyFile(src: string, dest: string): Promise<void> {
Expand Down
14 changes: 12 additions & 2 deletions packages/project/src/strings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { pathExists, readFile, writeFile } from '@ionic/utils-fs';
import { relative } from 'path';
import { Logger } from './logger';
import { MobileProject } from './project';
import { assertParentDirs } from './util/fs';
import { generateStrings, parseStrings, StringsEntries } from './util/strings';
import { VFS, VFSFile, VFSStorable } from './vfs';
Expand Down Expand Up @@ -90,10 +92,18 @@ export class StringsFile extends VFSStorable {
return parseStrings(contents);
}

private commitFn = async (file: VFSFile) => {
private commitFn = async (file: VFSFile, project: MobileProject) => {
const f = file.getData() as StringsFile;
const src = generateStrings(f.doc);
await assertParentDirs(file.getFilename());
return writeFile(file.getFilename(), src);

const shouldAdd = !(await pathExists(this.path));
await writeFile(file.getFilename(), src);

// Add the file to the project
if (shouldAdd) {
const rel = relative(project.config.ios?.path ?? '', this.path);
project.ios?.addFile(rel);
}
}
}
13 changes: 7 additions & 6 deletions packages/project/src/vfs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Diff from 'diff';
import { MobileProject } from './project';

export interface VFSDiff {
file?: VFSFile;
Expand All @@ -21,7 +22,7 @@ export class VFSRef<T extends VFSStorable> {
constructor(
private filename: string,
private data: T | null,
private commitFn: (file: VFSFile) => Promise<void>,
private commitFn: (file: VFSFile, project: MobileProject) => Promise<void>,
private diffFn?: (file: VFSFile) => Promise<VFSDiff>,
) {}

Expand All @@ -42,8 +43,8 @@ export class VFSRef<T extends VFSStorable> {
this.modified = true;
}

commit(): Promise<void> {
return this.commitFn(this);
commit(project: MobileProject): Promise<void> {
return this.commitFn(this, project);
}

async diff(): Promise<VFSDiff> {
Expand All @@ -70,7 +71,7 @@ export class VFS {
open<T extends VFSStorable>(
filename: string,
data: T,
commitFn: (file: VFSFile) => Promise<void>,
commitFn: (file: VFSFile, project: MobileProject) => Promise<void>,
diffFn?: (file: VFSFile) => Promise<VFSDiff>,
) {
const ref = new VFSRef(filename, data, commitFn, diffFn);
Expand All @@ -93,8 +94,8 @@ export class VFS {
}, {} as { [key: string]: VFSFile });
}

async commitAll() {
await Promise.all(Object.values(this.openFiles).map(file => file.commit()));
async commitAll(project: MobileProject) {
await Promise.all(Object.values(this.openFiles).map(file => file.commit(project)));
}

async diffAll() {
Expand Down
12 changes: 10 additions & 2 deletions packages/project/src/xcconfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { pathExists, readFile, writeFile } from '@ionic/utils-fs';
import { relative } from 'path';
import { Logger } from './logger';
import { MobileProject } from './project';
import { assertParentDirs } from './util/fs';
import { VFS, VFSFile, VFSStorable } from './vfs';

Expand Down Expand Up @@ -76,9 +78,15 @@ export class XCConfigFile extends VFSStorable {
return contents;
}

private commitFn = async (file: VFSFile) => {
private commitFn = async (file: VFSFile, project: MobileProject) => {
const src = this.generate();
await assertParentDirs(file.getFilename());
return writeFile(file.getFilename(), src);
const shouldAdd = !(await pathExists(this.path));
await writeFile(file.getFilename(), src);
// Add the file to the project
if (shouldAdd) {
const rel = relative(project.config.ios?.path ?? '', this.path);
project.ios?.addFile(rel);
}
}
}
39 changes: 38 additions & 1 deletion packages/project/test/project.ios.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tempy from 'tempy';
import { join } from 'path';
import { copy, pathExists, readFile, rm } from '@ionic/utils-fs';
import { MobileProject } from '../src';
import { MobileProject, StringsFile, XCConfigFile, XmlFile } from '../src';
import { MobileProjectConfig } from '../src/config';
import { PlistFile } from '../src/plist';

Expand Down Expand Up @@ -381,6 +381,43 @@ describe('project - ios standard', () => {
const destContents = await readFile(dest);
expect(destContents.length).toBeGreaterThan(0);
});

it('should add source files when committing', async () => {
const stringsFile = project.ios?.getProjectFile<StringsFile>(
"NewStrings.strings",
(filename: string) => new StringsFile(filename, project.vfs)
);
await stringsFile?.load();

const xcconfigFile = project.ios?.getProjectFile<XCConfigFile>(
"NewConfig.xcconfig",
(filename: string) => new XCConfigFile(filename, project.vfs)
);
await xcconfigFile?.load();

const plistFile = project.ios?.getProjectFile<PlistFile>(
"NewPlist.plist",
(filename: string) => new PlistFile(filename, project.vfs)
);
await plistFile?.load();

/*
const xmlFile = project.ios?.getProjectFile<XmlFile>(
"NewXml.xml",
(filename: string) => new XmlFile(filename, project.vfs)
);
await xmlFile?.load();
*/

await project.commit();

const pbx = project.ios?.getPbxProject();
expect(!!pbx?.hasFile('NewStrings.strings')).toBe(true);
expect(!!pbx?.hasFile('NewConfig.xcconfig')).toBe(true);
expect(!!pbx?.hasFile('NewPlist.plist')).toBe(true);
// expect(!!pbx?.hasFile('NewXml.xml')).toBe(true);
});

});

describe('ios - no info plist case', () => {
Expand Down