From d7a2556dd8dc0666790ec02366ebca1217766698 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 6 Aug 2014 19:59:24 -0600 Subject: [PATCH] added setup command for local grav installs --- system/src/Grav/Console/SetupCommand.php | 283 +++++++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 system/src/Grav/Console/SetupCommand.php diff --git a/system/src/Grav/Console/SetupCommand.php b/system/src/Grav/Console/SetupCommand.php new file mode 100644 index 000000000..822312a80 --- /dev/null +++ b/system/src/Grav/Console/SetupCommand.php @@ -0,0 +1,283 @@ + '/index.php', + '/composer.json' => '/composer.json', + '/bin' => '/bin', + '/system' => '/system', + '/vendor' => '/vendor', + '/user/plugins/error' => '/user/plugins/error', + '/user/plugins/problems' => '/user/plugins/problems', + '/user/themes/antimatter' => '/user/themes/antimatter', + ); + + protected $default_file = "---\ntitle: HomePage\n---\n# HomePage\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque porttitor eu felis sed ornare. Sed a mauris venenatis, pulvinar velit vel, dictum enim. Phasellus ac rutrum velit. Nunc lorem purus, hendrerit sit amet augue aliquet, iaculis ultricies nisl. Suspendisse tincidunt euismod risus, quis feugiat arcu tincidunt eget. Nulla eros mi, commodo vel ipsum vel, aliquet congue odio. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Pellentesque velit orci, laoreet at adipiscing eu, interdum quis nibh. Nunc a accumsan purus."; + + protected $source; + protected $destination; + + protected function configure() + { + $this + ->setName('setup') + ->setDescription('Setup of a base Grav system in your webroot') + ->addArgument( + 'destination', + InputArgument::REQUIRED, + 'The destination directory to symlink into' + ) + ->addOption( + 'symlink', + 's', + InputOption::VALUE_NONE, + 'Symlink the base grav system' + ) + ->setHelp(<<setup command help create a development environment that uses symbolic links to link the core of grav to the git cloned repository +EOT + ); + $this->source = getcwd(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->destination = $input->getArgument('destination'); + + // Create a red output option + $output->getFormatter()->setStyle('red', new OutputFormatterStyle('red')); + $output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan')); + $output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta')); + + // Symlink the Core Stuff + if ($input->getOption('symlink')) { + // Create Some core stuff if it doesn't exist + $this->createDirectories($output); + + // Loop through the symlink mappings and create the symlinks + $this->symlink($output); + + // Copy the Core STuff + } else { + $options = true; + // Create Some core stuff if it doesn't exist + $this->createDirectories($output); + + // Loop through the symlink mappings and copy what otherwise would be symlinks + $this->copy($output); + } + + $this->pages($output); + $this->initFiles($output); + $this->perms($output); + } + + private function createDirectories($output) + { + $output->writeln(''); + $output->writeln('Creating Directories'); + $dirs_created = false; + + if (!file_exists($this->destination)) { + mkdir($this->destination, 0777, true); + } + + foreach ($this->directories as $dir) { + if (!file_exists($this->destination . $dir)) { + $dirs_created = true; + $output->writeln(' ' . $dir . ''); + mkdir($this->destination . $dir, 0777, true); + } + } + + if (!$dirs_created) { + $output->writeln(' Directories already exist'); + } + } + + private function copy($output) + { + $output->writeln(''); + $output->writeln('Copying Files'); + + + foreach ($this->mappings as $source => $target) { + if ((int) $source == $source) { + $source = $target; + } + + $from = $this->source . $source; + $to = $this->destination . $target; + + $output->writeln(' ' . $source . ' -> ' . $to); + $this->rcopy($from, $to); + } + } + + private function symlink($output) + { + $output->writeln(''); + $output->writeln('Resetting Symbolic Links'); + + + foreach ($this->mappings as $source => $target) { + if ((int) $source == $source) { + $source = $target; + } + + $from = $this->source . $source; + $to = $this->destination . $target; + + $output->writeln(' ' . $source . ' -> ' . $to); + @unlink ($to); + symlink ($from, $to); + } + } + + private function initFiles($output) + { + $this->check($output); + + $output->writeln(''); + $output->writeln('File Initializing'); + $files_init = false; + + // Copy files if they do not exist + foreach ($this->files as $source => $target) { + if ((int) $source == $source) { + $source = $target; + } + + $from = $this->source . $source; + $to = $this->destination . $target; + + if (!file_exists($to)) { + $files_init = true; + copy($from, $to); + $output->writeln(' '.$target.' -> Created'); + } + } + + if (!$files_init) { + $output->writeln(' Files already exist'); + } + + + } + + private function pages($output) + { + $output->writeln(''); + $output->writeln('Pages Initializing'); + + // get pages files and initialize if no pages exist + $pages_dir = $this->destination . '/user/pages'; + $pages_files = array_diff(scandir($pages_dir), array('..', '.')); + + if (count($pages_files) == 0) { + $destination = $this->source . '/user/pages'; + $this->rcopy($destination, $pages_dir); + $output->writeln(' '.$destination.' -> Created'); + + } + } + + private function perms($output) + { + $output->writeln(''); + $output->writeln('Permisions Initializing'); + + $dir_perms = 0755; + + // get pages files and initialize if no pages exist + chmod($this->destination.'/bin/grav', $dir_perms); + $output->writeln(' bin/grav permissions reset to '. decoct($dir_perms)); + } + + + private function check($output) + { + $success = true; + + if (!file_exists($this->destination)) { + $output->writeln(' file: $this->destination does not exist!'); + $success = false; + } + + foreach ($this->directories as $dir) { + if (!file_exists($this->destination . $dir)) { + $output->writeln(' directory: ' . $dir . ' does not exist!'); + $success = false; + } + } + + foreach ($this->mappings as $target => $link) { + if (!file_exists($this->destination . $target)) { + $output->writeln(' mappings: ' . $target . ' does not exist!'); + $success = false; + } + } + if (!$success) { + $output->writeln(''); + $output->writeln('install should be run with --symlink|--s to symlink first'); + exit; + } + } + + private function rcopy($src, $dest){ + + // If the src is not a directory do a simple file copy + if(!is_dir($src)) { + copy($src, $dest); + return true; + } + + // If the destination directory does not exist create it + if(!is_dir($dest)) { + if(!mkdir($dest)) { + // If the destination directory could not be created stop processing + return false; + } + } + + // Open the source directory to read in files + $i = new \DirectoryIterator($src); + foreach($i as $f) { + if($f->isFile()) { + copy($f->getRealPath(), "$dest/" . $f->getFilename()); + } else if(!$f->isDot() && $f->isDir()) { + $this->rcopy($f->getRealPath(), "$dest/$f"); + } + } + } +}