Skip to content

Instantly share code, notes, and snippets.

@mekegi
Created April 17, 2015 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mekegi/8a357c557df3bd4303ce to your computer and use it in GitHub Desktop.
Save mekegi/8a357c557df3bd4303ce to your computer and use it in GitHub Desktop.
whiptail menu for phing
<?php
/**
* usage:
* example 1
<taskdef name="menu" classname="MenuPhingTask" />
<menu title="select enviropment" choices="dev,staging,hotfix,live" />
<property name="build.env" value="${menu.result}" override="true" />
*
* example 2
<menu title="Do you really whant to deploy?" choices="No,Yes" />
<if>
<not>
<equals arg1="${menu.result}" arg2="Yes" />
</not>
<then>
<fail message="cancellation deploy" />
</then>
</if>
*
*/
/**
* @since 17.04.15 15:37
* @author Arsen Abdusalamov
*/
class MenuPhingTask extends Task
{
protected $title;
protected $choices;
function setTitle($title)
{
$this->title = $title;
return $this;
}
function setChoices($choices)
{
$this->choices = explode(',', $choices);
return $this;
}
public function main()
{
$descriptorSpec = array(
STDIN,
STDOUT,
array("pipe", "w"),
);
$rawChoices = implode('" "" "', $this->choices);
$fullCommand = sprintf('whiptail --title "%1$s" --noitem --menu "%1$s" 14 40 %2$d "%3$s" ""', $this->title, count($this->choices), $rawChoices);
echo $fullCommand . PHP_EOL;
// open the child
$proc = proc_open($fullCommand, $descriptorSpec, $pipes, getcwd());
// set all streams to non blocking mode
foreach ($descriptorSpec as $key => $ds) {
if (is_array($ds)) {
stream_set_blocking($pipes[$key], 0);
} else {
stream_set_blocking($ds, 0);
}
}
if (is_resource($proc)) {
// Loop while running
while (true) {
if (false !== $status = proc_get_status($proc)) {
if ($status['running'] === false) {
$output = stream_get_contents($pipes[2]);
$exitCode = $status['exitcode'];
fclose($pipes[2]);
break;
}
} else {
throw new Exception('Could not get proc status');
}
}
} else {
throw new Exception('Cannot execute child process');
}
$this->project->setProperty('menu.result', $output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment