Completed
Pull Request — master (#6)
by Tim
06:47
created

RoboFile::createPhar()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 50
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 9.3333
c 0
b 0
f 0
cc 3
eloc 23
nc 4
nop 0
1
<?php
2
3
/**
4
 * RoboFile.php
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
use Lurker\Event\FilesystemEvent;
22
23
use Symfony\Component\Finder\Finder;
24
25
/**
26
 * Defines the available build tasks.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-cli-simple
32
 * @link      http://www.techdivision.com
33
 */
34
class RoboFile extends \Robo\Tasks
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
35
{
36
37
    /**
38
     * The build properties.
39
     *
40
     * @var array
41
     */
42
    protected $properties = array(
43
        'base.dir' => __DIR__,
44
        'src.dir' => __DIR__ . '/src',
45
        'dist.dir' => __DIR__ . '/dist',
46
        'vendor.dir' => __DIR__ . '/vendor',
47
        'target.dir' => __DIR__ . '/target',
48
        'webapp.name' => 'import-cli-simple',
49
        'webapp.version' => '1.0.0-alpha4'
50
    );
51
52
    /**
53
     * Run's the composer install command.
54
     *
55
     * @return void
56
     */
57
    public function composerInstall()
58
    {
59
        // optimize autoloader with custom path
60
        $this->taskComposerInstall()
61
             ->preferDist()
62
             ->optimizeAutoloader()
63
             ->run();
64
    }
65
66
    /**
67
     * Run's the composer update command.
68
     *
69
     * @return void
70
     */
71
    public function composerUpdate()
72
    {
73
        // optimize autoloader with custom path
74
        $this->taskComposerUpdate()
75
             ->preferDist()
76
             ->optimizeAutoloader()
77
             ->run();
78
    }
79
80
    /**
81
     * Clean up the environment for a new build.
82
     *
83
     * @return void
84
     */
85
    public function clean()
86
    {
87
        $this->taskDeleteDir($this->properties['target.dir'])->run();
88
    }
89
90
    /**
91
     * Prepare's the environment for a new build.
92
     *
93
     * @return void
94
     */
95
    public function prepare()
96
    {
97
        $this->taskFileSystemStack()
98
             ->mkdir($this->properties['dist.dir'])
99
             ->mkdir($this->properties['target.dir'])
100
             ->mkdir(sprintf('%s/reports', $this->properties['target.dir']))
101
             ->run();
102
    }
103
104
    /**
105
     * Creates the a PHAR archive from the sources.
106
     *
107
     * @return void
108
     */
109
    public function createPhar()
110
    {
111
112
        // prepare the PHAR archive name
113
        $archiveName = sprintf(
114
            '%s/%s.phar',
115
            $this->properties['target.dir'],
116
            $this->properties['webapp.name']
117
        );
118
119
        // prepare the task
120
        $pharTask = $this->taskPackPhar($archiveName)
121
            ->compress()
122
            ->stub('stub.php');
123
124
        // load a list with all the source files
125
        $finder = Finder::create()
126
            ->name('*.php')
127
            ->in($this->properties['src.dir']);
128
129
        // iterate over the source files and add them to the PHAR archive
130
        foreach ($finder as $file) {
131
            $pharTask->addFile('src/' . $file->getRelativePathname(), $file->getRealPath());
132
        }
133
134
        // load a list with all the source files from the vendor directory
135
        $finder = Finder::create()->files()
136
            ->name('*.php')
137
            ->in($this->properties['vendor.dir']);
138
139
        // iterate over the source files of the vendor directory and add them to the PHAR archive
140
        foreach ($finder as $file) {
141
            $pharTask->addStripped('vendor/' . $file->getRelativePathname(), $file->getRealPath());
142
        }
143
144
        // create the PHAR archive
145
        $pharTask->run();
146
147
        // verify PHAR archive is packed correctly
148
        $this->_exec(sprintf('php %s', $archiveName));
149
150
        // prepare the PHAR archive distribution name
151
        $distArchiveName = sprintf('%s/%s.phar', $this->properties['dist.dir'], $this->properties['webapp.name']);
152
153
        // clean up the dist directory
154
        $this->taskCleanDir($this->properties['dist.dir'])->run();
155
156
        // copy the latest PHAR archive to the dist directory
157
        $this->taskFilesystemStack()->copy($archiveName, $distArchiveName)->run();
158
    }
159
160
    /**
161
     * Run's the PHPMD.
162
     *
163
     * @return void
164
     */
165 View Code Duplication
    public function runMd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
168
        // run the mess detector
169
        $this->_exec(
170
            sprintf(
171
                '%s/bin/phpmd %s xml phpmd.xml --reportfile %s/reports/pmd.xml --ignore-violations-on-exit',
172
                $this->properties['vendor.dir'],
173
                $this->properties['src.dir'],
174
                $this->properties['target.dir']
175
            )
176
        );
177
    }
178
179
    /**
180
     * Run's the PHPCPD.
181
     *
182
     * @return void
183
     */
184 View Code Duplication
    public function runCpd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186
187
        // run the copy past detector
188
        $this->_exec(
189
            sprintf(
190
                '%s/bin/phpcpd %s --log-pmd %s/reports/pmd-cpd.xml',
191
                $this->properties['vendor.dir'],
192
                $this->properties['src.dir'],
193
                $this->properties['target.dir']
194
            )
195
        );
196
    }
197
198
    /**
199
     * Run's the PHPCodeSniffer.
200
     *
201
     * @return void
202
     */
203 View Code Duplication
    public function runCs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
206
        // run the code sniffer
207
        $this->_exec(
208
            sprintf(
209
                '%s/bin/phpcs -n --extensions=php --standard=psr2 --report-full --report-checkstyle=%s/reports/phpcs.xml %s',
210
                $this->properties['vendor.dir'],
211
                $this->properties['target.dir'],
212
                $this->properties['src.dir']
213
            )
214
        );
215
    }
216
217
    /**
218
     * Run's the PHPUnit tests.
219
     *
220
     * @return void
221
     */
222
    public function runTests()
223
    {
224
225
        // run PHPUnit
226
        $this->taskPHPUnit(sprintf('%s/bin/phpunit', $this->properties['vendor.dir']))
227
             ->configFile('phpunit.xml')
228
             ->run();
229
    }
230
231
    /**
232
     * The complete build process.
233
     *
234
     * @return void
235
     */
236
    public function build()
237
    {
238
        $this->clean();
239
        $this->prepare();
240
        $this->runCs();
241
        $this->runCpd();
242
        $this->runMd();
243
        $this->runTests();
244
    }
245
}
246