Skip to content
This repository was archived by the owner on Oct 25, 2019. It is now read-only.

Commit 84a7e51

Browse files
committed
v2 stable release
1 parent ae4fc31 commit 84a7e51

File tree

8 files changed

+76
-49
lines changed

8 files changed

+76
-49
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
/*/*.log
77

88
# My dirty tests
9-
/src/tests.php
9+
/src/tests.php

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Changelog
22

3+
### v2.0.0 - 2018-04-11 - stable
4+
* clean up code
5+
36
### v2.0.0-beta8 - 2018-04-11
47
* Fix [#28](https://github.com/bchatard/jetbrains-alfred-workflow/issues/28): Notification on new version
58
* add new action under `jb` keyword : _Check workflow update_

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ JetBrains: Open project
33

44
__Alfred3__ workflow to easily open your projects with your favorite JetBrains product.
55

6-
⚠ Currently in beta, feedbacks welcome - [stable](https://github.com/bchatard/jetbrains-alfred-workflow/tree/v1.0)
7-
86

97
## Requirements
108
You need Alfred __3.5+__
68 Bytes
Binary file not shown.

src/Action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct($action)
3434
}
3535

3636
/**
37-
* @param null $query
37+
* @param null|string $query
3838
* @return string
3939
*/
4040
public function execute($query = null)

src/Project.php

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class Project
2525
const XPATH_PROJECT_NAME = "(//component[@name='ProjectView']/panes/pane[@id='ProjectPane']/subPane/PATH/PATH_ELEMENT/option/@value)[1]";
2626
const XPATH_PROJECT_NAME_ALT = "(//component[@name='ProjectView']/panes/pane[@id='ProjectPane']/subPane/expand/path/item[contains(@type, ':ProjectViewProjectNode')]/@name)[1]";
2727
const XPATH_PROJECT_NAME_AS = "((/project/component[@name='ChangeListManager']/ignored[contains(@path, '.iws')]/@path)[1])";
28-
// doesn't works: http://php.net/manual/en/simplexmlelement.xpath.php#93730
29-
// const XPATH_PROJECT_NAME_AS = "substring-before(((/project/component[@name='ChangeListManager']/ignored[contains(@path, '.iws')]/@path)[1]), '.iws')";
3028

3129

3230
/**
@@ -48,7 +46,7 @@ class Project
4846
/**
4947
* @var bool
5048
*/
51-
private $debug;
49+
private $debug = false;
5250
/**
5351
* @var string
5452
*/
@@ -66,7 +64,7 @@ class Project
6664

6765
/**
6866
* @param string $jetbrainsApp
69-
* @throws \RuntimeException
67+
* @throws WorkflowException
7068
*/
7169
public function __construct($jetbrainsApp)
7270
{
@@ -77,11 +75,13 @@ public function __construct($jetbrainsApp)
7775
$this->jetbrainsApp = $jetbrainsApp;
7876
$this->result = new Result();
7977

80-
$this->debug = isset($_SERVER['jb_debug']) ? (bool)$_SERVER['jb_debug'] : false;
78+
if (isset($_SERVER['jb_debug'])) {
79+
$this->debug = (bool)$_SERVER['jb_debug'];
80+
}
8181

8282
$this->cacheDir = $_SERVER['alfred_workflow_cache'];
8383
if (!mkdir($this->cacheDir) && !is_dir($this->cacheDir)) {
84-
throw new \RuntimeException(sprintf('Directory "%s" was not created', $this->cacheDir));
84+
throw new WorkflowException(sprintf('Directory "%s" was not created', $this->cacheDir));
8585
}
8686

8787
if ($this->debug) {
@@ -166,21 +166,7 @@ private function getProjectsData()
166166
return $projectsData;
167167
}
168168

169-
if (is_readable($this->jetbrainsAppConfigPath . self::PATH_RECENT_PROJECT_DIRECTORIES)) {
170-
$this->log(' Work with: ' . self::PATH_RECENT_PROJECT_DIRECTORIES);
171-
$file = $this->jetbrainsAppConfigPath . self::PATH_RECENT_PROJECT_DIRECTORIES;
172-
$xpath = self::XPATH_RECENT_PROJECT_DIRECTORIES;
173-
} elseif (is_readable($this->jetbrainsAppConfigPath . self::PATH_RECENT_PROJECTS)) {
174-
$this->log(' Work with: ' . self::PATH_RECENT_PROJECTS);
175-
$file = $this->jetbrainsAppConfigPath . self::PATH_RECENT_PROJECTS;
176-
$xpath = self::XPATH_RECENT_PROJECTS;
177-
} elseif (is_readable($this->jetbrainsAppConfigPath . self::PATH_RECENT_SOLUTIONS)) {
178-
$this->log(' Work with: ' . self::PATH_RECENT_SOLUTIONS);
179-
$file = $this->jetbrainsAppConfigPath . self::PATH_RECENT_SOLUTIONS;
180-
$xpath = self::XPATH_RECENT_SOLUTIONS;
181-
} else {
182-
throw new \RuntimeException("Can't find 'options' XML in '{$this->jetbrainsAppConfigPath}'", 100);
183-
}
169+
list($file, $xpath) = $this->getFileAndXpath();
184170

185171
$projectsData = [];
186172

@@ -225,6 +211,32 @@ private function getProjectsData()
225211
return $projectsData;
226212
}
227213

