Skip to content

Commit

Permalink
Merge pull request consolidation#253 from greg-1-anderson/defer-write
Browse files Browse the repository at this point in the history
Defer write
  • Loading branch information
DavertMik committed Feb 5, 2016
2 parents 112fab5 + 669b674 commit d62faca
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"bin":["robo"],
"require": {
"php": ">=5.4.0",
"php": ">=5.5.9",
"symfony/finder": "~2.5|~3.0",
"symfony/console": "~2.5|~3.0",
"symfony/process": "~2.5|~3.0",
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 50 additions & 11 deletions src/Task/File/Write.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Write extends BaseTask
use \Robo\Common\DynamicParams;

protected $filename;
protected $body = "";
protected $append = false;

public function __construct($filename)
Expand All @@ -40,7 +39,7 @@ public function __construct($filename)
*/
public function line($line)
{
$this->body .= $line . "\n";
$this->text($line . "\n");
return $this;
}

Expand All @@ -53,7 +52,7 @@ public function line($line)
*/
public function lines(array $lines)
{
$this->body .= implode("\n", $lines) . "\n";
$this->text(implode("\n", $lines) . "\n");
return $this;
}

Expand All @@ -66,20 +65,26 @@ public function lines(array $lines)
*/
public function text($text)
{
$this->body .= $text;
$this->stack[] = array_merge([__FUNCTION__ . 'Collect'], func_get_args());
return $this;
}

/**
* add a text from a file
*
* Note that the file is read in the run() method of this task.
* To load text from the current state of a file (e.g. one that may
* be deleted or altered by other tasks prior the execution of this one),
* use:
* $task->text(file_get_contents($filename));
*
* @param string $filename
*
* @return Write The current instance
*/
public function textFromFile($filename)
{
$this->text(file_get_contents($filename));
$this->stack[] = array_merge([__FUNCTION__ . 'Collect'], func_get_args());
return $this;
}

Expand Down Expand Up @@ -107,7 +112,7 @@ public function place($name, $val)
*/
public function replace($string, $replacement)
{
$this->body = str_replace($string, $replacement, $this->body);
$this->stack[] = array_merge([__FUNCTION__ . 'Collect'], func_get_args());
return $this;
}

Expand All @@ -121,20 +126,54 @@ public function replace($string, $replacement)
*/
public function regexReplace($pattern, $replacement)
{
$this->body = preg_replace($pattern, $replacement, $this->body);
$this->stack[] = array_merge([__FUNCTION__ . 'Collect'], func_get_args());
return $this;
}

public function run()
protected function textFromFileCollect($contents, $filename)
{
$this->printTaskInfo("Writing to <info>{$this->filename}</info>.");
return $contents . file_get_contents($filename);
}

protected function replaceCollect($contents, $string, $replacement)
{
return str_replace($string, $replacement, $contents);
}

protected function regexReplaceCollect($contents, $pattern, $replacement)
{
return preg_replace($pattern, $replacement, $contents);
}

protected function textCollect($contents, $text)
{
return $contents . $text;
}

protected function getContents()
{
$contents = "";
if ($this->append) {
$this->body = file_get_contents($this->filename) . $this->body;
$contents = file_get_contents($this->filename);
}
foreach ($this->stack as $action) {
$command = array_shift($action);
if (method_exists($this, $command)) {
array_unshift($action, $contents);
$contents = call_user_func_array([$this, $command], $action);
}
}
return $contents;
}

public function run()
{
$this->printTaskInfo("Writing to <info>{$this->filename}</info>.");
$contents = $this->getContents();
if (!file_exists(dirname($this->filename))) {
mkdir(dirname($this->filename), 0777, true);
}
$res = file_put_contents($this->filename, $this->body);
$res = file_put_contents($this->filename, $contents);
if ($res === false) {
return Result::error($this, "File {$this->filename} couldn't be created");
}
Expand Down

0 comments on commit d62faca

Please sign in to comment.