You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2017/01/25 15:50:09 UTC

[2/4] cordova-windows git commit: CB-12163 Add resource-file reference functionality through a flag

CB-12163 Add resource-file reference functionality through a flag


Project: http://git-wip-us.apache.org/repos/asf/cordova-windows/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-windows/commit/8f89a1f4
Tree: http://git-wip-us.apache.org/repos/asf/cordova-windows/tree/8f89a1f4
Diff: http://git-wip-us.apache.org/repos/asf/cordova-windows/diff/8f89a1f4

Branch: refs/heads/master
Commit: 8f89a1f4d72d3422b8be7e29e714fb4fc5ae9e1c
Parents: c71379a
Author: ktop <kt...@gmail.com>
Authored: Tue Dec 13 13:00:22 2016 -0500
Committer: Vladimir Kotikov <ko...@gmail.com>
Committed: Wed Jan 25 18:49:56 2017 +0300

----------------------------------------------------------------------
 .../plugin.xml                                  | 10 ++++
 .../src/windows/dummy1.json                     |  3 ++
 .../src/windows/dummy2.json                     |  3 ++
 .../src/windows/dummy3.json                     |  3 ++
 .../src/windows/dummy4.json                     |  3 ++
 spec/unit/pluginHandler/windows.spec.js         | 50 ++++++++++++++++++++
 template/cordova/lib/JsprojManager.js           |  7 ++-
 template/cordova/lib/PluginHandler.js           | 37 ++++++++++++---
 8 files changed, 109 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/8f89a1f4/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/plugin.xml
----------------------------------------------------------------------
diff --git a/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/plugin.xml b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/plugin.xml
new file mode 100644
index 0000000..855b220
--- /dev/null
+++ b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/plugin.xml
@@ -0,0 +1,10 @@
+<?xml version='1.0' encoding='utf-8'?>
+<plugin id="org.test.plugins.resourcereferenceplugin" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
+    <name>org.test.plugins.resourcereferenceplugin</name>
+    <platform name="windows">
+        <resource-file src="src/windows/dummy1.json" target="dummy.json" arch="x86" reference="true" />
+        <resource-file src="src/windows/dummy2.json" target="dummy.json" versions="&gt;=8.1" reference="true" />
+        <resource-file src="src/windows/dummy3.json" target="dummy.json" device-target="phone" reference="true"/>
+        <resource-file src="src/windows/dummy4.json" target="dummy.json" device-target="windows" versions="8.0" arch="x64" reference="true" />
+    </platform>
+</plugin>

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/8f89a1f4/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy1.json
----------------------------------------------------------------------
diff --git a/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy1.json b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy1.json
new file mode 100644
index 0000000..70bc71c
--- /dev/null
+++ b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy1.json
@@ -0,0 +1,3 @@
+{
+    "name": "dummy1"
+}

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/8f89a1f4/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy2.json
----------------------------------------------------------------------
diff --git a/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy2.json b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy2.json
new file mode 100644
index 0000000..7a96e3b
--- /dev/null
+++ b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy2.json
@@ -0,0 +1,3 @@
+{
+    "name": "dummy2"
+}

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/8f89a1f4/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy3.json
----------------------------------------------------------------------
diff --git a/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy3.json b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy3.json
new file mode 100644
index 0000000..18798c0
--- /dev/null
+++ b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy3.json
@@ -0,0 +1,3 @@
+{
+    "name": "dummy3"
+}

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/8f89a1f4/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy4.json
----------------------------------------------------------------------
diff --git a/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy4.json b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy4.json
new file mode 100644
index 0000000..9935709
--- /dev/null
+++ b/spec/unit/fixtures/org.test.plugins.resourcereferenceplugin/src/windows/dummy4.json
@@ -0,0 +1,3 @@
+{
+    "name": "dummy4"
+}

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/8f89a1f4/spec/unit/pluginHandler/windows.spec.js
----------------------------------------------------------------------
diff --git a/spec/unit/pluginHandler/windows.spec.js b/spec/unit/pluginHandler/windows.spec.js
index 6aea7a4..d87d00d 100644
--- a/spec/unit/pluginHandler/windows.spec.js
+++ b/spec/unit/pluginHandler/windows.spec.js
@@ -50,6 +50,11 @@ var invalid_source = faultyPluginInfo.getSourceFiles('windows');
 var invalid_resourceFiles = faultyPluginInfo.getResourceFiles('windows');
 var invalid_libfiles = faultyPluginInfo.getLibFiles('windows');
 
