Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize options #2

Merged
merged 3 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 83 additions & 18 deletions app/Commands/ConfigureSystemCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Commands;

use Illuminate\Support\Facades\File;

class ConfigureSystemCommand extends StepCommand
{
/**
Expand Down Expand Up @@ -36,26 +34,93 @@ public function handle()
*/
protected function configureGithub()
{
$current_username = $this->terminal()->run('git config user.name');
$current_email = $this->terminal()->run('git config user.email');
$this->task('Setting up user settings...', function () {
$this->addUserSettings();

return true;
}, '');

$this->task('Setting up default config...', function () {
$this->addConfigSettings();

return true;
}, '');

$this->task('Setting up authentication...', function () {
$this->setupAuthentication();

return true;
}, '');
}

/**
* Add user values.
*/
protected function addUserSettings()
{
$name = trim((string) $this->terminal()->run('git config --global user.name'));
$email = trim((string) $this->terminal()->run('git config --global user.email'));
$username = trim((string) $this->terminal()->run('git config --global user.username'));

while (strlen($name) === 0) {
$name = $this->ask('What is your name?', trim($name));
}

while (strlen($email) === 0) {
$email = $this->ask('What is your Github user email?', trim($email));
}

while (strlen($username) === 0) {
$username = $this->ask('What is your Github username?', trim($username));
}

$git_username = $this->ask('What is your Git name', trim((string) $current_username));
$git_email = $this->ask('What is your Git email', trim((string) $current_email));
$this->terminal()->output($this)->run("git config --global user.name '$name'");
$this->terminal()->output($this)->run("git config --global user.email '$email'");
$this->terminal()->output($this)->run("git config --global user.username '$username'");
$this->terminal()->output($this)->run("git config --global github.user '$username'");
}

/**
* Add .gitconfig settings.
*/
protected function addConfigSettings()
{
$settings = [
'filters' => [
'clean' => 'git-lfs clean -- %f',
'smudge' => 'git-lfs smudge -- %f',
'process' => 'git-lfs filter-process',
'required' => 'true',
],
'pull' => [
'rebase' => 'false',
],
];

$this->terminal()->output($this)->run("git config --global user.name '$git_username'");
$this->terminal()->output($this)->run("git config --global user.email '$git_email'");
foreach ($settings as $group => $values) {
foreach ($values as $key => $value) {
$this->terminal()->output($this)->run("git config --global $group.$key '$value'");
}
}
}

if ($this->confirm('Do you want to set up Github authentication now?', true)) {
if ($token = $this->secret('Create a token on Github (https://github.com/settings/tokens/new) and enter it')) {
/**
* Set up Github authentication using GCM.
*/
protected function setupAuthentication()
{
if ($this->shouldInstall('which git-credential-manager-core')) {
if ($this->confirm('Do you want to set up Github authentication now?', true)) {
$this->task('Installing GCM...', function () {
if ($this->shouldInstall('which git')) {
$this->terminal()->output($this)->run('brew install git');
}

// Set git to user the OSX keychain.
$this->terminal()->output($this)->run('git config --global credential.helper osxkeychain');
$this->terminal()->output($this)->run('brew tap microsoft/git');
$this->terminal()->output($this)->run('brew install --cask git-credential-manager-core');

// Store the credentials.
File::put(
$this->homeDirectory('.git'),
"https://{$git_username}:{$token}@github.com/{$git_username}"
);
$this->comment('Git Credential Manager was successfully installed. You will be prompted to log in via the browser on your first connection to git.');
});
}
}
}
Expand All @@ -65,7 +130,7 @@ protected function configureGithub()
*/
protected function setupGlobalIgnore()
{
$path = $_SERVER['HOME'].'/.gitignore';
$path = home_path('/.gitignore');

if (! is_file($path)) {
$url = 'https://raw.githubusercontent.com/freekmurze/dotfiles/master/shell/.global-gitignore';
Expand Down
96 changes: 96 additions & 0 deletions app/Commands/ConfigureiTerm2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace App\Commands;

use Illuminate\Support\Facades\File;
use ZipArchive;

class ConfigureiTerm2 extends StepCommand
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'configure:iterm2';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Install the Python API for iTerm2.';

protected static $packageName = 'iterm2env';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
// Get manifest...
$this->comment('Downloading python for iTerm2...');

$manifest = json_decode(file_get_contents('https://iterm2.com/downloads/pyenv/manifest.json'), true);

// Download file locally.
$url = $manifest[0]['url'];

$tempFolder = storage_path('tmp');
$path = $tempFolder.'/env.zip';

if (File::isDirectory($tempFolder)) {
File::deleteDirectory($tempFolder);
}

File::makeDirectory($tempFolder);

$remote_file_contents = file_get_contents($url);

file_put_contents($path, $remote_file_contents);

// Unzip
$this->comment('Unzipping package...');

$zipArchive = new ZipArchive();

if ($zipArchive->open($path)) {
$zipArchive->extractTo($tempFolder);
$zipArchive->close();

// Delete the zip file.
File::delete($path);

$sourceFolder = $tempFolder.'/'.static::$packageName;

// Move files.
$versions = $manifest[0]['python_versions'];
$versions[] = '';

foreach ($versions as $version) {
$folder = static::$packageName;

if (strlen($version) > 0) {
$folder .= '-'.$version;
}

$destinationFolder = $this->homePath('Library/ApplicationSupport/iTerm2/'.$folder);

$this->comment("Moving package to: $destinationFolder");

$this->terminal()->output($this)->with([
'source' => $sourceFolder.'/',
'destination' => $destinationFolder,
])->run('rsync --progress --stats --human-readable --recursive --timeout=300 {{ $source }} {{ $destination }}');
}

// Cleanup
$this->comment('Cleaning up...');
File::deleteDirectory($tempFolder);
}

$this->line('python for iTerm2 installed.');
}
}
59 changes: 13 additions & 46 deletions app/Commands/InstallAppsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ public function handle()

foreach ($selections as $selection) {

// We only want to install an app if it passes checks based on it's "type"
// We only want to install an app if it passes checks based on its "type"
$checks = $config['type'] === 'anyOf' ? [$selection['check']] : Arr::pluck($selections, 'check');

$this->task("Installing {$selection['name']}", function () use ($checks, $selection) {
if ($this->shouldInstallApp($checks)) {
$this->terminal()->output($this)->run($selection['command']);
}

return true;
}, 'installing...');
if ($this->shouldInstall($checks)) {
$this->install($selection);
}
}
}
}
Expand All @@ -60,7 +56,13 @@ public function handle()
*/
protected function getSelections($config): array
{
$choices = $this->buildQuestion($config);
$term = $config['type'] === 'anyOf' ? 'apps' : 'app';

$choices = $this->buildQuestion(
"Select the {$config['group']} {$term} you want to install",
$this->buildOptions($config['options']),
$config['type'] === 'anyOf'
);

if (! is_array($choices)) {
$choices = [$choices];
Expand All @@ -75,48 +77,13 @@ protected function getSelections($config): array
}, $choices);
}

