Skip to content

Commit c9fb467

Browse files
authored
Add the correct extension when needed. (#15)
1 parent 3abb57a commit c9fb467

File tree

4 files changed

+54
-33
lines changed

4 files changed

+54
-33
lines changed

build.png

-2.38 KB
Loading

build.xml

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22

3-
<project name="VisualizerTask" description="Create a diagram from buildfile" default="help">
3+
<project name="VisualizerTask" description="Create buildfile map" default="help">
44

55
<target name="setup" depends="composer:install"
66
description="Prepare project for development"/>
@@ -9,19 +9,22 @@
99

1010
<target name="help" description="Create buildfile diagram">
1111
<uptodate property="uptodate.build.xml" srcfile="build.xml" targetfile="build.png"/>
12-
<runtarget target="visualizer"/>
12+
<runtarget target="map"/>
1313
<open path="build.png"/>
1414
</target>
1515

16-
<target name="visualizer" unless="uptodate.build.xml">
16+
<target name="map" unless="uptodate.build.xml">
1717
<visualizer format="puml" showTitle="true" showDescription="true"
1818
footer="Visit https://www.phing.info/"/>
1919
<visualizer format="png" showTitle="true" showDescription="true"
2020
footer="Visit https://www.phing.info/"/>
2121
</target>
2222

23-
<target name="composer:normalize" description="Normalize composer.json content">
24-
<composer command="normalize"/>
23+
<target name="composer:normalize" description="Normalize composer.json">
24+
<composer command="normalize">
25+
<arg value="--no-interaction"/>
26+
<arg value="--ansi"/>
27+
</composer>
2528
</target>
2629

2730
<target name="composer:install" description="Install Composer dependencies">
@@ -34,7 +37,10 @@
3437
</target>
3538

3639
<target name="composer:validate" description="Validate composer.json syntax">
37-
<composer command="validate"/>
40+
<composer command="validate">
41+
<arg value="--no-interaction"/>
42+
<arg value="--ansi"/>
43+
</composer>
3844
</target>
3945

4046
<target name="phpstan:analyse" description="Static code analysis">

composer.json

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "phing/task-visualizer",
3-
"type": "phing-extension",
4-
"description": "VisualizerTask creates diagrams using buildfiles, these diagrams represents calls and depends among targets.",
3+
"description": "VisualizerTask creates buildfile maps, these diagrams display calls and depends among targets.",
54
"license": "LGPL-3.0-only",
5+
"type": "phing-extension",
66
"authors": [
77
{
88
"name": "Siad Ardroumli",
@@ -26,16 +26,22 @@
2626
"phpstan/phpstan": "^0.12",
2727
"psr/http-message": "^1.0"
2828
},
29-
"extra": {
30-
"phing-custom-taskdefs": {
31-
"visualizer": "Phing\\Task\\Ext\\VisualizerTask"
32-
}
33-
},
29+
"minimum-stability": "dev",
30+
"prefer-stable": true,
3431
"autoload": {
3532
"psr-4": {
3633
"Phing\\Task\\Ext\\": "src/"
3734
}
3835
},
39-
"minimum-stability": "dev",
40-
"prefer-stable": true
36+
"config": {
37+
"allow-plugins": {
38+
"phing/phing-composer-configurator": true,
39+
"ergebnis/composer-normalize": true
40+
}
41+
},
42+
"extra": {
43+
"phing-custom-taskdefs": {
44+
"visualizer": "Phing\\Task\\Ext\\VisualizerTask"
45+
}
46+
}
4147
}

src/VisualizerTask.php

+27-18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Phing\Io\FileWriter;
2828
use Phing\Io\File;
2929
use Phing\Project;
30+
use Phing\Util\StringHelper;
3031
use Psr\Http\Message\ResponseInterface;
3132
use SimpleXMLElement;
3233
use XSLTProcessor;
@@ -113,9 +114,9 @@ public function init(): void
113114
$this->log($exceptionMessage, Project::MSG_ERR);
114115
throw new BuildException($exceptionMessage);
115116
}
116-
$this->setFormat(VisualizerTask::FORMAT_PNG);
117-
$this->setServer(VisualizerTask::SERVER);
118-
$this->setDirection(VisualizerTask::ARROWS_VERTICAL);
117+
$this->setFormat(self::FORMAT_PNG);
118+
$this->setServer(self::SERVER);
119+
$this->setDirection(self::ARROWS_VERTICAL);
119120
$this->setShowTitle(true);
120121
$this->setShowDescription(false);
121122
$this->setFooter('');
@@ -140,6 +141,7 @@ public function main(): void
140141
* Retrieves loaded buildfiles and generates a PlantUML diagram
141142
*
142143
* @return string
144+
* @throws \Phing\Io\IOException
143145
*/
144146
protected function generatePumlDiagram(): string
145147
{
@@ -158,6 +160,7 @@ protected function generatePumlDiagram(): string
158160
* @param \Phing\Io\File[] $buildFiles
159161
*
160162
* @return string
163+
* @throws \Phing\Io\IOException
161164
*/
162165
protected function generatePuml(array $buildFiles): string
163166
{
@@ -167,17 +170,17 @@ protected function generatePuml(array $buildFiles): string
167170
throw new BuildException($exceptionMessage);
168171
}
169172

170-
$puml = $this->transformToPuml($firstBuildFile, VisualizerTask::XSL_HEADER);
173+
$puml = $this->transformToPuml($firstBuildFile, self::XSL_HEADER);
171174

172175
$puml = array_reduce($buildFiles, function (string $carry, File $buildFile) {
173-
return $carry . $this->transformToPuml($buildFile, VisualizerTask::XSL_CALLS);
176+
return $carry . $this->transformToPuml($buildFile, self::XSL_CALLS);
174177
}, $puml);
175178

176179
$puml = array_reduce($buildFiles, function (string $carry, File $buildFile) {
177-
return $carry . $this->transformToPuml($buildFile, VisualizerTask::XSL_TARGETS);
180+
return $carry . $this->transformToPuml($buildFile, self::XSL_TARGETS);
178181
}, $puml);
179182

180-
$puml .= $this->transformToPuml($firstBuildFile, VisualizerTask::XSL_FOOTER);
183+
$puml .= $this->transformToPuml($firstBuildFile, self::XSL_FOOTER);
181184

182185
return $puml;
183186
}
@@ -263,10 +266,10 @@ public function getFormat(): string
263266
public function setFormat(string $format): VisualizerTask
264267
{
265268
switch ($format) {
266-
case VisualizerTask::FORMAT_PUML:
267-
case VisualizerTask::FORMAT_PNG:
268-
case VisualizerTask::FORMAT_EPS:
269-
case VisualizerTask::FORMAT_SVG:
269+
case self::FORMAT_PUML:
270+
case self::FORMAT_PNG:
271+
case self::FORMAT_EPS:
272+
case self::FORMAT_SVG:
270273
$this->format = $format;
271274
break;
272275
default:
@@ -301,9 +304,9 @@ public function setDestination(?string $destination): self
301304
/**
302305
* Figure diagram's file path
303306
*
304-
* @param string $buildfilePath Path to main buildfile
305-
* @param string $format Extension to use
306-
* @param null|string $destination Desired destination provided by user
307+
* @param string $buildfilePath Path to main buildfile, this is used as fallback dir.
308+
* @param string $format Extension to use.
309+
* @param null|string $destination Desired destination provided by user.
307310
*
308311
* @return string
309312
*/
@@ -321,7 +324,12 @@ protected function resolveDestination(string $buildfilePath, string $format, ?st
321324
$destination .= DIRECTORY_SEPARATOR . $buildfileInfo['filename'] . '.' . $format;
322325
}
323326

324-
// Check if path is available
327+
// Adding right extension if necessary
328+
if (!StringHelper::endsWith(".$format", $destination)) {
329+
$destination .= ".$format";
330+
}
331+
332+
// Parent directory must exist
325333
if (!is_dir(dirname($destination))) {
326334
$exceptionMessage = "Directory '$destination' is invalid";
327335
$this->log($exceptionMessage, Project::MSG_ERR);
@@ -342,7 +350,7 @@ protected function resolveDestination(string $buildfilePath, string $format, ?st
342350
*/
343351
protected function generateImage(string $pumlDiagram, string $format): string
344352
{
345-
if ($format === VisualizerTask::FORMAT_PUML) {
353+
if ($format === self::FORMAT_PUML) {
346354
$this->log('Bypassing, no need to call server', Project::MSG_DEBUG);
347355

348356
return $pumlDiagram;
@@ -360,6 +368,7 @@ protected function generateImage(string $pumlDiagram, string $format): string
360368
*
361369
* @param string $pumlDiagram
362370
* @param string $format
371+
* @throws \Jawira\PlantUmlClient\ClientException
363372
*/
364373
protected function prepareImageUrl(string $pumlDiagram, string $format): void
365374
{
@@ -466,7 +475,7 @@ protected function processResponse(ResponseInterface $response): void
466475
$this->log("Response status: $status", Project::MSG_DEBUG);
467476
$this->log("Response reason: $reasonPhrase", Project::MSG_DEBUG);
468477

469-
if ($status !== VisualizerTask::STATUS_OK) {
478+
if ($status !== self::STATUS_OK) {
470479
$exceptionMessage = "Request unsuccessful. Response from server: $status $reasonPhrase";
471480
$this->log($exceptionMessage, Project::MSG_ERR);
472481
throw new BuildException($exceptionMessage);
@@ -484,7 +493,7 @@ protected function processResponse(ResponseInterface $response): void
484493
protected function saveToFile(string $content, File $destination): void
485494
{
486495
$path = $destination->getPath();
487-
$this->log("Writing: $path", Project::MSG_INFO);
496+
$this->log("Writing: $path");
488497

489498
(new FileWriter($destination))->write($content);
490499
}

0 commit comments

Comments
 (0)