/** Filesystem */ public function testFs() { $dir = __DIR__ . '/03/output'; $command = new Commands\Filesystem\Directory(); $command->clean($dir); $res = glob($dir . "/*"); $this->assertCount(0, $res); $command = new Commands\Filesystem\Directory(); $command->create($dir . '/test-dir', 777); $this->assertFileExists($dir . '/test-dir'); $iterator = $command->read($dir); $this->assertCount(1, $iterator); $this->assertEquals('test-dir', $iterator->current()->getFileName()); $command = new Commands\Filesystem\File(); $command->create($dir . '/testfile.json', '{}'); $this->assertFileExists($dir . '/testfile.json'); $this->assertEquals('{}', file_get_contents($dir . '/testfile.json')); $command = new Commands\Filesystem\File(); $command->copy($dir . '/testfile.json', $dir . '/testfile2.json'); $this->assertFileExists($dir . '/testfile.json'); // old file still exists $this->assertFileExists($dir . '/testfile2.json'); $this->assertEquals('{}', file_get_contents($dir . '/testfile2.json')); $command = new Commands\Filesystem\Symlink(); $command->create($dir . '/testfile.json', $dir . '/sym-testfile.json'); $this->assertFileExists($dir . '/sym-testfile.json'); $this->assertTrue(is_link($dir . '/sym-testfile.json')); $command = new Commands\Filesystem\File(); $command->delete($dir . '/testfile.json'); $this->assertFileNotExists($dir . '/testfile.json'); $command = new Commands\Filesystem\Directory(); $command->clean($dir); $res = glob($dir . "/*"); $this->assertCount(0, $res); // filesystem class $command = new Commands\Filesystem\Filesystem(); $command->addDirectoriesToCreate([$dir . '/testfs1' => 777]); $command->addDirectoriesToCreate([$dir . '/testfs2' => 777]); $command->addFilesToCopy([$dir . '/testfs2/test.js' => $dir . '/../gulpfile.js']); ob_start(); $command->execute(); ob_end_clean(); $res = glob($dir . "/*"); $this->assertCount(2, $res); $this->assertTrue(is_dir($dir . '/testfs1')); $this->assertTrue(is_dir($dir . '/testfs2')); $this->assertFileExists($dir . '/testfs2/test.js'); $command = new Commands\Filesystem\Filesystem(); $command->addDirectoriesToClean([$dir]); ob_start(); $command->execute(); ob_end_clean(); $res = glob($dir . "/*"); $this->assertCount(0, $res); }
public function execute() { if (count($this->directoriesToCreate) > 0) { $this->log(Cli::getColoredString('Creating directories', 'light_blue')); $command = new Directory(); foreach ($this->directoriesToCreate as $directory => $chmod) { if (is_dir($directory)) { $this->log("Directory '{$directory}' already exists, skipping ..."); continue; } $command->create($directory, $chmod); $this->log("Directory '{$directory}' created."); } } if (count($this->directoriesToClean) > 0) { $this->log(Cli::getColoredString('Cleaning directories', 'light_blue')); $command = new Directory(); foreach ($this->directoriesToClean as $directory) { $command->clean($directory); $this->log("Directory '{$directory}' cleaned."); } } if (count($this->filesToCopy) > 0) { $this->log(Cli::getColoredString('Copying files', 'light_blue')); $command = new File(); foreach ($this->filesToCopy as $destination => $options) { if (is_file($destination)) { if ($options['onDuplicate'] == self::ERROR) { $this->error("File '{$destination}' already exists."); } elseif ($options['onDuplicate'] == self::SKIP) { $this->log("File '{$destination}' already exists, skipping ..."); continue; } elseif ($options['onDuplicate'] == self::REWRITE) { $command->delete($destination); } } $command->copy($options['source'], $destination); $this->log("File '{$options['source']}' copied to '{$destination}'."); } } if (count($this->symlinksRelativeToCreate) > 0) { $this->log(Cli::getColoredString('Creating symlinks', 'light_blue')); $command = new \Genesis\Commands\Filesystem\Symlink(); foreach ($this->symlinksRelativeToCreate as $baseDir => $symlinks) { foreach ($symlinks as $link => $target) { $absoluteLinkPath = $baseDir . '/' . $link; if (is_link($absoluteLinkPath)) { $this->log("Symlink '{$link}' already exists, skipping ..."); continue; } $command->createRelative($baseDir, $target, $link); $this->log("Symlink '{$link}' created."); } } } }