-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
109 lines (90 loc) · 3.81 KB
/
extension.js
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
'use strict';
const {GObject, GLib, Gio, St, Clutter} = imports.gi;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const PhpLaravelValet = GObject.registerClass(
class PhpLaravelValet extends PanelMenu.Button {
_init() {
super._init(0.0, null, false);
this._settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.php-laravel-valet');
this._indicatorText = new St.Label({text: 'Loading...', y_align: Clutter.ActorAlign.CENTER});
this.add_actor(this._indicatorText);
// Initialising the menu with demo item
this.menu.addMenuItem(new PopupMenu.PopupMenuItem('Loading...'));
this._refreshIndicator();
this.menu.connect('open-state-changed', (menu, open) => {
if (open) this._refreshMenu()
});
}
_refreshIndicator() {
const phpVersion = Utils.phpVersion();
if (phpVersion) {
this._indicatorText.set_text(phpVersion);
} else {
this._indicatorText.set_text('PHP not found');
}
}
_refreshMenu() {
this.menu.removeAll();
// valet status menu
const valetStatus = Utils.valetStatus();
if (valetStatus.length > 0) {
valetStatus.forEach(item => {
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(item.replace(/\.\.\./g, '')));
})
} else {
this.menu.addMenuItem(new PopupMenu.PopupMenuItem('Valet not found'));
}
// menu separator
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// switch php sub menu
const phpSubMenu = new PopupMenu.PopupSubMenuMenuItem('Switch PHP');
const phpList = Utils.phpList();
if (phpList.length > 0) {
phpList.forEach(item => {
const subMenu = new PopupMenu.PopupMenuItem('Switch to ' + item);
subMenu.connect('activate', () => this._switchPhp(item));
phpSubMenu.menu.addMenuItem(subMenu);
})
} else {
phpSubMenu.menu.addMenuItem(new PopupMenu.PopupMenuItem('PHP not found'));
}
this.menu.addMenuItem(phpSubMenu);
// valet start/restart menu
const valetRestart = new PopupMenu.PopupMenuItem('Valet start/restart');
valetRestart.connect('activate', () => Utils.valetRestart());
this.menu.addMenuItem(valetRestart);
// valet stop menu
const valetStop = new PopupMenu.PopupMenuItem('Valet stop');
valetStop.connect('activate', () => Utils.valetStop());
this.menu.addMenuItem(valetStop);
}
_switchPhp(version) {
const terminal = this._settings.get_string('default-shell').split(' ');
try {
let proc = Gio.Subprocess.new(
terminal.concat(['valet', 'use', version]),
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
);
proc.communicate_utf8_async(null, null, (proc, res) => {
if (proc.get_successful()) this._refreshIndicator();
});
} catch (e) {
logError(e);
}
}
}
)
let phpLaravelValet = null;
function enable() {
phpLaravelValet = new PhpLaravelValet();
Main.panel.addToStatusArea('php-laravel-valet', phpLaravelValet);
}
function disable() {
phpLaravelValet.destroy();
phpLaravelValet = null;
}