-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from zngly/feat/dev-env
Feat/dev env
- Loading branch information
Showing
8 changed files
with
315 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,7 @@ composer.lock | |
# OS specific | ||
.DS_Store/ | ||
|
||
wordpress | ||
wordpress | ||
|
||
node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "wp-graphql-acf-mutations", | ||
"version": "1.0.0", | ||
"description": "wp-graphql-acf-mutations", | ||
"homepage": "https://github.com/zngly/wp-graphql-acf-mutations#readme", | ||
"author": "Vlad-Anton Medves", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/zngly/wp-graphql-acf-mutations/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/zngly/wp-graphql-acf-mutations.git" | ||
}, | ||
"scripts": {}, | ||
"devDependencies": { | ||
"chokidar": "^3.5.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Zngly\ACF\Scripts; | ||
|
||
use Zngly\ACF\Scripts\Utils; | ||
|
||
|
||
class Install | ||
{ | ||
|
||
// create root_dir variable | ||
private string $root_dir; | ||
|
||
// singleton | ||
private static $instance = null; | ||
|
||
// construct | ||
public function __construct() | ||
{ | ||
$this->root_dir = dirname(dirname(__FILE__)); | ||
} | ||
|
||
public static function getInstance() | ||
{ | ||
if (self::$instance == null) | ||
self::$instance = new Install(); | ||
return self::$instance; | ||
} | ||
|
||
public static function run() | ||
{ | ||
|
||
self::getInstance()->wordpress(); | ||
self::getInstance()->npm(); | ||
} | ||
|
||
private function wordpress() | ||
{ | ||
self::print("Installing Wordpress...\n"); | ||
|
||
// install wordpress | ||
$command = "cd {$this->root_dir} && composer install --prefer-dist --no-interaction"; | ||
echo "running: " . $command . "\n"; | ||
$output = shell_exec($command); | ||
echo $output; | ||
|
||
self::$instance->modify_wordpress(); | ||
|
||
self::print("Wordpress finished.\n"); | ||
} | ||
|
||
private function modify_wordpress() | ||
{ | ||
self::print("Modifying Wordpress...\n"); | ||
|
||
$wordpress_dir = $this->root_dir . '/wordpress'; | ||
$wp_config_path = $wordpress_dir . '/wp-config-sample.php'; | ||
|
||
Utils::add_to_file($wp_config_path, "if(!defined('ABSPATH'))", 'require ABSPATH . "vendor/autoload.php";'); | ||
|
||
// delete all themes except twentytwentytwo | ||
$themes_dir = $wordpress_dir . '/wp-content/themes'; | ||
$themes = scandir($themes_dir); | ||
foreach ($themes as $theme) { | ||
if ($theme != '.' && $theme != '..' && $theme != 'twentytwentytwo') { | ||
$theme_path = $themes_dir . '/' . $theme; | ||
Utils::delete_dir($theme_path); | ||
} | ||
} | ||
|
||
Utils::copy_plugin(); | ||
} | ||
|
||
private function npm() | ||
{ | ||
self::print("Installing NPM...\n"); | ||
|
||
// install npm | ||
$command = "cd {$this->root_dir} && npm install --no-save"; | ||
echo "running: " . $command . "\n"; | ||
$output = shell_exec($command); | ||
echo $output; | ||
} | ||
|
||
public static function print(string $message) | ||
{ | ||
echo "\n" . $message . "\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Zngly\ACF\Scripts; | ||
|
||
class Utils | ||
{ | ||
/** | ||
* Modifies a file by adding a string if a match is found. | ||
* The string added is added two lines before the match. | ||
* | ||
* @param string $file_path | ||
* @param string $match_string - the match will be compared without any spaces | ||
* @param string $replace_string | ||
*/ | ||
public static function add_to_file(string $file_path, string $match_str, string $replace_str) | ||
{ | ||
// update file | ||
$lines = file($file_path); | ||
|
||
// check for error, throw error if file is not found | ||
if (!$lines) | ||
throw new \Exception("File not found: " . $file_path); | ||
|
||
|
||
$new_config = []; | ||
|
||
foreach ($lines as $key => $value) { | ||
$trim_val = preg_replace('/\s+/', '', $value); | ||
$trim_replace = preg_replace('/\s+/', '', $replace_str); | ||
|
||
// if the replace string exists, exit gracefully | ||
if (strpos($trim_val, $trim_replace) !== false) | ||
return; | ||
|
||
// we add our code before this line | ||
if (strpos($trim_val, $match_str) !== false) | ||
$new_config[] = "{$replace_str}\n\n"; | ||
|
||
// add the new line | ||
$new_config[] = $value; | ||
} | ||
|
||
$new_file_content = implode("", $new_config); | ||
file_put_contents($file_path, $new_file_content); | ||
} | ||
|
||
// delete a directory and all its contents | ||
public static function delete_dir(string $dir) | ||
{ | ||
if (is_dir($dir)) { | ||
$objects = scandir($dir); | ||
foreach ($objects as $object) { | ||
if ($object != "." && $object != "..") { | ||
if (filetype($dir . "/" . $object) == "dir") | ||
self::delete_dir($dir . "/" . $object); | ||
else | ||
unlink($dir . "/" . $object); | ||
} | ||
} | ||
reset($objects); | ||
rmdir($dir); | ||
} | ||
} | ||
|
||
public static function copy_plugin() | ||
{ | ||
// get the directory to watch | ||
$src_folder = dirname(dirname(__FILE__)) . '/src'; | ||
|
||
// get all the files in the directory recursively | ||
$plugin_name = self::get_plugin_name(); | ||
$plugin_folder = self::get_root_dir() . "/wordpress/wp-content/plugins/{$plugin_name}"; | ||
|
||
// if the plugin folder doesn't exist, create it | ||
if (!file_exists("{$plugin_folder}")) | ||
mkdir("{$plugin_folder}/", 0777, true); | ||
|
||
// copy plugin_idx to the plugins directory | ||
copy(self::get_root_dir() . "/{$plugin_name}.php", "{$plugin_folder}/{$plugin_name}.php"); | ||
|
||
// copy the src folder and the contents recursively to the plugins directory | ||
self::copy_folder($src_folder . '/', $plugin_folder . '/src'); | ||
} | ||
|
||
// function to copy a folder recursively | ||
public static function copy_folder($src, $dest) | ||
{ | ||
$dir = opendir($src); | ||
@mkdir($dest); | ||
while (false !== ($file = readdir($dir))) | ||
if (($file != '.') && ($file != '..')) | ||
if (is_dir($src . '/' . $file)) self::copy_folder($src . '/' . $file, $dest . '/' . $file); | ||
else copy($src . '/' . $file, $dest . '/' . $file); | ||
closedir($dir); | ||
} | ||
|
||
|
||
public static function get_root_dir() | ||
{ | ||
return dirname(dirname(__FILE__)); | ||
} | ||
|
||
public static function get_root_name() | ||
{ | ||
return basename(dirname(dirname(__FILE__))); | ||
} | ||
|
||
public static function get_plugin_name() | ||
{ | ||
return basename(dirname(dirname(__FILE__))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Zngly\ACF\Scripts; | ||
|
||
require dirname(dirname(__FILE__)) . '/wordpress/vendor/autoload.php'; | ||
|
||
use Spatie\Watcher\Watch; | ||
|
||
|
||
// class will watch a directory for changes and run a command when a file is added or modified | ||
class Watcher | ||
{ | ||
|
||
public static function run() | ||
{ | ||
// get the directory to watch | ||
$src_folder = dirname(dirname(__FILE__)) . '/src'; | ||
|
||
// get all the files in the directory recursively | ||
$plugin_name = Utils::get_plugin_name(); | ||
$plugin_folder = Utils::get_root_dir() . "/wordpress/wp-content/plugins/{$plugin_name}"; | ||
|
||
// watch for any file changes in the directory | ||
self::watch_dirs([$src_folder, "{$plugin_folder}/{$plugin_name}.php"]); | ||
} | ||
|
||
|
||
|
||
// function to watch a directory for changes | ||
public static function watch_dirs(array $dirs) | ||
{ | ||
$root_dir = Utils::get_root_dir(); | ||
$plugins_folder = $root_dir . "/wordpress/wp-content/plugins/" . Utils::get_plugin_name(); | ||
|
||
Watch::paths(...$dirs) | ||
->onAnyChange(function (string $type, string $path) use ($root_dir, $plugins_folder) { | ||
$path_parts = explode($root_dir, $path); | ||
$new_path = $plugins_folder . $path_parts[1]; | ||
|
||
switch ($type) { | ||
case Watch::EVENT_TYPE_DIRECTORY_CREATED: | ||
mkdir($new_path, 0777, true); | ||
break; | ||
case Watch::EVENT_TYPE_DIRECTORY_DELETED: | ||
rmdir($new_path); | ||
break; | ||
case Watch::EVENT_TYPE_FILE_DELETED: | ||
unlink($new_path); | ||
break; | ||
case Watch::EVENT_TYPE_FILE_CREATED: | ||
case Watch::EVENT_TYPE_FILE_UPDATED: | ||
copy($path, $new_path); | ||
break; | ||
|
||
default: | ||
echo "Event Happened: {$type} - from {$path} - to {$new_path}\n"; | ||
break; | ||
} | ||
}) | ||
->start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters