From 5af63f771adbc1fae67444ec1f1966f2441bcf60 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Wed, 8 Dec 2021 02:36:46 +0100 Subject: [PATCH] Added init command, improved plugin api --- .gitignore | 2 +- runtime/SparkPlug.php | 27 +++++++++++++++++++++++ src/Commands/InitCommand.php | 42 ++++++++++++++++++++++++++++++++++++ src/SparkApplication.php | 1 + 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/Commands/InitCommand.php diff --git a/.gitignore b/.gitignore index b385f21..741dfee 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ /.phpunit.cache /*.phar /.spark -/.spark.json +/.spark.json \ No newline at end of file diff --git a/runtime/SparkPlug.php b/runtime/SparkPlug.php index 4bf0671..1f0fb83 100644 --- a/runtime/SparkPlug.php +++ b/runtime/SparkPlug.php @@ -1,6 +1,33 @@ getPluginManager()->getPlugin($name); + } + + function get_resource(string $name) { + return SparkApplication::$instance->getResourceManager()->getNamedResource($name); + } + + function read_config($file=null) { + if (!$file) return; + $abs = get_environment()->getConfigDirectory() . "/" . $file; + if (!file_exists($abs)) { + //fprintf(STDERR, "warning: Can't read config file %s\n", $abs); + return []; + } + return (array)json_decode(file_get_contents($abs), true); + } + + function getProjectDirectory() + { + return SparkApplication::$instance->getEnvironment()->getProjectDirectory(); + } + } \ No newline at end of file diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php new file mode 100644 index 0000000..2dee991 --- /dev/null +++ b/src/Commands/InitCommand.php @@ -0,0 +1,42 @@ +writeln("Spark already initialized?"); + return Command::FAILURE; + } + + mkdir($configDir); + mkdir($configDir."/plugins"); + file_put_contents($configDir."/spark.json", json_encode([ + 'preload' => [ + './.spark/plugins/*' + ], + 'scripts' => [ + 'hello' => "echo 'Hello World\n'" + ] + ], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); + + $output->writeln("Initialized new spark in ".getcwd()."/.spark"); + + return Command::SUCCESS; + } +} diff --git a/src/SparkApplication.php b/src/SparkApplication.php index 7f20177..d2542d6 100644 --- a/src/SparkApplication.php +++ b/src/SparkApplication.php @@ -37,6 +37,7 @@ class SparkApplication extends Application $this->add(new Commands\RunCommand()); $this->add(new Commands\ResourcesCommand()); $this->add(new Commands\ReplCommand()); + $this->add(new Commands\InitCommand()); }