Skip to content

Commit d02dca9

Browse files
committed
Add check command for debugging
Fix several bugs where strings were not being returned Fix choices helper, return default if there are no choices
1 parent aae7e77 commit d02dca9

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ somnambulist:
100100

101101
```
102102

103+
You can check the current `spm` setup by using `spm check`. This will show you all the
104+
current configuration locations and if the configuration was initialised properly. Add
105+
`--debug` or `-d` to output the config and env file contents if they exist.
106+
103107
### Terminal and IDE integration
104108

105109
spm includes two helpers to make navigating a project easier: `open` and `goto`. `open` will

src/Commands/CheckCommand.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Somnambulist\ProjectManager\Commands;
4+
5+
use Somnambulist\ProjectManager\Commands\Behaviours\ProjectConfigAwareCommand;
6+
use Somnambulist\ProjectManager\Contracts\ProjectConfigAwareInterface;
7+
use Somnambulist\ProjectManager\Models\Template;
8+
use Symfony\Component\Console\Helper\Table;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use function file_exists;
13+
use function file_get_contents;
14+
use function filesize;
15+
use function is_dir;
16+
use function trim;
17+
use const DIRECTORY_SEPARATOR;
18+
19+
/**
20+
* Class CheckCommand
21+
*
22+
* @package Somnambulist\ProjectManager\Commands
23+
* @subpackage Somnambulist\ProjectManager\Commands\CheckCommand
24+
*/
25+
class CheckCommand extends AbstractCommand implements ProjectConfigAwareInterface
26+
{
27+
28+
use ProjectConfigAwareCommand;
29+
30+
protected function configure()
31+
{
32+
$this
33+
->setName('check')
34+
->setDescription('Check spm configuration for issues to help with debugging')
35+
->addOption('debug', 'd', InputOption::VALUE_NONE, 'Display debug information with check data')
36+
;
37+
}
38+
39+
protected function execute(InputInterface $input, OutputInterface $output)
40+
{
41+
$this->setupConsoleHelper($input, $output);
42+
43+
$dir = $_SERVER['SOMNAMBULIST_PROJECTS_CONFIG_DIR'];
44+
$cfg = $dir . DIRECTORY_SEPARATOR . 'project_manager.yaml';
45+
$env = $dir . DIRECTORY_SEPARATOR . '.env';
46+
47+
$this->tools()->info('spm config dir: <info>%s</info>', $dir);
48+
$this->tools()->info('spm config dir exists: <info>%s</info>', (is_dir($dir) ? 'yes' : 'no'));
49+
$this->tools()->info('spm config file: <info>%s</info>', $cfg);
50+
$this->tools()->info('spm config file initialised: <info>%s</info>', (0 !== filesize($cfg) ? 'yes' : 'no'));
51+
$this->tools()->info('spm config file exists: <info>%s</info>', (file_exists($cfg) ? 'yes' : 'no'));
52+
53+
if (!file_exists($cfg) || 0 === filesize($cfg)) {
54+
$this->tools()->warning('config file has not been properly initialised, re-run <info>spm init</info>');
55+
$this->tools()->newline();
56+
57+
return 0;
58+
}
59+
60+
if ($input->getOption('debug') && file_exists($cfg)) {
61+
$this->tools()->info('spm config');
62+
$this->tools()->message(trim(file_get_contents($cfg)));
63+
}
64+
65+
$this->tools()->newline();
66+
$this->tools()->info('spm environment file: <info>%s</info>', $env);
67+
$this->tools()->info('spm environment file exists: <info>%s</info>', (file_exists($env) ? 'yes' : 'no'));
68+
69+
if ($input->getOption('debug') && file_exists($env)) {
70+
$this->tools()->info('spm environment');
71+
$this->tools()->message(trim(file_get_contents($env)));
72+
}
73+
74+
$this->tools()->newline();
75+
$info = $this->config->active() ? sprintf('(includes templates for <info>%s</info>)', $this->config->active()) : '';
76+
77+
$this->tools()->info('configured templates %s', $info);
78+
79+
$table = new Table($output);
80+
$table->setHeaders(['Name', 'Type', 'Source']);
81+
82+
$this->config->templates()->list()->each(function (Template $template) use ($table) {
83+
$table->addRow([$template->name(), $template->type(), $template->source() ?: '~']);
84+
});
85+
86+
$table->render();
87+
$this->tools()->newline();
88+
89+
return 0;
90+
}
91+
}

src/Models/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public function home(): string
5757

5858
public function projectsDir(): string
5959
{
60-
return $this->config->get('projects_dir');
60+
return $this->config->value('projects_dir', 'Projects');
6161
}
6262

6363
public function active(): string
6464
{
65-
return $_SERVER['SOMNAMBULIST_ACTIVE_PROJECT'];
65+
return $_SERVER['SOMNAMBULIST_ACTIVE_PROJECT'] ?? '';
6666
}
6767

6868
public function config(): FrozenCollection

src/Services/Console/ConsoleHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public function ask(string $question, bool $confirm = true, string ...$args)
116116

117117
public function choose(string $question, array $choices = [], $default = null)
118118
{
119+
if (empty($choices)) {
120+
return $default;
121+
}
122+
119123
$h = new QuestionHelper();
120124
$h->setHelperSet($this->helperSet);
121125

0 commit comments

Comments
 (0)