Microsoft_Console
[ class tree: Microsoft_Console ] [ index: Microsoft_Console ] [ all elements ]

Source for file Package.php

Documentation is available at Package.php

  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. /**
  37.  * @see Microsoft_AutoLoader
  38.  */
  39. require_once dirname(__FILE__) . '/../../AutoLoader.php';
  40.  
  41. /**
  42.  * Package commands
  43.  * 
  44.  * @category   Microsoft
  45.  * @package    Microsoft_WindowsAzure_CommandLine
  46.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  47.  * @license    http://phpazure.codeplex.com/license
  48.  * 
  49.  * @command-handler package
  50.  * @command-handler-description Windows Azure Package commands
  51.  * @command-handler-header Windows Azure SDK for PHP
  52.  * @command-handler-header Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  53.  * @command-handler-footer
  54.  * @command-handler-footer All commands support the --ConfigurationFile or -F parameter.
  55.  * @command-handler-footer The parameter file is a simple INI file carrying one parameter
  56.  * @command-handler-footer value per line. It accepts the same parameters as one can
  57.  * @command-handler-footer use from the command line command.
  58.  */
  59.     extends Microsoft_Console_Command
  60. {    
  61.     /**
  62.      * Packages a Windows Azure project structure.
  63.      * 
  64.      * @command-name Create
  65.      * @command-description Packages a Windows Azure project structure.
  66.      * 
  67.      * @command-parameter-for $path Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile|Microsoft_Console_Command_ParameterSource_StdIn --InputPath|-in Required. The path to package.
  68.      * @command-parameter-for $runDevFabric Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile --RunDevFabric|-dev Required. Switch. Run and deploy to the Windows Azure development fabric.
  69.      * @command-parameter-for $outputPath Microsoft_Console_Command_ParameterSource_Argv|Microsoft_Console_Command_ParameterSource_ConfigFile --OutputPath|-out Optional. The output path for the resulting package.
  70.      */
  71.     public function createPackageCommand($path, $runDevFabric, $outputPath)
  72.     {
  73.         // See if the $path variable requires some cleaning due to piping commands
  74.         if (strpos($path, 'finished at location: ') !== false) {
  75.             $path = explode('finished at location: ', $path);
  76.             $path = rtrim($path[1]);
  77.         }
  78.         
  79.         // Create output paths
  80.         if (is_null($outputPath) || $outputPath == '') {
  81.             $outputPath = realpath($path . '/../');
  82.         }
  83.         $packageOut = $outputPath . '/' . basename($path) . '.cspkg';
  84.         if (!file_exists($outputPath)) {
  85.             if (@mkdir($outputPath) === false) {
  86.                 throw new Microsoft_Console_Exception('The path ' . $outputPath . ' could not be created.');
  87.             }
  88.         }
  89.  
  90.         // Find Windows Azure SDK bin folders
  91.         $csPackFolderCandidates = array_merge(
  92.             isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  93.             isset($_SERVER['ProgramFiles(x86)']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  94.             isset($_SERVER['ProgramW6432']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array()
  95.         );
  96.         if (count($csPackFolderCandidates) == 0) {
  97.             throw new Microsoft_Console_Exception('Could not locate the Windows Azure SDK. Download the tools from www.azure.com or using the Web Platform Installer.');
  98.         }
  99.         $cspack = '"' . $csPackFolderCandidates[count($csPackFolderCandidates) - 1] . '\cspack.exe' . '"';
  100.         
  101.         $csRunFolderCandidates = array_merge(
  102.             isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  103.             isset($_SERVER['ProgramFiles(x86)']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  104.             isset($_SERVER['ProgramW6432']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure SDK\*\bin', GLOB_NOSORT) : array(),
  105.             isset($_SERVER['ProgramFiles']) ? glob($_SERVER['ProgramFiles'] . '\Windows Azure Emulator\emulator', GLOB_NOSORT) : array(),
  106.             isset($_SERVER['ProgramFiles(x86)']) ? glob($_SERVER['ProgramFiles(x86)'] . '\Windows Azure Emulator\emulator', GLOB_NOSORT) : array(),
  107.             isset($_SERVER['ProgramW6432']) ? glob($_SERVER['ProgramW6432'] . '\Windows Azure Emulator\emulator', GLOB_NOSORT) : array()
  108.         );
  109.         if (count($csRunFolderCandidates) == 0) {
  110.             throw new Microsoft_Console_Exception('Could not locate the Windows Azure SDK. Download the tools from www.azure.com or using the Web Platform Installer.');
  111.         }
  112.         $csrun = '"' . $csRunFolderCandidates[count($csRunFolderCandidates) - 1] . '\csrun.exe' . '"';
  113.         
  114.         // Open the ServiceDefinition.csdef file and check for role paths
  115.         $serviceDefinitionFile = $path . '/ServiceDefinition.csdef';
  116.         if (!file_exists($serviceDefinitionFile)) {
  117.             throw new Microsoft_Console_Exception('Could not locate ServiceDefinition.csdef at ' . $serviceDefinitionFile . '.');
  118.         }
  119.         $serviceDefinition = simplexml_load_file($serviceDefinitionFile);
  120.         $xmlRoles = array();
  121.  
  122.         if ($serviceDefinition->WebRole) {
  123.             if (count($serviceDefinition->WebRole) > 1) {
  124.                 foreach ($serviceDefinition->WebRole as $role) {
  125.                     $xmlRoles[] = $role;
  126.                 }
  127.             } else {
  128.                 $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WebRole));
  129.             }
  130.         }
  131.         if ($serviceDefinition->WorkerRole) {
  132.             if (count($serviceDefinition->WorkerRole) > 1) {
  133.                 foreach ($serviceDefinition->WorkerRole as $role) {
  134.                     $xmlRoles[] = $role;
  135.                 }
  136.             } else {
  137.                 $xmlRoles = array_merge($xmlRoles, array($serviceDefinition->WorkerRole));
  138.             }
  139.         }
  140.  
  141.         // Build '/role:' command parameter
  142.         $roleArgs = array();
  143.         foreach ($xmlRoles as $xmlRole) {
  144.             if ($xmlRole["name"]) {
  145.                 $roleArgs[] = '/role:' . $xmlRole["name"] . ';' . realpath($path . '/' . $xmlRole["name"]);
  146.             }
  147.         }
  148.  
  149.         // Build command
  150.         $command = $cspack;
  151.         $args = array(
  152.             $path . '\ServiceDefinition.csdef',
  153.             implode(' ', $roleArgs),
  154.             '/out:' . $packageOut
  155.         );
  156.         if ($runDevFabric) {
  157.             $args[] = '/copyOnly';
  158.         }
  159.         passthru($command . ' ' . implode(' ', $args));
  160.         
  161.         // Can we copy a configuration file?
  162.         $serviceConfigurationFile = $path . '/ServiceConfiguration.cscfg';
  163.         $serviceConfigurationFileOut = $outputPath . '/ServiceConfiguration.cscfg';
  164.         if (file_exists($serviceConfigurationFile) && !file_exists($serviceConfigurationFileOut)) {
  165.             copy($serviceConfigurationFile, $serviceConfigurationFileOut);
  166.         }
  167.         
  168.         // Do we have to start the development fabric?
  169.         if ($runDevFabric) {
  170.             passthru($csrun . ' /devstore:start');
  171.             passthru($csrun . ' /devfabric:start');
  172.             passthru($csrun . ' /removeAll');
  173.             passthru($csrun . ' /run:"' . $packageOut . ';' . $serviceConfigurationFileOut . '" /launchBrowser');
  174.         }
  175.         
  176.         // Echo package
  177.         echo $packageOut;
  178.     }
  179. }

Documentation generated on Sat, 03 Dec 2011 13:59:30 +0100 by phpDocumentor 1.4.3