Skip to content

Commit cb7f01b

Browse files
committed
up: add new tool class and update cmd build
1 parent 74a9075 commit cb7f01b

File tree

3 files changed

+197
-5
lines changed

3 files changed

+197
-5
lines changed

Diff for: src/Cmd/AbstractCmdBuilder.php

-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Toolkit\Sys\Cmd;
1111

1212
use RuntimeException;
13-
use Toolkit\Stdlib\Helper\Assert;
1413
use Toolkit\Stdlib\Str;
1514
use Toolkit\Sys\Exec;
1615
use function trim;
@@ -241,10 +240,6 @@ public function getWorkDir(): string
241240
*/
242241
public function setWorkDir(string $workDir): static
243242
{
244-
if ($workDir) {
245-
Assert::isDir($workDir, "workdir is not exists. path: $workDir");
246-
}
247-
248243
$this->workDir = $workDir;
249244
return $this;
250245
}

Diff for: src/Cmd/CmdBuilder.php

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Toolkit\Sys\Cmd;
1111

12+
use Toolkit\Stdlib\Helper\Assert;
1213
use Toolkit\Stdlib\Str;
1314
use function sprintf;
1415

@@ -163,6 +164,10 @@ public function run(bool $printOutput = false): static
163164
{
164165
$this->printOutput = $printOutput;
165166

167+
if ($workDir = $this->workDir) {
168+
Assert::isDir($workDir, "workdir is not exists. path: $workDir");
169+
}
170+
166171
$command = $this->buildCommandLine();
167172
$this->innerExecute($command, $this->workDir);
168173

Diff for: src/Tool/Clipboard.php

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Sys\Tool;
4+
5+
use Toolkit\Stdlib\Obj\AbstractObj;
6+
use Toolkit\Stdlib\OS;
7+
use Toolkit\Sys\Exec;
8+
use function addslashes;
9+
use function file_put_contents;
10+
use function tempnam;
11+
12+
/**
13+
* Class Clipboard
14+
*
15+
* @package Inhere\Kite\Helper
16+
*/
17+
class Clipboard extends AbstractObj
18+
{
19+
public const WRITER_ON_MAC = 'pbcopy';
20+
public const WRITER_ON_WIN = 'clip';
21+
public const WRITER_ON_LINUX = 'xsel';
22+
23+
public const READER_ON_MAC = 'pbpaste';
24+
public const READER_ON_WIN = 'clip';
25+
public const READER_ON_LINUX = 'xclip';
26+
27+
/**
28+
* @var string
29+
*/
30+
private string $writerApp;
31+
32+
/**
33+
* @var string
34+
*/
35+
private string $readerApp;
36+
37+
/**
38+
* @return string
39+
*/
40+
public static function readAll(): string
41+
{
42+
return (new self())->read();
43+
}
44+
45+
/**
46+
* @param string $text
47+
*
48+
* @return bool
49+
*/
50+
public static function writeString(string $text): bool
51+
{
52+
return (new self())->write($text);
53+
}
54+
55+
public function __construct()
56+
{
57+
parent::__construct();
58+
59+
$this->writerApp = $this->getWriterByOS();
60+
$this->readerApp = $this->getReaderByOS();
61+
}
62+
63+
/**
64+
* @param string $contents
65+
* @param bool $addSlashes
66+
*
67+
* @return bool
68+
*/
69+
public function write(string $contents, bool $addSlashes = false): bool
70+
{
71+
$program = $this->writerApp;
72+
if (!$program) {
73+
return false;
74+
}
75+
76+
// $contents = trim($contents);
77+
if ($addSlashes) {
78+
$contents = addslashes($contents);
79+
}
80+
81+
// $contents = str_replace("\n", " \\\n", $contents);
82+
$multiLine = str_contains($contents, "\n");
83+
84+
// linux:
85+
// # Copy input to clipboard
86+
// echo -n "$input" | xclip -selection c
87+
// Mac:
88+
// echo hello | pbcopy
89+
// pbcopy < tempfile.txt
90+
if ($multiLine) {
91+
$file = tempnam(OS::tempDir(), "tmp_");
92+
93+
// File::write($contents, $file);
94+
file_put_contents($file, $contents);
95+
$command = "$program < $file";
96+
} else {
97+
$command = "echo $contents | $program";
98+
}
99+
100+
$result = Exec::auto($command);
101+
return (int)$result['status'] === 0;
102+
}
103+
104+
/**
105+
* @return string
106+
*/
107+
public function read(): string
108+
{
109+
$program = $this->readerApp;
110+
if (!$program) {
111+
return '';
112+
}
113+
114+
$result = Exec::auto($program);
115+
116+
return $result['output'];
117+
}
118+
119+
/**
120+
* @param string $file
121+
*
122+
* @return bool
123+
*/
124+
public function readToFile(string $file): bool
125+
{
126+
$program = $this->readerApp;
127+
if (!$program) {
128+
return false;
129+
}
130+
131+
// Mac: pbpaste >> tasklist.txt
132+
$result = Exec::auto("$program >> $file");
133+
134+
return (int)$result['status'] === 0;
135+
}
136+
137+
/**
138+
* @return string
139+
*/
140+
protected function getWriterByOS(): string
141+
{
142+
if (OS::isWindows()) {
143+
return self::WRITER_ON_WIN;
144+
}
145+
146+
if (OS::isMac()) {
147+
return self::WRITER_ON_MAC;
148+
}
149+
150+
if (OS::isLinux()) {
151+
return self::WRITER_ON_LINUX;
152+
}
153+
154+
return '';
155+
}
156+
157+
/**
158+
* @return string
159+
*/
160+
protected function getReaderByOS(): string
161+
{
162+
if (OS::isWindows()) {
163+
return self::READER_ON_WIN;
164+
}
165+
166+
if (OS::isMac()) {
167+
return self::READER_ON_MAC;
168+
}
169+
170+
if (OS::isLinux()) {
171+
return self::READER_ON_LINUX;
172+
}
173+
174+
return '';
175+
}
176+
177+
/**
178+
* @return string
179+
*/
180+
public function getReaderApp(): string
181+
{
182+
return $this->readerApp;
183+
}
184+
185+
/**
186+
* @return string
187+
*/
188+
public function getWriterApp(): string
189+
{
190+
return $this->writerApp;
191+
}
192+
}

0 commit comments

Comments
 (0)