|
| 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 | +} |
0 commit comments