/**
* Build and display apps question.
*
* @param $config
* @param $options
* @return array|string
*/
protected function buildQuestion($config)
{
$term = $config['type'] === 'anyOf' ? 'apps' : 'app';

return $this->choice(
"Select the {$config['group']} {$term} you want to install",
$this->buildOptions($config['options']),
'none', null, $config['type'] === 'anyOf',
);
}

/**
* Build options for apps question.
*
* @param array $options
* @return array
*/
protected function buildOptions(array $options): array
{
$items = ['none' => 'None'];

foreach ($options as $key => $option) {
$items[$key] = "{$option['name']} ({$option['url']})";
}

return $items;
}

/**
* Check if an app should be installed.
*
* @param array $checks
* @param $checks
* @return bool
*/
protected function shouldInstallApp(array $checks): bool
protected function shouldInstall($checks): bool
{
$results = array_filter($checks, function ($check) {
return $this->terminal()->run("mdfind \"kMDItemKind == 'Application'\" | grep -i $check")->ok();
Expand Down
45 changes: 15 additions & 30 deletions app/Commands/InstallCliToolsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ public function handle()
));

foreach ($selections as $selection) {
$this->task("Installing {$selection['name']}", function () use ($selection) {
if ($this->shouldInstallCliTool($selection)) {
$this->terminal()->output($this)->run($selection['command']);
}

return true;
}, 'installing...');
if ($this->shouldInstall($selection['check'])) {
$this->install($selection);
}
}
}

Expand All @@ -63,7 +59,14 @@ public function handle()
*/
protected function getSelections(array $groups): array
{
$choices = $this->buildQuestion();
$choices = $this->buildQuestion(
'Select the environments you want to install',
[
'backend' => 'Backend',
'frontend' => 'Frontend',
],
true
);

if (! is_array($choices)) {
$choices = [$choices];
Expand All @@ -84,37 +87,19 @@ protected function getSelections(array $groups): array
return $selections;
}

/**
* Build and display apps question.
*
* @return array|string
*/
protected function buildQuestion()
{
return $this->choice(
'Select the environments you want to install',
[
'none' => 'None',
'backend' => 'Backend',
'frontend' => 'Frontend',
],
'none', null, true
);
}

/**
* Check if a CLI tool should be installed, or if it exists already.
*
* @param array $selection
* @param $checks
* @return bool
*/
protected function shouldInstallCliTool(array $selection): bool
protected function shouldInstall($checks): bool
{
if (! array_key_exists('check', $selection)) {
if (! array_key_exists('check', $checks)) {
return true;
}

$response = $this->terminal()->run($selection['check']);
$response = $this->terminal()->run($checks['check']);

return ! $response->ok()
|| $response->getExitCode() === 1
Expand Down
Loading