Skip to content

Commit 25f4cc2

Browse files
author
Adam Campbell
authored
Merge pull request #1 from getclair/ohmyzsh-setup
Adds Oh My Zsh setup
2 parents bc0987b + 76c74d6 commit 25f4cc2

14 files changed

+783
-65
lines changed

app/Commands/HelloCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class HelloCommand extends Command
2727
protected $steps = [
2828
'install:cli-tools',
2929
'configure',
30-
'install:shell',
3130
'install:apps',
31+
'install:shell',
3232
'install:repos',
3333
];
3434

app/Commands/InstallCliToolsCommand.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function handle()
4646

4747
foreach ($selections as $selection) {
4848
$this->task("Installing {$selection['name']}", function () use ($selection) {
49-
if ($this->shouldInstallCliTool($selection['check'])) {
49+
if ($this->shouldInstallCliTool($selection)) {
5050
$this->terminal()->output($this)->run($selection['command']);
5151
}
5252

@@ -105,12 +105,16 @@ protected function buildQuestion()
105105
/**
106106
* Check if a CLI tool should be installed, or if it exists already.
107107
*
108-
* @param $check
108+
* @param array $selection
109109
* @return bool
110110
*/
111-
protected function shouldInstallCliTool($check): bool
111+
protected function shouldInstallCliTool(array $selection): bool
112112
{
113-
$response = $this->terminal()->run($check);
113+
if (! array_key_exists('check', $selection)) {
114+
return true;
115+
}
116+
117+
$response = $this->terminal()->run($selection['check']);
114118

115119
return ! $response->ok()
116120
|| $response->getExitCode() === 1

app/Commands/InstallShellCommand.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function handle()
3434
if ($choice !== 'none') {
3535
$config = $options[$choice];
3636

37-
$this->installZsh();
3837
$this->installShell($config);
3938
}
4039
}
@@ -60,23 +59,15 @@ protected function buildQuestion(array $options): string
6059
);
6160
}
6261

63-
/**
64-
* Install zsh.
65-
*/
66-
public function installZsh()
67-
{
68-
if (! $this->terminal()->run('which zsh')->ok()) {
69-
$this->terminal()->output($this)->run('brew install zsh');
70-
}
71-
}
72-
7362
/**
7463
* Install the chosen shell.
7564
*
7665
* @param array $config
7766
*/
7867
public function installShell(array $config)
7968
{
80-
$this->terminal()->output($this)->run($config['command']);
69+
if (! $this->terminal()->run($config['check'])->ok()) {
70+
$this->terminal()->output($this)->run($config['command']);
71+
}
8172
}
8273
}

app/Commands/SetupOhMyZsh.php

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
namespace App\Commands;
4+
5+
use Illuminate\Support\Facades\File;
6+
use ZipArchive;
7+
8+
class SetupOhMyZsh extends StepCommand
9+
{
10+
/**
11+
* The signature of the command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'configure:oh-my-zsh';
16+
17+
/**
18+
* The description of the command.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Configure Oh My Zsh.';
23+
24+
protected static $packageName = 'iterm2env';
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
* @return void
30+
*/
31+
public function handle()
32+
{
33+
$this->newLine();
34+
$this->line('Configuring Oh My Zsh...');
35+
$this->newLine();
36+
37+
$tasks = [
38+
[
39+
'description' => 'Adding Zsh theme...',
40+
'source' => resource_path('config/cobalt2-custom.zsh-theme'),
41+
'destination' => $this->homeDirectory('.oh-my-zsh/themes/cobalt2-clair.zsh-theme'),
42+
],
43+
[
44+
'description' => 'Adding iTerm2 theme...',
45+
'source' => resource_path('config/iTerm2-custom.zsh'),
46+
'destination' => $this->homeDirectory('.oh-my-zsh/custom/iTerm2-clair.zsh'),
47+
],
48+
[
49+
'description' => 'Adding .zshrc...',
50+
'source' => resource_path('config/.zshrc'),
51+
'destination' => $this->homeDirectory('/.zshrc-tmp'),
52+
],
53+
[
54+
'description' => 'Configuring shell aliases...',
55+
'command' => "echo 'alias python2=\"/usr/bin/python\"' >> ~/.zshrc && echo 'alias python=\"/usr/local/bin/python3\"' >> ~/.zshrc && source ~/.zshrc",
56+
],
57+
[
58+
'description' => 'Installing Powerline...',
59+
'command' => 'pip3 install iterm2 && pip3 install --user powerline-status && cd ~ && git clone https://github.com/powerline/fonts && cd fonts && ./install.sh && cd ~',
60+
],
61+
[
62+
'description' => 'Configuring iTerm2...',
63+
'source' => resource_path('scripts/default-profile.py'),
64+
'destination' => $this->homeDirectory('/Library/Application Support/iTerm2/Scripts/AutoLaunch/clair-profile.py'),
65+
'method' => 'installiTerm2Python',
66+
],
67+
];
68+
69+
foreach ($tasks as $task) {
70+
$this->task($task['description'], function () use ($task) {
71+
if (array_key_exists('source', $task)) {
72+
File::copy($task['source'], $task['destination']);
73+
}
74+
75+
if (array_key_exists('command', $task)) {
76+
$this->newLine();
77+
$this->terminal()->output($this)->run($task['command']);
78+
}
79+
80+
if (array_key_exists('method', $task) && method_exists($this, $task['method'])) {
81+
$this->newLine();
82+
call_user_func([$this, $task['method']]);
83+
}
84+
85+
return true;
86+
}, '');
87+
}
88+
89+
$this->newLine();
90+
$this->comment('Oh My Zsh successfully configured.');
91+
$this->newLine();
92+
}
93+
94+
protected function installiTerm2Python()
95+
{
96+
// Get manifest...
97+
$this->comment('Downloading package...');
98+
99+
$manifest = json_decode(file_get_contents('https://iterm2.com/downloads/pyenv/manifest.json'), true);
100+
101+
// Download file locally.
102+
$url = $manifest[0]['url'];
103+
104+
$tempFolder = storage_path('tmp');
105+
$path = $tempFolder.'/env.zip';
106+
107+
if (File::isDirectory($tempFolder)) {
108+
File::deleteDirectory($tempFolder);
109+
}
110+
111+
File::makeDirectory($tempFolder);
112+
113+
$remote_file_contents = file_get_contents($url);
114+
115+
file_put_contents($path, $remote_file_contents);
116+
117+
// Unzip
118+
$this->comment('Unzipping package...');
119+
120+
$zipArchive = new ZipArchive();
121+
122+
if ($zipArchive->open($path)) {
123+
$zipArchive->extractTo($tempFolder);
124+
$zipArchive->close();
125+
126+
// Delete the zip file.
127+
File::delete($path);
128+
129+
$sourceFolder = $tempFolder.'/'.static::$packageName;
130+
131+
// Move files.
132+
$versions = $manifest[0]['python_versions'];
133+
$versions[] = '';
134+
135+
foreach ($versions as $version) {
136+
$folder = static::$packageName;
137+
138+
if (strlen($version) > 0) {
139+
$folder .= '-'.$version;
140+
}
141+
142+
$destinationFolder = $this->homeDirectory('Library/ApplicationSupport/iTerm2/'.$folder);
143+
144+
$this->comment("Moving package to: $destinationFolder");
145+
146+
$this->terminal()->output($this)->with([
147+
'source' => $sourceFolder.'/',
148+
'destination' => $destinationFolder,
149+
])->run('rsync --progress --stats --human-readable --recursive --timeout=300 {{ $source }} {{ $destination }}');
150+
}
151+
152+
// Cleanup
153+
$this->comment('Cleaning up...');
154+
File::deleteDirectory($tempFolder);
155+
}
156+
}
157+
}

app/Commands/StepCommand.php

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Commands;
44

5+
use Illuminate\Support\Str;
56
use LaravelZero\Framework\Commands\Command;
67
use TitasGailius\Terminal\Terminal;
78

@@ -22,6 +23,10 @@ protected function terminal($context = null)
2223
*/
2324
protected function homeDirectory($path = null): string
2425
{
26+
if (Str::startsWith($path, '/')) {
27+
$path = Str::replaceFirst('/', '', $path);
28+
}
29+
2530
return implode('/', [getenv('HOME'), $path]);
2631
}
2732
}

