8000 Add grok:install command · grok-php/laravel@c5353be · GitHub
[go: up one dir, main page]

Skip to content

Commit c5353be

Browse files
committed
Add grok:install command
1 parent 7fe47f8 commit c5353be

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

src/Commands/InstallGrokCommand.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace GrokPHP\Laravel\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
8+
class InstallGrokCommand extends Command
9+
{
10+
protected $signature = 'grok:install';
11+
protected $description = 'Prepares the Grok AI client for use in Laravel.';
12+
13+
private const REPO_URL = 'https://github.com/grok-php/laravel';
14+
15+
public function handle(): void
16+
{
17+
// Check if the package is already installed
18+
if ($this->isAlreadyInstalled()) {
19+
$this->warn('⚠️ Grok AI is already installed. No changes were made.');
20+
return;
21+
}
22+
23+
$this->info('🚀 Installing Grok AI for Laravel...');
24+
25+
$this->copyConfig();
26+
$this->addEnvKeys('.env');
27+
$this->addEnvKeys('.env.example');
28+
29+
// Mark installation as complete
30+
$this->markAsInstalled();
31+
32+
$this->info("\n✅ Installation complete! Don't forget to set your API key in your .env file.");
33+
34+
// Ask user to star the repository
35+
if ($this->askToStarRepository()) {
36+
$this->openRepositoryInBrowser();
37+
}
38+
}
39+
40+
/**
41+
* Checks if Grok AI is already installed.
42+
*/
43+
private function isAlreadyInstalled(): bool
44+
{
45+
return File::exists(storage_path('grok_installed.lock'));
46+
}
47+
48+
/**
49+
* Marks the package as installed by creating a lock file.
50+
*/
51+
private function markAsInstalled(): void
52+
{
53+
File::put(storage_path('grok_installed.lock'), now()->toDateTimeString());
54+
}
55+
56+
/**
57+
* Publishes the config file if it doesn't already exist.
58+
*/
59+
private function copyConfig(): void
60+
{
61+
if (file_exists(config_path('grok.php'))) {
62+
$this->warn('⚠️ Config file already exists: config/grok.php');
63+
return;
64+
}
65+
66+
$this->callSilent('vendor:publish', [
67+
'--tag' => 'grok-config',
68+
]);
69+
70+
$this->info('✅ Config file published: config/grok.php');
71+
}
72+
73+
/**
74+
* Adds missing environment variables to the given env file with comments.
75+
*/
76+
private function addEnvKeys(string $envFile): void
77+
{
78+
$filePath = base_path($envFile);
79+
80+
if (! file_exists($filePath)) {
81+
$this->warn("⚠️ Skipping: {$envFile} not found.");
82+
return;
83+
}
84+
85+
$fileContent = file_get_contents($filePath);
86+
87+
// Grok AI environment variables with comments
88+
$envSection = <<<EOL
89+
90+
# --------------------------------------------------------------------------
91+
# 🧠 GROK AI CONFIGURATION
92+
# These variables are required for Grok AI to function in Laravel.
93+
# --------------------------------------------------------------------------
94+
95+
GROK_API_KEY=
96+
GROK_BASE_URI=https://api.x.ai/v1/
97+
GROK_DEFAULT_MODEL=grok-2
98+
GROK_DEFAULT_TEMPERATURE=0.7
99+
GROK_ENABLE_STREAMING=false
100+
GROK_API_TIMEOUT=30
101+
102+
EOL;
103+
104+
// Check if any of the variables already exist
105+
if (str_contains($fileContent, 'GROK_API_KEY')) {
106+
$this->info("{$envFile} is already up to date.");
107+
return;
108+
}
109+
110+
// Append the section to the .env file
111+
file_put_contents($filePath, PHP_EOL . $envSection . PHP_EOL, FILE_APPEND);
112+
113+
$this->info("✅ Added Grok AI environment variables to {$envFile}");
114+
}
115+
116+
/**
117+
* Asks the user if they want to star the GitHub repository.
118+
*/
119+
private function askToStarRepository(): bool
120+
{
121+
if (! $this->input->isInteractive()) {
122+
return false;
123+
}
124+
125+
return $this->confirm('⭐ Want to show Grok AI some love by starring it on GitHub?', false);
126+
}
127+
128+
/**
129+
* Opens the repository in the user's default browser.
130+
*/
131+
private function openRepositoryInBrowser(): void
132+
{
133+
$this->info('Opening GitHub repository... 🌍');
134+
135+
if (PHP_OS_FAMILY === 'Darwin') {
136+
exec('open ' . self::REPO_URL);
137+
} elseif (PHP_OS_FAMILY === 'Windows') {
138+
exec('start ' . self::REPO_URL);
139+
} elseif (PHP_OS_FAMILY === 'Linux') {
140+
exec('xdg-open ' . self::REPO_URL);
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)
0