Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TravisYmlView.php « CoreConsole « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9190f220ad863943179cfa5afa5fafca577a1ac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Plugins\CoreConsole;

use Piwik\View;
use Symfony\Component\Console\Output\OutputInterface;
use Exception;

/**
 * View class for the travis.yml.twig template file. Generates the contents for a .travis.yml file.
 */
class TravisYmlView extends View
{
    /**
     * The .travis.yml section names that are overwritten by this command.
     * 
     * @var string[]
     */
    private static $travisYmlSectionNames = array(
        'php',
        'language',
        'script',
        'before_install',
        'install',
        'before_script',
        'after_script',
        'after_success'
    );

    /**
     * The names of .travis.yml sections that can be extended w/ custom steps by plugins. Twig templates
     * in the plugins/PluginName/tests/travis directory can be used to insert travis commands at the
     * beginning or end of a section. For example, before_install.before.yml will add steps
     * at the beginning of the before_install: section.
     *
     * @var string[]
     */
    private static $travisYmlExtendableSectionNames = array(
        'before_install',
        'install',
        'before_script',
        'after_script',
        'after_success'
    );

    /**
     * Constructor.
     */
    public function __construct()
    {
        parent::__construct("@CoreConsole/travis.yml");
    }

    /**
     * Parse existing data in a .travis.yml file that should be preserved in the output .travis.yml.
     * Includes comments.
     * 
     * @var string $existingYmlPath The path to the existing .travis.yml file.
     */
    public function processExistingTravisYml($existingYmlPath)
    {
        $existingYamlText = file_get_contents($existingYmlPath);
        foreach ($this->getRootSectionsFromYaml($existingYamlText) as $sectionName => $offset) {
            $section = $this->getRootSectionText($existingYamlText, $offset);
            if ($sectionName == 'env') {
                $this->existingEnv = $section;
            } else if ($sectionName == 'matrix') {
                $this->existingMatrix = $section;
            } else if (!in_array($sectionName, self::$travisYmlSectionNames)) {
                $this->extraSections .= "\n\n$sectionName:" . $section;
            }
        }
    }

    /**
     * Sets the name of plugin the generated .travis.yml file is for.
     *
     * @param string $pluginName ie, ExamplePlugin, UserSettings, etc.
     */
    public function setPlugin($pluginName)
    {
        $this->pluginName = $pluginName;

        $customTravisBuildSteps = array();

        foreach (self::$travisYmlExtendableSectionNames as $name) {
            $customTravisBuildSteps[$name] = array();

            $beforeStepsTemplate = $this->getPathToCustomTravisStepsFile($name, 'before');
            if (file_exists($beforeStepsTemplate)) {
                $customTravisBuildSteps[$name]['before'] = $this->changeIndent(file_get_contents($beforeStepsTemplate), '  ');
            }

            $afterStepsTemplate = $this->getPathToCustomTravisStepsFile($name, 'after');
            if (file_exists($afterStepsTemplate)) {
                $customTravisBuildSteps[$name]['after'] = $this->changeIndent(file_get_contents($afterStepsTemplate), '  ');
            }
        }

        $this->customTravisBuildSteps = $customTravisBuildSteps;
    }

    /**
     * Set extra global environment variables that should be set in the generated .travis.yml file. The entries
     * should be whole statements like `"MY_VAR=myvalue"` or `"secure: mysecurevalue"`.
     *
     * @param string[] $extraVars
     */
    public function setExtraGlobalEnvVars($extraVars)
    {
        $this->extraGlobalEnvVars = $extraVars;
    }

    /**
     * Sets the self-referential command that will generate the .travis.yml file on travis.
     *
     * @param string $consoleCommand ie, `"./console generate:travis-yml ..."`
     */
    public function setGenerateYmlCommand($consoleCommand)
    {
        $this->consoleCommand = addslashes($consoleCommand);
    }

    /**
     * Sets the PHP versions to run tests against in travis.
     *
     * @param string[] $phpVersions ie, `array("5.3.3", "5.4", "5.5")`.
     */
    public function setPhpVersions($phpVersions)
    {
        $this->phpVersions = $phpVersions;
    }

    /**
     * Renders the view. See {@link Piwik\View::render()}.
     *
     * @return string
     */
    public function render()
    {
        list($this->testsToRun, $this->testsToExclude) = $this->getTestsToRun();

        return parent::render();
    }

