Skip to content

Commit

Permalink
Refactoring + Added Phoenix support
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidakram committed Aug 16, 2018
1 parent b36ea41 commit 51b2268
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
45 changes: 31 additions & 14 deletions LatestMigration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class LatestMigrationCommand(sublime_plugin.WindowCommand):
'shard.yml', 'src', 'config', 'db'
],
'fe': 'sql',
},
'phoenix': {
'folder':'priv/repo//migrations',
'expected_items': [
'mix.exs', 'lib', 'config', 'priv'
],
'fe': 'exs',
}
}

Expand All @@ -40,10 +47,15 @@ def run(self):
raise NothingOpen("Please open a file or folder in order to search for the latest migration")

if root:
migrations_dir = os.path.join(root, LatestMigrationCommand.SUPPORTED_APPS[app_type]['folder'])
migrations_dir = os.path.join(
root,
LatestMigrationCommand.SUPPORTED_APPS[app_type]['folder']
)
migrations = os.listdir(migrations_dir)

pattern = re.compile("^\d+_\w+.{0}$".format(LatestMigrationCommand.SUPPORTED_APPS[app_type]['fe']))
pattern = re.compile(
"^\d+_\w+.{0}$".format(LatestMigrationCommand.SUPPORTED_APPS[app_type]['fe'])
)
migrations = sorted([m for m in migrations if pattern.match(m)])
latest_migration = os.path.join(migrations_dir, migrations[-1])

Expand All @@ -52,22 +64,27 @@ def run(self):
# Recursively searches each up a directory structure for the
# expected items that are common to a Rails/Amber application.
def find_root(self, path):
rails_expected_items = LatestMigrationCommand.SUPPORTED_APPS['rails']['expected_items']
amber_expected_items = LatestMigrationCommand.SUPPORTED_APPS['amber']['expected_items']
files = os.listdir(path)
app_type = None

# 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 supported app.
if path == '/':
raise NotSupportedApp("Cannot recognize this file structure as a Rails or Amber app")
for app, items in LatestMigrationCommand.SUPPORTED_APPS.items():

if len([x for x in rails_expected_items if x in files]) == len(rails_expected_items):
return path, 'rails'
elif len([x for x in amber_expected_items if x in files]) == len(amber_expected_items):
return path, 'amber'
if len([x for x in items['expected_items'] if x in files]) == len(items['expected_items']):
app_type = app
break


if app_type:
return path, app_type
else:
return self.find_root(self.parent_path(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 supported app.
if path == '/':
raise NotSupportedApp("Cannot recognize this file structure as a Rails, Phoenix or Amber app")
else:
return self.find_root(self.parent_path(path))


# Returns the parent path of the path passed to it.
def parent_path(self, path):
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ Opens the latest migration created in a supported web frameworks. Works with Sub

* Ruby on Rails
* Amber
* Phoenix

## Usage

#### Through Command Pallette
#### Through Command Pallette

1. Open any file in your app.
2. Bring up your command pallette (`Cmd + Shift + P` / `Ctrl + Shift + P`) and type in:
Expand All @@ -32,7 +33,7 @@ Opens the latest migration created in a supported web frameworks. Works with Sub
## TODOs

- [x] Amber
- [ ] Phoenix
- [x] Phoenix
- [ ] Any other ?

## Special Thanks
Expand Down

0 comments on commit 51b2268

Please sign in to comment.