public function __construct($releases_dir, $message, $number) { $this->whippet_init(); $this->load_plugins_lock(); $git = new \Dxw\Whippet\Git\Git($this->project_dir); $this->number = $number; $this->time = date('r'); $this->deployed_commit = $git->current_commit(); $this->release_dir = "{$releases_dir}/{$this->deployed_commit}"; }
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(); }
/** * Updates plugins.lock based on the contents of the current plugins manifest. * * This method works because $this->plugins_manifest is updated as Whippet carries out plugin installations, updates and deletions. */ private function update_plugins_lock() { if (!empty($this->plugins_locked)) { $this->old_plugins_locked = $this->plugins_locked; } $this->plugins_lock_file = "{$this->project_dir}/plugins.lock"; $this->plugins_locked = new \stdClass(); foreach (scandir($this->plugin_dir) as $dir) { if ($dir[0] == '.') { continue; } if (!isset($this->plugins_manifest->{$dir})) { continue; } $git = new \Dxw\Whippet\Git\Git("{$this->plugin_dir}/{$dir}"); if (!($commit = $git->current_commit())) { echo "Unable to determine current commit; aborting\n"; exit(1); } $this->plugins_locked->{$dir} = new \stdClass(); $this->plugins_locked->{$dir}->repository = $this->plugins_manifest->{$dir}->repository; $this->plugins_locked->{$dir}->revision = $this->plugins_manifest->{$dir}->revision; $this->plugins_locked->{$dir}->commit = $commit; } return file_put_contents($this->plugins_lock_file, json_encode($this->plugins_locked, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); }