|
| 1 | +import os |
1 | 2 | import sublime
|
2 |
| -import sys |
3 | 3 | import subprocess
|
4 |
| -import os |
| 4 | +import sys |
5 | 5 |
|
6 | 6 |
|
7 | 7 | def plugin_loaded():
|
| 8 | + logfile = os.path.join( |
| 9 | + sublime.packages_path(), "0_install_package_control_helper", "log" |
| 10 | + ) |
8 | 11 |
|
9 |
| - pc_settings = sublime.load_settings("Package Control.sublime-settings") |
| 12 | + log = open(logfile, "a", encoding="utf-8") |
10 | 13 |
|
11 |
| - logfile = os.path.join( |
12 |
| - sublime.packages_path(), |
13 |
| - "0_install_package_control_helper", |
14 |
| - "log") |
| 14 | + sys.stdout = log |
| 15 | + sys.stderr = log |
15 | 16 |
|
16 | 17 | def kill_subl(restart=False):
|
17 | 18 | if sublime.platform() == "osx":
|
18 | 19 | cmd = "sleep 1; pkill [Ss]ubl; pkill plugin_host; sleep 1; "
|
19 | 20 | if restart:
|
20 |
| - cmd = cmd + "osascript -e 'tell application \"Sublime Text\" to activate'" |
| 21 | + cmd = ( |
| 22 | + cmd + "osascript -e 'tell application \"Sublime Text\" to activate'" |
| 23 | + ) |
21 | 24 | elif sublime.platform() == "linux":
|
22 | 25 | cmd = "sleep 1; pkill [Ss]ubl; pkill plugin_host; sleep 1; "
|
23 | 26 | if restart:
|
24 | 27 | cmd = cmd + "subl"
|
25 | 28 | elif sublime.platform() == "windows":
|
26 | 29 | cmd = "sleep 1 & taskkill /F /im sublime_text.exe & sleep 1 "
|
27 | 30 | if restart:
|
28 |
| - cmd = cmd + "& \"{}\"".format(sublime.executable_path()) |
| 31 | + cmd = cmd + '& "{}"'.format(sublime.executable_path()) |
| 32 | + else: |
| 33 | + return |
29 | 34 |
|
30 | 35 | subprocess.Popen(cmd, shell=True)
|
31 | 36 |
|
32 | 37 | def touch(file_name):
|
33 | 38 | f = os.path.join(
|
34 |
| - sublime.packages_path(), |
35 |
| - "0_install_package_control_helper", |
36 |
| - file_name) |
37 |
| - open(f, 'a').close() |
| 39 | + sublime.packages_path(), "0_install_package_control_helper", file_name |
| 40 | + ) |
| 41 | + open(f, "a").close() |
38 | 42 |
|
39 |
| - def check_bootstrap(): |
40 |
| - if pc_settings.get("bootstrapped", False): |
41 |
| - touch("bootstrapped") |
42 |
| - kill_subl(True) |
| 43 | + def satisfy_libraries(): |
| 44 | + if "Package Control" in sys.modules: |
| 45 | + package_control = sys.modules["Package Control"].package_control |
43 | 46 | else:
|
44 |
| - sublime.set_timeout(check_bootstrap, 5000) |
45 |
| - |
46 |
| - def check_dependencies(): |
47 |
| - if 'Package Control' in sys.modules: |
48 |
| - package_control = sys.modules['Package Control'].package_control |
49 |
| - else: |
50 |
| - sublime.set_timeout(check_dependencies, 5000) |
| 47 | + sublime.set_timeout(satisfy_libraries, 5000) |
51 | 48 | return
|
52 | 49 |
|
53 | 50 | manager = package_control.package_manager.PackageManager()
|
54 |
| - required_dependencies = set(manager.find_required_dependencies()) |
55 |
| - |
56 |
| - class myPackageCleanup(package_control.package_cleanup.PackageCleanup): |
57 |
| - |
58 |
| - def finish(self, installed_packages, found_packages, found_dependencies): |
59 |
| - missing_dependencies = required_dependencies - set(found_dependencies) |
60 | 51 |
|
61 |
| - if len(missing_dependencies) == 0: |
62 |
| - touch("success") |
63 |
| - kill_subl() |
64 |
| - else: |
65 |
| - with open(logfile, "a") as f: |
66 |
| - f.write("missing dependencies:" + "\n") |
67 |
| - f.write(" ".join(missing_dependencies) + "\n") |
68 |
| - sublime.set_timeout(_check_dependencies, 5000) |
69 |
| - |
70 |
| - def _check_dependencies(): |
71 |
| - myPackageCleanup().run() |
72 |
| - |
73 |
| - _check_dependencies() |
| 52 | + # query and install missing libraries |
| 53 | + required_libraries = manager.find_required_libraries() |
| 54 | + if required_libraries: |
| 55 | + manager.install_libraries(required_libraries, fail_early=False) |
| 56 | + |
| 57 | + # re-query missing libraries |
| 58 | + missing_libraries = manager.find_missing_libraries( |
| 59 | + required_libraries=required_libraries |
| 60 | + ) |
| 61 | + if missing_libraries: |
| 62 | + log.write("missing dependencies:" + "\n") |
| 63 | + log.write(" ".join(sorted(missing_libraries)) + "\n") |
| 64 | + else: |
| 65 | + touch("success") |
74 | 66 |
|
75 |
| - if not os.path.exists(os.path.join( |
76 |
| - sublime.packages_path(), |
77 |
| - "0_install_package_control_helper", |
78 |
| - "bootstrapped")): |
79 |
| - check_bootstrap() |
80 |
| - else: |
81 |
| - # restart sublime when `sublime.error_message` is run |
82 |
| - def error_message(message): |
83 |
| - with open(logfile, "a") as f: |
84 |
| - f.write(message + "\n") |
| 67 | + kill_subl() |
85 | 68 |
|
86 |
| - kill_subl(True) |
| 69 | + # restart sublime when `sublime.error_message` is run |
| 70 | + def error_message(message): |
| 71 | + # log.write(message + "\n") |
| 72 | + kill_subl(True) |
87 | 73 |
|
88 |
| - sublime.error_message = error_message |
89 |
| - check_dependencies() |
| 74 | + sublime.error_message = error_message |
| 75 | + sublime.set_timeout(satisfy_libraries, 5000) |
0 commit comments