audiofield.drush.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for audiofield.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. *
  9. * Defining a drush command to install the Audiofield external libraries.
  10. */
  11. function audiofield_drush_command() {
  12. $items = [];
  13. $items['audiofield-download'] = [
  14. 'description' => dt('Downloads the suggested Audiofield libraries from their remote repos.'),
  15. 'callback' => 'audiofield_drush_lib_download',
  16. 'arguments' => [
  17. 'updateLibrary' => dt('The name of the library to install. If omitted, all libraries will be installed.'),
  18. ],
  19. ];
  20. $items['audiofield-update'] = [
  21. 'description' => dt('Updates the Audiofield libraries from their remote repos if they are out of date.'),
  22. 'callback' => 'audiofield_drush_lib_update',
  23. 'arguments' => [
  24. 'installLibrary' => dt('The name of the library to update. If omitted, all libraries will be updated.'),
  25. ],
  26. ];
  27. return $items;
  28. }
  29. /**
  30. * Implements hook_drush_help().
  31. *
  32. * Help description for the audiofield drush command.
  33. */
  34. function audiofield_drush_help($section) {
  35. switch ($section) {
  36. case 'drush:audiofield-download':
  37. return dt("Downloads the AudioField external libraries.");
  38. case 'drush:audiofield-update':
  39. return dt("Updates the AudioField external libraries.");
  40. }
  41. }
  42. /**
  43. * Drush command callback.
  44. *
  45. * Updates the external libraries required for Audiofield plugins.
  46. *
  47. * @param string $updateLibrary
  48. * The name of the library to be updated, or blank for all libraries.
  49. */
  50. function audiofield_drush_lib_update($updateLibrary = '') {
  51. // Get a list of the audiofield plugins.
  52. $pluginList = \Drupal::service('plugin.manager.audiofield')->getDefinitions();
  53. // If there is an argument, check to make sure its valid.
  54. if (!empty($updateLibrary)) {
  55. if (!isset($pluginList[$updateLibrary . '_audio_player'])) {
  56. drush_log(dt('Error: @library is not a valid Audiofield library.', [
  57. '@library' => $updateLibrary,
  58. ], 'error'), 'error');
  59. return;
  60. }
  61. // If the argument is valid, we only want to install that plugin.
  62. $pluginList = [$updateLibrary . '_audio_player' => $pluginList[$updateLibrary . '_audio_player']];
  63. }
  64. // Loop over each plugin and make sure it's library is installed.
  65. foreach ($pluginList as $pluginName => $plugin) {
  66. // Create an instance of this plugin.
  67. $pluginInstance = \Drupal::service('plugin.manager.audiofield')->createInstance($pluginName);
  68. // Only check install if there is a library for the plugin.
  69. if ($pluginInstance->getPluginLibrary()) {
  70. // Get the library install path.
  71. $path = DRUPAL_ROOT . $pluginInstance->getPluginLibraryPath();
  72. // Only update if the library is already installed.
  73. if ($pluginInstance->checkInstalled(FALSE)) {
  74. // Only updating the library if its out of date.
  75. if (!$pluginInstance->checkVersion(FALSE)) {
  76. // Move the current installation to the temp directory.
  77. drush_move_dir($path, file_directory_temp() . '/temp_audiofield', TRUE);
  78. // If the directory failed to move, just delete it.
  79. if (is_dir($path)) {
  80. drush_delete_dir($path);
  81. }
  82. // Run the install command now to get the latest version.
  83. audiofield_drush_lib_download($updateLibrary, FALSE);
  84. // Check if library has been properly installed.
  85. if ($pluginInstance->checkInstalled()) {
  86. // Remove the temporary directory.
  87. drush_delete_dir(file_directory_temp() . '/temp_audiofield');
  88. drush_log(dt('Audiofield library for @library has been successfully updated at @location', [
  89. '@library' => $pluginInstance->getPluginTitle(),
  90. '@location' => $pluginInstance->getPluginLibraryPath(),
  91. ], 'success'), 'success');
  92. }
  93. else {
  94. // Remove the directory where we tried to install.
  95. drush_delete_dir($path);
  96. drush_log(dt('Error: unable to update Audiofield library @library', [
  97. '@library' => $pluginInstance->getPluginTitle(),
  98. ], 'error'), 'error');
  99. // Restore the original install since we failed to update.
  100. drush_move_dir(file_directory_temp() . '/temp_audiofield', $path, TRUE);
  101. }
  102. }
  103. else {
  104. drush_log(dt('Audiofield library for @library is already up to date', [
  105. '@library' => $pluginInstance->getPluginTitle(),
  106. ], 'success'), 'ok');
  107. }
  108. }
  109. else {
  110. // The library isn't installed at all, so we just run the install.
  111. audiofield_drush_lib_download($pluginInstance->getPluginLibraryName());
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Drush command callback.
  118. *
  119. * Installs the external libraries required for Audiofield plugins.
  120. *
  121. * @param string $installLibrary
  122. * The name of the library to be installed, or blank for all libraries.
  123. * @param bool $print_messages
  124. * Flag to indicate if messages should be printed.
  125. */
  126. function audiofield_drush_lib_download($installLibrary = '', $print_messages = TRUE) {
  127. // Get a list of the audiofield plugins.
  128. $pluginList = \Drupal::service('plugin.manager.audiofield')->getDefinitions();
  129. // If there is an argument, check to make sure its valid.
  130. if (!empty($installLibrary)) {
  131. if (!isset($pluginList[$installLibrary . '_audio_player'])) {
  132. drush_log(dt('Error: @library is not a valid Audiofield library.', [
  133. '@library' => $installLibrary,
  134. ], 'error'), 'error');
  135. return;
  136. }
  137. // If the argument is valid, we only want to install that plugin.
  138. $pluginList = [$installLibrary . '_audio_player' => $pluginList[$installLibrary . '_audio_player']];
  139. }
  140. // Loop over each plugin and make sure it's library is installed.
  141. foreach ($pluginList as $pluginName => $plugin) {
  142. // Create an instance of this plugin.
  143. $pluginInstance = \Drupal::service('plugin.manager.audiofield')->createInstance($pluginName);
  144. // Only check install if there is a library for the plugin.
  145. if ($pluginInstance->getPluginLibrary()) {
  146. // Check if the plugin is installed and that there is an available source.
  147. if (!$pluginInstance->checkInstalled()) {
  148. // Get the library install path.
  149. $path = DRUPAL_ROOT . $pluginInstance->getPluginLibraryPath();
  150. // Create the install directory if it does not exist.
  151. if (!is_dir($path)) {
  152. drush_mkdir($path);
  153. }
  154. // Attempt to download and unzip.
  155. if (drush_op('chdir', $path) &&
  156. drush_shell_exec('wget ' . $pluginInstance->getPluginRemoteSource() . ' -O audiofield-dl.zip') &&
  157. drush_shell_exec('unzip audiofield-dl.zip') &&
  158. drush_shell_exec('rm -rf audiofield-dl.zip')
  159. ) {
  160. // If the library still is not installed, we need to move files.
  161. if (!$pluginInstance->checkInstalled()) {
  162. // Find all folders in this directory and move their
  163. // subdirectories up to the parent directory.
  164. foreach (drush_scan_directory($path, '%.*%', ['.', '..', 'CVS'], 0, FALSE) as $dir) {
  165. if (is_dir($dir->basename)) {
  166. drush_move_dir($path . '/' . $dir->basename, '../tmp', TRUE);
  167. drush_move_dir('../tmp', $path, TRUE);
  168. }
  169. }
  170. // Projekktor source files need to be installed.
  171. if ($pluginInstance->getPluginId() == 'projekktor_audio_player') {
  172. drush_op('chdir', '..');
  173. drush_op('chdir', $path);
  174. drush_shell_exec('npm install');
  175. drush_shell_exec('grunt --force');
  176. }
  177. }
  178. if ($pluginInstance->checkInstalled()) {
  179. if ($print_messages) {
  180. drush_log(dt('Audiofield library for @library has been successfully installed at @location', [
  181. '@library' => $pluginInstance->getPluginTitle(),
  182. '@location' => $pluginInstance->getPluginLibraryPath(),
  183. ], 'success'), 'success');
  184. }
  185. }
  186. else {
  187. // Remove the directory where we tried to install.
  188. drush_delete_dir($path);
  189. if ($print_messages) {
  190. drush_log(dt('Error: unable to install Audiofield library @library', [
  191. '@library' => $pluginInstance->getPluginTitle(),
  192. ], 'error'), 'error');
  193. }
  194. }
  195. }
  196. else {
  197. // Remove the directory where we tried to install.
  198. drush_delete_dir($path);
  199. if ($print_messages) {
  200. drush_log(dt('Error: unable to download Audiofield library @library', [
  201. '@library' => $pluginInstance->getPluginTitle(),
  202. ], 'error'), 'error');
  203. }
  204. }
  205. }
  206. else {
  207. if ($print_messages) {
  208. drush_log(dt('Audiofield library for @library is already installed at @location', [
  209. '@library' => $pluginInstance->getPluginTitle(),
  210. '@location' => $pluginInstance->getPluginLibraryPath(),
  211. ], 'success'), 'ok');
  212. }
  213. }
  214. }
  215. }
  216. }
  217. /**
  218. * Implements drush_MODULE_post_COMMAND().
  219. *
  220. * This is used to install libraries after the module is enabled in drush.
  221. */
  222. function drush_audiofield_post_pm_enable() {
  223. $extensions = func_get_args();
  224. // Deal with comma delimited extension list.
  225. if (strpos($extensions[0], ',') !== FALSE) {
  226. $extensions = explode(',', $extensions[0]);
  227. }
  228. if (in_array('audiofield', $extensions) && !drush_get_option('skip')) {
  229. if (drush_confirm('Do you want to install external libraries for use with Audiofield? (Note: you can install manually later)')) {
  230. audiofield_drush_lib_download();
  231. }
  232. }
  233. }