1
|
|
|
#!/usr/bin/env php |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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.