function core_cli_bashrc

3.x core.drush.inc core_cli_bashrc($drush_command)
4.x core.drush.inc core_cli_bashrc($drush_command, $interactive_mode)

File

commands/core/core.drush.inc, line 861
Core drush commands.

Code

function core_cli_bashrc($drush_command, $interactive_mode) {
  $bashrc_data = '';

  $initial_site = '';
  $site_list = drush_sitealias_resolve_sitespecs(array('@self'));
  if (!empty($site_list)) {
    $site_list = array_keys($site_list);
    if ($site_list[0] != "@self") {
      $initial_site = $site_list[0];
    }
  }
  if (empty($initial_site)) {
    drush_sitealias_load_all(TRUE);
    $initial_site = _drush_sitealias_find_local_alias_name(drush_get_context('DRUSH_DRUPAL_ROOT'), str_replace('http://', '', drush_get_context('DRUSH_URI')));
  }

  $searchpath[] = drush_get_context('SHARE_PREFIX', '/usr') . '/share/drush/commands';
  $searchpath[] = drush_get_context('ETC_PREFIX', '') . '/etc/drush';
  $searchpath[] = realpath(dirname(__FILE__) . '/../../');
  $bashrc_searchpath = implode(' ', $searchpath);

  // Set the prompt to 'drush> ' in interactive mode.
  if ($interactive_mode) {
    $bashrc_data .= "# Set our prompt to 'drush> '\nPS1='drush> '\n\n";
  }

  // Set up our default bashrc file for the drush cli
  $bashrc_data .= <<<EOD
# Drush .bashrc file.  Copy selected portions of this
# into your own bash resource file to add features of the
# drush core-cli command to your default shell.

# Where we will search for .bashrc files
BASHRC_PATH="~/.drush $bashrc_searchpath"

# Set the DRUSH_CLI variable; it may also be of use
# in the user's .bashrc, or elsewhere.  If we have not
# already set DRUSH_CLI, add ~ to our BASHRC_PATH.
# This allows us to source ~/.bashrc when we are running
# `drush core-cli` without causing problems with reentrancy
# if someone sources this file from ~/.bashrc.
[ -z \$DRUSH_CLI ] && BASHRC_PATH="~ \$BASHRC_PATH"
DRUSH_CLI=true

# Cache our initial site
INITIAL_SITE=$initial_site

# Look for drush-specific bashrc files and
# source any that are found.  Will also source
# \$HOME/.bashrc if run via `drush core-cli`
for d in \$BASHRC_PATH ; do
  [ -r \$d/.bashrc ] && {
    . \$d/.bashrc
  }
done

# Display our initial site in the bash prompt.
INITIAL_PS1="\$PS1"

# When using the drush shell, all commands will
# by default target the site specified by the
# shell variable \$DRUPAL_SITE.  To run a command
# on some other site, use either:
#   drush @site command
# or
#   on @site command
alias on="$drush_command"

# To specify a new DRUPAL_SITE without changing
# your current directory:
#   use @site
# To use the previous site (like `cd -`):
#   use -
# To use the initial site set when the shell was
# first entered:
#   use ~
function use() {
  if [ "x\$1" == "x-" ]
  then
    use \$PREVIOUS_SITE
  # `use ~1` will go back to our initial site.  Note, however,
  # that the shell will convert ~ to \$HOME before we get here,
  # so we will simply test for either ~ or \$HOME for this feature.
  elif [ "x\$1" == "x~" ] || [ "x\$1" == "x\$HOME" ]
  then
    use \$INITIAL_SITE
  elif [ "x\$1" != "x" ] && [ "x\$1" != "x@self" ]
  then
    SITE=`drush site-alias \$1 --short 2>/dev/null`
    if [ \$? != 0 ] || [ -z "\$SITE" ]
    then
      echo "Alias \$1 not found."
    else
      REMOTE_DRUPAL=`drush site-alias \$1 --component='remote-host' 2>/dev/null`
      PREVIOUS_SITE=\$DRUPAL_SITE
      DRUPAL_SITE=$1
      if [ -n "\$REMOTE_DRUPAL" ]
      then
        PS1="\$REMOTE_DRUPAL:\${1:-drush}> "
      else
        PS1="\${1:-drush}> "
      fi
    fi
  else
    PREVIOUS_SITE=\$DRUPAL_SITE
    DRUPAL_SITE=
    PS1="\$INITIAL_PS1"
  fi
}

# We override the cd command to allow convenient
# shorthand notations, such as:
#   cd @site1
#   cd %modules
#   cd %devel
#   cd @site2:%files
# When changing the cwd to a new site, the DRUPAL_SITE
# variable is also changed, so the new site becomes
# the default drush target.
function cd() {
  d=\$1;
  # If we do not have a \$DRUPAL_SITE from `use @alias`,
  # then 'cd @alias' will still cd to @alias, but all other
  # patterns will be passed to builtin cd.  The same behavior
  # will be used if \$DRUPAL_SITE points to a remote alias.
  if [ -z \$DRUPAL_SITE ] || [ -n "\$REMOTE_DRUPAL" ]
  then
    if [ -n "\$d" ] && [ \${d:0:1} == "@" ]
    then
      cdd \$@;
    else
      builtin cd "\$d";
    fi
  else
    # First special-case the no-parameter cd
    if [ -z "\$d" ]
    then
      cdd %root;
    # Next test and see if this should be an 'ordinary' cd
    elif [ "\$1" == "-" ] || [ -d "\$d" ]
    then
      builtin cd "\$d";
    else
      cdd \$@
    fi
  fi
}

# Do a special drush core-cli cd, handling
# shorthand notation directory names -- anything
# understood by drupal-directory
function cdd() {
  DEST=`drupal-directory \$1 --local 2>/dev/null`
  if [ \$? == 0 ]
  then
    SITE=`drupal-directory \$1 --component=name 2>/dev/null`
    if [ -n "\$SITE" ]
    then
      use \$SITE;
    fi
    echo "cd \$DEST";
    builtin cd "\$DEST";
  else
    builtin cd "\$1"
  fi
}

# The command lsd will quickly do an 'ls' on the
# specified special drush directory, e.g.:
#   lsd %modules
function lsd() {
  # First special-case the no-parameter cd
  if [ "x\$1" == "x" ]
  then
    lsd %root
  else
    # Do a special drush core-cli cd, handling
    # shorthand notation directory names -- anything
    # understood by drupal-directory
    DEST=`drupal-directory \$1 --local`
    if [ \$? == 0 ]
    then
      echo "ls \$DEST";
      ls \$DEST;
    fi
  fi
}

EOD;

  // Add aliases for all drush commands
  $bashrc_data .= "# Add aliases for all drush commands\n";
  $commands = drush_get_commands();
  $cli_overrides = _convert_csv_to_array(drush_get_option('override', 'dd,help,sa'));
  $cli_contextual_commands = _convert_csv_to_array(drush_get_option('contextual', 'cc,cron,rsync,status,sync,updatedb'));
  $cli_ignore = _convert_csv_to_array(drush_get_option('ignore', 'core-cli,cli'));
  foreach ($commands as $key => $command) {
    // Filter out old commands that still have spaces;
    // do not mask any existing bash command (e.g. 'grep')
    // or builtin (e.g. 'eval') with drush aliases.
    if ((strpos($key, ' ') === FALSE) && (!in_array($key, $cli_ignore))) {
      $conflict = FALSE;
      $is_shell_builtin = (drush_shell_exec("bash -c \"builtin $key\"") == "1");
      if (!in_array($key, $cli_overrides)) {
        $status = drush_shell_exec("which $key 2>&1");
        if ($status !== FALSE) {
          $output_lines = drush_shell_exec_output();
          $conflict = $output_lines[0];
        }
        elseif ($is_shell_builtin) {
          $conflict = "builtin $key";
        }
      }
      elseif ($is_shell_builtin && ($key != "help")) {
        drush_set_error('DRUSH_OVERRIDE_BUILTIN', dt("Cannot override shell builtin function !builtin", array('!builtin' => $key)));
      }
      if (!$conflict) {
        $bashrc_data .= "function $key() {\n  $drush_command \$DRUPAL_SITE $key \"\$@\"\n}\n";
      }
      elseif (in_array($key, $cli_contextual_commands)) {
        if ($is_shell_builtin) {
          $warning = dt("Warning: overriding shell built-in command !builtin.  This could cause unpredictable results with shell scripts.", array('!builtin' => $key));
          drush_log($warning, 'warning');
          $bashrc_data .= "# $warning\n";
        }
        if ($key == 'rsync') {
          $bashrc_data .= "function $key() { \n  HAS_SITE_PARAMS=\n  for p in \"\$@\" ; do\n    if [ \"x\${p:0:1}\" == \"x@\" ] || [ \"x\${p:0:1}\" == \"x%\" ]\n    then\n      HAS_SITE_PARAMS=true\n    fi\n  done\n  if [ -z \$HAS_SITE_PARAMS ]\n  then \n      $conflict \"\$@\"\n  else\n      $drush_command \$DRUPAL_SITE $key \"\$@\"\n  fi\n}\n";
        }
        else {
          $bashrc_data .= "function $key() {\n  if [ -z \$DRUPAL_SITE ]\n  then \n    $conflict \"\$@\"\n  else\n    $drush_command \$DRUPAL_SITE $key \"\$@\"\n  fi\n}\n";
        }
      }
      else {
        $bashrc_data .= "# drush $key skipped; conflicts with $conflict\n";
      }
    }
  }

  // Make bash aliases for all drush site aliases.
  // @sitealias becomes an alias for "drush @sitealias".
  // This allows:
  //   $ @sitealias status
  drush_sitealias_load_all();
  foreach (drush_get_context('site-aliases') as $site_name => $site_record) {
    $bashrc_data .= "alias $site_name='drush $site_name'\n";
  }

  // Add some additional statements to the bashrc data that
  // should not go into --pipe mode.
  if ($interactive_mode) {
    // If there is an initial site, then cd to it.  This will set the prompt.
    if (!empty($initial_site)) {
      $bashrc_data .= "cd $initial_site\n";
    }
    // Before entering the new bash script, cd to the current site root,
    // if any.  This will make our default site for drush match the
    // currently bootstrapped site (at least until the user cd's away).
    // We do this -after- the drush_print_pipe($bashrc_data) because
    // we DO NOT want to change the working directory if this bashrc
    // is being used by a login shell.
    elseif ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
      $site_root = drush_get_context('DRUSH_DRUPAL_ROOT') . '/' . $site_root;
      $bashrc_data .= "builtin cd $site_root\n";
    }
    if ($drush_command != 'drush') {
      $bashrc_data .= "alias drush=\"$drush_command\"\n";
    }
  }
  // Add some additional statements that should only appear in non-interactive mode.
  else {
    // If there is an initial site, then use.  This will set the prompt.
    if (!empty($initial_site)) {
      $bashrc_data .= "use $initial_site\n";
    }
  }

  return $bashrc_data;
}