Ejemplo n.º 1
0
 public function install($internal = false)
 {
     $this->deprecationNotice($internal);
     $this->whippet_init();
     $this->load_plugins_manifest();
     $this->load_plugins_lock();
     if (count(get_object_vars($this->plugins_manifest)) == 0) {
         echo "The plugin manifest file is empty\n";
     }
     //
     // If there is no lock file:
     //
     //  1. Install everything from the manifest
     //  2. Update the lockfile
     //
     if (!$this->plugins_lock_file) {
         foreach ($this->plugins_manifest as $dir => $plugin) {
             $git = new \Dxw\Whippet\Git\Git("{$this->plugin_dir}/{$dir}");
             // Is the repo there already?
             if (!$git->is_repo()) {
                 echo "[Adding {$dir}] ";
                 // We don't have the repo. Clone it.
                 if (!$git->clone_repo($plugin->repository)) {
                     echo "Aborting...\n";
                     die;
                 }
             }
             // Make sure repo is up to date.
             echo "[Checking {$dir}] ";
             if (!$git->checkout($plugin->revision)) {
                 echo "Aborting...\n";
                 die;
             }
             $git->checkout($git->current_commit());
             if (!$git->submodule_update()) {
                 echo "Aborting...\n";
                 die;
             }
         }
     } else {
         //
         // If there is a lock file:
         //
         //  1. Compare the lockfile to the manifest. Delete any plugins that have been removed.
         //  2. Check that the installed plugins are on the lockfile commit. Checkout the correct commit if not.
         //  3. Compare the lockfile to the manifest. Clone any plugins that have been added.
         //  4. Update the lockfile with the new list of commits.
         //
         //  1. Compare the lockfile to the manifest. Delete any plugins that have been removed.
         //
         // Make sure every entry in the lockfile also appears in the manifest
         $plugins_to_delete = array_keys((array) $this->plugins_locked);
         foreach ($this->plugins_locked as $lock_dir => $lock_plugin) {
             foreach ($this->plugins_manifest as $manifest_dir => $manifest_plugin) {
                 if ($lock_dir == $manifest_dir) {
                     unset($plugins_to_delete[array_search($lock_dir, $plugins_to_delete)]);
                 }
             }
         }
         // Delete the ones that don't:
         $gitignore = new \Dxw\Whippet\Git\Gitignore($this->project_dir);
         $ignores = $gitignore->get_ignores();
         foreach ($plugins_to_delete as $dir) {
             echo "[Removing {$dir}]\n";
             $git = new \Dxw\Whippet\Git\Git("{$this->plugin_dir}/{$dir}");
             $git->delete_repo();
             // remove from ignores:
             $plugin_dir = "/wp-content/plugins/{$dir}\n";
             if (($index = array_search($plugin_dir, $ignores)) !== false) {
                 unset($ignores[$index]);
             }
             // Remove from the lockfile
             unset($this->plugins_locked->{$dir});
         }
         $gitignore->save_ignores($ignores);
         //
         // 2. Check that the installed plugins are on the lockfile commit. Checkout the correct commit if not.
         //
         foreach ($this->plugins_locked as $dir => $plugin) {
             $git = new \Dxw\Whippet\Git\Git("{$this->plugin_dir}/{$dir}");
             if (!$git->is_repo()) {
                 // The repo has gone missing. Let's add it back.
                 echo "[Adding {$dir}] ";
                 $git->clone_repo($plugin->repository);
             }
             if ($this->plugins_manifest->{$dir}->repository != $plugin->repository) {
                 // The remote has changed. Zap the plugin and add it again.
                 $git->delete_repo();
                 // The repo should be re-added below when we add new plugins
                 continue;
             }
             // Check out a new revision, or if no new revision, check the existing one out again (in case of naughty changes)
             if ($this->plugins_manifest->{$dir}->revision == $plugin->revision) {
                 echo "[Checking {$dir}] ";
                 $git->checkout($plugin->commit);
             } else {
                 echo "[Updating {$dir}] ";
                 $git->checkout($this->plugins_manifest->{$dir}->revision);
             }
             if (!$git->submodule_update()) {
                 echo "Aborting...\n";
                 die;
             }
         }
         //
         // 3. Compare the lockfile to the manifest. Clone any plugins that have been added.
         //
         // Make sure every entry in the lockfile also appears in the manifest
         $plugins_to_clone = array_keys((array) $this->plugins_manifest);
         foreach ($this->plugins_manifest as $manifest_dir => $manifest_plugin) {
             foreach ($this->plugins_locked as $lock_dir => $lock_plugin) {
                 if ($lock_dir == $manifest_dir && $manifest_plugin->repository == $lock_plugin->repository) {
                     unset($plugins_to_clone[array_search($manifest_dir, $plugins_to_clone)]);
                 }
             }
         }
         foreach ($plugins_to_clone as $dir) {
             $plugin = $this->plugins_manifest->{$dir};
             echo "[Adding {$dir}] ";
             $git = new \Dxw\Whippet\Git\Git("{$this->plugin_dir}/{$dir}");
             // Is the repo there already?
             if (!$git->is_repo()) {
                 // We don't have the repo. Clone it.
                 if (!$git->clone_repo($plugin->repository)) {
                     echo "Aborting...\n";
                     die;
                 }
             }
             // Make sure repo is up to date.
             if (!$git->checkout($plugin->revision)) {
                 echo "Aborting...\n";
                 die;
             }
             if (!$git->submodule_update()) {
                 echo "Aborting...\n";
                 die;
             }
         }
     }
     //
     // Update the lockfile
     //
     $this->update_plugins_lock();
     //
     // Make sure that Whippet-managed plugins are gitignored
     //
     $gitignore = new \Dxw\Whippet\Git\Gitignore($this->project_dir);
     $ignores = $gitignore->get_ignores();
     foreach ($this->plugins_locked as $dir => $plugin) {
         $plugin_dir = "/wp-content/plugins/{$dir}\n";
         if (array_search($plugin_dir, $ignores) === false) {
             $ignores[] = $plugin_dir;
         }
     }
     $gitignore->save_ignores($ignores);
     echo "Completed successfully\n";
     return \Result\Result::ok();
 }