    /**
     * Extracts the name and offset of all root elements of a YAML document. This method does this by
     * checking for text that starts at the beginning of a line and ends with a ':'.
     *
     * @param string $yamlText The YAML text to search through.
     * @return array Array mapping string section names with the starting offset of the text in the YAML.
     */
    private function getRootSectionsFromYaml($yamlText)
    {
        preg_match_all("/^[a-zA-Z_]+:/m", $yamlText, $allMatches, PREG_OFFSET_CAPTURE);

        $result = array();

        foreach ($allMatches[0] as $match) {
            $matchLength = strlen($match[0]);
            $sectionName = substr($match[0], 0, $matchLength - 1);

            $result[$sectionName] = $match[1] + $matchLength;
        }

        return $result;
    }

    /**
     * Gets the text of a root YAML element in a YAML doc using the name of the element and the starting
     * offset of the element's text. This is accomplished by searching for the first line that doesn't
     * start with whitespace after the given offset and using the text between the given offset and the
     * line w/o starting whitespace.
     *
     * @param string $yamlText The YAML text to search through.
     * @param int $offset The offset start of the YAML text (does not include the element name and colon, ie
     *                    the offset is after `'element:'`).
     * @return string
     */
    private function getRootSectionText($yamlText, $offset)
    {
        preg_match("/^[^\s]/m", $yamlText, $endMatches, PREG_OFFSET_CAPTURE, $offset);

        $endPos = isset($endMatches[0][1]) ? $endMatches[0][1] : strlen($yamlText);

        return substr($yamlText, $offset, $endPos - $offset);
    }

    private function getTestsToRun()
    {
        $testsToRun = array();
        $testsToExclude = array();

        if ($this->isTargetPluginContainsPluginTests()) {
            $testsToRun[] = array('name' => 'IntegrationTests',
                                  'vars' => "MYSQL_ADAPTER=PDO_MYSQL");
            $testsToRun[] = array('name' => 'IntegrationTests',
                                  'vars' => "MYSQL_ADAPTER=PDO_MYSQL TEST_AGAINST_CORE=latest_stable");

            $testsToExclude[] = array('description' => 'execute latest stable tests only w/ PHP 5.5',
                                      'php' => '5.3.3',
                                      'env' => 'TEST_SUITE=IntegrationTests MYSQL_ADAPTER=PDO_MYSQL TEST_AGAINST_CORE=latest_stable');
            $testsToExclude[] = array('php' => '5.4',
                                      'env' => 'TEST_SUITE=IntegrationTests MYSQL_ADAPTER=PDO_MYSQL TEST_AGAINST_CORE=latest_stable');
        }

        if ($this->isTargetPluginContainsUITests()) {
            $testsToRun[] = array('name' => 'UITests',
                                  'vars' => "MYSQL_ADAPTER=PDO_MYSQL");

            $testsToExclude[] = array('description' => 'execute UI tests only w/ PHP 5.5',
                                      'php' => '5.3.3',
                                      'env' => 'TEST_SUITE=UITests MYSQL_ADAPTER=PDO_MYSQL');
            $testsToExclude[] = array('php' => '5.4',
                                      'env' => 'TEST_SUITE=UITests MYSQL_ADAPTER=PDO_MYSQL');
            $testsToExclude[] = array('php' => '5.6',
                                      'env' => 'TEST_SUITE=UITests MYSQL_ADAPTER=PDO_MYSQL');
        }

        if (!empty($this->pluginName)
            && empty($testsToRun)
        ) {
            throw new Exception("No tests to run for this plugin, aborting .travis.yml generation.");
        }

        return array($testsToRun, $testsToExclude);
    }

    private function isTargetPluginContainsPluginTests()
    {
        $pluginPath = $this->getPluginRootFolder();
        return $this->doesFolderContainPluginTests($pluginPath . "/tests")
            || $this->doesFolderContainPluginTests($pluginPath . "/Test");
    }

    private function doesFolderContainPluginTests($folderPath)
    {
        $testFiles = array_merge(glob($folderPath . "/**/*Test.php"), glob($folderPath . "/*Test.php"));
        return !empty($testFiles);
    }

    private function isTargetPluginContainsUITests()
    {
        $pluginPath = $this->getPluginRootFolder();
        return $this->doesFolderContainUITests($pluginPath . "/tests")
            || $this->doesFolderContainUITests($pluginPath . "/Test");
    }

    private function doesFolderContainUITests($folderPath)
    {
        $testFiles = array_merge(glob($folderPath . "/**/*_spec.js"), glob($folderPath . "/*_spec.js"));
        return !empty($testFiles);
    }

    private function changeIndent($text, $newIndent)
    {
        $text = trim($text);

        return preg_replace("/^\\s*/", $newIndent, $text);
    }

    public function getPluginRootFolder()
    {
        return PIWIK_INCLUDE_PATH . "/plugins/{$this->pluginName}";
    }

    private function getPathToCustomTravisStepsFile($sectionName, $type)
    {
        return $this->getPluginRootFolder() . "/tests/travis/$sectionName.$type.yml";
    }
}