File size: 11,843 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php

/**
 *
 * @package workflow.engine.classes
 */
class CLI
{
    public static $tasks = array ();
    public static $currentTask = null;

    /**
     * Adds a new task defined by it's name.
     * All other task functions will
     * remember the current task defined here.
     *
     * @param string $name name of the task, used in the command-line
     */
    public static function taskName ($name)
    {
        self::$currentTask = $name;
        self::$tasks[$name] = array (
            'name' => $name,
            'description' => null,
            'args' => array (),
            'function' => null,
            'opt' => array (
                'short' => '',
                'long' => array (),
                'descriptions' => array ()
            )
        );
    }

    /**
     * Adds a description to the current task.
     * The description should contain a
     * one-line description of the command and a few lines of text with more
     * information.
     *
     * @param string $description task description
     */
    public static function taskDescription ($description)
    {
        assert( self::$currentTask !== null );
        self::$tasks[self::$currentTask]["description"] = $description;
    }

    /**
     * Adds an argument to the current task.
     * The options will affect how it is
     * displayed in the help command. Optional will put [] in the argument and
     * multiple will put ... in the end. Arguments are displayed together with
     * the task name in the help command.
     *
     * @param string $name argument name
     */
    public static function taskArg ($name, $optional = true, $multiple = false)
    {
        assert( self::$currentTask !== null );
        self::$tasks[self::$currentTask]["args"][$name] = array ('optional' => $optional,'multiple' => $multiple
        );
    }

    /**
     * Defines short and long options as used by getopt to the current task.
     *
     * @param string $short short options
     * @param array $long long options
     */
    public static function taskOpt ($name, $description, $short, $long = null)
    {
        assert( self::$currentTask !== null );
        $opts = self::$tasks[self::$currentTask]["opt"];
        if ($short) {
            $opts['short'] .= $short;
        }
        if ($long) {
            $opts['long'][] = $long;
        }
        $opts['descriptions'][$name] = array ('short' => $short,'long' => $long,'description' => $description
        );
        self::$tasks[self::$currentTask]["opt"] = $opts;
    }

    /**
     * Defines the function to run for the current task.
     *
     * @param callback $function function to run
     */
    public static function taskRun ($function)
    {
        assert( self::$currentTask !== null );
        self::$tasks[self::$currentTask]["function"] = $function;
    }

    /**
     * Displays the help instructions.
     *
     * @param array $args if defined, the task name should be argument 0
     * @param array $opts options as returned by getopt
     */
    public static function help ($args, $opts = null)
    {
        global $argv;
        $scriptName = $argv[0];
        if (is_array($args) && count($args) > 0 ) {
            $taskName = $args[0];
        } else {
            $taskName = $args;
        }

        if (! $taskName) {
            echo "usage: processmaker <task> [options] [args]\n";
            echo "       If using Linux/UNIX, prepend './' to specify the directory: " . $scriptName . " <task> [options] [args]\n";
            echo "Type   'processmaker help <task>' for help on a specific task.";
            echo "\n\nAvailable tasks:\n";
            $tasks = array ();
            ksort( self::$tasks );
            foreach (self::$tasks as $name => $data) {
                $description = explode( "\n", $data['description'] );
                $tasks[] = "  $name";
            }
            $tasks = join( "\n", $tasks );
            echo $tasks . "\n\n";
        } else {
            $options = array();
            $tasks = array();
            ksort( self::$tasks );
            foreach (self::$tasks as $name => $data) {
                $description = explode( "\n", $data['description'] );
                $options[] = "$name";
            }
            if (!in_array($taskName, $options)) {
                echo "\nThe task does not exist \n";
                echo "Use one of the following tasks:\n";
                $tasks = array ();
                ksort( self::$tasks );
                foreach (self::$tasks as $name => $data) {
                    $description = explode( "\n", $data['description'] );
                    $tasks[] = "  $name";
                }
                $tasks = join( "\n", $tasks );
                echo $tasks . "\n\n";
            } else{
                $valid_args = array ();
                foreach (self::$tasks[$taskName]['args'] as $arg => $data) {
                    $arg = strtoupper( $arg );
                    if ($data['multiple']) {
                        $arg = "$arg...";
                    }
                    if ($data['optional']) {
                        $arg = "[$arg]";
                    }
                    $valid_args[] = $arg;
                }

            $nameHotfixFile = ($taskName == "hotfix-install")? "HOTFIX-FILE" : "";

            $valid_args = join( " ", $valid_args );
            $description = explode( "\n", self::$tasks[$taskName]['description'] );
            $taskDescription = trim( array_shift( $description ) );
            $description = trim( implode( "\n", $description ) );
            $message = <<< EOT
$taskName: {$taskDescription}
Usage: processmaker $taskName $nameHotfixFile $valid_args

  $description

EOT;
            $valid_options = array ();
            foreach (self::$tasks[$taskName]['opt']['descriptions'] as $opt => $data) {
                $optString = array ();
                if ($data['short']) {
                    $optString[] = "-{$data['short']}";
                }
                if ($data['long']) {
                    $optString[] = "--{$data['long']}";
                }
                $valid_options[] = "  " . join( ", ", $optString ) . "\n\t" . wordwrap( $data['description'], 70, "\n\t" );
            }
            $valid_options = join( "\n", $valid_options );
            if ($valid_options) {
                $message .= <<< EOT

Options:

$valid_options

EOT;
            }
            echo $message . "\n";
        }
        }
    }

