-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaravelLatestMigration.py
66 lines (53 loc) · 2.02 KB
/
LaravelLatestMigration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sublime, sublime_plugin
import os
import re
import sys
class LaravelLatestMigrationCommand(sublime_plugin.WindowCommand):
def run(self):
try:
# Try to get a path from the currently open file.
cur_path = self.window.active_view().file_name()
except AttributeError:
cur_path = None
# If no file is open, try to get it from the currently open folders.
if cur_path == None:
if self.window.folders():
cur_path = self.window.folders()[0]
if cur_path:
if os.path.isfile(cur_path):
cur_path = os.path.dirname(cur_path)
root = self.find_laravel_root(cur_path)
else:
raise NothingOpen("Please open a file or folder in order to search for the latest migration")
if root:
migrations_dir = os.path.join(root, 'database', 'migrations')
migrations = os.listdir(migrations_dir)
pattern = re.compile('^\d+_\d+_\d+_\d+_\w+.php$')
migrations = sorted([m for m in migrations if pattern.match(m)])
latest_migration = os.path.join(migrations_dir, migrations[-1])
self.window.open_file(latest_migration)
# Recursively searches each up a directory structure for the
# expected items that are common to a Laravel application.
def find_laravel_root(self, path):
expected_items = ['.env', 'app', 'config', 'database']
files = os.listdir(path)
# The recursive search has gone too far and we've reached the system's
# root directory! At this stage it's safe to assume that we won't come
# across a familiar directory structure for a Laravel app.
if path == '/':
raise NotLaravelApp("Cannot recognize this file structure as a Laravel app")
if len([x for x in expected_items if x in files]) == len(expected_items):
return path
else:
return self.find_laravel_root(self.parent_path(path))
# Returns the parent path of the path passed to it.
def parent_path(self, path):
return os.path.abspath(os.path.join(path, '..'))
class Error(Exception):
def __init__(self, msg):
self.msg = msg
sublime.error_message(self.msg)
class NotLaravelApp(Error):
pass
class NothingOpen(Error):
pass