Skip to content
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

Robo command to fetch git-submodules #4

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions .ddev/config.local.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ hooks:
- exec: drush @self.ddev uli
- exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS basic; GRANT ALL ON basic.* to 'db'@'%';"
service: db
auto-generated: true
- exec: drush @basic.ddev site-install server -y --existing-config --sites-subdir=basic
auto-generated: true
- exec: drush @basic.ddev uli
- exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS umami; GRANT ALL ON umami.* to 'db'@'%';"
service: db
auto-generated: true
# - exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS umami; GRANT ALL ON umami.* to 'db'@'%';"
# service: db
# - exec: drush @umami.ddev site-install server -y --existing-config --sites-subdir=umami
# - exec: drush @umami.ddev uli
160 changes: 160 additions & 0 deletions RoboFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

use Symfony\Component\Yaml\Yaml;

/**
* This is project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
class RoboFile extends \Robo\Tasks
{

const GITMODULES_REGEX = '/\[submodule "web\/sites\/(.*)"\]/';

public function fetch(string $filename)
{
$result = $this
->taskExec('git status -s')
->printOutput(FALSE)
->run();

if ($result->getMessage()) {
throw new Exception('The working directory is dirty. Please commit any pending changes.');
}

// Remove directories.
$gitmodules = file_get_contents('.gitmodules');
preg_match_all(self::GITMODULES_REGEX, $gitmodules, $matches);

if (empty($matches[1])) {
throw new Exception('No directories found in .gitmodules');
}

$directoryNames = $matches[1];

$task = $this
->taskExecStack()
->stopOnFail();

// Delete symlinks.
foreach ($directoryNames as $directoryName) {
$task->exec('rm config/'.$directoryName);

$path = "web/sites/$directoryName";

$task->exec("git submodule deinit -f -- $path");
$task->exec("rm -rf .git/modules/$path");
$task->exec("git rm $path");
}

$task->run();

// Get new subsites from file.
$subSites = [];
if (($handle = fopen($filename, 'r')) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
$subSites[] = $data;
}
fclose($handle);
}

// Add sub-modules
$task = $this
->taskExecStack()
->stopOnFail();

foreach ($subSites as $row) {
list($name, $git, $branch) = $row;

$branch = $branch ?: 'master';

$path = "web/sites/$name";
// Cleanup folder if in case we already have an older version.
$task->exec("rm -rf $path");

// Add submodule.
$task->exec("git submodule add --force -b $branch $git $path");
}

$task->run();

// Adapt DDEV config
$ddevFilename = '.ddev/config.local.yaml.example';
$ddevConfig = Yaml::parseFile($ddevFilename);
$ddevConfig['additional_hostnames'] = [];

foreach ($subSites as $row) {
list($name,,) = $row;
$path = "web/sites/$name";

// Create symlink.
$this->_symlink($path, "config/$name");

// Copy an adapted `settings.php`
$this->_copy('robo/settings.php', $path.'/settings.php', true);

$this->taskReplaceInFile("$path/settings.php")
->from('{{ name }}')
->to($name)
->run();

$ddevConfig['additional_hostnames'][] = $name;
}

// Remove previous DDEV `post-start` commands.
foreach ($ddevConfig['hooks']['post-start'] as $index => $row) {
if (!empty($row['auto-generated'])) {
unset($ddevConfig['hooks']['post-start'][$index]);
}
}

// Add new DDEV `post-start` commands.
foreach ($subSites as $row) {
list($name,,) = $row;

$newRows = [
[
'exec' => "mysql -uroot -proot -e \"CREATE DATABASE IF NOT EXISTS $name; GRANT ALL ON basic.* to 'db'@'%';\"",
'service' => 'db',
'auto-generated' => true,
],

[
'exec' => "drush @$name.ddev site-install server -y --existing-config --sites-subdir=$name",
'auto-generated' => true,
],

[
'exec' => "drush @$name.ddev uli",
'auto-generated' => true,
],
];

$ddevConfig['hooks']['post-start'] = array_merge($ddevConfig['hooks']['post-start'], $newRows);
}

$yaml = Yaml::dump($ddevConfig);
file_put_contents($ddevFilename, $yaml);

$this->_copy($ddevFilename, '.ddev/config.local.yaml', true);

// Restart DDEV.
$this->_exec('ddev restart');

}

/**
* Reset directory and git after running the `fetch` command.
*/
public function reset() {
$this
->taskExecStack()
->stopOnFail()
->exec('git reset --hard HEAD')
->exec('git clean -fd')
->exec('git submodule update --init --recursive --force')
->exec('git status')
->run();
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"drush/drush": "^10.2"
},
"require-dev": {
"consolidation/robo": "^1.0.0"
},
"conflict": {
"drupal/drupal": "*"
Expand Down
3 changes: 2 additions & 1 deletion web/sites/umami/settings.php → robo/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
if (file_exists($app_root . '/sites/default/settings.ddev.php')) {
include $app_root . '/sites/default/settings.ddev.php';
}
$databases['default']['default']['database'] = 'umami';
$databases['default']['default']['database'] = '{{ name }}';
$config['config_split.config_split.{{ name }}']['status'] = TRUE;
2 changes: 2 additions & 0 deletions robo/sites-collection1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
basic,[email protected]:Gizra/multi-repo-basic.git,remove-settings
foo,[email protected]:Gizra/multi-repo-basic.git,remove-settings