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

Adds targets phing task to allow target definitions in project.yml #439

Closed
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
1 change: 1 addition & 0 deletions phing/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- Include custom task classes -->
<includepath classpath="${phing.dir}" />
<taskdef name="behat" classname="phingcludes.BehatTask"/>
<taskdef name="targets" classname="phingcludes.TargetsTask"/>
<taskdef name="drush" classname="phingcludes.DrushTask"/>
<taskdef name="randomstring" classname="phingcludes.RandomStringTask" />
<taskdef name="filterFileListByFileSet" classname="phingcludes.FilterFileListByFileSetTask"/>
Expand Down
242 changes: 242 additions & 0 deletions phing/phingcludes/TargetsTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<?php

/**
* A phing task to loop through properties.
*/
require_once 'phing/Task.php';
use Symfony\Component\Yaml\Yaml;

/**
* @author Steve Worley <[email protected]>
*/
class TargetsTask extends PropertyTask {

/**
* Path to a file that contains targets.
*
* @var string
*/
protected $file;

/**
* Key by which to find targets.
*
* @var string
*/
protected $key;

/**
* Return property name.
*
* @var string
*/
protected $property;

/**
* Delimteter to join return values by.
*
* @var string
*/
protected $glue;

/**
* Default targets to return if $key is not found in $file.
*
* Should match the expected input for Phing <foreach>.
*
* @example
* validate:all,ci:setup,tests:all
*
* @see https://www.phing.info/docs/guide/trunk/ForeachTask.html
*
* @var string
*/
protected $default;

/**
* Set the file path.
*
* @param string $file
* A path to a valid yml file.
*/
public function setFile($file) {
$this->file = $file;
}

/**
* Set the key to lookup.
*
* @param string $key
* A key to look for in $file.
*/
public function setKey($key) {
$this->key = $key;
}

/**
* Set the return property name.
*
* @param string $property
* A name that will be accessible by other phing tasks in the target.
*/
public function setProperty($property) {
$this->property = $property;
}

/**
* Set the glue for the return value.
*
* This will be used by implode; if passing to phing target <foreach> the
* value given here should be set in foreach as well.
*
* @see https://www.phing.info/docs/guide/trunk/ForeachTask.html
*
* @param string $glue
* A string to join the return value.
*/
public function setGlue($glue) {
$this->glue = $glue;
}

/**
* Default list of targets to call.
*
* Should match the expected input for Phing <foreach>.
*
* @example
* validate:all,ci:setup,tests:all
*
* @see https://www.phing.info/docs/guide/trunk/ForeachTask.html
*
* @param string $default
* A glue separated string.
*/
public function setDefault($default) {
$this->default = $default;
}

/**
* Getter for file property.
*
* @return string
*/
public function getFile() {
return $this->file;
}

/**
* Getter for key property.
*
* @return string
*/
public function getKey() {
return $this->key;
}

/**
* Getter for property property.
*
* @return string
*/
public function getProperty() {
return empty($this->property) ? 'targets' : $this->property;
}

/**
* Getter for glue property.
*
* @return string
*/
public function getGlue() {
return empty($this->glue) ? ',' : $this->glue;
}

/**
* Getter for default property.
*
* @return string
*/
public function getDefault() {
return $this->default;
}

/**
* The main entry point method.
*
* @return bool
* @throws BuildException
*/
public function main() {

$file = $this->loadFile($this->getFile());
$targets = $file['targets'];

if (empty($targets) || empty($targets[$this->getKey()])) {
$this->log("No targets defined for {$this->getKey()}");
$this->project->setProperty($this->getProperty(), $this->getDefault());
return FALSE;
}

$values = $targets[$this->getKey()];
$parsed_target_list = [];

foreach ($this->arrayFlatten($values) as $name => $value) {
array_push($parsed_target_list, $name);
}

$parsed_target_list = implode($this->getGlue(), $parsed_target_list);
$this->project->setProperty($this->getProperty(), $parsed_target_list);
return TRUE;
}

/**
* Load the Yaml file given.
*
* @param string $file
* A file path.
*
* @return array
* An associative array of parsed YAML.
*
* @throws BuildException
*/
public function loadFile($file) {
try {
if (file_exists($file)) {
$data = Yaml::parse(file_get_contents($file));
}
} catch (Exception $e) {
throw new BuildException('Could not load given file.');
}

return $data;
}

/**
* Flatten array values into a string.
* @param [type] $array [description]
* @return [type] [description]
*/
public function arrayFlatten($array) {
$result = array();
$stack = array();
array_push($stack, array("", $array));

while (count($stack) > 0) {
list($prefix, $array) = array_pop($stack);

foreach ($array as $key => $value) {
$new_key = $prefix . strval($key);

if (is_array($value)) {
array_push($stack, array($new_key . ':', $value));
} else {
$result[$new_key] = $value;
}
}
}

return $result;
}

}
5 changes: 5 additions & 0 deletions phing/tasks/blt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
</drush>
</target>

<target name="blt:call" description="Call targets available to BLT">
<!-- Phingcall cannot be used as a target for <foreach> so this is a wrapper -->
<phingcall target="${target}" />
</target>

<target name="blt:rsync-template">
<echo>Copying files from BLT's template into your project.</echo>
<!-- @todo Do not overwrite structured or executable files. Instead, update them intelligently settings.php, drush.wrapper etc. -->
Expand Down
9 changes: 7 additions & 2 deletions phing/tasks/ci.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
</target>

<target name="ci:build:validate:test"
description="Builds, validates, tests, and deploys an artifact; uses ci drush alias."
depends="validate:all, ci:setup, tests:all">
description="Builds, validates, tests, and deploys an artifact; uses ci drush alias.">
<targets
key="ci"
property="targets"
file="${repo.root}/project.yml"
default="validate:all,ci:setup,tests:all" />
<foreach list="${targets}" param="target" target="blt:call" />
</target>

<target name="ci:setup" description="Install dependencies, builds docroot, installs Drupal; uses ci drush alias.">
Expand Down