| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of Cecil. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright (c) Arnaud Ligny <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Cecil\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Cecil\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Cecil\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Input\InputDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * Creates a new website. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | class NewSite extends AbstractCommand | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ->setName('new:site') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             ->setDescription('Creates a new website') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ->setDefinition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 new InputDefinition([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     new InputOption('force', 'f', InputOption::VALUE_NONE, 'Override directory if it already exists'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     new InputOption('demo', null, InputOption::VALUE_NONE, 'Add demo content (pages, templates and assets)'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |             ->setHelp('Creates a new website in the current directory, or in <path> if provided'); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @throws RuntimeException | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $force = $input->getOption('force'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $demo = $input->getOption('demo'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             // ask to override existing site? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             if (Util\File::getFS()->exists(Util::joinFile($this->getPath(), $this->findConfigFile('name') ?: self::CONFIG_FILE[0])) && !$force) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $output->writeln('<comment>Website already exists.</comment>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 if (!$this->io->confirm('Do you want to override it?', false)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             // define root path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $root = Util\Plateform::isPhar() ? Util\Plateform::getPharPath() . '/' : realpath(Util::joinFile(__DIR__, '/../../')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             // setup questions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $title = $this->io->ask('Give a title to your new site', 'Site title'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $baseline = $this->io->ask('Describe your site in few words', ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $baseurl = $this->io->ask('Base URL?', 'https://domain.tld/', [$this, 'validateUrl']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $description = $this->io->ask('Write a full description of your site', 'Site description.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $authorName = $this->io->ask('What is the author name?', 'Cecil'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $authorUrl = $this->io->ask('What is the author URL?', 'https://cecil.app', [$this, 'validateUrl']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $demo = $this->io->confirm('Add demo content?', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             // override skeleton default config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $config = Yaml::parseFile(Util::joinPath($root, 'resources/skeleton', self::CONFIG_FILE[0])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $config = array_replace_recursive($config, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 'title'       => $title, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 'baseline'    => $baseline, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 'baseurl'     => $baseurl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 'description' => $description, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 'author'      => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     'name' => $authorName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     'url'  => $authorUrl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $configYaml = Yaml::dump($config, 2, 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             Util\File::getFS()->dumpFile(Util::joinPath($this->getPath(), $this->findConfigFile('name') ?: self::CONFIG_FILE[0]), $configYaml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             // creates dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             foreach ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     (string) $this->getBuilder()->getConfig()->get('assets.dir'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     (string) $this->getBuilder()->getConfig()->get('layouts.dir'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     (string) $this->getBuilder()->getConfig()->get('pages.dir'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     (string) $this->getBuilder()->getConfig()->get('static.dir'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 ] as $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 Util\File::getFS()->mkdir(Util::joinPath($this->getPath(), $value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             // copy files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             foreach ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     'assets/favicon.png', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     'pages/index.md', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     'static/cecil-card.png', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 ] as $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 Util\File::getFS()->copy( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     Util::joinPath($root, 'resources/skeleton', $value), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                     Util::joinPath($this->getPath(), $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             // demo: copy all files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             if ($demo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 foreach ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                         (string) $this->getBuilder()->getConfig()->get('assets.dir'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                         (string) $this->getBuilder()->getConfig()->get('layouts.dir'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                         (string) $this->getBuilder()->getConfig()->get('pages.dir'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                         (string) $this->getBuilder()->getConfig()->get('static.dir'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     ] as $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                     Util\File::getFS()->mirror( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                         Util::joinPath($root, 'resources/skeleton', $value), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                         Util::joinPath($this->getPath(), $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             // done | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             $output->writeln(sprintf('<info>Your new Cecil site is created in %s.</info>', realpath($this->getPath()))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $this->io->newLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $this->io->listing([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 'You can download a theme from https://cecil.app/themes/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 'You can create a new page with "cecil new:page"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 'Start the built-in preview server via "cecil serve"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $this->io->text('Visit https://cecil.app for full documentation.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             throw new RuntimeException(sprintf($e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  |         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  |