forked from rakatyal/cordova-docs-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Task Runner Explorer Binding hook to a separate section
- Loading branch information
Showing
4 changed files
with
178 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright (c) Microsoft Corporation | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#Cordova Hook to Execute VS Task Runner Explorer Bindings from the Command Line | ||
|
||
License: MIT | ||
|
||
Use this hook if you want to be able to use the "Before Build" and "After Build" event bindings **outside** of Visual Studio (say from the Cordova CLI itself or during a CI build). It wires in the "Before Build" binding in the Task Runner Explorer to "Before Prepare" in Cordova and "After Build" in the Task Runner Explorer to "After Compile" in Cordova. **Note that currently this hook only supports bindings in gulpfile.js in the root of your project.** | ||
|
||
To install it: | ||
|
||
1. Grab the js file and drop it a "hooks" folder in your project root | ||
2. Update config.xml with the following: | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
<hook type="before_prepare" src="hooks/hook-task-runner-binding.js" /> | ||
<hook type="after_prepare" src="hooks/hook-task-runner-binding.js" /> | ||
<hook type="before_compile" src="hooks/hook-task-runner-binding.js" /> | ||
<hook type="after_compile" src="hooks/hook-task-runner-binding.js" /> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
##Using the Visual Studio 2015 Task Runner Explorer | ||
The Visual Studio Task Runner Explorer allows users to tie Gulp tasks to specific build events. To use the Task Runner Explorer: | ||
|
||
1. Go to View > Other Windows > Task Runner Explorer | ||
2. You will now see any gulpfiles you have in your project. (Hit the refresh icon if you added one after opening the window.) | ||
3. To bind a task to an event, right click on the task, go to Bindings, and select the event you want | ||
|
||
**Note: The hook currently only works if "gulpfile.js" is in the root of the project. In addition, Clean and ProjectOpened do not have direct analogs in the Cordova CLI and are currently ignored.** | ||
|
||
This results in a comment being added to gulpfile.js with the bindings. Ex: | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
/// <binding BeforeBuild='before-build-gulp-task' AfterBuild='after-build-gulp-task' Clean='after-build-gulp-task' ProjectOpened='before-build-gulp-task' /> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The config.xml elements above will map the BeforeBuild task into the Cordova CLI before\_prepare event and the AfterBuild task to the Cordova CLI after\_compile event. These same events are fired by the cordova "build" command which combines "prepare" and "compile" in one comand while still allowing each of these commands to function separatley. | ||
|
||
While not supported via Visual Studio UI, you can wire in Gulp tasks to additiona Cordova hooks by simply using the hook name using the syntax above. Ex: | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
/// <binding after_prepare='after-prepare-gulp-task' /> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The hook also automatically detects if Gulp is not installed in the node_modules folder in your project and will "npm install" the contents of package.json in your project to further streamline development. | ||
|
||
## Terms of Use | ||
By downloading and running this project, you agree to the license terms of the third party application software, Microsoft products, and components to be installed. | ||
|
||
The third party software and products are provided to you by third parties. You are responsible for reading and accepting the relevant license terms for all software that will be installed. Microsoft grants you no rights to third party software. | ||
|
||
|
||
## License | ||
Unless otherwise mentioned, the code samples are released under the MIT license. | ||
|
||
``` | ||
The MIT License (MIT) | ||
Copyright (c) Microsoft Corporation | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
``` | ||
|
94 changes: 94 additions & 0 deletions
94
tutorial-gulp/hook-task-runner-binding/hook-task-runner-binding.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright (c) Microsoft. All rights reserved. | ||
Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
*/ | ||
|
||
var exec, fork, fs, path, Q, hookTasks; | ||
|
||
module.exports = function (context) { | ||
|
||
// Skip processing if being called from within Visual Studio | ||
if (process.env["VisualStudioEdition"]) { | ||
return; | ||
} | ||
|
||
fs = require('fs'); | ||
path = require('path'); | ||
Q = context.requireCordovaModule('q'); | ||
exec = Q.nfbind(require('child_process').exec); | ||
fork = require('child_process').fork; | ||
|
||
// Syntax: | ||
/// <binding BeforeBuild='before-build' AfterBuild='after-build' Clean='after-build' /> | ||
|
||
// Check gulpfile.js for tasks to run for this hook | ||
if (fs.existsSync(path.join(process.cwd(), "gulpfile.js"))) { | ||
// Hooktasks should be cached after first execution since its at the module level | ||
if (!hookTasks) { | ||
console.log("Checking gulpfile for tasks to run for Cordova events.") | ||
hookTasks = {}; | ||
var gulpfile = fs.readFileSync("gulpfile.js", "utf8"); | ||
var matches = gulpfile.match(/\/\/\/\s<binding(.+)(?=\/>)/ig); | ||
if (matches) { | ||
var bindings = matches[0].split(" "); | ||
bindings.forEach(function (binding) { | ||
binding = binding.replace(/["|']/g, ""); | ||
var bindParts = binding.split("=") | ||
var hook = bindParts[0]; | ||
var task = bindParts[1]; | ||
if (hook.indexOf("_") == -1) { | ||
switch (hook) { | ||
case "BeforeBuild": hook = "before_prepare"; break; | ||
case "AfterBuild": hook = "after_compile"; break; | ||
case "Clean": return; // No cordova clean event | ||
case "ProjectOpened": return; // No cordova project open event | ||
} | ||
} else { | ||
hook = hook.toLowerCase(); | ||
} | ||
if (hookTasks[hook]) { | ||
hookTasks[hook].push(task); | ||
} else { | ||
hookTasks[hook] = [task]; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
if (hookTasks[context.hook]) { | ||
// Install dependencies in package.json if gulp not present - Run the task either way | ||
if (!fs.existsSync(path.join(process.cwd(), "node_modules", "gulp"))) { | ||
console.log("Gulp not found. Installing package dependencies.") | ||
return exec("npm install") | ||
.then(function (result) { | ||
console.log(result[0]); | ||
if (result[1] != "") { | ||
console.error(result[1]); | ||
}; | ||
}) | ||
.then(function () { return runGulpTask(context.hook); }); | ||
} else { | ||
return runGulpTask(context.hook); | ||
} | ||
} | ||
} | ||
|
||
function runGulpTask(hook) { | ||
var deferred = Q.defer(); | ||
console.log("Running gulp task " + hookTasks[hook] + " for Cordova event " + hook); | ||
var child = fork("./node_modules/gulp/bin/gulp.js", hookTasks[hook]); | ||
child.on("error", function (err) { | ||
deferred.reject(err); | ||
}); | ||
child.on("exit", function (code, signal) { | ||
if (code === 0 && signal === null) { | ||
deferred.resolve(); | ||
} else { | ||
deferred.reject("Non-zero exit code or signal. Code: " + code + ", Signal: " + signal); | ||
} | ||
}); | ||
return deferred.promise; | ||
} | ||
|
||
}; | ||
|