Skip to content

Instantly share code, notes, and snippets.

@brad-jones
Created February 6, 2015 06:09
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 brad-jones/9cbb59ef3bf811ef2676 to your computer and use it in GitHub Desktop.
Save brad-jones/9cbb59ef3bf811ef2676 to your computer and use it in GitHub Desktop.
Sublime Text Git Projects

Sublime Text Git Projects

The basic idea is to replicate the same sort of functionality that this atom plugin (https://github.com/prrrnd/atom-git-projects) provides but for Sublime.

I really like Atom for its feature set but its just still too slow. This was one of the things that I missed when I went back to using Sublime.

I tried to get this working as a Python Plugin but I couldn't work out how to edit the session data once Sublime had started. Not to mention I have never coded a line of Python in my life and every line of code I had to google what to do.

TODO: Learn Python lol

Anyway this hacky PHP script combined with an inotify cron job does what I want. I now just have to clone a new project and start sublime and the project already exists.

NOTE: If Sublime is already running the session file wont get updated because when sublime closes it will overwrite it again.

To install incrond

http://www.cyberciti.biz/faq/linux-inotify-examples-to-replicate-directories/

/path/to/all/your/projects IN_CREATE,IN_DELETE /path/to/sublime-git-projects
#!/usr/bin/env php
<?php
// A few config values
$base_path = '/path/to/all/your/projects';
$session_path = '/path/to/your/Session.sublime_session';
// Grab a list of our projects
$projects = [];
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($base_path), RecursiveIteratorIterator::SELF_FIRST) as $name => $object)
{
$parts = explode('/', $name);
if (array_pop($parts) == '.git')
{
$key = $project_name = array_pop($parts);
if (isset($projects[$project_name]))
{
$count = 0;
foreach (array_keys($projects) as $existing_key)
{
if (strpos($existing_key, $project_name) !== false)
{
$count++;
}
}
$key = $project_name.' ('.$count.')';
}
$projects[$key] = implode('/', $parts).'/'.$project_name;
}
}
// This is our new list of workspaces
$workspaces = [];
// Make sure the project config files actually exist, if not lets create them.
foreach ($projects as $name => $path)
{
$workspace_file_path = $path.'/'.$name.'.sublime-workspace';
$project_file_path = $path.'/'.$name.'.sublime-project';
if (!file_exists($workspace_file_path))
{
file_put_contents($workspace_file_path, '{}');
}
if (!file_exists($project_file_path))
{
file_put_contents($project_file_path, json_encode
([
'folders' =>
[
['path' => $path]
]
]));
}
$workspaces[] = $workspace_file_path;
}
// Read in the sublime session json
$session = json_decode(file_get_contents($session_path), true);
// Replace the session workspaces with our new list
$session['workspaces']['recent_workspaces'] = $workspaces;
// Save the new session json
file_put_contents($session_path, json_encode($session));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment