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 watch2 apps/extensions #56

Merged
merged 16 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from 15 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
69 changes: 62 additions & 7 deletions lib/pbxProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -1476,14 +1476,43 @@ pbxProject.prototype.addTarget = function(name, type, subfolder) {
this.addToPbxCopyfilesBuildPhase(productFile)

// this.addBuildPhaseToTarget(newPhase.buildPhase, this.getFirstTarget().uuid)

};
} else if (targetType === 'watch2_app') {
// Create CopyFiles phase in first target
this.addBuildPhase(
[targetName + '.app'],
'PBXCopyFilesBuildPhase',
'Embed Watch Content',
this.getFirstTarget().uuid,
targetType,
'"$(CONTENTS_FOLDER_PATH)/Watch"'
);
} else if (targetType === 'watch2_extension') {
// Create CopyFiles phase in watch target (if exists)
var watch2Target = this.getTarget(producttypeForTargettype('watch2_app'));
if (watch2Target) {
this.addBuildPhase(
[targetName + '.appex'],
'PBXCopyFilesBuildPhase',
'Embed App Extensions',
watch2Target.uuid,
targetType
);
}
}

// Target: Add uuid to root project
this.addToPbxProjectSection(target);

// Target: Add dependency for this target to first (main) target
brodycj marked this conversation as resolved.
Show resolved Hide resolved
this.addTargetDependency(this.getFirstTarget().uuid, [target.uuid]);
if (targetType === 'watch2_extension') {
// Target: Add dependency for this target to watch target (if exists)
var watch2Target = this.getTarget(producttypeForTargettype('watch2_app'));
if (watch2Target) {
this.addTargetDependency(watch2Target.uuid, [target.uuid]);
}
} else {
// Target: Add dependency for this target to first (main) target
this.addTargetDependency(this.getFirstTarget().uuid, [target.uuid]);
}


// Return target on success
Expand Down Expand Up @@ -1550,7 +1579,9 @@ function pbxCopyFilesBuildPhaseObj(obj, folderType, subfolderPath, phaseName) {
static_library: 'products_directory',
unit_test_bundle: 'wrapper',
watch_app: 'wrapper',
watch_extension: 'plugins'
watch2_app: 'products_directory',
watch_extension: 'plugins',
watch2_extension: 'plugins'
}
var SUBFOLDERSPEC_BY_DESTINATION = {
absolute_path: 0,
Expand Down Expand Up @@ -1683,7 +1714,9 @@ function producttypeForTargettype (targetType) {
static_library: 'com.apple.product-type.library.static',
unit_test_bundle: 'com.apple.product-type.bundle.unit-test',
watch_app: 'com.apple.product-type.application.watchapp',
watch_extension: 'com.apple.product-type.watchkit-extension'
watch2_app: 'com.apple.product-type.application.watchapp2',
watch_extension: 'com.apple.product-type.watchkit-extension',
watch2_extension: 'com.apple.product-type.watchkit2-extension'
};

return PRODUCTTYPE_BY_TARGETTYPE[targetType]
Expand All @@ -1701,7 +1734,9 @@ function filetypeForProducttype (productType) {
'com.apple.product-type.library.static': '"archive.ar"',
'com.apple.product-type.bundle.unit-test': '"wrapper.cfbundle"',
'com.apple.product-type.application.watchapp': '"wrapper.application"',
'com.apple.product-type.watchkit-extension': '"wrapper.app-extension"'
'com.apple.product-type.application.watchapp2': '"wrapper.application"',
'com.apple.product-type.watchkit-extension': '"wrapper.app-extension"',
'com.apple.product-type.watchkit2-extension': '"wrapper.app-extension"'
};

return FILETYPE_BY_PRODUCTTYPE[productType]
Expand Down Expand Up @@ -1737,6 +1772,26 @@ pbxProject.prototype.getFirstTarget = function() {
}
}

pbxProject.prototype.getTarget = function(productType) {
// Find target by product type
var targets = this.getFirstProject()['firstProject']['targets'];
var nativeTargets = this.pbxNativeTargetSection();
for (var i = 0; i < targets.length; i++) {
var target = targets[i];
var targetUuid = target.value;
if (nativeTargets[targetUuid]['productType'] === '"' + productType + '"') {
// Get pbxNativeTarget
var nativeTarget = this.pbxNativeTargetSection()[targetUuid];
return {
uuid: targetUuid,
target: nativeTarget
};
}
}

return null;
}

/*** NEW ***/

