Skip to content

Commit 22b7d26

Browse files
authored
144 pbxproj files (#146)
* Working on pbx * Add strings plist and xcconfig files to xcode project * Fix build
1 parent 310afe6 commit 22b7d26

File tree

9 files changed

+114
-19
lines changed

9 files changed

+114
-19
lines changed

packages/configure/src/tasks/run.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ async function checkModifiedFiles(ctx: Context) {
8787
Object.keys(files).map(k => {
8888
const file = files[k];
8989
log(c.log.WARN(c.strong(`updated`)), file.getFilename());
90-
const diff = diffs.find(d => d.file === file);
90+
const diff = diffs.find((d: any) => d.file === file);
9191
if (diff && ctx.args.diff) {
9292
printDiff(diff);
9393
}
@@ -110,12 +110,12 @@ async function checkModifiedFiles(ctx: Context) {
110110
);
111111

112112
if (answers.apply) {
113-
return ctx.project.vfs.commitAll();
113+
return ctx.project.vfs.commitAll(ctx.project);
114114
} else {
115115
log('Not applying changes. Exiting');
116116
}
117117
} else if (!ctx.args.dryRun && ctx.args.y) {
118118
logger.info('-y provided, automatically applying configuration');
119-
return ctx.project.vfs.commitAll();
119+
return ctx.project.vfs.commitAll(ctx.project);
120120
}
121121
}
0 Bytes
Binary file not shown.

packages/project/src/ios/project.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,29 @@ export class IosProject extends PlatformProject {
518518
return copy(srcPath, destPath);
519519
}
520520

521+
/**
522+
* Add a source file to the project. this attemps to add the file
523+
* to the main "app" target, or adds it to the empty group (i.e. the root of
524+
* the project tree) if the app target can't be found.
525+
*/
526+
async addFile(path: string): Promise<void> {
527+
const groups = this.pbxProject?.hash.project.objects['PBXGroup'] ?? [];
528+
const emptyGroup = Object.entries(groups).find(([key, value]: [string, any]) => {
529+
return value.isa === 'PBXGroup' && typeof value.name === 'undefined'
530+
});
531+
532+
const appTarget = this.getAppTargetName();
533+
const appGroup = Object.entries(groups).find(([key, value]: [string, any]) => {
534+
return value.isa === 'PBXGroup' && (value.name === appTarget || value.path === appTarget);
535+
});
536+
537+
if (appGroup) {
538+
this.pbxProject?.addSourceFile(path, {}, appGroup?.[0]);
539+
} else {
540+
this.pbxProject?.addSourceFile(path, {}, emptyGroup?.[0]);
541+
}
542+
}
543+
521544
private async assertEntitlementsFile(targetName: IosTargetName, buildName: IosBuildName | null) {
522545
let file = this.getEntitlementsFile(targetName, buildName ?? undefined);
523546

packages/project/src/plist.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import plist, { PlistObject, PlistValue } from "plist";
2-
import { readFile, writeFile } from '@ionic/utils-fs';
2+
import { relative } from 'path';
3+
import { pathExists, readFile, writeFile } from '@ionic/utils-fs';
34
import { mergeWith, union } from 'lodash';
45

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

27+
async exists() {
28+
return pathExists(this.path);
29+
}
30+
2631
async load() {
2732
if (this.vfs.isOpen(this.path)) {
2833
return;
2934
}
3035

31-
this.doc = await parsePlist(this.path);
36+
if (await this.exists()) {
37+
this.doc = await parsePlist(this.path);
38+
} else {
39+
this.doc = {};
40+
}
41+
3242
Logger.v('plist', 'read', `Loaded plist file at ${this.path}`, this.doc);
3343
this.vfs.open(this.path, this, this.plistCommitFn, this.plistDiffFn);
3444
}
3545

36-
private plistCommitFn = async (file: VFSFile) => {
46+
private plistCommitFn = async (file: VFSFile, project: MobileProject) => {
3747
const data = file.getData() as PlistFile;
3848
const xml = plist.build(data.getDocument() ?? {}, {
3949
indent: ' ', // Tab character
4050
offset: -1,
4151
newline: '\n'
4252
});
53+
const shouldAdd = !(await pathExists(this.path));
4354
await assertParentDirs(file.getFilename());
44-
return writeFile(file.getFilename(), xml);
55+
await writeFile(file.getFilename(), xml);
56+
// Add the file to the project
57+
if (shouldAdd) {
58+
const rel = relative(project.config.ios?.path ?? '', this.path);
59+
project.ios?.addFile(rel);
60+
}
4561
}
4662

4763
plistDiffFn = async (file: VFSFile) => {

packages/project/src/project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class MobileProject {
7676
}
7777

7878
commit(): Promise<void> {
79-
return this.vfs.commitAll();
79+
return this.vfs.commitAll(this);
8080
}
8181

8282
async copyFile(src: string, dest: string): Promise<void> {

packages/project/src/strings.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { pathExists, readFile, writeFile } from '@ionic/utils-fs';
2+
import { relative } from 'path';
23
import { Logger } from './logger';
4+
import { MobileProject } from './project';
35
import { assertParentDirs } from './util/fs';
46
import { generateStrings, parseStrings, StringsEntries } from './util/strings';
57
import { VFS, VFSFile, VFSStorable } from './vfs';
@@ -90,10 +92,18 @@ export class StringsFile extends VFSStorable {
9092
return parseStrings(contents);
9193
}
9294

93-
private commitFn = async (file: VFSFile) => {
95+
private commitFn = async (file: VFSFile, project: MobileProject) => {
9496
const f = file.getData() as StringsFile;
9597
const src = generateStrings(f.doc);
9698
await assertParentDirs(file.getFilename());
97-
return writeFile(file.getFilename(), src);
99+
100+
const shouldAdd = !(await pathExists(this.path));
101+
await writeFile(file.getFilename(), src);
102+
103+
// Add the file to the project
104+
if (shouldAdd) {
105+
const rel = relative(project.config.ios?.path ?? '', this.path);
106+
project.ios?.addFile(rel);
107+
}
98108
}
99109
}

packages/project/src/vfs.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Diff from 'diff';
2+
import { MobileProject } from './project';
23

34
export interface VFSDiff {
45
file?: VFSFile;
@@ -21,7 +22,7 @@ export class VFSRef<T extends VFSStorable> {
2122
constructor(
2223
private filename: string,
2324
private data: T | null,
24-
private commitFn: (file: VFSFile) => Promise<void>,
25+
private commitFn: (file: VFSFile, project: MobileProject) => Promise<void>,
2526
private diffFn?: (file: VFSFile) => Promise<VFSDiff>,
2627
) {}
2728

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

45-
commit(): Promise<void> {
46-
return this.commitFn(this);
46+
commit(project: MobileProject): Promise<void> {
47+
return this.commitFn(this, project);
4748
}
4849

4950
async diff(): Promise<VFSDiff> {
@@ -70,7 +71,7 @@ export class VFS {
7071
open<T extends VFSStorable>(
7172
filename: string,
7273
data: T,
73-
commitFn: (file: VFSFile) => Promise<void>,
74+
commitFn: (file: VFSFile, project: MobileProject) => Promise<void>,
7475
diffFn?: (file: VFSFile) => Promise<VFSDiff>,
7576
) {
7677
const ref = new VFSRef(filename, data, commitFn, diffFn);
@@ -93,8 +94,8 @@ export class VFS {
9394
}, {} as { [key: string]: VFSFile });
9495
}
9596

96-
async commitAll() {
97-
await Promise.all(Object.values(this.openFiles).map(file => file.commit()));
97+
async commitAll(project: MobileProject) {
98+
await Promise.all(Object.values(this.openFiles).map(file => file.commit(project)));
9899
}
99100

100101
async diffAll() {

packages/project/src/xcconfig.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { pathExists, readFile, writeFile } from '@ionic/utils-fs';
2+
import { relative } from 'path';
23
import { Logger } from './logger';
4+
import { MobileProject } from './project';
35
import { assertParentDirs } from './util/fs';
46
import { VFS, VFSFile, VFSStorable } from './vfs';
57

@@ -76,9 +78,15 @@ export class XCConfigFile extends VFSStorable {
7678
return contents;
7779
}
7880

79-
private commitFn = async (file: VFSFile) => {
81+
private commitFn = async (file: VFSFile, project: MobileProject) => {
8082
const src = this.generate();
8183
await assertParentDirs(file.getFilename());
82-
return writeFile(file.getFilename(), src);
84+
const shouldAdd = !(await pathExists(this.path));
85+
await writeFile(file.getFilename(), src);
86+
// Add the file to the project
87+
if (shouldAdd) {
88+
const rel = relative(project.config.ios?.path ?? '', this.path);
89+
project.ios?.addFile(rel);
90+
}
8391
}
8492
}

packages/project/test/project.ios.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import tempy from 'tempy';
22
import { join } from 'path';
33
import { copy, pathExists, readFile, rm } from '@ionic/utils-fs';
4-
import { MobileProject } from '../src';
4+
import { MobileProject, StringsFile, XCConfigFile, XmlFile } from '../src';
55
import { MobileProjectConfig } from '../src/config';
66
import { PlistFile } from '../src/plist';
77

@@ -381,6 +381,43 @@ describe('project - ios standard', () => {
381381
const destContents = await readFile(dest);
382382
expect(destContents.length).toBeGreaterThan(0);
383383
});
384+
385+
it('should add source files when committing', async () => {
386+
const stringsFile = project.ios?.getProjectFile<StringsFile>(
387+
"NewStrings.strings",
388+
(filename: string) => new StringsFile(filename, project.vfs)
389+
);
390+
await stringsFile?.load();
391+
392+
const xcconfigFile = project.ios?.getProjectFile<XCConfigFile>(
393+
"NewConfig.xcconfig",
394+
(filename: string) => new XCConfigFile(filename, project.vfs)
395+
);
396+
await xcconfigFile?.load();
397+
398+
const plistFile = project.ios?.getProjectFile<PlistFile>(
399+
"NewPlist.plist",
400+
(filename: string) => new PlistFile(filename, project.vfs)
401+
);
402+
await plistFile?.load();
403+
404+
/*
405+
const xmlFile = project.ios?.getProjectFile<XmlFile>(
406+
"NewXml.xml",
407+
(filename: string) => new XmlFile(filename, project.vfs)
408+
);
409+
await xmlFile?.load();
410+
*/
411+
412+
await project.commit();
413+
414+
const pbx = project.ios?.getPbxProject();
415+
expect(!!pbx?.hasFile('NewStrings.strings')).toBe(true);
416+
expect(!!pbx?.hasFile('NewConfig.xcconfig')).toBe(true);
417+
expect(!!pbx?.hasFile('NewPlist.plist')).toBe(true);
418+
// expect(!!pbx?.hasFile('NewXml.xml')).toBe(true);
419+
});
420+
384421
});
385422

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

0 commit comments

Comments
 (0)