1#!/usr/bin/env php
2<?php
3
4$tasks = [
5
6    'buildzip' => [
7        'init', 'test', 'clean',
8    ],
9    'markrelease' => [
10        'init', 'test', 'clean',
11    ],
12    'clean' => [],
13    'test' => [
14        'composerupdate',
15    ],
16    'init' => [],
17    'composerupdate' => [],
18 ];
19
20$default = 'buildzip';
21
22$baseDir = __DIR__ . '/../';
23chdir($baseDir);
24
25$currentTask = $default;
26if ($argc > 1) $currentTask = $argv[1];
27$version = null;
28if ($argc > 2) $version = $argv[2];
29
30if (!isset($tasks[$currentTask])) {
31    echo "Task not found: ",  $currentTask, "\n";
32    die(1);
33}
34
35// Creating the dependency graph
36$newTaskList = [];
37$oldTaskList = [$currentTask => true];
38
39while(count($oldTaskList)>0) {
40
41    foreach($oldTaskList as $task=>$foo) {
42
43        if (!isset($tasks[$task])) {
44            echo "Dependency not found: " . $task, "\n";
45            die(1);
46        }
47        $dependencies = $tasks[$task];
48
49        $fullFilled = true;
50        foreach($dependencies as $dependency) {
51            if (isset($newTaskList[$dependency])) {
52                // Already in the fulfilled task list.
53                continue;
54            } else {
55                $oldTaskList[$dependency] = true;
56                $fullFilled = false;
57            }
58
59        }
60        if ($fullFilled) {
61            unset($oldTaskList[$task]);
62            $newTaskList[$task] = 1;
63        }
64
65    }
66
67}
68
69foreach(array_keys($newTaskList) as $task) {
70
71    echo "task: " . $task, "\n";
72    call_user_func($task);
73    echo "\n";
74
75}
76
77function init() {
78
79    global $version;
80    if (!$version) {
81        include __DIR__ . '/../vendor/autoload.php';
82        $version = Sabre\DAV\Version::VERSION;
83    }
84
85    echo "  Building sabre/dav " . $version, "\n";
86
87}
88
89function clean() {
90
91    global $baseDir;
92    echo "  Removing build files\n";
93    $outputDir = $baseDir . '/build/SabreDAV';
94    if (is_dir($outputDir)) {
95        system('rm -r ' . $baseDir . '/build/SabreDAV');
96    }
97
98}
99
100function composerupdate() {
101
102    global $baseDir;
103    echo "  Updating composer packages to latest version\n\n";
104    system('cd ' . $baseDir . '; composer update');
105}
106
107function test() {
108
109    global $baseDir;
110
111    echo "  Running all unittests.\n";
112    echo "  This may take a while.\n\n";
113    system(__DIR__ . '/phpunit --configuration ' . $baseDir . '/tests/phpunit.xml --stop-on-failure', $code);
114    if ($code != 0) {
115        echo "PHPUnit reported error code $code\n";
116        die(1);
117    }
118
119}
120
121function buildzip() {
122
123    global $baseDir, $version;
124    echo "  Generating composer.json\n";
125
126    $input = json_decode(file_get_contents(__DIR__ . '/../composer.json'), true);
127    $newComposer = [
128        "require" => $input['require'],
129        "config"  => [
130            "bin-dir" => "./bin",
131        ],
132        "prefer-stable" => true,
133        "minimum-stability" => "alpha",
134    ];
135    unset(
136        $newComposer['require']['sabre/vobject'],
137        $newComposer['require']['sabre/http'],
138        $newComposer['require']['sabre/uri'],
139        $newComposer['require']['sabre/event']
140    );
141    $newComposer['require']['sabre/dav'] = $version;
142    mkdir('build/SabreDAV');
143    file_put_contents('build/SabreDAV/composer.json', json_encode($newComposer, JSON_PRETTY_PRINT));
144
145    echo "  Downloading dependencies\n";
146    system("cd build/SabreDAV; composer install -n", $code);
147    if ($code!==0) {
148        echo "Composer reported error code $code\n";
149        die(1);
150    }
151
152    echo "  Removing pointless files\n";
153    unlink('build/SabreDAV/composer.json');
154    unlink('build/SabreDAV/composer.lock');
155
156    echo "  Moving important files to the root of the project\n";
157
158    $fileNames = [
159        'CHANGELOG.md',
160        'LICENSE',
161        'README.md',
162        'examples',
163    ];
164    foreach($fileNames as $fileName) {
165        echo "    $fileName\n";
166        rename('build/SabreDAV/vendor/sabre/dav/' . $fileName, 'build/SabreDAV/' . $fileName);
167    }
168
169    // <zip destfile="build/SabreDAV-${sabredav.version}.zip" basedir="build/SabreDAV" prefix="SabreDAV/" />
170
171    echo "\n";
172    echo "Zipping the sabredav distribution\n\n";
173    system('cd build; zip -qr sabredav-' . $version . '.zip SabreDAV');
174
175    echo "Done.";
176
177}
178