Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Jul 28, 2017
0 parents commit 1ec2d91
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) <Marcel Pociot>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
13 changes: 13 additions & 0 deletions botman
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env php
<?php

if (file_exists(__DIR__.'/../../autoload.php')) {
require __DIR__.'/../../autoload.php';
} else {
require __DIR__.'/vendor/autoload.php';
}

$app = new Symfony\Component\Console\Application('BotMan Installer', '1.0.0');
$app->add(new BotMan\Installer\Console\NewCommand);

$app->run();
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "botman/installer",
"description": "BotMan Studio installer.",
"keywords": ["botman"],
"license": "MIT",
"authors": [
{
"name": "Marcel Pociot",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"BotMan\\Installer\\Console\\": "src/"
}
},
"require": {
"ext-zip": "*",
"guzzlehttp/guzzle": "~4.0|~5.0|~6.0",
"symfony/console": "~2.3|~3.0",
"symfony/filesystem": "~2.3|~3.0",
"symfony/process": "~2.3|~3.0"
},
"bin": [
"botman"
]
}
228 changes: 228 additions & 0 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php

namespace BotMan\Installer\Console;

use ZipArchive;
use RuntimeException;
use GuzzleHttp\Client;
use Symfony\Component\Process\Process;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

class NewCommand extends Command
{
/**
* Configure the command options.
*
* @return void
*/
protected function configure()
{
$this
->setName('new')
->setDescription('Create a new BotMan application.')
->addArgument('name', InputArgument::OPTIONAL)
->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
->addOption('force', null, InputOption::VALUE_NONE, 'Forces install even if the directory already exists');
}

/**
* Execute the command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (! class_exists('ZipArchive')) {
throw new RuntimeException('The Zip PHP extension is not installed. Please install it and try again.');
}

$directory = ($input->getArgument('name')) ? getcwd().'/'.$input->getArgument('name') : getcwd();

if (! $input->getOption('force')) {
$this->verifyApplicationDoesntExist($directory);
}

$output->writeln('<info>Building your next great chatbot...</info>');

$version = $this->getVersion($input);

$this->download($zipFile = $this->makeFilename(), $version)
->extract($zipFile, $directory)
->prepareWritableDirectories($directory, $output)
->cleanUp($zipFile);

$composer = $this->findComposer();

$commands = [
$composer.' install --no-scripts',
$composer.' run-script post-root-package-install',
$composer.' run-script post-install-cmd',
$composer.' run-script post-create-project-cmd',
];

if ($input->getOption('dev')) {
unset($commands[2]);

$commands[] = $composer.' run-script post-autoload-dump';
}

if ($input->getOption('no-ansi')) {
$commands = array_map(function ($value) {
return $value.' --no-ansi';
}, $commands);
}

$process = new Process(implode(' && ', $commands), $directory, null, null, null);

if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
$process->setTty(true);
}

$process->run(function ($type, $line) use ($output) {
$output->write($line);
});

$output->writeln('<comment>BotMan Studio ready! Build an amazing chatbot!</comment>');
}

/**
* Verify that the application does not already exist.
*
* @param string $directory
* @return void
*/
protected function verifyApplicationDoesntExist($directory)
{
if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) {
throw new RuntimeException('Application already exists!');
}
}

/**
* Generate a random temporary filename.
*
* @return string
*/
protected function makeFilename()
{
return getcwd().'/botman_'.md5(time().uniqid()).'.zip';
}

/**
* Download the temporary Zip to the given file.
*
* @param string $zipFile
* @param string $version
* @return $this
*/
protected function download($zipFile, $version = 'master')
{
switch ($version) {
case 'develop':
$filename = 'latest-develop.zip';
break;
case 'master':
$filename = 'latest.zip';
break;
}

$response = (new Client)->get('http://botman.io/studio/'.$filename);

file_put_contents($zipFile, $response->getBody());

return $this;
}

/**
* Extract the Zip file into the given directory.
*
* @param string $zipFile
* @param string $directory
* @return $this
*/
protected function extract($zipFile, $directory)
{
$archive = new ZipArchive;

$archive->open($zipFile);

$archive->extractTo($directory);

$archive->close();

return $this;
}

/**
* Clean-up the Zip file.
*
* @param string $zipFile
* @return $this
*/
protected function cleanUp($zipFile)
{
@chmod($zipFile, 0777);

@unlink($zipFile);

return $this;
}

/**
* Make sure the storage and bootstrap cache directories are writable.
*
* @param string $appDirectory
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return $this
*/
protected function prepareWritableDirectories($appDirectory, OutputInterface $output)
{
$filesystem = new Filesystem;

try {
$filesystem->chmod($appDirectory.DIRECTORY_SEPARATOR."bootstrap/cache", 0755, 0000, true);
$filesystem->chmod($appDirectory.DIRECTORY_SEPARATOR."storage", 0755, 0000, true);
} catch (IOExceptionInterface $e) {
$output->writeln('<comment>You should verify that the "storage" and "bootstrap/cache" directories are writable.</comment>');
}

return $this;
}

/**
* Get the version that should be downloaded.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @return string
*/
protected function getVersion(InputInterface $input)
{
if ($input->getOption('dev')) {
return 'develop';
}

return 'master';
}

/**
* Get the composer command for the environment.
*
* @return string
*/
protected function findComposer()
{
if (file_exists(getcwd().'/composer.phar')) {
return '"'.PHP_BINARY.'" composer.phar';
}

return 'composer';
}
}
8 changes: 8 additions & 0 deletions zipper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
wget https://github.com/botman/studio/archive/master.zip
unzip master.zip -d working
cd working/studio-master
zip -ry ../../latest.zip .
cd ../..
rm -rf working
rm master.zip

0 comments on commit 1ec2d91

Please sign in to comment.