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

Cecilapp / Cecil / 13135572963

04 Feb 2025 12:35PM UTC coverage: 83.508% (+0.04%) from 83.465%
13135572963

push

github

ArnaudLigny
refactor: static files copy

16 of 23 new or added lines in 1 file covered. (69.57%)

2 existing lines in 1 file now uncovered.

2952 of 3535 relevant lines covered (83.51%)

0.84 hits per line

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

76.6
/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)
UNCOV
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
        // copying mounts
87
        if ($this->config->get('static.mounts')) {
1✔
88
            foreach ($this->config->get('static.mounts') as $source => $destination) {
1✔
89
                $this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination);
1✔
90
            }
91
        }
92

93
        if ($this->count === 0) {
1✔
UNCOV
94
            $this->builder->getLogger()->info('Nothing to copy');
×
95

96
            return;
×
97
        }
98
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
1✔
99
    }
100

101
    /**
102
     * Copying a file or files in a directory from $from (if exists) to $to (relative to output path).
103
     * Exclude files or directories with $exclude array.
104
     */
105
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): void
106
    {
107
        try {
108
            if (Util\File::getFS()->exists($from)) {
1✔
109
                // copy a file
110
                if (is_file($from)) {
1✔
111
                    Util\File::getFS()->copy($from, Util::joinFile($this->config->getOutputPath(), $to), true);
1✔
112

113
                    return;
1✔
114
                }
115
                // copy a directory
116
                $finder = Finder::create()
1✔
117
                    ->files()
1✔
118
                    ->in($from)
1✔
119
                    ->ignoreDotFiles(false);
1✔
120
                // exclude files or directories
121
                if (\is_array($exclude)) {
1✔
122
                    $finder->notPath($exclude);
1✔
123
                    $finder->notName($exclude);
1✔
124
                }
125
                $this->count += $finder->count();
1✔
126
                Util\File::getFS()->mirror(
1✔
127
                    $from,
1✔
128
                    Util::joinFile($this->config->getOutputPath(), $to ?? ''),
1✔
129
                    $finder,
1✔
130
                    ['override' => true]
1✔
131
                );
1✔
132
            }
NEW
133
        } catch (\Exception $e) {
×
NEW
134
            throw new RuntimeException(\sprintf('Error during static files copy: %s', $e->getMessage()));
×
135
        }
136
    }
137
}
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

© 2026 Coveralls, Inc