PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/CommandLine/Scaffolder.php

https://bitbucket.org/ktos/tinyshare
PHP | 193 lines | 82 code | 18 blank | 93 comment | 16 complexity | 479114a9a05307d2238abd8eb84138fe MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2011, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_Console
  30. * @subpackage Exception
  31. * @version $Id: Exception.php 55733 2011-01-03 09:17:16Z unknown $
  32. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  33. * @license http://phpazure.codeplex.com/license
  34. */
  35. /**
  36. * @see Microsoft_AutoLoader
  37. */
  38. require_once dirname(__FILE__) . '/../../AutoLoader.php';
  39. /**
  40. * Scaffold commands
  41. *
  42. * @category Microsoft
  43. * @package Microsoft_WindowsAzure_CommandLine
  44. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  45. * @license http://phpazure.codeplex.com/license
  46. *
  47. * @command-handler scaffolder
  48. * @command-handler-description Windows Azure Package commands
  49. * @command-handler-header Windows Azure SDK for PHP
  50. * @command-handler-header Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  51. * @command-handler-footer
  52. * @command-handler-footer All commands support the --ConfigurationFile or -F parameter.
  53. * @command-handler-footer The parameter file is a simple INI file carrying one parameter
  54. * @command-handler-footer value per line. It accepts the same parameters as one can
  55. * @command-handler-footer use from the command line command.
  56. */
  57. class Microsoft_WindowsAzure_CommandLine_Scaffolder
  58. extends Microsoft_Console_Command
  59. {
  60. /**
  61. * Runs a scaffolder and creates a Windows Azure project structure which can be customized before packaging.
  62. *
  63. * @command-name Run
  64. * @command-description Runs a scaffolder and creates a Windows Azure project structure which can be customized before packaging.
  65. *
  66. * @command-parameter-for $path Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile --OutputPath|-out Required. The path to create the Windows Azure project structure.
  67. * @command-parameter-for $scaffolder Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile|Microsoft_Console_Command_ParameterSource_Env --Scaffolder|-s Optional. The path to the scaffolder to use. Defaults to Scaffolders/DefaultScaffolder.phar
  68. */
  69. public function runCommand($path, $scaffolder, $argv)
  70. {
  71. // Default parameter value
  72. if (is_null($scaffolder) || $scaffolder == '') {
  73. $scaffolder = 'DefaultScaffolder';
  74. }
  75. // Locate scaffolder
  76. $scaffolderFile = realpath($scaffolder);
  77. if (!is_file($scaffolderFile)) {
  78. $scaffolderFile = realpath(dirname(__FILE__) . '/../../../../scaffolders/' . str_replace('.phar', '', $scaffolder) . '.phar');
  79. }
  80. // Verify scaffolder
  81. if (!is_file($scaffolderFile)) {
  82. throw new Microsoft_Console_Exception('Could not locate the given scaffolder: ' . $scaffolder);
  83. }
  84. // Include scaffolder
  85. require_once $scaffolderFile;
  86. $scaffolderClass = str_replace('.phar', '', basename($scaffolderFile));
  87. if (!class_exists($scaffolderClass)) {
  88. $scaffolderClass = str_replace('-', '_', str_replace('.', '_', $scaffolderClass));
  89. if (!class_exists($scaffolderClass)) {
  90. $scaffolderClass = substr($scaffolderClass, 0, strpos($scaffolderClass, '_'));
  91. if (!class_exists($scaffolderClass)) {
  92. throw new Microsoft_Console_Exception('Could not locate a class named ' . $scaffolderClass . ' in the given scaffolder: ' . $scaffolder . '. Make sure the scaffolder package contains a file named index.php and contains a class named Scaffolder.');
  93. }
  94. }
  95. }
  96. // Add command parameters
  97. array_unshift($argv, '--OutputPath=' . $path);
  98. array_unshift($argv, '--Phar=' . $scaffolderFile);
  99. array_unshift($argv, 'Run');
  100. array_unshift($argv, $scaffolderClass);
  101. // Run scaffolder
  102. Microsoft_Console_Command::bootstrap($argv);
  103. // Echo output path
  104. echo "$scaffolderClass finished at location: $path\r\n";
  105. }
  106. /**
  107. * Shows help information for a specific scaffolder.
  108. *
  109. * @command-name Help
  110. * @command-description Shows help information for a specific scaffolder.
  111. *
  112. * @command-parameter-for $scaffolder Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile|Microsoft_Console_Command_ParameterSource_Env --Scaffolder|-s Optional. The path to the scaffolder to use. Defaults to Scaffolders/DefaultScaffolder.phar
  113. */
  114. public function scaffolderhelpCommand($scaffolder, $argv)
  115. {
  116. // Default parameter value
  117. if (is_null($scaffolder) || $scaffolder == '') {
  118. $scaffolder = 'DefaultScaffolder';
  119. }
  120. // Locate scaffolder
  121. $scaffolderFile = realpath($scaffolder);
  122. if (!is_file($scaffolderFile)) {
  123. $scaffolderFile = realpath(dirname(__FILE__) . '/../../../../scaffolders/' . str_replace('.phar', '', $scaffolder) . '.phar');
  124. }
  125. // Verify scaffolder
  126. if (!is_file($scaffolderFile)) {
  127. throw new Microsoft_Console_Exception('Could not locate the given scaffolder: ' . $scaffolder);
  128. }
  129. // Include scaffolder
  130. require_once $scaffolderFile;
  131. $scaffolderClass = str_replace('.phar', '', basename($scaffolderFile));
  132. if (!class_exists($scaffolderClass)) {
  133. $scaffolderClass = str_replace('-', '_', str_replace('.', '_', $scaffolderClass));
  134. if (!class_exists($scaffolderClass)) {
  135. $scaffolderClass = substr($scaffolderClass, 0, strpos($scaffolderClass, '_'));
  136. if (!class_exists($scaffolderClass)) {
  137. throw new Microsoft_Console_Exception('Could not locate a class named ' . $scaffolderClass . ' in the given scaffolder: ' . $scaffolder . '. Make sure the scaffolder package contains a file named index.php and contains a class named Scaffolder.');
  138. }
  139. }
  140. }
  141. // Add command parameters
  142. array_unshift($argv, '-h');
  143. array_unshift($argv, $scaffolderClass);
  144. // Run scaffolder
  145. Microsoft_Console_Command::bootstrap($argv);
  146. }
  147. /**
  148. * Builds a scaffolder from a given path.
  149. *
  150. * @command-name Build
  151. * @command-description Builds a scaffolder from a given path.
  152. *
  153. * @command-parameter-for $rootPath Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile --InputPath|-in Required. The path to package into a scaffolder.
  154. * @command-parameter-for $scaffolderFile Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile --OutputFile|-out Required. The filename of the scaffolder.
  155. */
  156. public function buildCommand($rootPath, $scaffolderFile)
  157. {
  158. $archive = new Phar($scaffolderFile);
  159. $archive->buildFromIterator(
  160. new RecursiveIteratorIterator(
  161. new SourceControlFilteredRecursiveFilterIterator(
  162. new RecursiveDirectoryIterator(realpath($rootPath)))),
  163. realpath($rootPath));
  164. echo $scaffolderFile;
  165. }
  166. }
  167. Microsoft_Console_Command::bootstrap($_SERVER['argv']);
  168. class SourceControlFilteredRecursiveFilterIterator
  169. extends RecursiveFilterIterator {
  170. public static $filters = array('.svn', '.git');
  171. public function accept() {
  172. return !in_array(
  173. $this->current()->getFilename(), self::$filters, true);
  174. }
  175. }