-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
install_main_menu.py
128 lines (93 loc) · 3.92 KB
/
install_main_menu.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#
# Licensing
#
# Install AmxxEditor Main Menus, install and uninstall package menus
# Copyright (C) 2018 Evandro Coan <https://github.com/evandrocoan>
#
# Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
#
# Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# Neither the name Evandro Coan nor the names of any
# contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or ( at
# your option ) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
# Fix Main Menus
Delete the default main menu for the `Amxmodx` package, because when it updates
we would get duplicated menu entries as the default menu for that package is set
on the `User/Main.sublime-menu` file.
"""
import os
import sublime
import sublime_plugin
from debug_tools import getLogger
from debug_tools.third_part import load_package_file_as_binary
# Debugger settings: 0 - disabled, 127 - enabled
log = getLogger( 1, __name__ )
AMXX_CHANNEL = "AmxxChannel"
MENU_FILE_NAME = "Main.sublime-menu"
CURRENT_PACKAGE_NAME = __package__
PACKAGE_ROOT_DIRECTORY = os.path.dirname( os.path.realpath( __file__ ) )
def plugin_loaded():
global USER_MENU_FILES_DIRECTORY
USER_MENU_FILES_DIRECTORY = os.path.join( sublime.packages_path(), "User", CURRENT_PACKAGE_NAME )
install_amxx_editor_menu_on_first_run()
def install_amxx_editor_menu_on_first_run():
# https://packagecontrol.io/docs/events
from package_control import events
if events.install( CURRENT_PACKAGE_NAME ):
log( 1, 'Installed %s!', events.install( CURRENT_PACKAGE_NAME ) )
from package_control.package_manager import PackageManager
package_manager = PackageManager()
all_packages = set( package_manager.list_packages() )
if AMXX_CHANNEL in all_packages:
amxx_channel_path = os.path.join( PACKAGE_ROOT_DIRECTORY, AMXX_CHANNEL )
if not os.path.exists( amxx_channel_path ):
add_main_menu()
class AmxxEditorInstallMainAmxxEditorMenu( sublime_plugin.ApplicationCommand ):
def run(self):
add_main_menu()
class AmxxEditorUninstallMainAmxxEditorMenu( sublime_plugin.ApplicationCommand ):
def run(self):
remove_main_menu()
def remove_main_menu():
"""
This can only be called after `plugin_loaded()` as being called by Sublime Text.
"""
package_menu_files = os.path.join( USER_MENU_FILES_DIRECTORY, MENU_FILE_NAME )
log( 1, "Removing the file menu file: %s", package_menu_files )
# print( log )
if os.path.exists( package_menu_files ):
os.remove( package_menu_files )
def add_main_menu():
"""
This can only be called after `plugin_loaded()` as being called by Sublime Text.
"""
base_file = os.path.join( PACKAGE_ROOT_DIRECTORY, MENU_FILE_NAME + ".hide" )
destine_file = os.path.join( USER_MENU_FILES_DIRECTORY, MENU_FILE_NAME )
resource_bytes = load_package_file_as_binary( base_file )
text = resource_bytes.decode('utf-8')
with open( destine_file, "w", newline='\n', encoding='utf-8' ) as file:
file.write( text )