composer.json

+58-47
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,60 @@
11
{
2-
"name": "getclair/hello-clair",
3-
"description": "The Laravel Zero Framework.",
4-
"keywords": ["framework", "laravel", "laravel zero", "console", "cli"],
5-
"type": "project",
6-
"license": "MIT",
7-
"support": {
8-
"source": "https://github.com/getclair/hello-clair"
9-
},
10-
"authors": [
11-
{
12-
"name": "Adam Campbell",
13-
"email": "[email protected]"
14-
}
15-
],
16-
"require": {
17-
"php": "^7.3|^8.0",
18-
"czproject/git-php": "^4.0",
19-
"hotmeteor/eco-env": "^1.1",
20-
"laminas/laminas-text": "^2.8",
21-
"laravel-zero/phar-updater": "^1.0.6",
22-
"nunomaduro/laravel-console-menu": "^3.2",
23-
"nunomaduro/laravel-console-task": "^1.6",
24-
"titasgailius/terminal": "^1.0"
25-
},
26-
"require-dev": {
27-
"laravel-zero/framework": "^8.8",
28-
"mockery/mockery": "^1.4.3",
29-
"pestphp/pest": "^1.3"
30-
},
31-
"autoload": {
32-
"psr-4": {
33-
"App\\": "app/"
34-
}
35-
},
36-
"autoload-dev": {
37-
"psr-4": {
38-
"Tests\\": "tests/"
39-
}
40-
},
41-
"config": {
42-
"preferred-install": "dist",
43-
"sort-packages": true,
44-
"optimize-autoloader": true
45-
},
46-
"minimum-stability": "dev",
47-
"prefer-stable": true,
48-
"bin": ["builds/clair"]
2+
"name": "getclair/hello-clair",
3+
"description": "The Laravel Zero Framework.",
4+
"keywords": [
5+
"framework",
6+
"laravel",
7+
"laravel zero",
8+
"console",
9+
"cli"
10+
],
11+
"type": "project",
12+
"license": "MIT",
13+
"support": {
14+
"source": "https://github.com/getclair/hello-clair"
15+
},
16+
"authors": [
17+
{
18+
"name": "Adam Campbell",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"require": {
23+
"php": "^7.3|^8.0",
24+
"ext-curl": "*",
25+
"ext-json": "*",
26+
"ext-zip": "*",
27+
"czproject/git-php": "^4.0",
28+
"hotmeteor/eco-env": "^1.1",
29+
"laminas/laminas-text": "^2.8",
30+
"laravel-zero/phar-updater": "^1.0.6",
31+
"nunomaduro/laravel-console-menu": "^3.2",
32+
"nunomaduro/laravel-console-task": "^1.6",
33+
"titasgailius/terminal": "^1.0"
34+
},
35+
"require-dev": {
36+
"laravel-zero/framework": "^8.8",
37+
"mockery/mockery": "^1.4.3",
38+
"pestphp/pest": "^1.3"
39+
},
40+
"autoload": {
41+
"psr-4": {
42+
"App\\": "app/"
43+
}
44+
},
45+
"autoload-dev": {
46+
"psr-4": {
47+
"Tests\\": "tests/"
48+
}
49+
},
50+
"config": {
51+
"preferred-install": "dist",
52+
"sort-packages": true,
53+
"optimize-autoloader": true
54+
},
55+
"minimum-stability": "dev",
56+
"prefer-stable": true,
57+
"bin": [
58+
"builds/clair"
59+
]
4960
}

0 commit comments

Comments
 (0)