File operations

Ruafozy edited this page Sep 21, 2013 · 6 revisions

Pake includes various utilities for common operations. Here they are:

File operations

pake_mkdirs($path, $mode = 0777)

Checks if $path directory exists, and if it doesn’t, creates $path directory recursively. You don’t need to do additional file_exists() or is_dir() calls. Just call pake_mkdirs(“foo/bar/baz”) and everything will be done automatically

pake_copy($origin_file, $target_file, $options = array())

Again, Pake will do all required checks and will even create target directory for you, if it doesn’t exist. By default, pake will only copy file if $origin_file is newer than $target_file. You can override it by giving array(‘override’ => true) as the last parameter.

pake_rename($origin, $target)

Renames file specified as the first parameter to the name specified by the second parameter.

pake_mirror($arg, $origin_dir, $target_dir, $options = array())

Copies all files and directories which correspond to the $arg rule from $origin_dir to $target_dir. Options parameter is forwarded to pake_copy(), which is used internally.

pake_remove($arg, $target_dir)

Deletes all files and directories which correspond to the $arg rule from $target_dir

pake_touch($arg, $target_dir)

Updates “modified time” of files, which correspond to the $arg rule in $target_dir

pake_replace_tokens_to_dir($arg, $src_dir, $target_dir, $begin_token, $end_token, $tokens)

TODO

pake_replace_tokens($arg, $target_dir, $begin_token, $end_token, $tokens)

TODO

pake_symlink($origin_dir, $target_dir, $copy_on_windows = false)

Creates symlink from $origin_dir to $target_dir. if $copy_on_windows is set to TRUE, then pake_mirror() will be used, in case when symlink functionality is not supported by OS.

pake_chmod($arg, $target_dir, $mode, $umask = 0000)

Applies new POSIX mode to files found by $arg rule in $target_dir. Notice: it is not safe to rely on umask in multithreaded environment.

pake_strip_php_comments($arg, $target_dir = ’’)

Parse all files found by $arg rule in $target_dir and, if those are php-files, removes comments from them.

OS interaction

pake_sh($cmd, $interactive = false)

Executes shell-command. If $interactive flag is not set STDOUT is not shown, but is returned instead. STDERR still goes straight to the screen, which allows to show password-request dialogs, for example.

On the other hand, if $interactive flag is set, then command is attached to standard stdin/stdout streams and user can interact with process. This is useful for running editors, for example.

pake_superuser_sh($cmd, $interactive = false)

Command if similar to pake_sh() but it tries to run command as superuser. If current user is “root”, already, then it just forwards call to pake_sh(). Otherwise, it will try to run command with sudo or su, depending on their availability.

Support utilities

pake_input($question, $default = null)

This function shows the question and then waits for input from the user. As soon as user presses Enter-key input is returned from this function. It works like this:

<?php
    $age = pake_input("How old are you?");
    pake_echo_comment('Your age is: '.$age);
?>
How old are you?
[>] 18
   # Your age is: 18

pake_select_input($question, array $options, $default = null)

This function shows the question and provides several options, waiting for input from the user. As soon as user presses Enter-key input is returned from this function. $default, if specified, has to be numeric index of default option. It works like this:

<?php
    $options = array(
        '< 18',
        '18–25',
        '25–45',
        '> 45'
    );
    $result = pake_select_input('How old are you?', $options, 1);
    pake_echo('Your age is: '.$result);
?>
How old are you?
  (1) < 18
  (2) 18–25
  (3) 25–45
  (4) > 45
[> default="2"] 2
Your age is: 18–25

pake_echo_action($section, $text)

This is a standard mean for outputting progress information to the user. Resulting string is divided in 2 columns (each of those is left-aligned). It looks like this:

>> section    action text goes here

pake_echo_comment($text)

Deliever commenting-message to the user (it will be yellow, if user’s terminal supports colors)

pake_echo_error($text)

Deliever error-message to the user (it will be red and alarming, if user’s terminal supports colors)

pake_echo($text)

This one just prints free-form message to the user (that is, if she want to see messages). No need to add “\n”

<?php
    pake_echo("Hello!");
    pake_echo("World!");
?>