214+
/**
215+
* @return array
216+
* @throws WorkflowException
217+
*/
218+
private function getFileAndXpath()
219+
{
220+
$message = ' Work with: %s';
221+
if (is_readable($this->jetbrainsAppConfigPath . self::PATH_RECENT_PROJECT_DIRECTORIES)) {
222+
$this->log(sprintf($message, self::PATH_RECENT_PROJECT_DIRECTORIES));
223+
$file = $this->jetbrainsAppConfigPath . self::PATH_RECENT_PROJECT_DIRECTORIES;
224+
$xpath = self::XPATH_RECENT_PROJECT_DIRECTORIES;
225+
} elseif (is_readable($this->jetbrainsAppConfigPath . self::PATH_RECENT_PROJECTS)) {
226+
$this->log(sprintf($message, self::PATH_RECENT_PROJECTS));
227+
$file = $this->jetbrainsAppConfigPath . self::PATH_RECENT_PROJECTS;
228+
$xpath = self::XPATH_RECENT_PROJECTS;
229+
} elseif (is_readable($this->jetbrainsAppConfigPath . self::PATH_RECENT_SOLUTIONS)) {
230+
$this->log(sprintf($message, self::PATH_RECENT_SOLUTIONS));
231+
$file = $this->jetbrainsAppConfigPath . self::PATH_RECENT_SOLUTIONS;
232+
$xpath = self::XPATH_RECENT_SOLUTIONS;
233+
} else {
234+
throw new WorkflowException("Can't find 'options' XML in '{$this->jetbrainsAppConfigPath}'", 100);
235+
}
236+
237+
return [$file, $xpath];
238+
}
239+
228240
/**
229241
* @param string $path
230242
* @return bool|string
@@ -259,7 +271,7 @@ private function getProjectName($path)
259271
/**
260272
*
261273
* @throws \InvalidArgumentException
262-
* @throws \RuntimeException
274+
* @throws WorkflowException
263275
*/
264276
private function checkJetbrainsApp()
265277
{
@@ -274,34 +286,19 @@ private function checkJetbrainsApp()
274286
if ($handle) {
275287
while (($row = fgets($handle)) !== false) {
276288

277-
foreach ($paths as $var => $field) {
278-
if (strpos($row, "{$var} =") === 0) {
279-
$path = str_replace("{$var} = u", '', $row);
280-
$path = trim($path);
281-
$path = trim($path, "'");
282-
if (is_dir($path) && is_readable($path)) {
283-
$this->$field = $path;
284-
285-
$this->log("{$field}: {$this->$field}");
286-
287-
break;
288-
}
289-
}
290-
291-
}
289+
$this->searchAppAndConfigPath($paths, $row);
292290

293291
if ($this->jetbrainsAppPath && $this->jetbrainsAppConfigPath) {
294292
$this->result->addVariable('bin', $this->jetbrainsApp);
295293

296294
break;
297295
}
298-
299296
}
300297
if (!$this->jetbrainsAppPath) {
301-
throw new \RuntimeException("Can't find application path for '{$this->jetbrainsApp}'");
298+
throw new WorkflowException("Can't find application path for '{$this->jetbrainsApp}'");
302299
}
303300
if (!$this->jetbrainsAppConfigPath) {
304-
throw new \RuntimeException("Can't find application configuration path for '{$this->jetbrainsApp}'");
301+
throw new WorkflowException("Can't find application configuration path for '{$this->jetbrainsApp}'");
305302
}
306303
} else {
307304
throw new \InvalidArgumentException("Can't find command line launcher for '{$this->jetbrainsApp}'");
@@ -316,6 +313,28 @@ private function checkJetbrainsApp()
316313
$this->cache = new Cache($cacheFile);
317314
}
318315

316+
/**
317+
* @param array $paths
318+
* @param string $row
319+
*/
320+
private function searchAppAndConfigPath($paths, $row)
321+
{
322+
foreach ($paths as $var => $field) {
323+
if (strpos($row, "{$var} =") === 0) {
324+
$path = str_replace("{$var} = u", '', $row);
325+
$path = trim($path);
326+
$path = trim($path, "'");
327+
if (is_dir($path) && is_readable($path)) {
328+
$this->$field = $path;
329+
330+
$this->log("{$field}: {$this->$field}");
331+
332+
break;
333+
}
334+
}
335+
}
336+
}
337+
319338
/**
320339
* @param string $name
321340
* @param string $path
@@ -329,7 +348,7 @@ private function addProjectItem($name, $path)
329348
->setSubtitle($path)
330349
->setArg($path)
331350
->setAutocomplete($name)
332-
->setIcon($this->jetbrainsAppPath, 'fileicon')
351+
->setIcon($this->jetbrainsAppPath, Item::ICON_TYPE_FILEICON)
333352
->setText($path, $path)
334353
->setVariables('name', $name);
335354

@@ -348,7 +367,7 @@ private function addNoProjectMatchItem($query)
348367
->setArg('')
349368
->setAutocomplete('')
350369
->setValid(false)
351-
->setIcon($this->jetbrainsAppPath, 'fileicon');
370+
->setIcon($this->jetbrainsAppPath, Item::ICON_TYPE_FILEICON);
352371

353372
$this->result->addItem($item);
354373

@@ -409,7 +428,7 @@ private function addDebugItem()
409428
}
410429

411430
/**
412-
* @param string|array|\stdClass $message
431+
* @param string|array|\stdClass|\Exception $message
413432
*/
414433
private function log($message)
415434
{
@@ -427,3 +446,7 @@ private function log($message)
427446
}
428447

429448
}
449+
450+
class WorkflowException extends \RuntimeException
451+
{
452+
}

src/lib/Item.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
class Item implements JsonSerializable
1111
{
12+
13+
const ICON_TYPE_FILEICON = 'fileicon';
14+
1215
/**
1316
* @var string
1417
*/

src/lib/ProjectName.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class ProjectName
1111
{
1212

13-
private static $LIST_XPATH_PROJECT_NAME = [
13+
private $listXpathProjectName = [
1414
'value' => Project::XPATH_PROJECT_NAME,
1515
'name' => Project::XPATH_PROJECT_NAME_ALT,
1616
];
@@ -55,7 +55,7 @@ public function getViaWorkspace($path, callable $logger)
5555
$logger(' Work with .idea/workspace.xml');
5656
$workspaceXml = new SimpleXMLElement($path, null, true);
5757

58-
foreach (static::$LIST_XPATH_PROJECT_NAME as $field => $xpath) {
58+
foreach ($this->listXpathProjectName as $field => $xpath) {
5959
$logger(" try with {$xpath} || $field");
6060
$nameElements = $workspaceXml->xpath($xpath);
6161
if (count($nameElements) > 0 && isset($nameElements[0]->$field)) {

0 commit comments

Comments
 (0)