Skip to content

Commit 9f2c38c

Browse files
committed
Initial commit
0 parents  commit 9f2c38c

File tree

7 files changed

+862
-0
lines changed

7 files changed

+862
-0
lines changed

LICENSE.md

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# PHP Laravel Valet
2+
3+
A PHP Laravel Valet status indicator and manager extension (GNOME Panel Applet) for GNOME Shell.
4+
5+
![Screenshot](./screenshot.png)
6+
7+
## Supports
8+
9+
- Tested on Pop!_OS 21.10 and GNOME Shell V40
10+
11+
## Prerequisite
12+
13+
- Properly installed and running [Laravel Valet Linux](https://cpriego.github.io/valet-linux/).
14+
15+
## Installation
16+
17+
- Install this extension from [extensions.gnome.org](https://extensions.gnome.org/extension/4985/php-laravel-valet).
18+
19+
### Or
20+
21+
- Download the zip from the source.
22+
- Create a folder named `php-laravel-valet@rahulhaque` under `~/.local/share/gnome-shell-extensions` folder and extract all the files in this repo.
23+
- Restart GNOME Shell with **[ALT]** + **[F2]**. Type '**r**' and **[Enter]**
24+
- Or logout and log back in.
25+
26+
## Credits
27+
28+
- [Rahul Haque](https://github.com/rahulhaque)
29+
- [All Contributors](../../contributors)
30+
31+
## License
32+
33+
Licensed under [GNU - General Public License v3](https://www.gnu.org/licenses/gpl-3.0.en.html). Please see the [License File](LICENSE.md) for more information.

extension.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict';
2+
3+
const {GObject, GLib, Gio, St, Clutter} = imports.gi;
4+
const Main = imports.ui.main;
5+
const PanelMenu = imports.ui.panelMenu;
6+
const PopupMenu = imports.ui.popupMenu;
7+
const ExtensionUtils = imports.misc.extensionUtils;
8+
const Me = ExtensionUtils.getCurrentExtension();
9+
const Utils = Me.imports.utils;
10+
11+
const Valet = GObject.registerClass(
12+
class Valet extends PanelMenu.Button {
13+
_init() {
14+
super._init(0.0, null, false);
15+
16+
this._indicatorText = new St.Label({text: 'Loading...', y_align: Clutter.ActorAlign.CENTER});
17+
this.add_actor(this._indicatorText);
18+
19+
this.menu.connect('open-state-changed', (menu, open) => {
20+
if (open) this._refreshMenu()
21+
});
22+
23+
this._refreshIndicator();
24+
25+
this._refreshMenu();
26+
}
27+
28+
_refreshIndicator() {
29+
const phpVersion = Utils.phpVersion();
30+
if (phpVersion) {
31+
this._indicatorText.set_text(phpVersion);
32+
} else {
33+
this._indicatorText.set_text('PHP not found');
34+
}
35+
}
36+
37+
_refreshMenu() {
38+
this.menu.removeAll();
39+
40+
// valet status menu
41+
const valetStatus = Utils.valetStatus();
42+
if (valetStatus.length > 0) {
43+
valetStatus.forEach(item => {
44+
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(item.replace(/\.\.\./g, '')));
45+
})
46+
} else {
47+
this.menu.addMenuItem(new PopupMenu.PopupMenuItem('Valet not found'));
48+
}
49+
50+
// menu separator
51+
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
52+
53+
// switch php sub menu
54+
const phpSubMenu = new PopupMenu.PopupSubMenuMenuItem('Switch PHP');
55+
const phpList = Utils.phpList();
56+
if (phpList.length > 0) {
57+
phpList.forEach(item => {
58+
const subMenu = new PopupMenu.PopupMenuItem('Switch to ' + item);
59+
subMenu.connect('activate', () => this._switchPhp(item));
60+
phpSubMenu.menu.addMenuItem(subMenu);
61+
})
62+
} else {
63+
phpSubMenu.menu.addMenuItem(new PopupMenu.PopupMenuItem('PHP not found'));
64+
}
65+
this.menu.addMenuItem(phpSubMenu);
66+
67+
// valet start/restart menu
68+
const valetRestart = new PopupMenu.PopupMenuItem('Valet start');
69+
valetRestart.connect('activate', () => Utils.valetRestart());
70+
this.menu.addMenuItem(valetRestart);
71+
72+
// valet stop menu
73+
const valetStop = new PopupMenu.PopupMenuItem('Valet stop');
74+
valetStop.connect('activate', () => Utils.valetStop());
75+
this.menu.addMenuItem(valetStop);
76+
}
77+
78+
_switchPhp(version) {
79+
try {
80+
let proc = Gio.Subprocess.new(
81+
['x-terminal-emulator', '-e', 'valet', 'use', version],
82+
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
83+
);
84+
85+
proc.communicate_utf8_async(null, null, (proc, res) => {
86+
if (proc.get_successful()) this._refreshIndicator();
87+
});
88+
} catch (e) {
89+
logError(e);
90+
}
91+
}
92+
}
93+
)
94+
95+
let valetIndicator = null;
96+
97+
function enable() {
98+
valetIndicator = new Valet();
99+
Main.panel.addToStatusArea('valet', valetIndicator);
100+
}
101+
102+
function disable() {
103+
valetIndicator.destroy();
104+
valetIndicator = null;
105+
}

icon.png

4.7 KB
Loading

metadata.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "PHP Laravel Valet",
3+
"description": "A PHP Laravel Valet status indicator and manager.",
4+
"uuid": "php-laravel-valet@rahulhaque",
5+
"shell-version": [
6+
"40",
7+
"41",
8+
"42"
9+
],
10+
"version": 2,
11+
"url": "https://github.com/rahulhaque/php-laravel-valet-gnome-shell-extension"
12+
}

screenshot.png

31 KB
Loading

utils.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const Bytes = imports.byteArray;
4+
const GLib = imports.gi.GLib;
5+
6+
function safeSpawn(cmd) {
7+
try {
8+
return GLib.spawn_command_line_sync(cmd);
9+
} catch (e) {
10+
return [false, Bytes.fromString(''), null, null];
11+
}
12+
}
13+
14+
function valetStatus() {
15+
const res = safeSpawn('/bin/bash -c "valet --version && valet status"');
16+
if (res[3] == 0) return Bytes.toString(res[1]).split('\n').filter(item => !!item);
17+
return false;
18+
}
19+
20+
function valetRestart() {
21+
GLib.spawn_command_line_async('x-terminal-emulator -e valet restart');
22+
}
23+
24+
function valetStop() {
25+
GLib.spawn_command_line_async('x-terminal-emulator -e valet stop');
26+
}
27+
28+
function phpVersion() {
29+
const res = safeSpawn('/bin/bash -c "php -v | grep -Po \'PHP\\s+\\d+.\\d+(?:(.\\d+))?\'"');
30+
if (res[3] == 0) return Bytes.toString(res[1]).replace(/\n$/, '');
31+
return false;
32+
}
33+
34+
function phpList() {
35+
const res = safeSpawn('ls /etc/php');
36+
if (res[3] == 0) return Bytes.toString(res[1]).split('\n').filter(item => !!item).reverse();
37+
return false;
38+
}

0 commit comments

Comments
 (0)