Skip to content

Drush commands for workspaces - #5933 #5934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: 13.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/Commands/core/WorkspacesCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Drush\Commands\core;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\workspaces\WorkspaceOperationFactory;
use Drush\Attributes as CLI;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
use Drush\Drush;

final class WorkspacesCommands extends DrushCommands
{
use AutowireTrait;

const PUBLISH = 'workspaces:publish';

private ?WorkspaceOperationFactory $workspacesOperationFactory;

/**
* Constructs a WorkspacesCommands object.
*/
public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct();
/**
* Since we use Autowire and our service is in a non-required module, we
* - Get the container ourselves.
* - Our service variable can be null.
*/
$container = Drush::getContainer();
if ($container->has('plugin.manager.migration')) {
$this->workspacesOperationFactory = $container->get('workspaces.operation_factory');
}
}

/**
* Publish a workspace.
*/
#[CLI\Command(name: self::PUBLISH)]
#[CLI\Argument(name: 'id', description: 'The workspace to publish.')]
#[CLI\Usage(name: 'workspaces:publish stage', description: 'Publish the stage workspace')]
#[CLI\ValidateModulesEnabled(modules: ['workspaces'])]
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));
}
}
44 changes: 44 additions & 0 deletions tests/integration/WorkspacesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Unish;

use Drush\Commands\core\PhpCommands;

/**
* Tests Workspaces commands
*
* @group commands
*/
class WorkspacesTest extends \Unish\UnishIntegrationTestCase
{
private \Drupal\workspaces\WorkspaceManagerInterface $workspaceManager;

protected function setUp(): void
{
parent::setUp();
$this->drush('pm:install', ['workspaces', 'node']);

// $this->workspaceManager = \Drupal::service('workspaces.manager');
}

public function testWorkspaces(): void
{
$this->drush(PhpCommands::SCRIPT, ['workspaces'], ['script-path' => __DIR__ . '/resources']);
}

/**
* {@inheritdoc}
*/
protected function tearDown(): void
{
// Cleanup any created content.
$this->drush('entity:delete', ['node']);

// Uninstall test modules.
// $this->drush('pm:uninstall', ['workspaces', 'node']);

parent::tearDown();
}
}
4 changes: 4 additions & 0 deletions tests/integration/resources/workspaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

$workspace = \Drupal\workspaces\Entity\Workspace::load('stage');
Drupal::service('workspaces.manager')->setActiveWorkspace($workspace);