Passed
Push — 4 ( d82cd0...c3b556 )
by Maxime
07:26
created

checkenv()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 9.2
1
#!/usr/bin/env php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 1.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
<?php
3
/**
4
 * Creates an index.html with links to all files in a given directory structure, recursively.
5
 * This is useful for Amazon S3 uploads with static file hosting, since it doesn't list files by default.
6
 *
7
 * Assumes to run in a SilverStripe webroot
8
 */
9
10
/**
11
 * Run a command
12
 *
13
 * @param string $cmd
14
 * @param bool $echo
15
 */
16
function run($cmd, $echo = true)
17
{
18
    if ($echo) {
19
        echo "+ $cmd\n";
20
    }
21
    passthru($cmd, $returnVar);
22
    if ($returnVar > 0) {
23
        die($returnVar);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
24
    }
25
}
26
27
/**
28
 * Check if an env variable is set
29
 *
30
 * @param $envs
31
 * @return bool
32
 */
33
function checkenv($envs)
34
{
35
    if ($envs) {
36
        foreach (explode(',', $envs) as $env) {
37
            if (!getenv($env)) {
38
                return false;
39
            }
40
        }
41
    }
42
    return true;
43
}
44
45
$opts = getopt('', array(
46
    'artifacts-path:',
47
    'target-path:',
48
    'if-env:',
49
    'artifacts-base-url:',
50
));
51
52
// --if-env=BEHAT_TEST means that this script will only be executed if the given environment var is set
53
if (empty($opts['if-env'])) {
54
    echo "--if-env option is mandatory";
55
    exit(0);
56
}
57
if (!checkenv($opts['if-env'])) {
58
    echo "Apache skipped; {$opts['if-env']} wasn't set.\n";
59
    exit(0);
60
}
61
62
if (isset($opts['artifacts-path'])) {
63
    $artifactsPath = $opts['artifacts-path'];
64
} elseif (is_dir(__DIR__ . '/artifacts/')) {
65
    $artifactsPath = __DIR__ . '/artifacts/';
66
} elseif (is_dir('~/artifacts/')) {
67
    $artifactsPath = '~/artifacts/';
68
} else {
69
    $artifactsPath = null;
70
}
71
72
$targetPath = $opts['target-path'];
73
$baseUrl = $opts['artifacts-base-url'];
74
75
if (!$artifactsPath || !is_dir($artifactsPath)) {
76
    echo "No artifacts found, skipped\n";
77
    exit(0);
78
}
79
80
echo "Installing artifacts script to ~/bin/artifacts\n";
81
run("curl -sL https://raw.githubusercontent.com/travis-ci/artifacts/master/install | bash");
82
83
echo "Creating {$artifactsPath}index.html...\n";
84
85
$html = '<html><head></head><body><ul>';
86
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath($artifactsPath)), RecursiveIteratorIterator::SELF_FIRST);
87
foreach ($objects as $name => $object) {
88
    if ($object->isDir()) {
89
        continue;
90
    }
91
    $relativePath = trim(str_replace(realpath($artifactsPath) . '/', '', $object->getPathName()), '/');
92
    $html .= sprintf('<li><a href="%s">%s</a></li>', $relativePath, $relativePath);
93
}
94
$html .= '</ul></body></html>';
95
96
file_put_contents("{$artifactsPath}index.html", $html);
97
98
run("~/bin/artifacts upload --permissions public-read --target-paths $targetPath $artifactsPath");
99
100
$fullPath = str_replace('//', '/', "$baseUrl/$targetPath/index.html");
101
$fullPath = str_replace('https:/s3', 'https://s3', $fullPath);
102
echo "Uploaded artifacts to $fullPath\n";
103