+var resourcereferenceplugin = path.join(__dirname, '../fixtures/org.test.plugins.resourcereferenceplugin');
+var resourcePluginInfo = new PluginInfo(resourcereferenceplugin);
+var valid_resourcereferenceFiles = resourcePluginInfo.getResourceFiles('windows');
+
+
 function copyArray(arr) {
     return Array.prototype.slice.call(arr, 0);
 }
@@ -74,6 +79,11 @@ beforeEach(function () {
     });
 });
 
+var getPluginFilePath = PluginHandler.__get__('getPluginFilePath');
+var computeResourcePath = function(resourceFile) {
+    return getPluginFilePath(dummyPluginInfo, resourceFile.src, cordovaProjectWindowsPlatformDir);
+};
+
 var PLATFORM_PROJECTS = {
     all: 'CordovaApp.projitems',
     phone: 'CordovaApp.Phone.jsproj',
@@ -209,6 +219,7 @@ describe('windows project handler', function () {
 
         describe('of <resource-file> elements', function () {
             var resourceFiles = copyArray(valid_resourceFiles);
+            var resourcereferenceFiles = copyArray(valid_resourcereferenceFiles);
             var invalidResourceFiles = copyArray(invalid_resourceFiles);
             var install = PluginHandler.getInstaller('resource-file');
 
@@ -229,6 +240,21 @@ describe('windows project handler', function () {
                 validateInstalledProjects('resource-file', resourceFiles[3], xpath, ['windows8']);
             });
 
+            it('should write to correct project files when conditions are specified with reference', function () {
+
+                var xpath = 'Content[@Include="' + computeResourcePath(resourcereferenceFiles[0]) + '"][@Condition="\'$(Platform)\'==\'x86\'"]';
+                validateInstalledProjects('resource-file', resourcereferenceFiles[0], xpath, ['all']);
+
+                xpath = 'Content[@Include="' + computeResourcePath(resourcereferenceFiles[1]) + '"]';
+                validateInstalledProjects('resource-file', resourcereferenceFiles[1], xpath, ['windows', 'phone', 'windows10']);
+
+                xpath = 'Content[@Include="' + computeResourcePath(resourcereferenceFiles[2]) + '"]';
+                validateInstalledProjects('resource-file', resourcereferenceFiles[2], xpath, ['phone']);
+
+                xpath = 'Content[@Include="' + computeResourcePath(resourcereferenceFiles[3]) + '"][@Condition="\'$(Platform)\'==\'x64\'"]';
+                validateInstalledProjects('resource-file', resourcereferenceFiles[3], xpath, ['windows8']);
+            });
+
             it('should throw if conditions are invalid', function () {
                 expect(function () {
                     install(invalidResourceFiles[0], faultyPluginInfo, dummyProject);
@@ -516,6 +542,30 @@ describe('windows project handler', function () {
                 targetConditions = {versions: '8.0', deviceTarget: 'windows', arch: 'x64'};
                 validateUninstalledProjects('resource-file', resourcefiles[3], path, incText, targetConditions, ['windows8']);
             });
+
+            it('should remove from correct project files when conditions specified with reference', function () {
+                var resourcereferencefiles = copyArray(valid_resourcereferenceFiles);
+
+                resourcereferencefiles.forEach(function(resourceFile) {
+                    install(resourceFile, resourcePluginInfo, dummyProject);
+                });
+                var path = 'ItemGroup/Content';
+                var incText = computeResourcePath(resourcereferencefiles[0]);
+                var targetConditions = {versions: undefined, deviceTarget: undefined, arch: 'x86', reference: 'true'};
+                validateUninstalledProjects('resource-file', resourcereferencefiles[0], path, incText, targetConditions, ['all']);
+
+                incText = computeResourcePath(resourcereferencefiles[1]);
+                targetConditions = {versions: '>=8.1', deviceTarget: undefined, arch: undefined, reference: 'true'};
+                validateUninstalledProjects('resource-file', resourcereferencefiles[1], path, incText, targetConditions, ['windows', 'phone', 'windows10']);
+
+                incText = computeResourcePath(resourcereferencefiles[2]);
+                targetConditions = {versions: undefined, deviceTarget: 'phone', arch: undefined, reference: 'true'};
+                validateUninstalledProjects('resource-file', resourcereferencefiles[2], path, incText, targetConditions, ['phone']);
+
+                incText = computeResourcePath(resourcereferencefiles[3]);
+                targetConditions = {versions: '8.0', deviceTarget: 'windows', arch: 'x64', reference: 'true'};
+                validateUninstalledProjects('resource-file', resourcereferencefiles[3], path, incText, targetConditions, ['windows8']);
+            });
         });
 
         describe('of <lib-file> elements', function () {

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/8f89a1f4/template/cordova/lib/JsprojManager.js
----------------------------------------------------------------------
diff --git a/template/cordova/lib/JsprojManager.js b/template/cordova/lib/JsprojManager.js
index 549288c..b701204 100644
--- a/template/cordova/lib/JsprojManager.js
+++ b/template/cordova/lib/JsprojManager.js
@@ -121,7 +121,12 @@ jsprojManager.prototype = {
         copyToOutputDirectory.text = 'Always';
         children.push(copyToOutputDirectory);
 
-        var item = createItemGroupElement('ItemGroup/Content', destPath, targetConditions, children);
+        var item;
+        if (targetConditions.reference)
+            item = createItemGroupElement('ItemGroup/Content', sourcePath, targetConditions, children);
+        else
+            item = createItemGroupElement('ItemGroup/Content', destPath, targetConditions, children);
+
         this._getMatchingProjects(targetConditions).forEach(function (project) {
             project.appendToRoot(item);
         });

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/8f89a1f4/template/cordova/lib/PluginHandler.js
----------------------------------------------------------------------
diff --git a/template/cordova/lib/PluginHandler.js b/template/cordova/lib/PluginHandler.js
index b39dade..6150845 100644
--- a/template/cordova/lib/PluginHandler.js
+++ b/template/cordova/lib/PluginHandler.js
@@ -25,6 +25,13 @@ var shell = require('shelljs');
 var events = require('cordova-common').events;
 var CordovaError = require('cordova-common').CordovaError;
 
+// returns relative file path for a file in the plugin's folder that can be referenced
+// from a project file.
+function getPluginFilePath(plugin, pluginFile, targetDir) {
+    var src = path.resolve(plugin.dir, pluginFile);
+    return '$(ProjectDir)' + path.relative(targetDir, src);
+}
+
 var handlers = {
     'source-file': {
         install:function(obj, plugin, project, options) {
@@ -46,13 +53,31 @@ var handlers = {
     },
     'resource-file':{
         install:function(obj, plugin, project, options) {
-            // as per specification resource-file target is specified relative to platform root
-            copyFile(plugin.dir, obj.src, project.root, obj.target);
-            project.addResourceFileToProject(obj.target, obj.target, getTargetConditions(obj));
+            var targetConditions = getTargetConditions(obj);
+            if (targetConditions.reference) {
+                // do not copy, but reference the file in the plugin folder. This allows to
+                // have multiple source files map to the same target and select the appropriate
+                // one based on the current build settings, e.g. architecture.
+                // also, we don't check for existence. This allows to insert build variables
+                // into the source file name, e.g.
+                // <resource-file src="$(Platform)/My.dll" target="My.dll" />
+                var relativeSrcPath = getPluginFilePath(plugin, obj.src, project.projectFolder);
+                project.addResourceFileToProject(relativeSrcPath, obj.target, targetConditions);
+            } else {
+                // as per specification resource-file target is specified relative to platform root
+                copyFile(plugin.dir, obj.src, project.root, obj.target);
+                project.addResourceFileToProject(obj.target, obj.target, targetConditions);
+            }
         },
         uninstall:function(obj, plugin, project, options) {
-            removeFile(project.root, obj.target);
-            project.removeResourceFileFromProject(obj.target, getTargetConditions(obj));
+            var targetConditions = getTargetConditions(obj);
+            if (targetConditions.reference) {
+                var relativeSrcPath = getPluginFilePath(plugin, obj.src, project.projectFolder);
+                project.removeResourceFileFromProject(relativeSrcPath, targetConditions);
+            } else {
+                removeFile(project.root, obj.target);
+                project.removeResourceFileFromProject(obj.target, targetConditions);
+            }
         }
     },
     'lib-file': {
@@ -180,7 +205,7 @@ module.exports.getUninstaller = function(type) {
 };
 
 function getTargetConditions(obj) {
-    return { versions: obj.versions, deviceTarget: obj.deviceTarget, arch: obj.arch };
+    return { versions: obj.versions, deviceTarget: obj.deviceTarget, arch: obj.arch, reference: obj.reference };
 }
 
 function copyFile (plugin_dir, src, project_dir, dest, link) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org