forked from bcharbonnier/SublimeFileSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesync.py
More file actions
105 lines (88 loc) · 2.97 KB
/
Copy pathfilesync.py
File metadata and controls
105 lines (88 loc) · 2.97 KB
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
import os
import sublime
import sublime_plugin
import os.path
import shutil
import functools
import string
_DEBUG = True
_enabled = False
_settings = None
_preferences = None
_settings_filename = "FileSync.sublime-settings"
_preferences_filename = "Preferences.sublime-settings"
class FileSyncOpenSettingsCommand(sublime_plugin.WindowCommand):
def run(self, file):
sublime.active_window().open_file(sublime.packages_path() + file)
def is_enabled(self):
global _enabled
return _enabled
class FileSyncEnableCommand(sublime_plugin.WindowCommand):
def run(self):
global _enabled
_enabled = not _enabled
_preferences.set("filesync_enabled", _enabled)
sublime.save_settings(_preferences_filename)
def is_checked(self):
global _enabled
return _enabled
class FileSyncFilesCommand(sublime_plugin.WindowCommand):
def run(self, files):
pass
def is_visible(self, files):
global _enabled
return _enabled and len(files) > 1 and check_files_syncables(files)
class FileSyncFileCommand(sublime_plugin.WindowCommand):
def run(self, files):
pass
def is_visible(self, files):
global _enabled
return _enabled and len(files) < 2 and check_files_syncables(files)
class FileSyncBuild(sublime_plugin.EventListener):
def on_post_save(self, view):
global _enabled
if _enabled:
self.do_sync(view)
def do_sync(self, view):
global _settings
#print "Filesync: Syncing " + view.file_name()
mappings = _settings.get("mappings")
file = view.file_name()
for mapping in mappings:
source_folder = os.path.abspath(mapping.get('source'))
dest_folder = os.path.abspath(mapping.get('destination'))
#print source_folder, "\n", dest_folder, "\n", file
if (source_folder in file):
dest = file.replace(source_folder, dest_folder)
final_dest_folder = os.path.dirname(dest)
if not os.path.exists(final_dest_folder):
os.makedirs(final_dest_folder)
shutil.copy2(file, dest)
#log(file + " -> " + dest)
self.updateStatus( "FileSync: " + file + " has been synchronised -> " + dest )
def updateStatus(self, text):
sublime.set_timeout(functools.partial(sublime.status_message, text), 1000)
def initFilesync():
global _settings, _preferences, _enabled
_settings = sublime.load_settings(_settings_filename)
_preferences = sublime.load_settings(_preferences_filename)
_enabled = _preferences.get("filesync_enabled")
#print "FileSync: enabled %s" % _enabled
sublime.set_timeout(functools.partial(initFilesync), 1000)
def check_files_syncables(files):
syncs = False
for file in files:
syncs = check_file_syncable(file)
return syncs
def check_file_syncable(file):
sync = False
mappings = _settings.get("mappings")
for mapping in mappings:
source_folder = os.path.abspath(mapping.get('source'))
if source_folder in file:
sync = True
return sync
def log(**kwargs):
print kwargs.keys()
if _DEBUG:
print "FileSync:", string.join(kwargs)