    /**
     * Run the CLI task, which will check which command is specified and run it.
     */
    public static function run ()
    {
        CLI::taskName( "help" );
        CLI::taskRun( array ('self','help'
        ) );
        global $argv;
        $args = $argv;
        $cliname = array_shift( $args );
        $taskName = array_shift( $args );
        while ($taskName{0} == '-') {
            $taskName = array_shift( $args );
        }
        if (! $taskName) {
            echo self::error( "Specify a task from the list below." ) . "\n\n";
            self::help( null, null );
            return;
        }
        $taskData = null;
        foreach (self::$tasks as $name => $data) {
            if (strcasecmp( $name, $taskName ) === 0) {
                $taskData = $data;
                break;
            }
        }
        if (! $taskData) {
            echo self::error( "Command not found: '$taskName'" ) . "\n\n";
            self::help( null, null );
            return;
        }

        $short = "h" . $taskData['opt']['short'];
        $long = array_merge( array ("help"
        ), $taskData['opt']['long'] );
        $getopt = Console_Getopt::getopt2( $args, $short, $long );
        if (! is_array( $getopt )) {
            echo self::error( "Invalid options (" . $getopt->getMessage() . ")" ) . "\n\n";
            self::help( $taskName );
            return;
        }
        list ($options, $arguments) = $getopt;
        foreach ($taskData['opt']['descriptions'] as $optName => $optDescription) {
            $short = str_replace( ":", "", $optDescription['short'] );
            $long = str_replace( "=", "", $optDescription['long'] );
            $validOpts[$short] = $optName;
            $validOpts[$long] = $optName;
        }
        $taskOpts = array ();
        try {
            foreach ($options as $opt) {
                list ($optName, $optArg) = $opt;
                if ($optName === "h" || $optName === "--help") {
                    self::help( $taskName );
                    return;
                }
                if (strpos( $optName, '--' ) === 0) {
                    $optName = substr( $optName, 2 );
                }
                if (! array_key_exists( $optName, $validOpts )) {
                    throw new Exception( "option not found: $optName" );
                }
                if (array_key_exists( $validOpts[$optName], $taskOpts )) {
                    throw new Exception( "'$optName' specified more then once" );
                }
                $taskOpts[$validOpts[$optName]] = $optArg;
            }
        } catch (Exception $e) {
            echo self::error( "Invalid options: " . $e->getMessage() ) . "\n\n";
            self::help( $taskName );
            return;
        }
        try {
            call_user_func( $taskData['function'], $arguments, $taskOpts );
        } catch (Exception $e) {
            echo self::error( "\n  Error executing '$taskName':\n\n  {$e->getMessage()}\n" ) . "\n";
            global $tempDirectory;
            if (!empty($tempDirectory)) {
                G::rm_dir($tempDirectory);
            }
        }
    }

    /**
     * Returns an information colorized version of the message.
     *
     * @param string $message the message to colorize
     */
    public static function info ($message)
    {
        return pakeColor::colorize( $message, "INFO" );
    }

    /**
     * Returns a warning colorized version of the message.
     *
     * @param string $message the message to colorize
     */
    public static function warning ($message)
    {
        return pakeColor::colorize( $message, "COMMENT" );
    }

    /**
     * Returns an error colorized version of the message.
     *
     * @param string $message the message to colorize
     */
    public static function error ($message)
    {
        return pakeColor::colorize( $message, "ERROR" );
    }

    /**
     * Prompt the user for information.
     *
     * @param string $message the message to display
     * @return string the text typed by the user
     */
    public static function prompt ($message)
    {
        echo "$message";
        $handle = fopen( "php://stdin", "r" );
        $line = fgets( $handle );
        return $line;
    }

    /**
     * Ask a question of yes or no.
     *
     * @param string $message the message to display
     * @return bool true if the user choosed no, false otherwise
     */
    public static function question ($message)
    {
        $input = strtolower( self::prompt( "$message [Y/n] " ) );
        return (array_search( trim( $input ), array ("y",""
        ) ) !== false);
    }

    /**
     * Display a message to the user.
     * If filename is specified, it will setup
     * a logging file where all messages will be recorded.
     *
     * @param string $message the message to display
     * @param string $filename the log file to write messages
     */
    public static function logging ($message, $filename = null)
    {
        static $log_file = null;
        if (isset( $filename )) {
            $log_file = fopen( $filename, "a" );
            fwrite( $log_file, " -- " . date( "c" ) . " " . $message . " --\n" );
        } else {
            if (isset( $log_file )) {
                fwrite( $log_file, $message );
            }
            echo $message;
        }
    }
}