Ejemplo n.º 2
0
 public function create(&$force)
 {
     //
     // Does this commit have a release directory already? If so, do nothing
     //
     if (!$force && file_exists($this->release_dir)) {
         return false;
     }
     // there's no point in forcing a non-existant release
     if ($force && !file_exists($this->release_dir)) {
         $force = false;
     }
     // Got whippet.{json,lock} or plugins.lock?
     if (is_file($this->project_dir . '/whippet.json') && is_file($this->project_dir . '/whippet.lock')) {
         $factory = new \Dxw\Whippet\Factory();
         $installer = new \Dxw\Whippet\Dependencies\Installer($factory, new \Dxw\Whippet\ProjectDirectory($this->project_dir));
     } elseif ($this->plugins_lock_file && file_exists($this->plugins_lock_file)) {
         $installer = new Plugin();
     } else {
         echo "Couldn't find plugins.lock in the project directory. (Did you run whippet plugins install?)\n";
         die(1);
     }
     //
     // If we're here, we must deploy
     //
     //    1. Clone WP
     //    2. Delete wp-content etc
     //    3. Make sure wp-content is up to date
     //    4. Copy our wp-content, omitting gitfoo
     //    5. ?? Theme/plugin build steps ?? (Makefile-esque thing?)
     //    6. Symlink required files from shared dir
     // Assuming we're not forcing, create a new directory for this release, or use only an empty existing dir
     if (!$force) {
         $this->check_and_create_dir($this->release_dir, true);
     } else {
         $this->release_dir = dirname($this->release_dir) . '/forced_release_tmp_' . sha1(microtime());
     }
     // Clone WP and remove things we don't want
     $wp = new \Dxw\Whippet\Git\Git($this->release_dir);
     $wp->clone_repo($this->application_config->wordpress->repository);
     $wp->checkout($this->application_config->wordpress->revision);
     foreach (['wp-content', '.git', 'readme.html', 'wp-config-sample.php'] as $delete) {
         if (is_dir("{$this->release_dir}/{$delete}")) {
             $this->recurse_rmdir("{$this->release_dir}/{$delete}");
         } else {
             unlink("{$this->release_dir}/{$delete}");
         }
     }
     // Make sure wp-content is up to date
     $result = $installer->install(true);
     if ($result->isErr()) {
         echo sprintf("ERROR: %s\n", $result->getErr());
         exit(1);
     }
     // Copy over wp-content
     $this->recurse_copy("{$this->project_dir}/wp-content", "{$this->release_dir}/wp-content");
     if (file_exists("{$this->release_dir}/wp-content/uploads")) {
         $this->recurse_rm("{$this->release_dir}/wp-content/uploads");
     }
     //
     // Remove unwanted git/test foo
     //
     $plugins = scandir("{$this->release_dir}/wp-content/plugins");
     foreach ($plugins as $dir) {
         $path = "{$this->release_dir}/wp-content/plugins/{$dir}";
         if ($dir === '.' || $dir === '..' || !is_dir($path)) {
             continue;
         }
         // Remove git files from all plugins
         foreach (['.git', '.gitmodules', '.gitignore'] as $delete) {
             $this->recurse_rm("{$this->release_dir}/wp-content/plugins/{$dir}/{$delete}");
         }
         // Remove test files from whippet plugins
         if ($this->is_whippet_plugin($path)) {
             foreach (['tests', 'Makefile', '.drone.yml'] as $delete) {
                 $this->recurse_rm("{$this->release_dir}/wp-content/plugins/{$dir}/{$delete}");
             }
         }
     }
     //
     // Copy public assets
     //
     if (is_dir("{$this->project_dir}/public")) {
         $this->recurse_copy("{$this->project_dir}/public", "{$this->release_dir}");
     }
     //
     // TODO: theme and plugin build steps
     //
     // Symlinkery
     symlink(realpath("{$this->release_dir}/../../shared/wp-config.php"), "{$this->release_dir}/wp-config.php");
     symlink(realpath("{$this->release_dir}/../../shared/uploads"), "{$this->release_dir}/wp-content/uploads");
     // FIN
 }