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); |
||
0 ignored issues
–
show
|
|||
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( |
||
87 | new RecursiveDirectoryIterator(realpath($artifactsPath)), |
||
88 | RecursiveIteratorIterator::SELF_FIRST |
||
89 | ); |
||
90 | foreach ($objects as $name => $object) { |
||
91 | if ($object->isDir()) { |
||
92 | continue; |
||
93 | } |
||
94 | $relativePath = trim(str_replace(realpath($artifactsPath) . '/', '', $object->getPathName()), '/'); |
||
95 | $html .= sprintf('<li><a href="%s">%s</a></li>', $relativePath, $relativePath); |
||
96 | } |
||
97 | $html .= '</ul></body></html>'; |
||
98 | |||
99 | file_put_contents("{$artifactsPath}index.html", $html); |
||
100 | |||
101 | run("~/bin/artifacts upload --permissions public-read --target-paths $targetPath $artifactsPath"); |
||
102 | |||
103 | $fullPath = str_replace('//', '/', "$baseUrl/$targetPath/index.html"); |
||
104 | $fullPath = str_replace('https:/s3', 'https://s3', $fullPath); |
||
105 | echo "Uploaded artifacts to $fullPath\n"; |
||
106 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.