forked from nicosantangelo/sublime-jasmine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy_run_jasmine.py
executable file
·48 lines (40 loc) · 1.89 KB
/
legacy_run_jasmine.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
import os
import re
import subprocess
import functools
import time
import sublime
import sublime_plugin
class LegacyJasmineSwitchCommand(sublime_plugin.TextCommand):
def window(self):
return self.view.window()
class BaseFile(object):
def __init__(self, file_name): self.folder_name, self.file_name = os.path.split(file_name)
def possible_alternate_files(self): return []
class JavascriptFile(BaseFile):
def possible_alternate_files(self): return [self.file_name.replace(".js", "_spec.js"), self.file_name.replace(".js", ".spec.js")]
class JasmineFile(BaseFile):
def possible_alternate_files(self): return [self.file_name.replace("_spec.js", ".js").replace(".spec.js", ".js")]
def current_file(self):
file_name = self.view.file_name()
if re.search('\w+[_|.]spec.js', file_name):
return LegacyJasmineSwitchCommand.JasmineFile(file_name)
elif re.search('\w+\.js', file_name):
return LegacyJasmineSwitchCommand.JavascriptFile(file_name)
else:
return LegacyJasmineSwitchCommand.BaseFile(file_name)
def run(self, args):
possible_alternates = self.current_file().possible_alternate_files()
alternates = self.project_files(lambda file: file in possible_alternates)
if alternates:
self.window().open_file(alternates.pop())
else:
sublime.error_message("could not find " + str(possible_alternates))
def walk(self, directory, ignored_directories = []):
ignored_directories = ['.git', 'vendor'] # Move this into config
for dir, dirnames, files in os.walk(directory):
dirnames[:] = [dirname for dirname in dirnames if dirname not in ignored_directories]
yield dir, dirnames, files
def project_files(self, file_matcher):
directories = self.window().folders()
return [os.path.join(dirname, file) for directory in directories for dirname, _, files in self.walk(directory) for file in filter(file_matcher, files)]