pbxProject.prototype.addToPbxGroupType = function (file, groupKey, groupType) {
Expand Down
10 changes: 10 additions & 0 deletions test/addBuildPhase.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,21 @@ exports.addBuildPhase = {
test.equal(buildPhase.dstSubfolderSpec, 1);
test.done();
},
'should set target to Products Directory given \'watch2_app\' as target': function (test) {
var buildPhase = proj.addBuildPhase(['file.m'], 'PBXCopyFilesBuildPhase', 'Copy Files', proj.getFirstTarget().uuid, 'watch2_app').buildPhase;
test.equal(buildPhase.dstSubfolderSpec, 16);
test.done();
},
'should set target to Plugins given \'watch_extension\' as target': function (test) {
var buildPhase = proj.addBuildPhase(['file.m'], 'PBXCopyFilesBuildPhase', 'Copy Files', proj.getFirstTarget().uuid, 'watch_extension').buildPhase;
test.equal(buildPhase.dstSubfolderSpec, 13);
test.done();
},
'should set target to Plugins given \'watch2_extension\' as target': function (test) {
var buildPhase = proj.addBuildPhase(['file.m'], 'PBXCopyFilesBuildPhase', 'Copy Files', proj.getFirstTarget().uuid, 'watch2_extension').buildPhase;
test.equal(buildPhase.dstSubfolderSpec, 13);
test.done();
},
'should add a script build phase to echo "hello world!"': function(test) {
var options = {shellPath: '/bin/sh', shellScript: 'echo "hello world!"'};
var buildPhase = proj.addBuildPhase([], 'PBXShellScriptBuildPhase', 'Run a script', proj.getFirstTarget().uuid, options).buildPhase;
Expand Down
26 changes: 26 additions & 0 deletions test/addTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ exports.addTarget = {

test.done();
},
'should have "wrapper.application" filetype for watch2_app product': function (test) {
var target = proj.addTarget(TARGET_NAME, 'watch2_app');
test.ok(target);
test.ok(target.pbxNativeTarget);
test.ok(target.pbxNativeTarget.productReference);

var productFile = proj.pbxFileReferenceSection()[target.pbxNativeTarget.productReference];
test.ok(productFile);
test.ok(productFile.explicitFileType);
test.equal(productFile.explicitFileType, '"wrapper.application"');

test.done();
},
'should have "wrapper.app-extension" filetype for watch_extension product': function (test) {
var target = proj.addTarget(TARGET_NAME, 'watch_extension');
test.ok(target);
Expand All @@ -251,6 +264,19 @@ exports.addTarget = {
test.ok(productFile.explicitFileType);
test.equal(productFile.explicitFileType, '"wrapper.app-extension"');

test.done();
},
'should have "wrapper.app-extension" filetype for watch2_extension product': function (test) {
var target = proj.addTarget(TARGET_NAME, 'watch2_extension');
test.ok(target);
test.ok(target.pbxNativeTarget);
test.ok(target.pbxNativeTarget.productReference);

var productFile = proj.pbxFileReferenceSection()[target.pbxNativeTarget.productReference];
test.ok(productFile);
test.ok(productFile.explicitFileType);
test.equal(productFile.explicitFileType, '"wrapper.app-extension"');

test.done();
}
}
165 changes: 165 additions & 0 deletions test/addWatch2App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
'License'); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

var fullProject = require('./fixtures/full-project')
fullProjectStr = JSON.stringify(fullProject),
pbx = require('../lib/pbxProject'),
pbxFile = require('../lib/pbxFile'),
proj = new pbx('.');

function cleanHash() {
return JSON.parse(fullProjectStr);
}

var TARGET_NAME = 'TestWatchApp',
TARGET_TYPE = 'watch2_app',
TARGET_SUBFOLDER_NAME = 'TestWatchAppFiles';

exports.setUp = function (callback) {
proj.hash = cleanHash();
callback();
}

exports.addWatchApp = {
'should create a new watch2 app target with the correct product type': function (test) {
var target = proj.addTarget(TARGET_NAME, TARGET_TYPE, TARGET_SUBFOLDER_NAME);

test.ok(typeof target == 'object');
test.ok(target.uuid);
test.ok(target.pbxNativeTarget);
test.ok(target.pbxNativeTarget.isa);
test.ok(target.pbxNativeTarget.name);
test.ok(target.pbxNativeTarget.productName);
test.ok(target.pbxNativeTarget.productReference);
test.ok(target.pbxNativeTarget.productType);
test.ok(target.pbxNativeTarget.buildConfigurationList);
test.ok(target.pbxNativeTarget.buildPhases);
test.ok(target.pbxNativeTarget.buildRules);
test.ok(target.pbxNativeTarget.dependencies);

test.equal(target.pbxNativeTarget.productType, '"com.apple.product-type.application.watchapp2"');

test.done();
},
'should create a new watch2 app target with the correct product type, without needing a subfolder name': function (test) {
var target = proj.addTarget(TARGET_NAME, TARGET_TYPE);

test.ok(typeof target == 'object');
test.ok(target.uuid);
test.ok(target.pbxNativeTarget);
test.ok(target.pbxNativeTarget.isa);
test.ok(target.pbxNativeTarget.name);
test.ok(target.pbxNativeTarget.productName);
test.ok(target.pbxNativeTarget.productReference);
test.ok(target.pbxNativeTarget.productType);
test.ok(target.pbxNativeTarget.buildConfigurationList);
test.ok(target.pbxNativeTarget.buildPhases);
test.ok(target.pbxNativeTarget.buildRules);
test.ok(target.pbxNativeTarget.dependencies);

test.equal(target.pbxNativeTarget.productType, '"com.apple.product-type.application.watchapp2"');

test.done();
},
'should create a new watch2 app target and add source, framework, resource and header files and the corresponding build phases': function (test) {
var target = proj.addTarget(TARGET_NAME, TARGET_TYPE, TARGET_SUBFOLDER_NAME),
options = { 'target' : target.uuid };

var sourceFile = proj.addSourceFile('Plugins/file.m', options),
sourcePhase = proj.addBuildPhase([], 'PBXSourcesBuildPhase', 'Sources', target.uuid),
resourceFile = proj.addResourceFile('assets.bundle', options),
resourcePhase = proj.addBuildPhase([], 'PBXResourcesBuildPhase', 'Resources', target.uuid),
frameworkFile = proj.addFramework('libsqlite3.dylib', options);
frameworkPhase = proj.addBuildPhase([], 'PBXFrameworkBuildPhase', 'Frameworks', target.uuid),
headerFile = proj.addHeaderFile('file.h', options);

test.ok(sourcePhase);
test.ok(resourcePhase);
test.ok(frameworkPhase);

test.equal(sourceFile.constructor, pbxFile);
test.equal(resourceFile.constructor, pbxFile);
test.equal(frameworkFile.constructor, pbxFile);
test.equal(headerFile.constructor, pbxFile);

test.ok(typeof target == 'object');
test.ok(target.uuid);
test.ok(target.pbxNativeTarget);
test.ok(target.pbxNativeTarget.isa);
test.ok(target.pbxNativeTarget.name);
test.ok(target.pbxNativeTarget.productName);
test.ok(target.pbxNativeTarget.productReference);
test.ok(target.pbxNativeTarget.productType);
test.ok(target.pbxNativeTarget.buildConfigurationList);
test.ok(target.pbxNativeTarget.buildPhases);
test.ok(target.pbxNativeTarget.buildRules);
test.ok(target.pbxNativeTarget.dependencies);

test.done();
},
'should create a new watch2 app target and add watch build phase': function (test) {
var target = proj.addTarget(TARGET_NAME, TARGET_TYPE);

test.ok(typeof target == 'object');
test.ok(target.uuid);
test.ok(target.pbxNativeTarget);
test.ok(target.pbxNativeTarget.isa);
test.ok(target.pbxNativeTarget.name);
test.ok(target.pbxNativeTarget.productName);
test.ok(target.pbxNativeTarget.productReference);
test.ok(target.pbxNativeTarget.productType);
test.ok(target.pbxNativeTarget.buildConfigurationList);
test.ok(target.pbxNativeTarget.buildPhases);
test.ok(target.pbxNativeTarget.buildRules);
test.ok(target.pbxNativeTarget.dependencies);

test.equal(target.pbxNativeTarget.productType, '"com.apple.product-type.application.watchapp2"');

var buildPhase = proj.buildPhaseObject('PBXCopyFilesBuildPhase', 'Embed Watch Content', target.uuid);

test.ok(buildPhase);
test.ok(buildPhase.files);
test.equal(buildPhase.files.length, 1);
test.ok(buildPhase.dstPath);
test.equal(buildPhase.dstPath, '"$(CONTENTS_FOLDER_PATH)/Watch"');
test.equal(buildPhase.dstSubfolderSpec, 16);

test.done();
},
'should create a new watch2 app with appropriate target extension': function (test) {
var target = proj.addTarget(TARGET_NAME, TARGET_TYPE);

var buildPhase = proj.buildPhaseObject('PBXCopyFilesBuildPhase', 'Embed Watch Content', target.uuid)

var buildPhaseFile = buildPhase.files[0];
test.ok(buildPhaseFile.value);
var buildPhaseFileSection = proj.pbxBuildFileSection()[buildPhaseFile.value];
test.ok(buildPhaseFileSection);
test.ok(buildPhaseFileSection.fileRef);

var buildPhaseFileRef = proj.pbxFileReferenceSection()[buildPhaseFileSection.fileRef];
test.ok(buildPhaseFileRef);
test.ok(buildPhaseFileRef.name);
test.ok(buildPhaseFileRef.path);

var quotedTargetPath = "\"" + TARGET_NAME + ".app\"";
test.equal(buildPhaseFileRef.name, quotedTargetPath);
test.equal(buildPhaseFileRef.path, quotedTargetPath);

test.done();
}
}
Loading