Skip to content

Commit

Permalink
added init command
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Sep 8, 2015
1 parent de455b9 commit e90976f
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 23 deletions.
14 changes: 10 additions & 4 deletions bin/hidev
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ if (!is_dir($VENDOR)) {
require("$VENDOR/autoload.php");
require("$VENDOR/yiisoft/yii2/Yii.php");

/// Check if it is init command
$isInit = $argv[1]==='init';

/// Look for config file in current directory and up.
$configDir = '.hidev';
for ($i = 0;$i < 9;++$i) {
if (is_dir($configDir)) {
break;
if (!$isInit) {
for ($i = 0;$i < 9;++$i) {
if (is_dir($configDir)) {
break;
}
chdir('..');
}
chdir('..');
}

Yii::setAlias('@prj', $PRJDIR);
Expand All @@ -48,6 +53,7 @@ Yii::setLogger(Yii::createObject('hidev\base\Logger'));
$application = new hidev\base\Application([
'id' => 'hidev',
'name' => 'HiDev - integrate your development',
'isInit' => $isInit,
'basePath' => '@prj/src',
'vendorPath' => '@vendor',
'runtimePath' => '@prj/runtime',
Expand Down
40 changes: 22 additions & 18 deletions src/base/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,33 @@
*/
class Application extends \yii\console\Application implements ViewContextInterface
{
public $isInit = false;

protected $_viewPath;

protected function bootstrap()
{
$require = Yii::createObject([
'class' => 'hidev\base\File',
'path' => '.hidev/config.yml',
])->load()['require'];
if ($require) {
Yii::createObject([
if (!$this->isInit) {
$require = Yii::createObject([
'class' => 'hidev\base\File',
'path' => '.hidev/composer.json',
])->save(compact('require'));
if (!is_dir('.hidev/vendor')) {
exec('cd .hidev;composer update --prefer-source');
}
$main = Yii::getAlias('@vendor/yiisoft/extensions.php');
$local = realpath('./.hidev/vendor/yiisoft/extensions.php');
if ($local !== $main) {
$this->extensions = array_merge(
is_file($main) ? include($main) : [],
is_file($local) ? include($local) : []
);
'path' => '.hidev/config.yml',
])->load()['require'];
if ($require) {
Yii::createObject([
'class' => 'hidev\base\File',
'path' => '.hidev/composer.json',
])->save(compact('require'));
if (!is_dir('.hidev/vendor')) {
exec('cd .hidev;composer update --prefer-source');
}
$main = Yii::getAlias('@vendor/yiisoft/extensions.php');
$local = realpath('./.hidev/vendor/yiisoft/extensions.php');
if ($local !== $main) {
$this->extensions = array_merge(
is_file($main) ? include($main) : [],
is_file($local) ? include($local) : []
);
}
}
}
parent::bootstrap();
Expand Down
5 changes: 4 additions & 1 deletion src/goals/ConfigGoal.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ public function getItem($name)
*/
public function bootstrap($app)
{
if ($app->isInit) {
return;
}
if (!$this->file->find($this->types)) {
throw new InvalidParamException('No config found. Use hidev init');
throw new InvalidParamException('No config found. Use hidev init vendor/package');
}
if ($app->pluginManager->configFiles) {
foreach ($app->pluginManager->configFiles as $path) {
Expand Down
78 changes: 78 additions & 0 deletions src/goals/InitGoal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* HiDev - integrate your development
*
* @link https://hidev.me/
* @package hidev
* @license BSD-3-Clause
* @copyright Copyright (c) 2014-2015, HiQDev (https://hiqdev.com/)
*/

namespace hidev\goals;

use hidev\helpers\Helper;
use yii\base\InvalidParamException;

/**
* Init goal to build files by template and params.
*/
class InitGoal extends TemplateGoal
{
protected $_file = '.hidev/config.yml';

protected $_fileType = 'template';

public function actionPerform($name, $template = '.hidev/config')
{
list($vendor, $package) = explode('/', $name, 2);
if (!$package || !$vendor) {
throw new InvalidParamException('No vendor/package given');
}
$this->vendor = $vendor;
$this->package = $package;
$this->template = $template;

if (!file_exists($this->dirname)) {
mkdir($this->dirname);
}

return parent::actionPerform();
}

public function options($actionId)
{
return array_merge(parent::options($actionId), explode(',', 'label,title,type,keywords,year'));
}

public function getPackage()
{
return $this->getItem('package');
}

public function getTitle()
{
return $this->getItem('title') ?: Helper::titleize($this->package);
}

public function getLabel()
{
return $this->getItem('label') ?: Helper::id2camel($this->package);
}

public function getType()
{
return $this->getItem('type') ?: 'package';
}

public function getKeywords()
{
return $this->getItem('keywords') ?: implode(', ', explode('-', $this->package));
}

public function getYear()
{
return $this->getItem('year') ?: date('Y');
}

}
11 changes: 11 additions & 0 deletions src/views/hidev/config.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package:
name: {{ config.init.package }}
label: {{ config.init.label }}
title: {{ config.init.title }}
type: {{ config.init.type }}
keywords: {{ config.init.keywords }}
year: {{ config.init.year }}

require:
{{ config.init.vendor }}/hidev-config: "*"
hiqdev/hidev-config-php: "*"

0 comments on commit e90976f

Please sign in to comment.