Skip to content

Commit

Permalink
Merge pull request #174 from palantirnet/detect-ddev
Browse files Browse the repository at this point in the history
Automatically run drush inside of ddev
  • Loading branch information
becw authored Jan 7, 2022
2 parents 44b1702 + 9aaec32 commit 5a9d7b5
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 9 deletions.
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"bin/the-build-installer"
],
"require": {
"palantirnet/phing-drush-task": "^1.1",
"cweagans/composer-patches": "^1.7",
"drupal/coder": "^8.3.6",
"drush/drush": "^9 || ^10",
"palantirnet/phing-drush-task": "^1.1",
"pear/http_request2": "^2.3",
"pear/versioncontrol_git": "@dev",
"phing/phing": "^2.14",
Expand All @@ -27,5 +28,12 @@
},
"config": {
"sort-packages": true
},
"extra": {
"patches": {
"phing/phing": {
"Support relative symliks in Phing": "https://raw.githubusercontent.com/palantirnet/the-build/7cdc28b6019fb88a0604261366f9ea35f1e21d96/patches/phing-relative-symlinks.patch"
}
}
}
}
2 changes: 1 addition & 1 deletion defaults/install/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

<!-- Include styleguide resources in the theme. This approach will symlink
resources in development environments, and copy them for artifact builds. -->
<!-- <includeresource source="styleguide/source/assets/css" dest="${drupal.root}/themes/custom/example_theme/css" /> -->
<!-- <includeresource relative="true" source="${build.dir}/styleguide/source/assets/css" dest="${build.dir}/${drupal.root}/themes/custom/example_theme/css" /> -->
</target>


Expand Down
9 changes: 9 additions & 0 deletions patches/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### phing-relative-symlinks.patch

Source: [https://github.com/phingofficial/phing/pull/695](https://github.com/phingofficial/phing/pull/695)

This patch updates Phing's SymlinkTask to allow creating relative symlinks, using the syntax:

```
<symlink link="path/to/destination" target="path/to/target" relative="true" />
```
76 changes: 76 additions & 0 deletions patches/phing-relative-symlinks.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
diff --git a/classes/phing/tasks/ext/SymlinkTask.php b/classes/phing/tasks/ext/SymlinkTask.php
index f132b4f747..87844a0d09 100644
--- a/classes/phing/tasks/ext/SymlinkTask.php
+++ b/classes/phing/tasks/ext/SymlinkTask.php
@@ -206,6 +206,46 @@ public function isRelative()
return $this->relative;
}

+ /**
+ * Given an existing path, convert it to a path relative to a given starting path.
+ *
+ * @param string $endPath Absolute path of target
+ * @param string $startPath Absolute path where traversal begins
+ *
+ * @return string Path of target relative to starting path
+ */
+ public function makePathRelative($endPath, $startPath)
+ {
+ // Normalize separators on Windows
+ if ('\\' === DIRECTORY_SEPARATOR) {
+ $endPath = str_replace('\\', '/', $endPath);
+ $startPath = str_replace('\\', '/', $startPath);
+ }
+
+ // Split the paths into arrays
+ $startPathArr = explode('/', trim($startPath, '/'));
+ $endPathArr = explode('/', trim($endPath, '/'));
+
+ // Find for which directory the common path stops
+ $index = 0;
+ while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) {
+ ++$index;
+ }
+
+ // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
+ $depth = count($startPathArr) - $index;
+
+ // Repeated "../" for each level need to reach the common path
+ $traverser = str_repeat('../', $depth);
+
+ $endPathRemainder = implode('/', array_slice($endPathArr, $index));
+
+ // Construct $endPath from traversing to the common path, then to the remaining $endPath
+ $relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
+
+ return '' === $relativePath ? './' : $relativePath;
+ }
+
/**
* Generates an array of directories / files to be linked
* If _filesets is empty, returns getTarget()
@@ -235,11 +275,7 @@ protected function getMap()
throw new BuildException('Link must be an existing directory when using fileset');
}

- if ($this->isRelative()) {
- $fromDir = $fs->getDir($this->getProject())->getPath();
- } else {
- $fromDir = $fs->getDir($this->getProject())->getAbsolutePath();
- }
+ $fromDir = $fs->getDir($this->getProject())->getAbsolutePath();

if (!is_dir($fromDir)) {
$this->log('Directory doesn\'t exist: ' . $fromDir, Project::MSG_WARN);
@@ -300,6 +336,11 @@ protected function symlink($target, $link)
{
$fs = FileSystem::getFileSystem();

+ if ($this->isRelative()) {
+ $link =(new PhingFile($link))->getAbsolutePath();
+ $target = rtrim($this->makePathRelative($target, dirname($link)), '/');
+ }
+
if (is_link($link) && @readlink($link) == $target) {
$this->log('Link exists: ' . $link, Project::MSG_INFO);

24 changes: 23 additions & 1 deletion src/TheBuild/IncludeResourceTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class IncludeResourceTask extends \Task {
*/
protected $dest = NULL;

