Skip to content

Commit

Permalink
Updated project add (#148)
Browse files Browse the repository at this point in the history
* Updated project add

* Remove log
  • Loading branch information
mlynch authored Dec 21, 2022
1 parent 54f6fde commit ed62b20
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 58 deletions.
2 changes: 1 addition & 1 deletion packages/configure/src/operations/ios/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function execute(ctx: Context, op: Operation) {

stringsFile = ctx.project.ios?.getProjectFile<StringsFile>(
filename!,
(filename: string) => new StringsFile(filename, ctx.project.vfs)
(filename: string) => new StringsFile(filename, ctx.project.vfs, ctx.project)
);

if (!stringsFile) {
Expand Down
2 changes: 1 addition & 1 deletion packages/configure/src/operations/ios/xcconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default async function execute(ctx: Context, op: Operation) {

file = ctx.project.ios?.getProjectFile<XCConfigFile>(
filename!,
(filename: string) => new XCConfigFile(filename, ctx.project.vfs)
(filename: string) => new XCConfigFile(filename, ctx.project.vfs, ctx.project)
);

if (!file) {
Expand Down
Binary file modified packages/gradle-parse/capacitor-gradle-parse.jar
Binary file not shown.
22 changes: 6 additions & 16 deletions packages/project/src/ios/project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import plist from 'plist';
import path, { join } from 'path';
import path, { join, sep } from 'path';
import fetch from 'cross-fetch';
import { copy, pathExists, readdir, writeFile } from '@ionic/utils-fs';

Expand Down Expand Up @@ -86,7 +86,7 @@ export class IosProject extends PlatformProject {
}

async getPlistFile(path: string) {
return this.getProjectFile(path, (filename: string) => new PlistFile(filename, this.project.vfs));
return this.getProjectFile(path, (filename: string) => new PlistFile(filename, this.project.vfs, this.project));
}

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

if (appGroup) {
this.pbxProject?.addSourceFile(path, {}, appGroup?.[0]);
const pathSplit = path.split(sep);
if (pathSplit[0] === appTarget && appGroup) {
this.pbxProject?.addSourceFile(pathSplit.slice(1).join(sep), {}, appGroup?.[0]);
} else {
this.pbxProject?.addSourceFile(path, {}, emptyGroup?.[0]);
}
Expand Down Expand Up @@ -623,7 +624,7 @@ export class IosProject extends PlatformProject {
return open.getData() as PlistFile;
}

const plistFile = new PlistFile(filename, this.project.vfs);
const plistFile = new PlistFile(filename, this.project.vfs, this.project);

await plistFile.load();

Expand Down Expand Up @@ -678,15 +679,4 @@ export class IosProject extends PlatformProject {
await writeFile(file.getFilename(), this.pbxProject.writeSync());
}
}

private plistCommitFn = async (file: VFSFile) => {
const data = file.getData() as PlistFile;
const xml = plist.build(data.getDocument() ?? {}, {
indent: ' ', // Tab character
offset: -1,
newline: '\n'
});
await assertParentDirs(file.getFilename());
return writeFile(file.getFilename(), xml);
}
}
18 changes: 9 additions & 9 deletions packages/project/src/plist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { assertParentDirs } from "./util/fs";
export class PlistFile extends VFSStorable {
doc: PlistObject | null = null;

constructor(private path: string, private vfs: VFS) {
constructor(private path: string, private vfs: VFS, private project?: MobileProject) {
super();
}

Expand All @@ -37,27 +37,27 @@ export class PlistFile extends VFSStorable {
this.doc = await parsePlist(this.path);
} else {
this.doc = {};

// Add the file to the project
if (this.project) {
const rel = relative(this.project.config.ios?.path ?? '', this.path);
this.project.ios?.addFile(rel);
}
}

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, project: MobileProject) => {
private plistCommitFn = async (file: VFSFile) => {
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());
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);
}
return writeFile(file.getFilename(), xml);
}

plistDiffFn = async (file: VFSFile) => {
Expand Down
19 changes: 8 additions & 11 deletions packages/project/src/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { VFS, VFSFile, VFSStorable } from './vfs';
*/
export class StringsFile extends VFSStorable {
private doc: StringsEntries = [];
constructor(public path: string, private vfs: VFS) {
constructor(public path: string, private vfs: VFS, private project?: MobileProject) {
super();
}

Expand Down Expand Up @@ -76,6 +76,11 @@ export class StringsFile extends VFSStorable {

if (!await pathExists(this.path)) {
this.doc = [];
// Add the file to the iOS project
if (this.project) {
const rel = relative(this.project.config.ios?.path ?? '', this.path);
this.project?.ios?.addFile(rel);
}
} else {
this.doc = await this.parse(this.path);
}
Expand All @@ -92,18 +97,10 @@ export class StringsFile extends VFSStorable {
return parseStrings(contents);
}

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

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);
}
return writeFile(file.getFilename(), src);
}
}
11 changes: 6 additions & 5 deletions packages/project/src/vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class VFSRef<T extends VFSStorable> {
constructor(
private filename: string,
private data: T | null,
private commitFn: (file: VFSFile, project: MobileProject) => Promise<void>,
private commitFn: (file: VFSFile) => Promise<void>,
private diffFn?: (file: VFSFile) => Promise<VFSDiff>,
) {}

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

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

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

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

async diffAll() {
Expand All @@ -120,4 +120,5 @@ export class VFS {
close(ref: VFSFile) {
delete this.openFiles[ref.getFilename()];
}

}
17 changes: 8 additions & 9 deletions packages/project/src/xcconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class XCConfigFile extends VFSStorable {
// by newlines or by the start of comments
private keyValueRegex = /^\s*([^ \/]+)\s*=[^\S\r\n]*(([^\n;](?!\/\/))*)/gm;

constructor(public path: string, private vfs: VFS) {
constructor(public path: string, private vfs: VFS, private project?: MobileProject) {
super();
}

Expand Down Expand Up @@ -62,6 +62,11 @@ export class XCConfigFile extends VFSStorable {

if (!await pathExists(this.path)) {
this.doc = "";

if (this.project) {
const rel = relative(this.project.config.ios?.path ?? '', this.path);
this.project.ios?.addFile(rel);
}
} else {
this.doc = await this.parse(this.path);
}
Expand All @@ -78,15 +83,9 @@ export class XCConfigFile extends VFSStorable {
return contents;
}

private commitFn = async (file: VFSFile, project: MobileProject) => {
private commitFn = async (file: VFSFile) => {
const src = this.generate();
await assertParentDirs(file.getFilename());
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);
}
return writeFile(file.getFilename(), src);
}
}
17 changes: 11 additions & 6 deletions packages/project/test/project.ios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,37 +384,42 @@ describe('project - ios standard', () => {

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

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

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

const pbx = project.ios?.getPbxProject();
/*
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);

const pbxOnDisk = await readFile(join(dir, 'ios/App/App.xcodeproj/project.pbxproj'), { encoding: 'utf-8' });

// console.log(pbx?.writeSync());
expect(pbxOnDisk.indexOf('NewStrings.strings')).toBeGreaterThanOrEqual(0);
// expect(!!pbx?.hasFile('NewXml.xml')).toBe(true);
});

Expand Down

1 comment on commit ed62b20

@vercel
Copy link

@vercel vercel bot commented on ed62b20 Dec 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.