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

Cecilapp / Cecil / 13056401730

30 Jan 2025 04:10PM UTC coverage: 83.475%. First build
13056401730

Pull #2105

github

web-flow
Merge 10f35a648 into 542b05337
Pull Request #2105: refactor: better files copy

13 of 20 new or added lines in 1 file covered. (65.0%)

2945 of 3528 relevant lines covered (83.48%)

0.83 hits per line

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

72.5
/src/Step/StaticFiles/Copy.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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
namespace Cecil\Step\StaticFiles;
15

16
use Cecil\Exception\RuntimeException;
17
use Cecil\Step\AbstractStep;
18
use Cecil\Util;
19
use Symfony\Component\Finder\Finder;
20

21
/**
22
 * Copying static files to site root.
23
 */
24
class Copy extends AbstractStep
25
{
26
    protected $count = 0;
27

28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getName(): string
32
    {
33
        return 'Copying static';
1✔
34
    }
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function init(array $options): void
40
    {
41
        if ($options['dry-run']) {
1✔
42
            return;
×
43
        }
44

45
        // reset output directory
46
        Util\File::getFS()->remove($this->config->getOutputPath());
1✔
47
        Util\File::getFS()->mkdir($this->config->getOutputPath());
1✔
48

49
        $this->canProcess = true;
1✔
50
    }
51

52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function process(): void
56
    {
57
        $target = $this->config->get('static.target');
1✔
58
        $exclude = $this->config->get('static.exclude');
1✔
59

60
        // copying assets in debug mode (for source maps)
61
        if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) {
1✔
62
            // copying content of '<theme>/assets/' dir if exists
NEW
63
            if ($this->config->hasTheme()) {
×
NEW
64
                $themes = array_reverse($this->config->getTheme());
×
NEW
65
                foreach ($themes as $theme) {
×
NEW
66
                    $this->copy($this->config->getThemeDirPath($theme, 'assets'));
×
67
                }
68
            }
69
            // copying content of 'assets/' dir if exists
NEW
70
            $this->copy($this->config->getAssetsPath());
×
71
            // cancel exclusion for static files (see below)
72
            $exclude = [];
×
73
        }
74

75
        // copying content of '<theme>/static/' dir if exists
76
        if ($this->config->hasTheme()) {
1✔
77
            $themes = array_reverse($this->config->getTheme());
1✔
78
            foreach ($themes as $theme) {
1✔
79
                $this->copy($this->config->getThemeDirPath($theme, 'static'), $target, $exclude);
1✔
80
            }
81
        }
82

83
        // copying content of 'static/' dir if exists
84
        $this->copy($this->config->getStaticPath(), $target, $exclude);
1✔
85

86
        if ($this->count === 0) {
1✔
87
            $this->builder->getLogger()->info('Nothing to copy');
×
88

89
            return;
×
90
        }
91
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
1✔
92
    }
93

94
    /**
95
     * Copying (mirror) files from $from to static/$to directory if $from path exists.
96
     *
97
     * @throws RuntimeException
98
     */
99
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): void
100
    {
101
        if (Util\File::getFS()->exists($from)) {
1✔
102
            try {
103
                // count...
104
                $finder = Finder::create()
1✔
105
                    ->files()
1✔
106
                    ->in($from)
1✔
107
                    ->ignoreDotFiles(false);
1✔
108
                if (\is_array($exclude)) {
1✔
109
                    $finder->notPath($exclude)->notName($exclude);
1✔
110
                }
111
                $this->count += $finder->count();
1✔
112
                // ...and copy
113
                Util\File::getFS()->mirror(
1✔
114
                    $from,
1✔
115
                    Util::joinFile($this->config->getOutputPath(), $to ?? ''),
1✔
116
                    $finder,
1✔
117
                    ['override' => true]
1✔
118
                );
1✔
NEW
119
            } catch (\Exception $e) {
×
NEW
120
                throw new RuntimeException(\sprintf('Error during static files copy: %s', $e->getMessage()));
×
121
            }
122
        }
123
    }
124
}
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