major refactoring under the hood
[pharext/pharext] / src / pharext / Task / Make.php
diff --git a/src/pharext/Task/Make.php b/src/pharext/Task/Make.php
new file mode 100644 (file)
index 0000000..9e71565
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+
+namespace pharext\Task;
+
+use pharext\ExecCmd;
+use pharext\Exception;
+use pharext\Task;
+
+/**
+ * Run make in the source dir
+ */
+class Make implements Task
+{
+       /**
+        * @var string
+        */
+       private $cwd;
+
+       /**
+        * @var array
+        */
+       private $args;
+
+       /**
+        * @var string
+        */
+       private $sudo;
+
+       /**
+        *
+        * @param string $cwd working directory
+        * @param array $args make's arguments
+        * @param string $sudo sudo command
+        */
+       public function __construct($cwd, array $args = null, $sudo = null) {
+               $this->cwd = $cwd;
+               $this->sudo = $sudo;
+               $this->args = $args;
+       }
+
+       /**
+        *
+        * @param bool $verbose
+        * @throws \pharext\Exception
+        */
+       public function run($verbose = false) {
+               $pwd = getcwd();
+               if (!chdir($this->cwd)) {
+                       throw new Exception;
+               }
+               try {
+                       $cmd = new ExecCmd("make", $verbose);
+                       if (isset($this->sudo)) {
+                               $cmd->setSu($this->sudo);
+                       }
+                       $args = $this->args;
+                       if (!$verbose) {
+                               $args = array_merge((array) $args, ["-s"]);
+                       }
+                       $cmd->run($args);
+               } finally {
+                       chdir($pwd);
+               }
+       }
+}