Passed
Push — master ( bb706c...93a2f5 )
by Quentin
06:42
created

Build   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 6.25%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 62
c 3
b 0
f 0
dl 0
loc 141
ccs 4
cts 64
cp 0.0625
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 50 5
A copyBlocks() 0 13 3
A copyComponents() 0 13 3
A runProcessInTwill() 0 12 2
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Symfony\Component\Process\Process;
8
9
class Build extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'twill:build {--noInstall} {--hot} {--watch}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = "Build Twill assets with custom Vue components/blocks";
24
25
    /**
26
     * @var Filesystem
27
     */
28
    protected $filesystem;
29
30
    /**
31
     * @param Filesystem $filesystem
32
     */
33 55
    public function __construct(Filesystem $filesystem)
34
    {
35 55
        parent::__construct();
36
37 55
        $this->filesystem = $filesystem;
38 55
    }
39
40
    /*
41
     * Executes the console command.
42
     *
43
     * @return mixed
44
     */
45
    public function handle()
46
    {
47
        $progressBar = $this->output->createProgressBar(5);
48
        $progressBar->setFormat("%current%/%max% [%bar%] %message%");
49
50
        $npmInstall = !$this->option('noInstall');
51
52
        $progressBar->setMessage(($npmInstall ? "Installing" : "Reusing") . " npm dependencies...\n\n");
53
54
        $progressBar->start();
55
56
        if ($npmInstall) {
57
            $this->runProcessInTwill(['npm', 'ci']);
58
        } else {
59
            sleep(1);
60
        }
61
62
        $this->info('');
63
        $progressBar->setMessage("Copying custom blocks...\n\n");
64
        $progressBar->advance();
65
66
        $this->copyBlocks();
67
        sleep(1);
68
69
        $this->info('');
70
        $progressBar->setMessage("Copying custom components...\n\n");
71
        $progressBar->advance();
72
73
        $this->copyComponents();
74
        sleep(1);
75
76
        $this->info('');
77
        $progressBar->setMessage("Building assets...\n\n");
78
        $progressBar->advance();
79
80
        if ($this->option('hot')) {
81
            $this->runProcessInTwill(['npm', 'run', 'serve'], true);
82
        } elseif ($this->option('watch')) {
83
            $this->runProcessInTwill(['npm', 'run', 'watch'], true);
84
        } else {
85
            $this->runProcessInTwill(['npm', 'run', 'build']);
86
87
            $this->info('');
88
            $progressBar->setMessage("Publishing assets...\n\n");
89
            $progressBar->advance();
90
            $this->call('twill:update');
91
92
            $this->info('');
93
            $progressBar->setMessage("Done.");
94
            $progressBar->finish();
95
        }
96
    }
97
98
    /**
99
     * @return void
100
     */
101
    private function runProcessInTwill(array $command, $disableTimeout = false)
102
    {
103
        $process = new Process($command, base_path(config('twill.vendor_path')));
104
        $process->setTty(Process::isTtySupported());
105
106
        if ($disableTimeout) {
107
            $process->setTimeout(null);
108
        } else {
109
            $process->setTimeout(config('twill.build_timeout', 300));
110
        }
111
112
        $process->mustRun();
113
    }
114
115
    /**
116
     * @return void
117
     */
118
    private function copyBlocks()
119
    {
120
        $localCustomBlocksPath = resource_path(config('twill.block_editor.custom_vue_blocks_resource_path', 'assets/js/blocks'));
121
        $twillCustomBlocksPath = base_path(config('twill.vendor_path')) . '/frontend/js/components/blocks/customs';
122
123
        if (!$this->filesystem->exists($twillCustomBlocksPath)) {
124
            $this->filesystem->makeDirectory($twillCustomBlocksPath, 0755, true);
125
        }
126
127
        $this->filesystem->cleanDirectory($twillCustomBlocksPath);
128
129
        if ($this->filesystem->exists($localCustomBlocksPath)) {
130
            $this->filesystem->copyDirectory($localCustomBlocksPath, $twillCustomBlocksPath);
131
        }
132
    }
133
134
    /**
135
     * @return void
136
     */
137
    private function copyComponents()
138
    {
139
        $localCustomComponentsPath = resource_path(config('twill.custom_components_resource_path', 'assets/js/components'));
140
        $twillCustomComponentsPath = base_path(config('twill.vendor_path')) . '/frontend/js/components/customs';
141
142
        if (!$this->filesystem->exists($twillCustomComponentsPath)) {
143
            $this->filesystem->makeDirectory($twillCustomComponentsPath, 0755, true);
144
        }
145
146
        $this->filesystem->cleanDirectory($twillCustomComponentsPath);
147
148
        if ($this->filesystem->exists($localCustomComponentsPath)) {
149
            $this->filesystem->copyDirectory($localCustomComponentsPath, $twillCustomComponentsPath);
150
        }
151
    }
152
}
153