Skip to content

Instantly share code, notes, and snippets.

@psynaptic
Created January 26, 2012 21:55
Show Gist options
  • Save psynaptic/1685352 to your computer and use it in GitHub Desktop.
Save psynaptic/1685352 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Drush commands and related functions.
*/
/**
* Implements hook_drush_command().
*/
function api_helper_drush_command() {
return array(
'api-helper-update-tracked-branches' => array(
'callback' => 'drush_api_helper_update_tracked_branches',
'description' => 'Update the tracked branches in the database.',
'options' => array(
'branch' => 'The branch to update.',
'project' => 'An individual project to update.',
),
),
'api-helper-prune' => array(
'callback' => 'drush_api_helper_prune',
'description' => 'Prune tracked branches from the database.',
'options' => array(
'projects' => 'A list of projects to remove.',
'branch' => 'The branch to prune update.',
),
),
'api-helper-delete-job' => array(
'callback' => 'drush_api_helper_delete_job',
'description' => 'Delete queued jobs matching a MySQL LIKE %string% condition.',
'arguments' => array(
'job' => 'The string to use in the LIKE %string% condition.',
),
),
'api-helper-clone' => array(
'callback' => 'drush_api_helper_clone',
'description' => 'Clone a git repo for a project for a specific core version.',
'options' => array(
'project' => 'The project to clone.',
'branch' => 'Branch name to clone.',
),
),
'api-helper-fetch' => array(
'callback' => 'drush_api_helper_fetch_all',
'description' => 'Run git fetch --all for all projects in a specific core version.',
'options' => array(
'branch' => 'Branch name to fetch for.',
),
),
'api-helper-update-code' => array(
'callback' => 'drush_api_helper_update_code',
'description' => 'Updates git checkouts using the tags from the database.',
'options' => array(
'projects' => 'A list of projects to update code for.',
'branch' => 'Branch name to update code for.',
),
),
'api-helper-get-tag' => array(
'callback' => 'drush_api_helper_get_tag',
'description' => 'Get the calculated tag for an individual project.',
'arguments' => array(
'project' => 'The project for which to get tag information.',
'core' => 'Branch name for the project.',
),
),
'api-helper-dequeue' => array(
'callback' => 'drush_api_helper_dequeue',
'description' => 'Run the job_queue module jobs i.e., parse the API files.',
),
'api-helper-api-update' => array(
'callback' => 'drush_api_helper_api_update',
'description' => 'Update all branches in the API.',
),
);
}
function drush_api_helper_api_update() {
module_load_include('inc', 'api', 'parser');
api_update_all_branches();
}
function drush_api_helper_dequeue() {
module_load_include('module', 'job_queue', 'job_queue');
$query = db_query("SELECT * FROM {job_queue} ORDER BY jqid");
while ($row = db_fetch_object($query)) {
$count = db_result(db_query("SELECT COUNT(*) FROM {job_queue}"));
$arguments = unserialize($row->arguments);
drush_print_r(t('Processing job !count (!jqid) in @branch: @file', array(
'!count' => $count,
'!jqid' => $row->jqid,
'@branch' => $arguments['%branch'],
'@file' => $arguments['%file'],
)));
job_queue_dequeue();
}
}
function drush_api_helper_clone() {
$branch = drush_get_option('branch');
$project = drush_get_option('project');
$contributions_directory = api_helper_get_contributions_directory($branch);
$project_directory = api_helper_get_project_directory($branch, $project);
$git_url = api_helper_git_url($project);
// Clone the project repo into the project directory.
drush_op_system("git clone '$git_url' '$project_directory'");
}
function drush_api_helper_delete_job($job) {
db_query('DELETE FROM {job_queue} WHERE arguments LIKE "%%s%"', array('job' => $job));
}
function drush_api_helper_get_tag($project, $core) {
drush_print(api_helper_get_tag($project, $core));
}
function drush_api_helper_prune() {
$projects = drush_get_option('projects');
$branch = drush_get_option('branch');
$query = 'DELETE FROM {api_helper_contributions}';
$conditions = array();
$args = array();
if ($projects) {
$projects_array = explode(',', $projects);
$conditions[] = 'name IN (' . db_placeholders($projects_array, 'varchar') . ')';
$args += $projects_array;
}
if ($branch) {
$conditions[] = 'core = %d';
$args[] = $branch;
}
if (!empty($conditions)) {
$query .= ' WHERE ' . implode(' AND ', $conditions);
}
db_query($query, $args);
}
function drush_api_helper_update_tracked_branches() {
$branch = drush_get_option('branch');
$project = drush_get_option('project');
// All projects should be updated.
if (empty($project)) {
// Get a list of all projects by scanning the filesystem.
$projects = api_helper_find_projects($branch);
}
// Only a specified list of projects should be updated.
else {
$projects = array($project);
}
$records = array();
foreach ($projects as $project) {
$record = array(
'name' => $project,
'core' => $branch,
'tag' => api_helper_calculate_release($project, $branch),
);
api_helper_update($record);
}
}
function drush_api_helper_fetch_all() {
$branch = drush_get_option('branch');
$projects = api_helper_find_projects($branch);
foreach ($projects as $project) {
$project_directory = api_helper_get_project_directory($branch, $project);
drush_shell_cd_and_exec($project_directory, 'git fetch --all');
$output_array = drush_shell_exec_output();
if (count($output_array) > 1) {
array_shift($output_array);
$output = '<pre>' . implode("\n", $output_array) . '</pre>';
$message = t('Fetched new objects for !project in !branch: !output');
$variables = array(
'!project' => $project,
'!branch' => $branch,
'!output' => $output,
);
watchdog('api_helper', $message, $variables);
}
}
}
function drush_api_helper_update_code() {
$branch = drush_get_option('branch');
$projects = api_helper_list_contributions_by_branch($branch);
foreach ($projects as $row) {
$project = $row['name'];
$project_directory = api_helper_get_project_directory($branch, $project);
if (!file_exists($project_directory)) {
$git_url = api_helper_git_url($project);
drush_op_system("git clone '$git_url' '$project_directory'");
}
// Find the current branch of the repo.
drush_shell_cd_and_exec($project_directory, 'git branch');
$output_array = drush_shell_exec_output();
$current_branch = substr($output_array[0], 2);
// Get the stored tag from the database.
$stored_tag = api_helper_get_tag($project, $branch);
if ($current_branch != $stored_tag) {
drush_shell_cd_and_exec($project_directory, 'git checkout ' . $stored_tag);
}
}
}
function api_helper_git_url($project) {
return 'git://git.drupal.org/project/' . $project . '.git';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment