Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Nov 3, 2024
2 parents d9ef81a + 44a38bc commit b5d0d21
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 20 deletions.
70 changes: 66 additions & 4 deletions App/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,57 @@ public function getConfig($prop = null, $quiet = true)
return $config;
}

public function goodbye(): void
{
// return a random goodbye message
$goodbyeMessages = [
"Have a nice day!",
"Goodbye!",
"See you later!",
"Take care!",
"Catch you later!",
"Farewell!",
"Bye for now!",
"Until next time!",
"Peace out!",
"Stay safe!",
"Adios!",
"Ciao!",
"Sayonara!",
"Au revoir!",
"Toodle-oo!",
"Cheerio!",
"Later, alligator!",
"After a while, crocodile!",
"Keep it real!",
"Stay classy!",
"Happy coding!",
"May the force be with you!",
"Keep calm and code on!",
"May your code be bug-free!",
"404: Goodbye message not found! No worries, it's a joke ;)",
"See you on the flip side!",
"Don't let the semicolon bite you!",
"Keep on hacking!",
"May your coffee be strong and your code be short!",
"Happy debugging!",
"Keep pull requests coming!",
"May your code compile on the first try!",
"Stay DRY!",
"Keep your code clean!",
"May your functions be pure and your variables immutable!",
"Don't let the bugs bite you!",
"Keep your codebase tidy!",
"May your tests always pass!",
"May your code be as efficient as a well-oiled machine!",
"Don't forget to take breaks!",
"Keep your code simple and your logic sound!",
"May your algorithms be swift and your data structures robust!"
];
// return a random goodbye message
$this->write($goodbyeMessages[array_rand($goodbyeMessages)]);
}

