Skip to content

Instantly share code, notes, and snippets.

@SamuelDavis
Last active August 29, 2015 14:11
Show Gist options
  • Save SamuelDavis/5492d1ef32e1b25baa3a to your computer and use it in GitHub Desktop.
Save SamuelDavis/5492d1ef32e1b25baa3a to your computer and use it in GitHub Desktop.
A PHP script, run from the command line, which parses cookbooks' metadata.rb files for dependencies and git clones them. Just drop it next to the cookbooks/ folder and run php git-cookbooks.php
<?php
$gitCookbookUrls = 'https://gist.githubusercontent.com/SamuelDavis/40f12c099f2c6ce19848/raw/36a3e32e4771b09f08cf5b91404ac85e68d21f5a/git-cookbook-urls.json';
$cookbookUrls = json_decode(file_get_contents($gitCookbookUrls), TRUE);
$existingCookbooks = scandir('./cookbooks');
if(!$existingCookbooks) {
die("No existing cookbooks found.\n");
}
foreach($existingCookbooks as $cookbook) {
if($cookbook !== '.' && $cookbook !== '..') {
getDepenencies($cookbook);
}
}
function getDepenencies($cookbook)
{
$metaFile = "./cookbooks/$cookbook/metadata.rb";
if(is_file($metaFile)) {
preg_match_all("/depends +['|\"]([a-zA-Z0-9-_]+)['|\"]/", file_get_contents($metaFile), $matches);
if($matches[1]) {
foreach($matches[1] as $dependency) {
gitCookbook($dependency);
}
}
else {
echo "Cookbook $cookbook has no dependencies\n";
}
}
else {
echo "Cookbook $cookbook has no metadata.rb\n";
}
}
function gitCookbook($cookbook)
{
global $cookbookUrls;
if(!array_key_exists($cookbook, $cookbookUrls)) {
echo "$cookbook has no git url\n";
return null;
}
$url = $cookbookUrls[$cookbook];
$dir = "./cookbooks/$cookbook";
if(is_dir($dir)) {
echo "$dir directory already exists\n";
}
else {
$cmd = "git clone $url cookbooks/$cookbook";
shell_exec($cmd);
}
getDepenencies($cookbook);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment