• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / plugin / 9065254634

13 May 2024 03:01PM UTC coverage: 43.355%. Remained the same
9065254634

push

github

twinh
feat(plugin): `g:plugin` 标题为空时根据插件 id 生成标题,id 传入 snake 格式时转换为 dash 格式

0 of 3 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

1256 of 2897 relevant lines covered (43.36%)

7.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Command/GPlugin.php
1
<?php
2

3
namespace Miaoxing\Plugin\Command;
4

5
use Miaoxing\Plugin\BasePlugin;
6
use Miaoxing\Plugin\Service\Fs;
7
use Nette\PhpGenerator\PhpFile;
8
use Nette\PhpGenerator\PsrPrinter;
9
use Symfony\Component\Console\Input\InputArgument;
10

11
/**
12
 * @mixin \PluginMixin
13
 * @mixin \StrMixin
14
 */
15
class GPlugin extends BaseCommand
16
{
17
    protected function configure()
18
    {
19
        $this->setDescription('Generate a plugin')
×
20
            ->addArgument('id', InputArgument::REQUIRED, 'The id of the plugin')
×
21
            ->addArgument('name', InputArgument::OPTIONAL, 'The name of the plugin', '');
×
22
    }
23

24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function handle()
28
    {
NEW
29
        $camelId = $this->str->camel($this->getArgument('id'));
×
NEW
30
        $dashId = $this->str->dash($camelId);
×
UNCOV
31
        $pascalId = ucfirst($camelId);
×
32

NEW
33
        $name = $this->getArgument('name') ?: ucwords(strtr($dashId, '-', ' '));
×
34

35
        if ($this->plugin->has($dashId)) {
×
36
            return $this->err(sprintf('Plugin "%s" exists', $dashId));
×
37
        }
38

39
        $printer = new PsrPrinter();
×
40
        $phpFile = new PhpFile();
×
41

42
        $class = $phpFile->addClass('Miaoxing\\' . $pascalId . '\\' . $pascalId . 'Plugin');
×
43
        $class->addExtend(BasePlugin::class);
×
44
        $class->addProperty('name', $name)->setProtected();
×
45
        $class->addProperty('description', '')->setProtected();
×
46

47
        $file = 'plugins/' . $dashId . '/src/' . $pascalId . 'Plugin.php';
×
48
        $content = $printer->printFile($phpFile);
×
49
        $this->createFile($file, $content);
×
50

51
        $replaces = [
×
52
            'composer.json' => [
×
53
                ['dash-id', 'PascalId'],
×
54
                [$dashId, $pascalId],
×
55
            ],
×
56
            'package.json' => [
×
57
                ['dash-id'],
×
58
                [$dashId],
×
59
            ],
×
60
            'README.md' => [
×
61
                ['Name'],
×
62
                [$name],
×
63
            ],
×
64
        ];
×
65

66
        $stubDir = $this->plugin->getById('plugin')->getBasePath() . '/stubs/plugin';
×
67
        $targetDir = 'plugins/' . $dashId;
×
68
        $this->createFiles($stubDir, $targetDir, $replaces);
×
69

70
        $this->plugin->getConfig(true);
×
71

72
        return $this->suc('创建成功');
×
73
    }
74

75
    protected function createFiles($stubDir, $targetDir, $replaces)
76
    {
77
        Fs::ensureDir($targetDir);
×
78
        $files = array_diff(scandir($stubDir), ['..', '.']);
×
79
        foreach ($files as $file) {
×
80
            $source = $stubDir . '/' . $file;
×
81
            $target = $targetDir . '/' . $file;
×
82
            if (isset($replaces[$file])) {
×
83
                $content = file_get_contents($source);
×
84
                $content = str_replace($replaces[$file][0], $replaces[$file][1], $content);
×
85
                $this->createFile($target, $content);
×
86
            } else {
87
                $this->suc('生成文件 ' . $target);
×
88
                copy($source, $target);
×
89
            }
90
        }
91
    }
92

93
    protected function createFile($file, $content)
94
    {
95
        $this->suc('生成文件 ' . $file);
×
96

97
        Fs::ensureDir(dirname($file));
×
98

99
        file_put_contents($file, $content);
×
100
        chmod($file, 0777);
×
101
    }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc