Skip to content

Commit a42cc11

Browse files
committed
feat: add basic system utility
1 parent da6f9e1 commit a42cc11

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/Sprout.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Leaf\Sprout\Process\Npm;
1111
use Leaf\Sprout\Style;
1212
use Leaf\Sprout\Prompt;
13+
use Leaf\Sprout\System;
1314

1415
/**
1516
* Leaf Sprout
@@ -53,6 +54,14 @@ public function prompt(array $prompt)
5354
return (new Prompt($prompt))->ask();
5455
}
5556

57+
/**
58+
* Return an instance of system utils
59+
*/
60+
public function system(): System
61+
{
62+
return new System();
63+
}
64+
5665
/**
5766
* Prompt a confirmation
5867
* @param string $message The confirmation message

src/Sprout/System.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Leaf\Sprout;
4+
5+
/**
6+
* Lightweight system utilities
7+
* ---
8+
* System related helper functions for your sprout applications
9+
*/
10+
class System
11+
{
12+
/**
13+
* Check if the current OS is Windows
14+
*/
15+
public static function isWindows(): bool
16+
{
17+
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
18+
}
19+
20+
/**
21+
* Check if the current OS is Linux
22+
*/
23+
public static function isLinux(): bool
24+
{
25+
return PHP_OS === 'Linux';
26+
}
27+
28+
/**
29+
* Check if the current OS is MacOS
30+
*/
31+
public static function isMacOS(): bool
32+
{
33+
return PHP_OS === 'Darwin';
34+
}
35+
36+
/**
37+
* Check if a system command exists
38+
* @return bool
39+
*/
40+
public static function commandExists(string $cmd)
41+
{
42+
if (static::isWindows()) {
43+
return !empty(shell_exec(sprintf('where %s 2>NUL', escapeshellarg($cmd))));
44+
}
45+
46+
return !empty(shell_exec(sprintf('command -v %s 2>/dev/null', escapeshellarg($cmd))));
47+
}
48+
}

0 commit comments

Comments
 (0)