/**
* Execute this command
*/
Expand Down Expand Up @@ -373,12 +424,23 @@ public function stub($file)
* Populate stub placeholders
* @return void
*/
public function stubPopulate($src, $dst, $vars = [])
{
public function stubPopulate(
$src,
$dst,
$vars = [],
$quiet = false,
$brackets = "{}"
) {
$src = realpath($src);
$content = file_get_contents($src);
$this->write("Writing $src to $dst");
if (!$quiet) {
$this->write("Writing $src");
$this->write(" to $dst");
}
$left = @$brackets[0];
$right = @$brackets[1];
foreach ($vars as $k => $v) {
$content = str_replace("{" . $k . "}", $v, $content);
$content = str_replace($left . $k . $right, $v, $content);
}
file_put_contents($dst, $content);
}
Expand Down
114 changes: 114 additions & 0 deletions App/Commands/ModuleCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace RockShell;

use Symfony\Component\Console\Input\InputOption;

use function ProcessWire\wire;

/**
* Create a new module with all necessary files
*/
class ModuleCreate extends Command
{

public function config()
{
$this->setDescription("Create a new module with all necessary files")
->addOption('name', null, InputOption::VALUE_OPTIONAL, "Module name");
}

private function copyFiles($name): void
{
$src = __DIR__ . '/../stubs/module-create';
$dst = wire()->config->paths->siteModules . $name;
$replace = [
"MyModule" => $name,
"mymodule" => strtolower($name),
];
wire()->files->mkdir($dst);
wire()->files->copy(
$src . '/.github',
$dst . '/.github',
);
wire()->files->copy(
$src . '/package.json',
$dst,
);
wire()->files->copy(
$src . '/.htaccess',
$dst,
);
$this->stubPopulate(
$src . '/MyModule.info.php',
$dst . "/$name.info.php",
$replace,
quiet: true,
brackets: false,
);
$this->stubPopulate(
$src . '/MyModule.module.txt',
$dst . "/$name.module.php",
$replace,
quiet: true,
brackets: false,
);
$this->stubPopulate(
$src . '/readme.md',
$dst . "/readme.md",
$replace,
quiet: true,
brackets: false,
);
}

private function getDir($name): string
{
return wire()->config->paths->siteModules . $name;
}

public function handle()
{
$types = ['Module', 'Process Module', 'Fieldtype Module', 'Inputfield Module'];
$type = $this->choice('Type of module', $types, 0);
if ($type !== 'Module') {
$this->warn("Sorry, not implemented yet. What a great opportunity for a PR!");
return $this->handle();
}
$name = $this->moduleName();
$this->copyFiles($name);

$dir = $this->getDir($name);
$this->success("Module created at $dir");
$this->goodbye();

return self::SUCCESS;
}

private function moduleName($name = null, $reset = false)
{
if (!$name) $name = $this->option('name');
if (!$name || $reset) $name = $this->ask("Please enter your module's name");
$name = ucfirst(wire()->sanitizer->camelCase($name));

if (!$name) return $this->moduleName(reset: $reset);

if (!$this->confirm("Confirm module name: $name", true)) {
return $this->moduleName();
}

// check if it exists
$dir = $this->getDir($name);
if (is_dir($dir)) {
$this->warn("$dir already exists");

if ($this->confirm("Create module in this folder? This will overwrite existing files.")) {
return $name;
}

return $this->moduleName(reset: true);
}

return $name;
}
}
39 changes: 39 additions & 0 deletions App/Commands/ModuleInstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace RockShell;

use Symfony\Component\Console\Input\InputOption;

use function ProcessWire\wire;

/**
* Install a module
*/
class ModuleInstall extends Command
{

public function config()
{
$this->setDescription("Install a module")
->addOption('name', 'm', InputOption::VALUE_OPTIONAL, "Module name");
}

public function handle()
{
// load RockMigrations
$rm = wire()->modules->get('RockMigrations');
if (!$rm) {
$this->error("RockMigrations module not found");
return self::FAILURE;
}

// check name
$name = $this->option('name');
while (!$name) $name = $this->ask("Please enter the module's name");

// install module
$rm->installModule($name);

return self::SUCCESS;
}
}
43 changes: 27 additions & 16 deletions App/Commands/PwInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,29 +439,40 @@ public function getStep()

public function host($site)
{
$defaulthost = getenv('DDEV_PROJECT')
? getenv('DDEV_PROJECT') . ".ddev.site"
: "example.com";
$defaulthost = getenv('DDEV_PROJECT') ? getenv('DDEV_PROJECT') . ".ddev.site" : "example.com";
$site = ltrim($site, "/");
$checkHTTP = !$this->host;
$host = $this->host ?: $this->option('host')
?: $this->ask('Enter host', $defaulthost);
$host = $this->host ?: $this->option('host') ?: $this->ask('Enter host', $defaulthost);
$this->host = $host;

// check if host is reachable via HTTP
if ($checkHTTP) {
$this->browser->request("GET", $host);
// Ports from environment
$httpPort = getenv('DDEV_ROUTER_HTTP_PORT');
$httpsPort = getenv('DDEV_ROUTER_HTTPS_PORT');

$urlsToCheck = [];
if (parse_url($host, PHP_URL_PORT) === null) { // No port in host
if ($httpsPort) $urlsToCheck[] = "https://$host:$httpsPort";
if ($httpPort) $urlsToCheck[] = "http://$host:$httpPort";
} else {
$urlsToCheck = ["https://$host", "http://$host"];
}

foreach ($urlsToCheck as $url) {
$this->browser->request("GET", $url);
$status = $this->browser->getInternalResponse()->getStatusCode();
if ($status !== 404) {
$this->error("Your host $host must be reachable via HTTP!");
$this->error("When using DDEV make sure it is running ");
exit(1);
} else {
$this->success("HTTP status check for host $host was OK");
if ($status === 200) {
$this->success("Status check for host $url was OK");
return "$url/$site";
}
if ($status === 403) {
$this->success("Status check for host $url was OK");
$this->warn("Access is forbidden (403). This may be expected during installation.");
return "$url/$site";
}
}

return "http://$host/$site";
$this->error("Your host $host must be reachable via HTTP or HTTPS!");
$this->error("When using DDEV make sure it is running.");
exit(1);
}

public function writeNotes()
Expand Down
29 changes: 29 additions & 0 deletions App/stubs/module-create/.github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Releases
on:
push:
branches:
- main

jobs:
changelog:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: conventional Changelog Action
id: changelog
uses: TriPSs/conventional-changelog-action@v5
with:
preset: "conventionalcommits"
github-token: ${{ secrets.github_token }}
# git-user-email: "[email protected]"

- name: create release
uses: actions/create-release@v1
if: ${{ steps.changelog.outputs.skipped == 'false' }}
env:
GITHUB_TOKEN: ${{ secrets.github_token }}
with:
tag_name: ${{ steps.changelog.outputs.tag }}
release_name: ${{ steps.changelog.outputs.tag }}
body: ${{ steps.changelog.outputs.clean_changelog }}
11 changes: 11 additions & 0 deletions App/stubs/module-create/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# block public access to all files
# add exceptions as needed
<IfModule mod_rewrite.c>
RewriteEngine On

# Example to allow access to js/css files in dst folder
# RewriteCond %{REQUEST_FILENAME} !^.*/dst/.*\.(js|css)$

# 403 forbidden
RewriteRule ^ - [F,L]
</IfModule>
15 changes: 15 additions & 0 deletions App/stubs/module-create/MyModule.info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ProcessWire;

$info = [
'title' => 'MyModule',
'version' => json_decode(file_get_contents(__DIR__ . "/package.json"))->version,
'summary' => '',
'autoload' => true,
'singular' => true,
'icon' => 'check',
'requires' => [
'PHP>=8.1',
],
];
23 changes: 23 additions & 0 deletions App/stubs/module-create/MyModule.module.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ProcessWire;

function mymodule(): MyModule
{
return wire()->modules->get('MyModule');
}

// info / infoc
class MyModule extends WireData implements Module, ConfigurableModule
{
public function init() {}

/**
* Config inputfields
* @param InputfieldWrapper $inputfields
*/
public function getModuleConfigInputfields($inputfields)
{
return $inputfields;
}
}
3 changes: 3 additions & 0 deletions App/stubs/module-create/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "0.0.1"
}
2 changes: 2 additions & 0 deletions App/stubs/module-create/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# MyModule

0 comments on commit b5d0d21

Please sign in to comment.