/**
* Whether to create relative symlinks
*
* @var boolean
*/
protected $relative = true;

/**
* Init tasks.
Expand All @@ -44,6 +50,10 @@ public function init() {
if (!is_null($mode)) {
$this->setMode($mode);
}
$relative = $this->getProject()->getProperty('includeresource.relative');
if (!is_null($relative)) {
$this->setRelative($relative);
}
}


Expand All @@ -70,7 +80,11 @@ public function main() {
}
else {
$this->log(sprintf("Linking '%s' to '%s'", $this->source->getPath(), $this->dest->getPath()));
FileSystem::getFileSystem()->symlink($this->source->getPath(), $this->dest->getPath());
$symlink_task = $this->project->createTask("symlink");
$symlink_task->setTarget($this->source->getPath());
$symlink_task->setLink($this->dest->getPath());
$symlink_task->setRelative($this->relative);
$symlink_task->main();
}
}

Expand Down Expand Up @@ -122,4 +136,12 @@ public function setDest(PhingFile $dest) {
$this->dest = $dest;
}

/**
* @param boolean $relative
*/
public function setRelative($relative)
{
$this->relative = $relative;
}

}
19 changes: 17 additions & 2 deletions targets/drupal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,25 @@ Or, you can specify the export file directly:
the permissions, the database will be created.
-->
<target name="drupal-has-database-connection" depends="set-site" hidden="true">

<!-- We have to format the test query differently when using "ddev drush" vs. when using "drush" directly. -->
<property name="test_query" value="SELECT 1" />
<exec command="which ddev" returnProperty="which_ddev" />
<if>
<and>
<available file="${build.dir}/.ddev/config.yaml" />
<equals arg1="${which_ddev}" arg2="0" />
<not><isset property="env.IS_DDEV_PROJECT" /></not>
</and>
<then>
<property name="test_query" override="true">\"SELECT 1\"</property>
</then>
</if>

<trycatch>
<try>
<drush command="sql-query">
<param>SELECT 1</param>
<param>${test_query}</param>
</drush>
<echo msg="Drupal database connection available." />
</try>
Expand Down Expand Up @@ -255,7 +270,7 @@ Or, you can specify the export file directly:
<target name="drupal-first-install" depends="set-site" hidden="true">
<fail unless="drupal.site.admin_user" />

<symlink link="${build.dir}/${drupal.root}/modules/contrib/the_build_utility" target="${build.thebuild.dir}/defaults/standard/modules/the_build_utility" />
<symlink link="${build.dir}/${drupal.root}/modules/contrib/the_build_utility" target="${build.thebuild.dir}/defaults/standard/modules/the_build_utility" relative="true" />

<composer command="require" composer="${composer.composer}">
<arg value="drupal/admin_toolbar" />
Expand Down
3 changes: 3 additions & 0 deletions targets/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@

<!-- Target: setup-templates -->
<target name="setup-templates" depends="set-site">
<!-- Set the project name in the ddev config. -->
<replaceregexp match="name: .*" replace="name: ${projectname}" file=".ddev/config.yaml" />

<!-- Copy the build file template.
This doesn't do any property substitution except for the "projectname",
Expand Down
17 changes: 13 additions & 4 deletions targets/the-build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,20 @@
<!-- Use the project directory name as the project name, if it's not configured. -->
<basename property="projectname" file="${build.dir}" suffix="local" />

<!-- Configure the 'drush' Phing task. -->
<!-- Configure the 'drush' Phing task. By default, we run the installed drush. -->
<property name="drush.bin" value="${build.dir}/vendor/bin/drush" />
<property name="drush.root" value="${build.dir}/${drupal.root}" />
<property name="drush.config" value="${build.dir}/drush/drushrc.php" />

<!-- If this is a ddev project, and we're outside of ddev, run ddev's drush command instead. -->
<exec command="which ddev" returnProperty="which_ddev" />
<if>
<and>
<available file="${build.dir}/.ddev/config.yaml" />
<equals arg1="${which_ddev}" arg2="0" />
<not><isset property="env.IS_DDEV_PROJECT" /></not>
</and>
<then>
<property name="drush.bin" value="ddev drush" override="true" />
</then>
</if>

<!-- Configure the composer command, depending on whether `composer` or `composer.phar` is available. -->
<exec command="command -v composer" outputProperty="composer.path.composer" />
Expand Down

0 comments on commit 5a9d7b5

Please sign in to comment.