Completed
Push — master ( 019e99...4ee78f )
by Sam
11:12
created

travis-upload-artifacts.php ➔ run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
	if($echo) echo "+ $cmd\n";
18
	passthru($cmd, $returnVar);
19
	if($returnVar > 0) die($returnVar);
0 ignored issues
show
Coding Style Compatibility introduced by
The function run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

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