Skip to content

Instantly share code, notes, and snippets.

@moorscode
Last active July 22, 2019 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moorscode/a91e1378053a278cea32055f1e6a064e to your computer and use it in GitHub Desktop.
Save moorscode/a91e1378053a278cea32055f1e6a064e to your computer and use it in GitHub Desktop.
Symlink creation script to activate a repository checkout to multiple Vagrant domains
<?php
if ( ! isset( $argv[1] ) ) {
print( 'Please provide a base directory.' . PHP_EOL );
exit( 1 );
}
if ( ! isset( $argv[2] ) ) {
print( 'Please provide the plugin to activate.' . PHP_EOL );
exit( 1 );
}
$base_directory = $argv[1];
$plugin = $argv[2];
$source_directory = ! empty( $argv[3] ) ? $argv[3] : 'plugins';
$server_basedir = ! empty( $argv[4] ) ? $argv[4] : implode( DIRECTORY_SEPARATOR, array( '', 'srv', 'global-wp-content', '' ) );
$relative_path = sprintf( '%s%s%s', $source_directory, DIRECTORY_SEPARATOR, $plugin );
$path = sprintf( '%s%s%s', $base_directory, DIRECTORY_SEPARATOR, $relative_path );
if ( ! is_dir( $path ) ) {
printf( 'The plugin [%s] could not be found.' . PHP_EOL, $path );
exit( 1 );
}
$server_path = $server_basedir . $relative_path;
$cwd = getcwd();
$parts = array_filter( explode( DIRECTORY_SEPARATOR, $cwd ) );
$target = is_dir( $cwd . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR ) ? $cwd : false;
if ( ! $target && in_array( 'wp-content', $parts, true ) ) {
$target = implode( DIRECTORY_SEPARATOR, array_slice( $parts, 0, array_search( 'wp-content', $parts, true ) - 1 ) );
}
if ( ! $target ) {
print( 'No WordPress installation could be found in the current path.' . PHP_EOL );
exit( 1 );
}
$local_path = implode( DIRECTORY_SEPARATOR, array( $target, 'wp-content', $source_directory, $plugin ) );
// Remove any existing file/directory, otherwise the symlink cannot be created.
system( sprintf( 'rm -rf %s', $local_path ) );
system( sprintf( 'ln -s %s %s', $server_path, $local_path ), $result );
if ( 0 !== $result ) {
printf( 'An error occurred while creating the symlink from %s to %s' . PHP_EOL, $server_path, $local_path );
exit( $result );
}
printf( 'Created symlink from %s to %s' . PHP_EOL, $path, $local_path );
exit( 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment