BuildPackageCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
B execute() 0 95 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Helper\ProgressBar;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Filesystem\Filesystem;
20
use Symfony\Component\Finder\Finder;
21
use Symfony\Component\Finder\SplFileInfo as SfSplFileInfo;
22
23
class BuildPackageCommand extends Command
24
{
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('build:package')
29
            ->setDescription('Packages Zikula')
30
            ->addUsage('my-buildname path/to/my/build/dir path/to/my/source/dir')
31
            ->addArgument('name', InputArgument::REQUIRED, 'Build name')
32
            ->addArgument('build-dir', InputArgument::REQUIRED, 'Build dir')
33
            ->addArgument('source-dir', InputArgument::REQUIRED, 'Source dir')
34
        ;
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $name = $input->getArgument('name');
40
        $buildDir = $input->getArgument('build-dir');
41
        $sourceDir = $input->getArgument('source-dir');
42
43
        $progress = new ProgressBar($output, 17);
44
        $progress->start();
45
46
        $filesystem = new Filesystem();
47
48
        $pwd = getcwd();
49
        if (is_dir($buildDir)) {
50
            system("rm -rf ${buildDir}");
51
        }
52
53
        // build env
54
        $filesystem->mkdir($buildDir, 0755);
55
        $progress->advance();
56
57
        $filesystem->mirror($sourceDir, "${buildDir}/${name}");
58
        $progress->advance();
59
60
        PurgeVendorsCommand::cleanVendors("${buildDir}/${name}/vendor", $progress);
61
62
        $writableArray = [
63
            "${buildDir}/${name}/var/cache",
64
            "${buildDir}/${name}/var/log",
65
            "${buildDir}/${name}/public/uploads",
66
        ];
67
        $filesystem->chmod($writableArray, 0777);
68
        $progress->advance();
69
70
        chdir($buildDir);
71
        $finder = new Finder();
72
        $finder
73
            ->in($name)
74
            ->files()
75
            ->ignoreDotFiles(false);
76
77
        $allFiles = [];
78
        /** @var SfSplFileInfo $file */
79
        foreach ($finder as $file) {
80
            $allFiles[] = $file->getRelativePathname();
81
        }
82
        $progress->advance();
83
84
        // build zip
85
        $zip = new ZipArchive();
86
        $fileName = "${name}.zip";
87
        if (true !== $zip->open($fileName, ZipArchive::CREATE)) {
88
            $output->writeln("<error>Error creating ${fileName}</error>");
89
        }
90
91
        foreach ($allFiles as $file) {
92
            $zip->addFile("${name}/${file}");
93
        }
94
        $progress->advance();
95
96
        $zip->close();
97
        $progress->advance();
98
99
        // build tar
100
        $fileName = "${name}.tar";
101
        system("tar cp ${name} > ${fileName}");
102
        $progress->advance();
103
        system("gzip ${fileName}");
104
        $progress->advance();
105
106
        // checksums
107
        $zipMd5 = md5_file("${name}.zip");
108
        $tarMd5 = md5_file("${name}.tar.gz");
109
        $zipSha1 = sha1_file("${name}.zip");
110
        $tarSha1 = sha1_file("${name}.tar.gz");
111
112
        $checksum = <<<CHECKSUM
113
-----------------md5sums-----------------
114
${zipMd5}  ${name}.zip
115
${tarMd5}  ${name}.tar.gz
116
-----------------sha1sums-----------------
117
${zipSha1}  ${name}.zip
118
${tarSha1}  ${name}.tar.gz
119
CHECKSUM;
120
        file_put_contents("${name}-checksum.txt", $checksum);
121
        $progress->advance();
122
123
        // cleanup
124
        system("rm -rf ${buildDir} ${name}");
125
        chdir($pwd);
126
        $progress->advance();
127
        $progress->finish();
128
129
        $output->writeln("<info>Artifacts built in ${buildDir}/ folder</info>");
130
131
        return 0;
132
    }
133
}
134