Created
January 14, 2015 19:44
-
-
Save philip/921e824a71b00b467fd4 to your computer and use it in GitHub Desktop.
This adds a link to the MySQL Workbench home screen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
This adds a custom link to your MySQL Workbench Home Screen. | |
This probably contains bugs and probably does not work on some platforms. | |
Known issues: | |
- I only tested on OS X, thus far | |
- It assumes the username is 'philip' | |
- Should create a feature to remove it, as well | |
- It's a hack | |
But, it (for the most part) works. For additional information, read the | |
documentation: | |
- http://dev.mysql.com/doc/workbench/en/wb-configuring-custom-home-link.html | |
*/ | |
// Custom 52x52 pixel icon that is copied into Workbench | |
$myicon = "/Users/philip/workbench/wb_starter_example_52.png"; | |
// entry: Uses your ID, where you basically change "example" to something else (no spaces) | |
// after: Script will add your link+icon after this entry, see starters_settings.xml for details | |
$mysettings = array( | |
'entry' => '<link type="object">com.mysql.wb.starter.example</link>', | |
'after' => '<link type="object">com.mysql.wb.starter.team-blog</link>', | |
); | |
// Paths to: | |
// user: Your home directory | |
// workbench: Your Workbench application | |
$paths = array( | |
'user' => array( | |
'win' => 'C:\Users\philip', | |
'osx' => '/Users/philip', | |
'nix' => '/home/philip', | |
), | |
'workbench' => array( | |
'win' => 'C:\Program Files (x86)\MySQL\MySQL Workbench CE 6.2.4', | |
'osx' => '/Applications/MySQLWorkbench.app/', | |
'nix' => '/usr/share/mysql-workbench/', | |
), | |
); | |
// Your Entry. See the documentation for how this should be defined, but | |
// Changing all instances of "example" to something else will likely work | |
// Note: The app.Starter uses the ID you defined above under $mysettings['entry'] | |
$mypredefined = | |
' | |
<value type="object" struct-name="app.Starter" id="com.mysql.wb.starter.example"> | |
<value type="string" key="type">Website</value> | |
<value type="string" key="title">Example</value> | |
<value type="string" key="description">My wonderful example.org</value> | |
<value type="string" key="publisher">Example Inc.</value> | |
<value type="string" key="authorHome">http://www.example.org/about</value> | |
<value type="string" key="smallIcon">wb_starter_example_52.png</value> | |
<value type="string" key="command">browse:http://example.org</value> | |
</value> | |
'; | |
// Hopefully you won't have to modify this but you might if your system is different than the default | |
$file_paths = array( | |
'osx' => array( | |
'settings' => "{$paths['user']['osx']}/Library/Application Support/MySQL/Workbench/", | |
'predefined' => "{$paths['workbench']['osx']}/Contents/Resources/data/", | |
'home_icons' => "{$paths['workbench']['osx']}/Contents/Resources/", | |
), | |
'win' => array( | |
'settings' => "'{$paths['user']['win']}\AppData\Roaming\'", | |
'predefined' => "'{$paths['workbench']['win']}\data\'", | |
'home_icons' => "'{$paths['workbench']['win']}\images\home\'", | |
), | |
'nix' => array( | |
'settings' => "{$paths['user']['nix']}/.mysql/workbench/", | |
'predefined' => "{$paths['workbench']['nix']}/data/", | |
'home_icons' => "{$paths['workbench']['nix']}/images/", | |
), | |
); | |
/****************** CODE *************************************************/ | |
if (!file_exists($myicon)) { | |
echo "The icon ($myicon) does not exist." . PHP_EOL; | |
exit; | |
} | |
$os = get_operating_system(); | |
if (!is_dir($paths['user'][$os])) { | |
echo "The user path '{$paths['user'][$os]}' does not exist." . PHP_EOL; | |
exit; | |
} | |
if (!is_dir($paths['workbench'][$os])) { | |
echo "The workbench path '{$paths['workbench'][$os]}' does not exist." . PHP_EOL; | |
exit; | |
} | |
$dirs = $file_paths[$os]; | |
if (!check_directories($dirs)) { | |
exit; | |
} | |
foreach ($dirs as $type => $dir) { | |
if ($type === 'settings') { | |
$file = $dir . 'starters_settings.xml'; | |
if (!file_exists($file)) { | |
echo "Error: The file ($file) does not exist" . PHP_EOL; | |
exit; | |
} | |
$contents = file_get_contents($file); | |
if (false !== strpos($contents, $mysettings['entry'])) { | |
echo "INFO: My Starters Settings Entry already exists, skipping." . PHP_EOL; | |
continue; | |
} | |
if (false === strpos($contents, $mysettings['after'])) { | |
echo "ERROR: Could not find {$mysettings['after']} inside of $file" . PHP_EOL; | |
exit; | |
} | |
$contents = str_replace($mysettings['after'], PHP_EOL . ' ' . $mysettings['entry'], $contents); | |
file_put_contents($file, $contents); | |
echo "INFO: Added entry to $file" . PHP_EOL; | |
} | |
if ($type === 'predefined') { | |
$file = $dir . 'predefined_starters.xml'; | |
if (!file_exists($file)) { | |
echo "Error: The file ($file) does not exist" . PHP_EOL; | |
exit; | |
} | |
$contents = file_get_contents($file); | |
if (false !== strpos($contents, $mypredefined)) { | |
echo "INFO: My Predefined Entry already exists, skipping." . PHP_EOL; | |
continue; | |
} | |
$contents = str_replace( | |
'<value type="list" key="items" content-type="object" content-struct-name="app.Starter">', | |
'<value type="list" key="items" content-type="object" content-struct-name="app.Starter">' . PHP_EOL . $mypredefined, | |
$contents); | |
file_put_contents($file, $contents); | |
echo "INFO: Added entry to $file" . PHP_EOL; | |
} | |
if ($type === 'home_icons') { | |
if (copy($myicon, $dir . basename($myicon))) { | |
echo "INFO: Copied icon." . PHP_EOL; | |
} else { | |
echo "ERROR: Could not copy icon from ($myicon) to ($dir" . basename($myicon) . ")" . PHP_EOL; | |
exit; | |
} | |
} | |
} | |
function check_directories($dirs) { | |
foreach ($dirs as $type => $dir) { | |
if (!is_dir($dir)) { | |
echo "Error: Directory ($dir) for type ($type) does not exist." . PHP_EOL; | |
return false; | |
} | |
} | |
return true; | |
} | |
function get_operating_system() { | |
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | |
return 'win'; | |
} | |
if (strtoupper(PHP_OS) === 'DARWIN') { | |
return 'osx'; | |
} | |
return 'nix'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment