-
Notifications
You must be signed in to change notification settings - Fork 3
/
generators.py
71 lines (58 loc) · 2.16 KB
/
generators.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
67
68
69
70
71
import os
import re
import sublime
try:
from QuickRails.QuickRails import QuickRailsWindowCommand, get_idea
from QuickRails.QuickExec import ProcessListener
except Exception:
from .QuickRails import QuickRailsWindowCommand, get_idea
from .QuickExec import ProcessListener
class QuickRailsGeneratorsCommand(QuickRailsWindowCommand, ProcessListener):
def is_spring_available(self):
return os.path.isfile('spring') and os.access('spring', os.X_OK)
def run(self):
self.generators = self.get_available_generators()
self.window.show_quick_panel(self.generators, self.on_selected)
def on_selected(self, selected):
if selected == 0:
if self.is_spring_available:
self.run_quick_command("spring rails g", self.window.folders()[0], self)
else:
self.run_quick_command("rails g", self.window.folders()[0], self)
elif selected > 0:
self.generate(self.generators[selected])
def on_data(self, proc, data):
pass
def on_finished(self, proc, alldata):
if alldata:
gens = self.parse_generators(alldata)
self.write_gens_to_file(gens)
self.window.show_quick_panel(gens, self.on_selected)
def generate(self, argument):
self.window.show_input_panel("rails generate", argument + " ", lambda s: self.run_generator(s), None, None)
def run_generator(self, argument):
command = 'rails generate {thing}'.format(thing=argument)
self.run_shell_command(command, self.window.folders()[0])
def parse_generators(self, generators_result):
gens = re.findall(" ([\w:]+)", generators_result)
#print gens
return gens
def write_gens_to_file(self, gens):
gens.sort()
data = "\n".join(gens)
f = open(os.path.join(get_idea(self.get_working_dir()), '.generators'), 'w')
f.write(data)
f.close()
def get_available_generators(self):
try:
f = open(os.path.join(get_idea(self.get_working_dir()), '.generators'), 'r')
data = f.read()
f.close()
gens = data.split()
gens.insert(0, "Update rails generators...")
except IOError:
gens = "".split()
gens.insert(0, "Update rails generators...")
return gens
def is_enabled(args):
return True