1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-25 16:22:42 +01:00

JSON output for Lint

Summary:
Refactor ArcanistLintRenderer into three independent classes, and let
the Workflow select between them based on its 'output' parameter.

Test Plan:
Introduce a Lint warning and lint with no --output, with --output summary and
with --output JSON.

Reviewed By: epriestley
Reviewers: epriestley
CC: aran, epriestley
Differential Revision: 96
This commit is contained in:
Aran Donohue 2011-04-03 18:04:20 -07:00
parent 4bce8732e5
commit 76c45a74f3
5 changed files with 112 additions and 41 deletions

View file

@ -0,0 +1,52 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Shows lint messages to the user.
*
* @group lint
*/
class ArcanistLintJSONRenderer {
const LINES_OF_CONTEXT = 3;
public function renderLintResult(ArcanistLintResult $result) {
$messages = $result->getMessages();
$path = $result->getPath();
$data = explode("\n", $result->getData());
array_unshift($data, ''); // make the line numbers work as array indices
$output = array($path => array());
foreach ($messages as $message) {
$output[$path][] = array(
'code' => $message->getCode(),
'name' => $message->getName(),
'severity' => $message->getSeverity(),
'line' => $message->getLine(),
'char' => $message->getChar(),
'context' => implode("\n", array_slice(
$data,
$message->getLine() - self::LINES_OF_CONTEXT,
self::LINES_OF_CONTEXT * 2 + 1
)),
);
}
return json_encode($output)."\n";
}
}

View file

@ -22,22 +22,7 @@
* @group lint * @group lint
*/ */
class ArcanistLintRenderer { class ArcanistLintRenderer {
private $summaryMode;
public function setSummaryMode($mode) {
$this->summaryMode = $mode;
}
public function renderLintResult(ArcanistLintResult $result) { public function renderLintResult(ArcanistLintResult $result) {
if ($this->summaryMode) {
return $this->renderResultSummary($result);
} else {
return $this->renderResultFull($result);
}
}
protected function renderResultFull(ArcanistLintResult $result) {
$messages = $result->getMessages(); $messages = $result->getMessages();
$path = $result->getPath(); $path = $result->getPath();
$lines = explode("\n", $result->getData()); $lines = explode("\n", $result->getData());
@ -76,26 +61,6 @@ class ArcanistLintRenderer {
return implode("\n", $text); return implode("\n", $text);
} }
protected function renderResultSummary(ArcanistLintResult $result) {
$messages = $result->getMessages();
$path = $result->getPath();
$text = array();
$text[] = $path.":";
foreach ($messages as $message) {
$name = $message->getName();
$severity = ArcanistLintSeverity::getStringForSeverity(
$message->getSeverity());
$line = $message->getLine();
$text[] = " {$severity} on line {$line}: {$name}";
}
$text[] = null;
return implode("\n", $text);
}
protected function renderContext( protected function renderContext(
ArcanistLintMessage $message, ArcanistLintMessage $message,
array $line_data) { array $line_data) {
@ -191,5 +156,4 @@ class ArcanistLintRenderer {
$line, $line,
$data); $data);
} }
} }

View file

@ -0,0 +1,43 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Shows lint messages to the user.
*
* @group lint
*/
class ArcanistLintSummaryRenderer {
public function renderLintResult(ArcanistLintResult $result) {
$messages = $result->getMessages();
$path = $result->getPath();
$text = array();
$text[] = $path.":";
foreach ($messages as $message) {
$name = $message->getName();
$severity = ArcanistLintSeverity::getStringForSeverity(
$message->getSeverity());
$line = $message->getLine();
$text[] = " {$severity} on line {$line}: {$name}";
}
$text[] = null;
return implode("\n", $text);
}
}

View file

@ -13,3 +13,5 @@ phutil_require_module('phutil', 'utils');
phutil_require_source('ArcanistLintRenderer.php'); phutil_require_source('ArcanistLintRenderer.php');
phutil_require_source('ArcanistLintSummaryRenderer.php');
phutil_require_source('ArcanistLintJSONRenderer.php');

View file

@ -53,9 +53,11 @@ EOTEXT
'help' => 'help' =>
"Show all lint warnings, not just those on changed lines." "Show all lint warnings, not just those on changed lines."
), ),
'summary' => array( 'output' => array(
'param' => 'format',
'help' => 'help' =>
"Show lint warnings in a more compact format." "With 'summary', show lint warnings in a more compact format. ".
"With 'json', show lint warnings in machine-readable JSON format."
), ),
'advice' => array( 'advice' => array(
'help' => 'help' =>
@ -193,10 +195,18 @@ EOTEXT
$wrote_to_disk = false; $wrote_to_disk = false;
$renderer = new ArcanistLintRenderer(); switch ($this->getArgument('output')) {
if ($this->getArgument('summary')) { case 'json':
$renderer->setSummaryMode(true); $renderer = new ArcanistLintJSONRenderer();
break;
case 'summary':
$renderer = new ArcanistLintSummaryRenderer();
break;
default:
$renderer = new ArcanistLintRenderer();
break;
} }
foreach ($results as $result) { foreach ($results as $result) {
if (!$result->getMessages()) { if (!$result->getMessages()) {
continue; continue;