Skip to content

Commit

Permalink
Drush commands for workspaces - drush-ops#5933
Browse files Browse the repository at this point in the history
  • Loading branch information
malcomio committed Mar 25, 2024
1 parent e5ea0f5 commit b483b51
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/Commands/core/WorkspacesCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Drush\Commands\core;

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Utility\Token;
use Drupal\workspaces\WorkspaceOperationFactory;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* A Drush commandfile.
*
* In addition to this file, you need a drush.services.yml
* in root of your module, and a composer.json file that provides the name
* of the services file to use.
*/
final class WorkspacesCommands extends DrushCommands {

/**
* Constructs a WorkspacesCommands object.
*/
public function __construct(
private readonly WorkspaceOperationFactory $factory,
private readonly EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct();
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('workspaces.operation_factory'),
$container->get('entity_type.manager'),
);
}

/**
* Publish a workspace.
*/
#[CLI\Command(name: 'workspaces:publish')]
#[CLI\Argument(name: 'id', description: 'The workspace to publish.')]
#[CLI\Usage(name: 'workspaces:publish stage', description: 'Publish the stage workspace')]
public function commandName($id) {

$workspace = $this->entityTypeManager->getStorage('workspace')->load($id);

$workspace_publisher = $this->factory->getPublisher($workspace);

$args = [
'%source_label' => $workspace->label(),
'%target_label' => $workspace_publisher->getTargetLabel(),
];

// Does this workspace have any content to publish?
$diff = $workspace_publisher->getDifferringRevisionIdsOnSource();
if (empty($diff)) {
$this->io()->warning(dt('There are no changes that can be published from %source_label to %target_label.', $args));
return;
}

$workspace->publish();
$this->logger()->success(dt('Workspace %source_label published.', $args));
}

}

0 comments on commit b483b51

Please sign in to comment.