Skip to content

Commit

Permalink
[BindTargets] Added BindTargets Task (#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored Dec 13, 2020
1 parent e59daf6 commit 2d04974
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 9 deletions.
14 changes: 5 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ name: Phing CI

on:
push:
pull_request_target:
pull_request:
branches:
# Branches from forks have the form 'user:branch-name' so we only run
# this job on pull_request events for branches that look like fork
# branches. Without this we would end up running this job twice for non
# forked PRs, once for the push and then once for opening the PR.
- '**:**'

jobs:
static_code_analysis:
Expand Down Expand Up @@ -117,6 +112,7 @@ jobs:
../bin/phing -debug -Dtests.codecoverage=true -listener "phing.listener.StatisticsListener"
- name: Transfer coverage
if: github.repository_owner == 'phingofficial'
run: |
bash <(curl -s https://codecov.io/bash) -f ./test/reports/clover-coverage.xml
wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=clover ./test/reports/clover-coverage.xml
Expand All @@ -129,12 +125,10 @@ jobs:
- test
- build_phar
- coverage
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_BUILDS }}
steps:
- name: Notify Slack
if: ${{env.SLACK_WEBHOOK_URL}} != 0
uses: 8398a7/action-slack@v3.8.0
continue-on-error: true
with:
status: custom
fields: workflow,job,commit,repo,ref,author,took
Expand All @@ -147,3 +141,5 @@ jobs:
text: `${process.env.AS_WORKFLOW}\n${process.env.AS_JOB} (${process.env.AS_COMMIT}) of ${process.env.AS_REPO}@${process.env.AS_REF} by ${process.env.AS_AUTHOR} succeeded in ${process.env.AS_TOOK}`,
}]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_BUILDS }}
1 change: 1 addition & 0 deletions classes/phing/tasks/defaults.properties
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pathconvert=phing.tasks.system.PathConvert
defaultexcludes=phing.tasks.system.DefaultExcludes
dependset=phing.tasks.system.DependSet
relentless=phing.tasks.system.Relentless
bindtargets=phing.tasks.system.BindTargets

; "Core" contributed tasks
; -- i.e. no taskdef needed.
Expand Down
71 changes: 71 additions & 0 deletions classes/phing/tasks/system/BindTargets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

/**
* Simple task which bind some targets to some defined extension point
*
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.tasks.system
*/
class BindTargets extends Task
{
/** @var string $extensionPoint */
private $extensionPoint;

/** @var Target[] $targets */
private $targets = [];

private $onMissingExtensionPoint = 'fail';

public function setExtensionPoint(string $extensionPoint)
{
$this->extensionPoint = $extensionPoint;
}

public function setOnMissingExtensionPoint(string $onMissingExtensionPoint)
{
if (!in_array($onMissingExtensionPoint, ['fail', 'warn', 'ignore'], true)) {
throw new BuildException("Invalid onMissingExtensionPoint: " . $onMissingExtensionPoint);
}
$this->onMissingExtensionPoint = $onMissingExtensionPoint;
}

public function setTargets(string $target)
{
$this->targets = array_values(array_filter(array_map('trim', explode(',', $target))));
}

public function main()
{
if ($this->extensionPoint === null) {
throw new BuildException('extensionPoint required', $this->getLocation());
}

if ($this->getOwningTarget() === null || "" !== $this->getOwningTarget()->getName()) {
throw new BuildException('bindtargets only allowed as a top-level task');
}

/** @var PhingXMLContext $ctx */
$ctx = $this->getProject()->getReference(ProjectConfigurator::PARSING_CONTEXT_REFERENCE);

foreach ($this->targets as $target) {
$ctx->addExtensionPoint([$this->extensionPoint, $target, $this->onMissingExtensionPoint, null]);
}
}
}
19 changes: 19 additions & 0 deletions etc/phing-grammar.rng
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
<ref name="attrib"/>
<ref name="available"/>
<ref name="basename"/>
<ref name="bindtargets"/>
<ref name="blockfor"/>
<ref name="chmod"/>
<ref name="chown"/>
Expand Down Expand Up @@ -1208,6 +1209,24 @@
</element>
</define>

<define name="bindtargets">
<element name="bindtargets">
<interleave>
<attribute name="targets" />
<attribute name="extensionpoint" />
<optional>
<attribute name="onmissingextensionpoint" a:defaultValue="fail">
<choice>
<value>fail</value>
<value>warn</value>
<value>ignore</value>
</choice>
</attribute>
</optional>
</interleave>
</element>
</define>

<define name="blockfor">
<element name="blockfor">
<interleave>
Expand Down
42 changes: 42 additions & 0 deletions test/classes/phing/tasks/system/BindTargetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

/**
* Tests the BindTargets Task
*
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.tasks.system
*/
class BindTargetsTest extends BuildFileTest
{
public function setUp(): void
{
$this->configureProject(
PHING_TEST_BASE . '/etc/tasks/system/BindTargets.xml'
);
}

public function testBind()
{
$this->executeTarget(__FUNCTION__);

$this->assertInLogs('bound #1');
$this->assertInLogs('bound #2');
}
}
39 changes: 39 additions & 0 deletions test/etc/tasks/system/BindTargets.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<!--
~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
~ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
~ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
~ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
~ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
~ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
~ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
~ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
~ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
~ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
~ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
~
~ This software consists of voluntary contributions made by many individuals
~ and is licensed under the LGPL. For more information please see
~ <http://phing.info>.
-->

<project name="BindTargets" default="invalid">

<target name="invalid">
<fail>This file should only be run via a testcase</fail>
</target>

<extension-point name="extension" />
<bindtargets targets="bound,bound2" extensionpoint="extension" onmissingextensionpoint="warn" />
<target name="testBind" depends="extension" />

<target name="bound">
<property name="test-bound" value="bound #1" />
<echo msg="${test-bound}" />
</target>

<target name="bound2">
<property name="test-bound2" value="bound #2" />
<echo msg="${test-bound2}" />
</target>
</project>

0 comments on commit 2d04974

Please sign in to comment.