Skip to content

Commit

Permalink
Rename bake option "theme" to "template".
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Aug 4, 2014
1 parent bf766e2 commit 3166446
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/Console/Command/BakeShell.php
Expand Up @@ -242,9 +242,9 @@ public function getOptionParser() {
'help' => __d('cake_console', 'Database connection to use in conjunction with `bake all`.'),
'short' => 'c',
'default' => 'default'
])->addOption('theme', [
])->addOption('template', [
'short' => 't',
'help' => __d('cake_console', 'Theme to use when baking code.')
'help' => __d('cake_console', 'Template to use when baking code.')
]);

foreach ($this->_taskMap as $task => $config) {
Expand Down
62 changes: 31 additions & 31 deletions src/Console/Command/Task/TemplateTask.php
Expand Up @@ -31,7 +31,7 @@ class TemplateTask extends Shell {

/**
* Paths to look for templates on.
* Contains a list of $theme => $path
* Contains a list of $template => $path
*
* @var array
*/
Expand All @@ -43,18 +43,18 @@ class TemplateTask extends Shell {
* @return void
*/
public function initialize() {
$this->templatePaths = $this->_findThemes();
$this->templatePaths = $this->_findTemplates();
}

/**
* Find the paths to all the installed shell themes in the app.
* Find the paths to all the installed shell templates in the app.
*
* Bake themes are directories not named `skel` inside a `Console/Templates` path.
* Bake templates are directories under `Template/Bake` path.
* They are listed in this order: app -> plugin -> default
*
* @return array Array of bake themes that are installed.
* @return array Array of bake templates that are installed.
*/
protected function _findThemes() {
protected function _findTemplates() {
$paths = App::path('Template');

$plugins = App::objects('Plugin');
Expand All @@ -66,17 +66,17 @@ protected function _findThemes() {
$Folder = new Folder($core . 'Bake' . DS . 'default');

$contents = $Folder->read();
$themeFolders = $contents[0];
$templateFolders = $contents[0];

$paths[] = $core;

foreach ($paths as $i => $path) {
$paths[$i] = rtrim($path, DS) . DS;
}

$this->_io->verbose('Found the following bake themes:');
$this->_io->verbose('Found the following bake templates:');

$themes = [];
$templates = [];
foreach ($paths as $path) {
$Folder = new Folder($path . 'Bake', false);
$contents = $Folder->read();
Expand All @@ -85,15 +85,15 @@ protected function _findThemes() {
$Folder = new Folder($path . 'Bake' . DS . $dir);
$contents = $Folder->read();
$subDirs = $contents[0];
if (array_intersect($contents[0], $themeFolders)) {
if (array_intersect($contents[0], $templateFolders)) {
$templateDir = $path . 'Bake' . DS . $dir . DS;
$themes[$dir] = $templateDir;
$templates[$dir] = $templateDir;

$this->_io->verbose(sprintf("- %s -> %s", $dir, $templateDir));
}
}
}
return $themes;
return $templates;
}

/**
Expand All @@ -111,8 +111,8 @@ public function generate($directory, $filename, $vars = null) {
if (empty($this->templatePaths)) {
$this->initialize();
}
$themePath = $this->getThemePath();
$templateFile = $this->_findTemplate($themePath, $directory, $filename);
$templatePath = $this->getTemplatePath();
$templateFile = $this->_findTemplate($templatePath, $directory, $filename);
if ($templateFile) {
extract($this->viewVars);
ob_start();
Expand All @@ -125,39 +125,39 @@ public function generate($directory, $filename, $vars = null) {
}

/**
* Find the theme name for the current operation.
* If there is only one theme in $templatePaths it will be used.
* If there is a -theme param in the cli args, it will be used.
* If there is more than one installed theme user interaction will happen
* Find the template name for the current operation.
* If there is only one template in $templatePaths it will be used.
* If there is a -template param in the cli args, it will be used.
* If there is more than one installed template user interaction will happen
*
* @return string returns the path to the selected theme.
* @throws \RuntimeException When the chosen theme cannot be found.
* @return string returns the path to the selected template.
* @throws \RuntimeException When the chosen template cannot be found.
*/
public function getThemePath() {
if (empty($this->params['theme'])) {
$this->params['theme'] = 'default';
public function getTemplatePath() {
if (empty($this->params['template'])) {
$this->params['template'] = 'default';
}
if (!isset($this->templatePaths[$this->params['theme']])) {
$msg = sprintf('Unable to locate "%s" bake theme templates.', $this->params['theme']);
if (!isset($this->templatePaths[$this->params['template']])) {
$msg = sprintf('Unable to locate "%s" bake template', $this->params['template']);
throw new \RuntimeException($msg);
}
$this->_io->verbose(sprintf('Using "%s" bake theme', $this->params['theme']));
return $this->templatePaths[$this->params['theme']];
$this->_io->verbose(sprintf('Using "%s" bake template', $this->params['template']));
return $this->templatePaths[$this->params['template']];
}

/**
* Find a template inside a directory inside a path.
* Will scan all other theme dirs if the template is not found in the first directory.
* Will scan all other template dirs if the template is not found in the first directory.
*
* @param string $path The initial path to look for the file on. If it is not found fallbacks will be used.
* @param string $directory Subdirectory to look for ie. 'views', 'objects'
* @param string $filename lower_case_underscored filename you want.
* @return string filename will exit program if template is not found.
*/
protected function _findTemplate($path, $directory, $filename) {
$themeFile = $path . $directory . DS . $filename . '.ctp';
if (file_exists($themeFile)) {
return $themeFile;
$templateFile = $path . $directory . DS . $filename . '.ctp';
if (file_exists($templateFile)) {
return $templateFile;
}
foreach ($this->templatePaths as $path) {
$templatePath = $path . $directory . DS . $filename . '.ctp';
Expand Down
48 changes: 24 additions & 24 deletions tests/TestCase/Console/Command/Task/TemplateTaskTest.php
Expand Up @@ -49,52 +49,52 @@ public function tearDown() {
}

/**
* test finding themes installed in
* test finding templates installed in
*
* @return void
*/
public function testFindingInstalledThemesForBake() {
public function testFindingInstalledTemplatesForBake() {
$consoleLibs = CAKE . 'Template' . DS;
$this->Task->initialize();
$this->assertPathEquals($this->Task->templatePaths['default'], $consoleLibs . 'Bake/default/');
}

/**
* test using an invalid theme name.
* test using an invalid template name.
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to locate "nope" bake theme
* @expectedExceptionMessage Unable to locate "nope" bake template
* @return void
*/
public function testGetThemePathInvalid() {
$defaultTheme = CAKE . 'Template/Bake/default/';
$this->Task->templatePaths = ['default' => $defaultTheme];
$this->Task->params['theme'] = 'nope';
$this->Task->getThemePath();
public function testGetTemplatePathInvalid() {
$defaultTemplate = CAKE . 'Template/Bake/default/';
$this->Task->templatePaths = ['default' => $defaultTemplate];
$this->Task->params['template'] = 'nope';
$this->Task->getTemplatePath();
}

/**
* test getting the correct theme name. Ensure that with only one theme, or a theme param
* that the user is not bugged. If there are more, find and return the correct theme name
* test getting the correct template name. Ensure that with only one template, or a template param
* that the user is not bugged. If there are more, find and return the correct template name
*
* @return void
*/
public function testGetThemePath() {
$defaultTheme = CAKE . 'Template/Bake/default/';
$this->Task->templatePaths = ['default' => $defaultTheme];
public function testGetTemplatePath() {
$defaultTemplate = CAKE . 'Template/Bake/default/';
$this->Task->templatePaths = ['default' => $defaultTemplate];

$result = $this->Task->getThemePath();
$this->assertEquals($defaultTheme, $result);
$result = $this->Task->getTemplatePath();
$this->assertEquals($defaultTemplate, $result);

$this->Task->templatePaths = ['other' => '/some/path', 'default' => $defaultTheme];
$this->Task->params['theme'] = 'other';
$result = $this->Task->getThemePath();
$this->Task->templatePaths = ['other' => '/some/path', 'default' => $defaultTemplate];
$this->Task->params['template'] = 'other';
$result = $this->Task->getTemplatePath();
$this->assertEquals('/some/path', $result);

$this->Task->params = array();
$result = $this->Task->getThemePath();
$this->assertEquals($defaultTheme, $result);
$this->assertEquals('default', $this->Task->params['theme']);
$result = $this->Task->getTemplatePath();
$this->assertEquals($defaultTemplate, $result);
$this->assertEquals('default', $this->Task->params['template']);
}

/**
Expand All @@ -112,14 +112,14 @@ public function testGenerate() {
}

/**
* test generate with a missing template in the chosen theme.
* test generate with a missing template in the chosen template.
* ensure fallback to default works.
*
* @return void
*/
public function testGenerateWithTemplateFallbacks() {
$this->Task->initialize();
$this->Task->params['theme'] = 'test';
$this->Task->params['template'] = 'test';
$this->Task->set(array(
'name' => 'Article',
'model' => 'Article',
Expand Down

0 comments on commit 3166446

Please sign in to comment.