-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublime_cakephp_find_path.py
1598 lines (1481 loc) · 58 KB
/
sublime_cakephp_find_path.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import sublime, sublime_plugin
import json
import re
import os
import sys
import time
import subprocess
import threading
import functools
if sublime.version().startswith('3'):
from .sublime_cakephp_find_inflector import Inflector
elif sublime.version().startswith('2'):
from sublime_cakephp_find_inflector import Inflector
class CakephpFindCoreList:
def __init__(self):
self.set_core_list()
return
def set_core_list(self):
self.core_list = ['']
list = [1, 2, 3]
for version in list:
file_path = sublime.packages_path() + "/sublime-cakephp-find/json/core" + str(version) + ".json"
f = open(file_path)
self.core_list.append('')
self.core_list[version] = json.load(f)
f.close()
# Sublime Text 2
if sublime.version().startswith('2'):
cakephp_find_core_list = CakephpFindCoreList()
# Sublime Text 3
def plugin_loaded():
global cakephp_find_core_list
cakephp_find_core_list = CakephpFindCoreList()
class CommandThread(threading.Thread):
def __init__(self, command):
self.command = command
self.stdin = None
self.stdout = subprocess.PIPE
threading.Thread.__init__(self)
def timeout(self, callback, *args):
sublime.set_timeout(functools.partial(callback, *args), 0)
def print_result(self, message):
if message is not None:
print(message)
def run(self):
callback = self.print_result
try:
if sys.platform.startswith('darwin'): # Mac OS X
output = subprocess.call(self.command)
elif os.name == "posix": # linux
output = subprocess.call(self.command)
elif os.name == "nt": # windows
output = os.startfile(self.command)
self.timeout(callback, output)
except (subprocess.CalledProcessError, e):
self.timeout(callback, e.returncode)
except:
self.timeout(callback, "Error.")
class SearchViewWordThread(threading.Thread):
def __init__(self, parent, search_name, func_match_text, func_return):
self.parent = parent
self.search_name = search_name
self.func_match_text = func_match_text
self.func_return = func_return
threading.Thread.__init__(self)
def run(self):
view_list = self.parent.path.get_file_list_recursive(self.parent.path.folder_path["view"])
find_list = []
for file_path in view_list:
if os.path.exists(file_path):
count = 0
for line in open(file_path):
result = self.func_match_text(line)
if result and result == self.search_name:
find_list.append({"root_path":file_path, 'line_number':count})
count += 1
# delete duplicate
find_list = self.parent.path.delete_duplicate_list_key(find_list, "root_path")
# call next
sublime.set_timeout(functools.partial(self.func_return,
self.parent.view,
find_list,
{"line_number": True}), 0)
class Path:
def __init__(self):
self.execute_extension_list = [
"jpeg", "jpe", "jpg", "gif", "png", "bmp", "dib", "tif", "tiff", "ico",
"doc", "docx", "dot", "dotx", "xls", "xlsx", "ppt", "pptx",
"ai", "dwt", "fla", "indd", "psd", "pdf",
"asf", "asx", "acd", "au", "avi", "aif", "mid", "midi", "mov", "mp3", "mpg", "swf", "wav", "wma",
"zip", "lzh", "tar", "gz", "tgz", "cab",
"exe", "dll", "jar"
]
return
def set_app(self, view, user_settings):
self.dir_path = {}
self.folder_path = {}
self.folder_path['core_top'] = None
self.folder_path['core'] = None
self.view_extension = 'ctp'
self.major_version = None
self.app_dir_name = None
self.open_file_view = None
self.open_file_callback = None
self.open_file_callback_arg = None
self.folder_path['app'] = None
self.folder_path['root'] = None
self.find_app(view)
self.find_core_top(view, user_settings)
if self.major_version is None:
return False
self.set_folder_path()
return True
def find_app(self, view):
dirname = os.path.dirname(self.convert_file_path(view))
count = 0
count_limit = 10
while count < count_limit:
if (os.path.exists(dirname + "/config/core.php") or
os.path.exists(dirname + "/Config/core.php") or
os.path.exists(dirname + "/Config/app.php")):
self.app_dir_name = dirname.split("/")[-1]
self.folder_path['app'] = dirname + "/"
self.folder_path['root'] = os.path.dirname(dirname) + "/"
return
count += 1
dirname = os.path.dirname(dirname)
def find_core_top(self, view, user_settings):
if self.folder_path['app'] is not None:
# find relative
if os.path.exists(self.folder_path['root'] + "cake/VERSION.txt"):
self.folder_path['core_top'] = self.folder_path['root'] + "cake/"
elif os.path.exists(self.folder_path['root'] + "lib/Cake/VERSION.txt"):
self.folder_path['core_top'] = self.folder_path['root'] + "lib/Cake/"
# composer install >= Version 2.1
elif os.path.exists(self.folder_path['root'] + "Vendor/pear-pear.cakephp.org/CakePHP/Cake/VERSION.txt"):
self.folder_path['core_top'] = self.folder_path['root'] + "Vendor/pear-pear.cakephp.org/CakePHP/Cake/"
# composer install >= Version 3.0
elif os.path.exists(self.folder_path['root'] + "vendor/cakephp/cakephp/VERSION.txt"):
self.folder_path['core_top'] = self.folder_path['root'] + "vendor/cakephp/cakephp/Cake/"
# .dotcake
dotcake = self.read_dotcake(self.folder_path['app'])
if dotcake is not None and 'cake' in dotcake:
if ':' in dotcake['cake']:
core_top = self.replace_file_path(os.path.normpath(dotcake['cake']))
else:
core_top = self.replace_file_path(os.path.normpath(self.folder_path['app'] + dotcake['cake']))
# version 2
if os.path.exists(core_top + '/Cake/'):
self.folder_path['core_top'] = core_top + '/Cake/'
# version 1
elif os.path.exists(core_top + '/libs/') and os.path.exists(core_top + '/VERSION.txt'):
self.folder_path['core_top'] = core_top + '/'
# find path by setting option
if (self.folder_path['core_top'] is None and
user_settings is not None and "project_path" in user_settings):
for project in user_settings['project_path']:
if "app" in project and "cake" in project:
app_path = self.add_ptah_tail_slash(self.replace_file_path(project["app"]))
cake_path = self.add_ptah_tail_slash(self.replace_file_path(project["cake"]))
if app_path == self.folder_path['app']:
self.folder_path['core_top'] = cake_path
break
if self.folder_path['core_top'] is not None:
self.get_major_version_from_file()
else:
dirname = os.path.dirname(self.convert_file_path(view))
count = 0
count_limit = 10
while count < count_limit:
# version 1, 2
if (os.path.exists(dirname + "/VERSION.txt") and
os.path.exists(dirname + "/bootstrap.php")):
self.folder_path['core_top'] = dirname + "/"
self.get_major_version_from_file()
break
# version 3
if (os.path.exists(dirname + "/VERSION.txt") and
os.path.exists(dirname + "/Cake")):
self.folder_path['core_top'] = dirname + "/Cake/"
self.get_major_version_from_file()
break
count += 1
dirname = os.path.dirname(dirname)
# find app path
if self.folder_path['core_top'] is not None:
count = 0
app_list = [
"/app/config/core.php",
"/app/Config/core.php",
"/App/Config/app.php",
]
while count < count_limit:
for app_path in app_list:
if (os.path.exists(dirname + app_path)):
self.folder_path['app'] = os.path.dirname(os.path.dirname(dirname + app_path)) + "/"
self.app_dir_name = self.folder_path['app'].split("/")[-2]
self.folder_path['root'] = os.path.dirname(self.folder_path['app'][0:-1]) + "/"
count += count_limit
break
count += 1
dirname = os.path.dirname(dirname)
# find version, root
if self.major_version is None and self.folder_path['app'] is not None:
self.get_major_version_from_path();
if self.folder_path['root'] is None and self.folder_path['core_top'] is not None:
if os.path.dirname(self.folder_path['core_top'][0:-1]).split("/")[-1] == 'lib':
self.folder_path['root'] = os.path.dirname(os.path.dirname(self.folder_path['core_top'][0:-1])) + "/"
elif self.folder_path['core_top'][0:-1].split("/")[-1] == 'cake':
self.folder_path['root'] = os.path.dirname(self.folder_path['core_top'][0:-1]) + "/"
def read_dotcake(self, app_path):
file_path = app_path + ".cake"
if not os.path.exists(file_path):
return None
f = open(file_path)
content = json.load(f)
f.close()
return content
def convert_file_path(self, view):
return self.replace_file_path(view.file_name())
def replace_file_path(self, file_path):
if file_path is None:
file_path = ""
if os.name == "nt":
file_path = file_path.replace("\\", "/")
return file_path
def add_ptah_tail_slash(self, file_path):
if file_path[-1] != '/':
file_path += '/'
return file_path
def get_major_version_from_file(self):
# version 1, 2
path = self.folder_path['core_top'] + "VERSION.txt"
if not os.path.exists(path):
# version 3
path = os.path.dirname(os.path.dirname(self.folder_path['core_top'])) + "/VERSION.txt"
if not os.path.exists(path):
return
for line in open(path, "r"):
match = re.search("([1-9])\.([0-9])\.([0-9])+", line)
if match is not None:
self.major_version = int(match.group(1))
def get_major_version_from_path(self):
if os.path.exists(self.folder_path['app'] + "Config/app.php"):
self.major_version = 3
elif os.path.exists(self.folder_path['app'] + "Controller"):
self.major_version = 2
elif os.path.exists(self.folder_path['app'] + "controllers"):
self.major_version = 1
def set_folder_path(self):
if self.major_version == 1:
self.dir_path['config'] = "config/"
self.dir_path['controller'] = "controllers/"
self.dir_path['model'] = "models/"
self.dir_path['view'] = "views/"
self.dir_path['component'] = "controllers/components/"
self.dir_path['behavior'] = "models/behaviors/"
self.dir_path['helper'] = "views/helpers/"
self.dir_path['lib'] = "libs/"
self.dir_path['authenticate'] = "Controller/Component/Auth/" # not found
self.dir_path['acl'] = "Controller/Component/Acl/" # not found
self.dir_path['datasource'] = "models/datasources/"
self.dir_path['layout'] = "views/layouts/"
self.dir_path['element'] = "views/elements/"
self.dir_path['error'] = "views/errors/"
self.dir_path['email'] = "views/elements/email/"
self.dir_path['email_layout'] = "views/layouts/email/"
self.dir_path['scaffold'] = "views/scaffolds/"
self.dir_path['test'] = "tests/cases/"
self.dir_path['fixture'] = "tests/fixtures/"
self.dir_path['locale'] = "locale/"
self.dir_path['component_test'] = "components/"
self.dir_path['behavior_test'] = "behaviors/"
self.dir_path['helper_test'] = "helpers/"
self.dir_path['vendor'] = "vendors/"
self.folder_path['vendor'] = self.folder_path['app'] + "vendors/"
self.folder_path['plugin'] = self.folder_path['app'] + "plugins/"
self.folder_path['css'] = self.folder_path['app'] + "webroot/css/"
self.folder_path['javascript'] = self.folder_path['app'] + "webroot/js/"
self.folder_path['image'] = self.folder_path['app'] + "webroot/img/"
self.folder_path['tmp'] = self.folder_path['app'] + "tmp/"
self.folder_path['cache'] = self.folder_path['app'] + "tmp/cache/"
if self.folder_path['core_top'] is not None:
self.folder_path['core'] = self.folder_path['core_top'] + "libs/"
self.folder_path['core_test'] = self.folder_path['core_top'] + "tests/cases/libs/"
self.folder_path['core_fixture'] = self.folder_path['core_top'] + "tests/fixtures/"
self.dir_path['core_test_relative'] = "tests/cases/libs/"
self.dir_path['core_controller'] = "controller/"
self.dir_path['core_model'] = "model/"
self.dir_path['core_datasource'] = "model/datasources/"
self.dir_path['core_view'] = "view/"
self.dir_path['core_component'] = "controller/components/"
self.dir_path['core_behavior'] = "model/behaviors/"
self.dir_path['core_helper'] = "view/helpers/"
self.dir_path['core_lib'] = ""
# cake define path
self.folder_path['cake'] = 'cake/'
self.folder_path['cake_core_include_path'] = os.path.dirname(self.folder_path['core_top'][:-1])[:-1]
self.folder_path['core_path'] = ''
self.folder_path['core_test_cases'] = self.folder_path['core_top'] + 'tests/cases/'
if self.folder_path['app'] is not None:
self.folder_path['app_test_cases'] = self.folder_path['app'] + 'cases'
elif self.major_version == 2:
self.dir_path['config'] = "Config/"
self.dir_path['controller'] = "Controller/"
self.dir_path['model'] = "Model/"
self.dir_path['view'] = "View/"
self.dir_path['component'] = "Controller/Component/"
self.dir_path['behavior'] = "Model/Behavior/"
self.dir_path['helper'] = "View/Helper/"
self.dir_path['lib'] = "Lib/"
self.dir_path['authenticate'] = "Controller/Component/Auth/"
self.dir_path['acl'] = "Controller/Component/Acl/"
self.dir_path['datasource'] = "Model/Datasource/"
self.dir_path['layout'] = "View/Layouts/"
self.dir_path['element'] = "View/Elements/"
self.dir_path['error'] = "View/Errors/"
self.dir_path['email'] = "View/Emails/"
self.dir_path['email_layout'] = "View/Layouts/Emails/"
self.dir_path['scaffold'] = "View/Scaffolds/"
self.dir_path['test'] = "Test/Case/"
self.dir_path['fixture'] = "Test/Fixture/"
self.dir_path['locale'] = "Locale/"
self.dir_path['component_test'] = "Controller/Component/"
self.dir_path['behavior_test'] = "Model/Behavior/"
self.dir_path['helper_test'] = "View/Helper/"
self.dir_path['vendor'] = "Vendor/"
self.folder_path['vendor'] = self.folder_path['app'] + "Vendor/"
self.folder_path['plugin'] = self.folder_path['app'] + "Plugin/"
self.folder_path['css'] = self.folder_path['app'] + "webroot/css/"
self.folder_path['javascript'] = self.folder_path['app'] + "webroot/js/"
self.folder_path['image'] = self.folder_path['app'] + "webroot/img/"
self.folder_path['tmp'] = self.folder_path['app'] + "tmp/"
self.folder_path['cache'] = self.folder_path['app'] + "tmp/cache/"
if self.folder_path['core_top'] is not None:
self.folder_path['core'] = self.folder_path['core_top']
self.folder_path['core_test'] = self.folder_path['core_top'] + "Test/Case/"
self.folder_path['core_fixture'] = self.folder_path['core_top'] + "Test/Fixture/"
self.dir_path['core_test_relative'] = self.dir_path['test']
self.dir_path['core_controller'] = "Controller/"
self.dir_path['core_model'] = "Model/"
self.dir_path['core_datasource'] = "Model/Datasource/"
self.dir_path['core_view'] = "View/"
self.dir_path['core_component'] = "Controller/Component/"
self.dir_path['core_behavior'] = "Model/Behavior/"
self.dir_path['core_helper'] = "View/Helper/"
self.dir_path['core_lib'] = "Utility/"
# cake define path
self.folder_path['cake'] = self.folder_path['core_top']
self.folder_path['cake_core_include_path'] = os.path.dirname(self.folder_path['core_top'][0:-1])
self.folder_path['core_path'] = self.folder_path['cake_core_include_path'] + '/'
self.folder_path['core_test_cases'] = self.folder_path['cake'] + 'Test/Case'
if self.folder_path['app'] is not None:
self.folder_path['app_test_cases'] = self.folder_path['app'] + 'Test/Case'
elif self.major_version == 3:
self.dir_path['config'] = "Config/"
self.dir_path['controller'] = "Controller/"
self.dir_path['model'] = "Model/"
self.dir_path['view'] = "View/"
self.dir_path['component'] = "Controller/Component/"
self.dir_path['behavior'] = "Model/Behavior/"
self.dir_path['helper'] = "View/Helper/"
self.dir_path['lib'] = "Lib/"
self.dir_path['authenticate'] = "Controller/Component/Auth/"
self.dir_path['acl'] = "Controller/Component/Acl/"
self.dir_path['datasource'] = "Model/Datasource/"
self.dir_path['vendor'] = "vendor/"
self.dir_path['layout'] = "View/Layout/"
self.dir_path['element'] = "View/Element/"
self.dir_path['error'] = "View/Errors/"
self.dir_path['email'] = "View/Email/"
self.dir_path['email_layout'] = "View/Layout/Email/"
self.dir_path['scaffold'] = "View/Scaffolds/"
self.dir_path['test'] = "Test/TestCase/"
self.dir_path['fixture'] = "Test/Fixture/"
self.dir_path['locale'] = "Locale/"
self.dir_path['component_test'] = "Controller/Component/"
self.dir_path['behavior_test'] = "Model/Behavior/"
self.dir_path['helper_test'] = "View/Helper/"
self.dir_path['vendor'] = "vendor/"
self.folder_path['vendor'] = self.folder_path['root'] + "vendor/"
self.folder_path['plugin'] = self.folder_path['root'] + "Plugin/"
self.folder_path['css'] = self.folder_path['root'] + "webroot/css/"
self.folder_path['javascript'] = self.folder_path['root'] + "webroot/js/"
self.folder_path['image'] = self.folder_path['root'] + "webroot/img/"
self.folder_path['tmp'] = self.folder_path['root'] + "tmp/"
self.folder_path['cache'] = self.folder_path['root'] + "tmp/cache/"
if self.folder_path['core_top'] is not None:
self.folder_path['core'] = self.folder_path['core_top']
self.folder_path['core_test'] = self.folder_path['core_top'] + "Test/TestCase/"
self.folder_path['core_fixture'] = self.folder_path['core_top'] + "Test/Fixture/"
self.dir_path['core_test_relative'] = self.dir_path['test']
self.dir_path['core_controller'] = "Controller/"
self.dir_path['core_model'] = "Model/"
self.dir_path['core_datasource'] = "Model/Datasource/"
self.dir_path['core_view'] = "View/"
self.dir_path['core_component'] = "Controller/Component/"
self.dir_path['core_behavior'] = "Model/Behavior/"
self.dir_path['core_helper'] = "View/Helper/"
self.dir_path['core_lib'] = "Utility/"
# cake define path
self.folder_path['cake'] = self.folder_path['core_top']
self.folder_path['cake_core_include_path'] = os.path.dirname(self.folder_path['core_top'][0:-1])
self.folder_path['core_path'] = self.folder_path['cake_core_include_path'] + '/'
self.folder_path['core_test_cases'] = self.folder_path['cake'] + 'Test/TestCase'
if self.folder_path['app'] is not None:
self.folder_path['app_test_cases'] = self.folder_path['app'] + 'Test/TestCase'
list = [
# common
'config',
'controller',
'model',
'view',
'component',
'behavior',
'helper',
'lib',
'authenticate',
'acl',
'datasource',
'layout',
'element',
'error',
'email',
'email_layout',
'scaffold',
'test',
'fixture',
'locale',
]
if self.folder_path['app'] is not None:
for category in list:
self.folder_path[category] = self.folder_path['app'] + self.dir_path[category]
if self.folder_path['core_top'] is not None:
list = [
'core_controller',
'core_model',
'core_datasource',
'core_view',
'core_component',
'core_behavior',
'core_helper',
'core_lib',
]
for category in list:
self.folder_path[category] = self.folder_path['core'] + self.dir_path[category]
# dotcake
self.folder_path['build'] = {}
if self.folder_path['app'] is not None:
dotcake = self.read_dotcake(self.folder_path['app'])
if dotcake is not None:
list = [
'models',
'behaviors',
'controllers',
'components',
'helpers',
'datasources',
'auths',
'acls',
'libs',
'vendors',
]
for category in list:
if dotcake['build_path'][category] is not None:
self.folder_path['build'][category] = []
for category_path in dotcake['build_path'][category]:
if ':' in category_path:
new_category_path = self.replace_file_path(os.path.normpath(category_path) + '/')
else:
new_category_path = self.replace_file_path(os.path.normpath(self.folder_path['app'] + category_path) + '/')
if os.path.exists(new_category_path):
self.folder_path['build'][category].append(new_category_path)
def get_this_dir(self, view):
return os.path.dirname(self.convert_file_path(view)) + "/"
def match(self, pattern, string):
match = re.search(pattern, string)
if match is None:
return False
else:
return match
def match_controller_file(self, view):
if self.folder_path['app'] is None:
return False
if self.major_version == 1:
regexp = self.folder_path['app'] + "controllers/([a-zA-Z0-9_]+)_controller\.php$"
elif (self.major_version == 2 or
self.major_version == 3):
regexp = self.folder_path['app'] + ".+/([a-zA-Z0-9_]+)Controller\.php$"
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
return match.group(1)
def match_model_file(self, view):
if self.folder_path['app'] is None:
return False
regexp = self.folder_path['model'] + "([^/]+)\.php"
match = self.match(regexp, self.convert_file_path(view))
if (match == False or
self.match_behavior_file(view) != False):
return False
return match.group(1)
def match_view_file(self, view):
if self.folder_path['app'] is None:
return None, None, False
regexp = self.folder_path['view'] + "(([^/]+/)+)([^/.]+)\.([a-z]+)$"
match = self.match(regexp, self.convert_file_path(view))
if (match == False or
self.match_helper_file(view) != False or
self.match_layout_file(view) != False):
return None, None, False
# check controller name
controller_path = match.group(1)
controller_path = controller_path[:len(controller_path)-1]
dir_split = controller_path.split("/")
if len(dir_split) == 1:
return controller_path, match.group(3), match.group(4)
# check list
controller_list = self.get_controller_list()
find_flag = False
for dir_name in dir_split:
complete_name = self.complete_file_name('controller', dir_name)
for contoller_file in controller_list:
if contoller_file == complete_name:
return dir_name, match.group(3), match.group(4)
return None, None, False
def match_component_file(self, view):
if self.folder_path['app'] is None:
return False
if self.major_version == 1:
regexp = (self.folder_path['component'] + "([a-zA-Z0-9_]+)\.php$")
elif (self.major_version == 2 or
self.major_version == 3):
regexp = (self.folder_path['component'] + "([a-zA-Z0-9_]+)Component\.php$")
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
return match.group(1)
def match_behavior_file(self, view):
if self.folder_path['app'] is None:
return False
if self.major_version == 1:
regexp = (self.folder_path['behavior'] + "([a-zA-Z0-9_]+)\.php$")
elif (self.major_version == 2 or
self.major_version == 3):
regexp = (self.folder_path['behavior'] + "([a-zA-Z0-9_]+)Behavior\.php$")
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
return match.group(1)
def match_helper_file(self, view):
if self.folder_path['app'] is None:
return False
if self.major_version == 1:
regexp = (self.folder_path['helper'] + "([a-zA-Z0-9_]+)\.php$")
elif (self.major_version == 2 or
self.major_version == 3):
regexp = (self.folder_path['helper'] + "([a-zA-Z0-9_]+)Helper\.php$")
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
return match.group(1)
def match_layout_file(self, view):
if self.folder_path['app'] is None:
return False
regexp = (self.folder_path['layout'] + "/([^/.]+)\.([a-z]+)$")
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
return match.group(1)
def match_css_file(self, view):
if self.folder_path['app'] is None:
return False
regexp = self.folder_path['css'] + "(.+)\.css$"
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
return match.group(1)
def match_plugin_file(self, view):
if self.folder_path['app'] is None:
return False
regexp = (self.folder_path['plugin'] + "(.+/)*([a-zA-Z0-9_]+)(\.test)?\.php$")
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
#split = match.group(1).split("/")
#plugin_name = split[0]
# match.group(2) : file_name
return True
def match_core_list_file(self, view):
if self.folder_path['core_top'] is None:
return False
regexp = (self.folder_path['core_top'] + "(.+/)*([a-zA-Z0-9_\.]+)\.php$")
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
return match.group(1)
def match_app_file(self, view):
if self.folder_path['app'] is None:
return False
regexp = (self.folder_path['app'] + "(.+/)*([a-zA-Z0-9_\-\.]+)$")
match = self.match(regexp, self.convert_file_path(view))
if match == False:
return False
return match.group(2)
def switch_to_category(self, view, category, name, option_name = None):
if (category == 'controller' or
category == 'model' or
category == 'component' or
category == 'behavior' or
category == 'helper' or
category == 'layout'):
file_path = (self.folder_path[category] + self.complete_file_name(category, name))
elif (category == 'view'):
# option_name : action_name
file_path = (self.folder_path[category] + name + "/" +
self.complete_file_name('view', option_name))
else:
return False
return self.switch_to_file(file_path, view)
def switch_to_file(self, file_path, view, new_flag = False):
if os.path.exists(file_path):
self.open_file(file_path, view)
return True
if new_flag:
if sublime.ok_cancel_dialog("Make new file?"):
open(file_path, "w")
if os.path.exists(file_path):
self.open_file(file_path, view)
return True
else:
sublime.error_message("Can't find " + file_path)
sublime.status_message("Can't switch to file.")
return False
def open_file(self, file_path, view):
self.open_file_view = view.window().open_file(file_path)
if self.open_file_callback is None:
return
thread_parent = self
self.check_open_file_loading()
class OpenFileThread(threading.Thread):
def run(self):
count = 0
while thread_parent.is_open_file_loading and count < 20:
time.sleep(0.1)
sublime.set_timeout(thread_parent.check_open_file_loading, 0)
count += 1
if not thread_parent.is_open_file_loading:
sublime.set_timeout(functools.partial(thread_parent.open_file_callback,
thread_parent.open_file_view,
thread_parent.open_file_callback_arg), 0)
OpenFileThread().start()
def check_open_file_loading(self):
self.is_open_file_loading = self.open_file_view.is_loading()
def set_open_file_callback(self, callback, *arg):
self.open_file_callback = callback
self.open_file_callback_arg = arg
def show_dir_list_by_folder(self, dirname, view):
if self.folder_path['app'] is not None:
self.show_dir_list(self.folder_path[dirname], view)
def show_dir_list(self, dir_path, view):
if not dir_path: return
if self.folder_path['app'] is None: return
self.show_list_view = view
if not dir_path.endswith("/"):
dir_path = dir_path + "/"
dir_list = []
file_list = []
# search dir list
for file in os.listdir(dir_path):
if os.path.isfile(dir_path + file):
file_list.append(file)
else:
dir_list.append(file)
dir_list.sort()
file_list.sort()
# create list
self.show_list_dir = dir_path
self.show_list = []
# out of app dir
if re.match(self.folder_path['root'], dir_path) is None:
return
if dir_path != self.folder_path['root']:
self.show_list = ["../"]
for dir_name in dir_list:
self.show_list.append(dir_name + "/")
for file_name in file_list:
self.show_list.append(file_name)
view.window().show_quick_panel(self.show_list, self.result_select_dir_list)
def result_select_dir_list(self, result):
if result == -1: return
if result == 0 and self.show_list[0] == "../":
self.show_dir_list(os.path.dirname(os.path.dirname(self.show_list_dir)) + "/",
self.show_list_view)
return
# open file or move dir
selected = self.show_list[result]
if selected.endswith("/"):
self.show_dir_list(self.show_list_dir + selected, self.show_list_view)
return
if self.is_execute_extension(self.show_list_dir + selected):
self.execute(self.show_list_dir + selected)
else:
self.switch_to_file(self.show_list_dir + selected, self.show_list_view )
def is_execute_extension(self, path):
for extension in self.execute_extension_list:
if path[-len("." + extension):] == "." + extension:
return True
return False
def execute(self, path):
if sys.platform.startswith('darwin'): # Mac OS X
command = ['open', path]
elif os.name == "posix": # linux
command = ['xdg-open', path]
elif os.name == "nt": # windows
command = path
else:
return
thread = CommandThread(command)
thread.start()
def search_file_recursive(self, search_file_name, root):
# "subdir/file", "root/" -> "file", "root/subdir/"
if len(search_file_name.split("/")) > 1:
dirs = search_file_name.split("/")
search_file_name = dirs.pop()
root = root + "/".join(dirs) + "/"
if not os.path.exists(root):
return False
# check direct
if os.path.exists(root + search_file_name):
return root + search_file_name
list = os.listdir(root)
for name in list:
if os.path.isdir(root + name):
dir_result = self.search_file_recursive(search_file_name, root + name + "/")
if dir_result == False:
continue
return dir_result
return False
def search_class_file_all_dir(self, search_class_name, current_file_type=None):
# 1
# app/controllers/components/
# app/models/
# app/models/behaviors/
# app/models/datasources/
# app/views/helpers/
# app/libs/ ../
# app/vendors/ ../
# app/plugins/****/
# cake/libs/ ../
# 2
# app/Controller/Component/
# app/Controller/Component/Auth/
# app/Controller/Component/Acl/
# app/Model/
# app/Model/Behavior/
# app/Model/Datasource/
# app/View/Helper/
# app/Lib/ ../
# app/Vendor/ ../
# app/Plugin/****/
# lib/Cake/ ../
if self.major_version == 1:
file_name = Inflector().underscore(search_class_name)
elif (self.major_version == 2 or
self.major_version == 3):
file_name = search_class_name
if self.folder_path['app'] is not None:
# check direct
direct_dir_list = ["component", "model", "behavior", "helper", "controller", "datasource", "authenticate", "acl"]
for dir_name in direct_dir_list:
if os.path.exists(self.folder_path[dir_name] + file_name + ".php"):
return self.folder_path[dir_name] + file_name + ".php"
# search recursive
recursive_dir_list = ["lib", "vendor"]
for dir_name in recursive_dir_list:
file_path = self.search_file_recursive(file_name + ".php", self.folder_path[dir_name])
if file_path:
return file_path
add_dir_list = self.get_search_add_dir_list(current_file_type)
# check direct
if (self.major_version == 2 or
self.major_version == 3):
# Find "Comment" ->
# Ver.1 : comment.php
# Ver.2 : CommentComponent.php
new_class_name = search_class_name
if current_file_type == 'view' or current_file_type == 'helper':
new_class_name = self.modify_helper_class_name(new_class_name)
for class_type in add_dir_list:
complete_name = self.complete_file_name(class_type, new_class_name)
if os.path.exists(self.folder_path[class_type] + complete_name):
return self.folder_path[class_type] + complete_name
file_path = self.search_class_file_plugin_all(search_class_name, current_file_type)
if file_path:
return file_path
# build path
build_direct_dir_list = ["components", "models", "behaviors", "helpers", "controllers", "datasources", "auths", "acls"]
for dir_name in build_direct_dir_list:
if self.folder_path['build'][dir_name] is not None:
for dir_path in self.folder_path['build'][dir_name]:
if os.path.exists(dir_path + file_name + ".php"):
return dir_path + file_name + ".php"
build_recursive_dir_list = ["libs", "vendors"]
for dir_name in build_recursive_dir_list:
if self.folder_path['build'][dir_name] is not None:
for dir_path in self.folder_path['build'][dir_name]:
file_path = self.search_file_recursive(file_name + ".php", dir_path)
if file_path:
return file_path
if self.folder_path['core_top'] is not None:
file_path = self.search_core_file(file_name)
if file_path:
return file_path
for class_type in add_dir_list:
file_path = self.search_core_file(self.complete_core_list_name(class_type, file_name))
if file_path: return file_path
return False
def search_class_file_plugin_all(self, search_class_name, current_file_type=None, plugin_name = None):
if self.major_version == 1:
file_name = Inflector().underscore(search_class_name)
if plugin_name is not None:
plugin_name = Inflector().underscore(plugin_name)
elif (self.major_version == 2 or
self.major_version == 3):
file_name = search_class_name
file_path = self.search_plugin_file(file_name + ".php", current_file_type, plugin_name)
if file_path:
return file_path
return False
def search_core_file(self, search_file_name):
path_list = cakephp_find_core_list.core_list[self.major_version]['path']
class_list = cakephp_find_core_list.core_list[self.major_version]['class']
if search_file_name in class_list:
path = path_list[class_list[search_file_name][0]['n']]
file_name = class_list[search_file_name][0]['f']
if file_name == "":
file_name = search_file_name + ".php"
return self.folder_path['core_top'] + path + file_name
return False
def get_search_add_dir_list(self, current_file_type = None):
list = ["component", "helper", "behavior", "authenticate"]
if current_file_type is not None:
# sort list
# because 'Session' word find 'SessionComponent' and 'SessionHelper'
change_file_type = None
if current_file_type == 'controller' or current_file_type == 'component':
change_file_type = 'component'
if current_file_type == 'view' or current_file_type == 'helper':
change_file_type = 'helper'
if current_file_type == 'model' or current_file_type == 'behavior':
change_file_type = 'behavior'
if change_file_type is not None:
list.remove(change_file_type)
list.insert(0, change_file_type)
return list
def modify_helper_class_name(self, class_name):
# change class name : $form->input() -> $Form->input()
if re.match('^[a-z]', class_name) is not None:
class_name = class_name[0:1].upper() + class_name[1:len(class_name)]
return class_name
def search_plugin_file(self, search_file_name, current_file_type = None, plugin_name = None):
direct_sub_dir_list = [
self.dir_path['controller'],
self.dir_path['component'],
self.dir_path['model'],
self.dir_path['behavior'],
self.dir_path['helper'],
self.dir_path['datasource'],
self.dir_path['authenticate'],
self.dir_path['acl'],
]
recursive_sub_dir_list = [
self.dir_path['lib'],
self.dir_path['vendor'],
]
root = self.folder_path['plugin']
if not os.path.exists(root):
return False
list = os.listdir(root)
for name in list:
if os.path.isfile(root + name):
continue
if plugin_name is not None and plugin_name != name:
continue
if os.path.isdir(root + name):
dir_path = root + name + "/"
# check direct
for sub_dir_name in direct_sub_dir_list:
if os.path.exists(dir_path + sub_dir_name + search_file_name):
return dir_path + sub_dir_name + search_file_name
# check recursive
for sub_dir_name in recursive_sub_dir_list:
file_path = self.search_file_recursive(search_file_name, dir_path + sub_dir_name)
if file_path:
return file_path
# check direct
if (self.major_version == 2 or
self.major_version == 3):
list = self.get_search_add_dir_list(current_file_type)
for class_type in list:
complete_file_name = self.complete_file_name(class_type, search_file_name)
if os.path.exists(dir_path + self.dir_path[class_type] + complete_file_name):
return dir_path + self.dir_path[class_type] + complete_file_name
return False
def complete_file_name(self, type, name, ext_flag = True):
ext = add_ext = '.php'
if not ext_flag: add_ext = ''
new_name = self.check_and_remove_tail(name, ext)
if type == 'controller':
if self.major_version == 1:
return self.add_tail(Inflector().underscore(new_name), '_controller') + add_ext
elif (self.major_version == 2 or
self.major_version == 3):
return self.add_tail(Inflector().camelize(new_name), 'Controller') + add_ext
elif type == 'model':
if self.major_version == 1:
return Inflector().underscore(new_name) + add_ext
elif (self.major_version == 2 or
self.major_version == 3):
return Inflector().camelize(new_name) + add_ext
elif type == 'component':
if self.major_version == 1:
return Inflector().underscore(new_name) + add_ext
elif (self.major_version == 2 or
self.major_version == 3):
return self.add_tail(Inflector().camelize(new_name), 'Component') + add_ext