Skip to content

Commit ed62b20

Browse files
authored
Updated project add (#148)
* Updated project add * Remove log
1 parent 54f6fde commit ed62b20

File tree

9 files changed

+50
-58
lines changed

9 files changed

+50
-58
lines changed

packages/configure/src/operations/ios/strings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default async function execute(ctx: Context, op: Operation) {
1818

1919
stringsFile = ctx.project.ios?.getProjectFile<StringsFile>(
2020
filename!,
21-
(filename: string) => new StringsFile(filename, ctx.project.vfs)
21+
(filename: string) => new StringsFile(filename, ctx.project.vfs, ctx.project)
2222
);
2323

2424
if (!stringsFile) {

packages/configure/src/operations/ios/xcconfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default async function execute(ctx: Context, op: Operation) {
1616

1717
file = ctx.project.ios?.getProjectFile<XCConfigFile>(
1818
filename!,
19-
(filename: string) => new XCConfigFile(filename, ctx.project.vfs)
19+
(filename: string) => new XCConfigFile(filename, ctx.project.vfs, ctx.project)
2020
);
2121

2222
if (!file) {
0 Bytes
Binary file not shown.

packages/project/src/ios/project.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import plist from 'plist';
2-
import path, { join } from 'path';
2+
import path, { join, sep } from 'path';
33
import fetch from 'cross-fetch';
44
import { copy, pathExists, readdir, writeFile } from '@ionic/utils-fs';
55

@@ -86,7 +86,7 @@ export class IosProject extends PlatformProject {
8686
}
8787

8888
async getPlistFile(path: string) {
89-
return this.getProjectFile(path, (filename: string) => new PlistFile(filename, this.project.vfs));
89+
return this.getProjectFile(path, (filename: string) => new PlistFile(filename, this.project.vfs, this.project));
9090
}
9191

9292
getPbxProject() {
@@ -534,8 +534,9 @@ export class IosProject extends PlatformProject {
534534
return value.isa === 'PBXGroup' && (value.name === appTarget || value.path === appTarget);
535535
});
536536

537-
if (appGroup) {
538-
this.pbxProject?.addSourceFile(path, {}, appGroup?.[0]);
537+
const pathSplit = path.split(sep);
538+
if (pathSplit[0] === appTarget && appGroup) {
539+
this.pbxProject?.addSourceFile(pathSplit.slice(1).join(sep), {}, appGroup?.[0]);
539540
} else {
540541
this.pbxProject?.addSourceFile(path, {}, emptyGroup?.[0]);
541542
}
@@ -623,7 +624,7 @@ export class IosProject extends PlatformProject {
623624
return open.getData() as PlistFile;
624625
}
625626

626-
const plistFile = new PlistFile(filename, this.project.vfs);
627+
const plistFile = new PlistFile(filename, this.project.vfs, this.project);
627628

628629
await plistFile.load();
629630

@@ -678,15 +679,4 @@ export class IosProject extends PlatformProject {
678679
await writeFile(file.getFilename(), this.pbxProject.writeSync());
679680
}
680681
}
681-
682-
private plistCommitFn = async (file: VFSFile) => {
683-
const data = file.getData() as PlistFile;
684-
const xml = plist.build(data.getDocument() ?? {}, {
685-
indent: ' ', // Tab character
686-
offset: -1,
687-
newline: '\n'
688-
});
689-
await assertParentDirs(file.getFilename());
690-
return writeFile(file.getFilename(), xml);
691-
}
692682
}

packages/project/src/plist.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { assertParentDirs } from "./util/fs";
1212
export class PlistFile extends VFSStorable {
1313
doc: PlistObject | null = null;
1414

15-
constructor(private path: string, private vfs: VFS) {
15+
constructor(private path: string, private vfs: VFS, private project?: MobileProject) {
1616
super();
1717
}
1818

@@ -37,27 +37,27 @@ export class PlistFile extends VFSStorable {
3737
this.doc = await parsePlist(this.path);
3838
} else {
3939
this.doc = {};
40+
41+
// Add the file to the project
42+
if (this.project) {
43+
const rel = relative(this.project.config.ios?.path ?? '', this.path);
44+
this.project.ios?.addFile(rel);
45+
}
4046
}
4147

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

46-
private plistCommitFn = async (file: VFSFile, project: MobileProject) => {
52+
private plistCommitFn = async (file: VFSFile) => {
4753
const data = file.getData() as PlistFile;
4854
const xml = plist.build(data.getDocument() ?? {}, {
4955
indent: ' ', // Tab character
5056
offset: -1,
5157
newline: '\n'
5258
});
53-
const shouldAdd = !(await pathExists(this.path));
5459
await assertParentDirs(file.getFilename());
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-
}
60+
return writeFile(file.getFilename(), xml);
6161
}
6262

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

packages/project/src/strings.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { VFS, VFSFile, VFSStorable } from './vfs';
1111
*/
1212
export class StringsFile extends VFSStorable {
1313
private doc: StringsEntries = [];
14-
constructor(public path: string, private vfs: VFS) {
14+
constructor(public path: string, private vfs: VFS, private project?: MobileProject) {
1515
super();
1616
}
1717

@@ -76,6 +76,11 @@ export class StringsFile extends VFSStorable {
7676

7777
if (!await pathExists(this.path)) {
7878
this.doc = [];
79+
// Add the file to the iOS project
80+
if (this.project) {
81+
const rel = relative(this.project.config.ios?.path ?? '', this.path);
82+
this.project?.ios?.addFile(rel);
83+
}
7984
} else {
8085
this.doc = await this.parse(this.path);
8186
}
@@ -92,18 +97,10 @@ export class StringsFile extends VFSStorable {
9297
return parseStrings(contents);
9398
}
9499

95-
private commitFn = async (file: VFSFile, project: MobileProject) => {
100+
private commitFn = async (file: VFSFile) => {
96101
const f = file.getData() as StringsFile;
97102
const src = generateStrings(f.doc);
98103
await assertParentDirs(file.getFilename());
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-
}
104+
return writeFile(file.getFilename(), src);
108105
}
109106
}

packages/project/src/vfs.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class VFSRef<T extends VFSStorable> {
2222
constructor(
2323
private filename: string,
2424
private data: T | null,
25-
private commitFn: (file: VFSFile, project: MobileProject) => Promise<void>,
25+
private commitFn: (file: VFSFile) => Promise<void>,
2626
private diffFn?: (file: VFSFile) => Promise<VFSDiff>,
2727
) {}
2828

@@ -43,8 +43,8 @@ export class VFSRef<T extends VFSStorable> {
4343
this.modified = true;
4444
}
4545

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

5050
async diff(): Promise<VFSDiff> {
@@ -71,7 +71,7 @@ export class VFS {
7171
open<T extends VFSStorable>(
7272
filename: string,
7373
data: T,
74-
commitFn: (file: VFSFile, project: MobileProject) => Promise<void>,
74+
commitFn: (file: VFSFile) => Promise<void>,
7575
diffFn?: (file: VFSFile) => Promise<VFSDiff>,
7676
) {
7777
const ref = new VFSRef(filename, data, commitFn, diffFn);
@@ -95,7 +95,7 @@ export class VFS {
9595
}
9696

9797
async commitAll(project: MobileProject) {
98-
await Promise.all(Object.values(this.openFiles).map(file => file.commit(project)));
98+
await Promise.all(Object.values(this.openFiles).map(file => file.commit()));
9999
}
100100

101101
async diffAll() {
@@ -120,4 +120,5 @@ export class VFS {
120120
close(ref: VFSFile) {
121121
delete this.openFiles[ref.getFilename()];
122122
}
123+
123124
}

packages/project/src/xcconfig.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class XCConfigFile extends VFSStorable {
1414
// by newlines or by the start of comments
1515
private keyValueRegex = /^\s*([^ \/]+)\s*=[^\S\r\n]*(([^\n;](?!\/\/))*)/gm;
1616

17-
constructor(public path: string, private vfs: VFS) {
17+
constructor(public path: string, private vfs: VFS, private project?: MobileProject) {
1818
super();
1919
}
2020

@@ -62,6 +62,11 @@ export class XCConfigFile extends VFSStorable {
6262

6363
if (!await pathExists(this.path)) {
6464
this.doc = "";
65+
66+
if (this.project) {
67+
const rel = relative(this.project.config.ios?.path ?? '', this.path);
68+
this.project.ios?.addFile(rel);
69+
}
6570
} else {
6671
this.doc = await this.parse(this.path);
6772
}
@@ -78,15 +83,9 @@ export class XCConfigFile extends VFSStorable {
7883
return contents;
7984
}
8085

81-
private commitFn = async (file: VFSFile, project: MobileProject) => {
86+
private commitFn = async (file: VFSFile) => {
8287
const src = this.generate();
8388
await assertParentDirs(file.getFilename());
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-
}
89+
return writeFile(file.getFilename(), src);
9190
}
9291
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,37 +384,42 @@ describe('project - ios standard', () => {
384384

385385
it('should add source files when committing', async () => {
386386
const stringsFile = project.ios?.getProjectFile<StringsFile>(
387-
"NewStrings.strings",
388-
(filename: string) => new StringsFile(filename, project.vfs)
387+
"App/NewStrings.strings",
388+
(filename: string) => new StringsFile(filename, project.vfs, project)
389389
);
390390
await stringsFile?.load();
391391

392392
const xcconfigFile = project.ios?.getProjectFile<XCConfigFile>(
393393
"NewConfig.xcconfig",
394-
(filename: string) => new XCConfigFile(filename, project.vfs)
394+
(filename: string) => new XCConfigFile(filename, project.vfs, project)
395395
);
396396
await xcconfigFile?.load();
397397

398398
const plistFile = project.ios?.getProjectFile<PlistFile>(
399399
"NewPlist.plist",
400-
(filename: string) => new PlistFile(filename, project.vfs)
400+
(filename: string) => new PlistFile(filename, project.vfs, project)
401401
);
402402
await plistFile?.load();
403403

404+
const pbx = project.ios?.getPbxProject();
404405
/*
405406
const xmlFile = project.ios?.getProjectFile<XmlFile>(
406407
"NewXml.xml",
407408
(filename: string) => new XmlFile(filename, project.vfs)
408409
);
409410
await xmlFile?.load();
410411
*/
411-
412412
await project.commit();
413413

414-
const pbx = project.ios?.getPbxProject();
414+
415415
expect(!!pbx?.hasFile('NewStrings.strings')).toBe(true);
416416
expect(!!pbx?.hasFile('NewConfig.xcconfig')).toBe(true);
417417
expect(!!pbx?.hasFile('NewPlist.plist')).toBe(true);
418+
419+
const pbxOnDisk = await readFile(join(dir, 'ios/App/App.xcodeproj/project.pbxproj'), { encoding: 'utf-8' });
420+
421+
// console.log(pbx?.writeSync());
422+
expect(pbxOnDisk.indexOf('NewStrings.strings')).toBeGreaterThanOrEqual(0);
418423
// expect(!!pbx?.hasFile('NewXml.xml')).toBe(true);
419424
});
420425

0 commit comments

Comments
 (0)