]> khamidou — GitHub mirrors - arcanist.git/commitdiff
JSON output for Lint
authorAran Donohue <aran@facebook.com>
Mon, 4 Apr 2011 01:04:20 +0000 (18:04 -0700)
committerAran Donohue <aran@facebook.com>
Mon, 4 Apr 2011 17:07:06 +0000 (10:07 -0700)
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

src/lint/renderer/ArcanistLintJSONRenderer.php [new file with mode: 0644]
src/lint/renderer/ArcanistLintRenderer.php
src/lint/renderer/ArcanistLintSummaryRenderer.php [new file with mode: 0644]
src/lint/renderer/__init__.php
src/workflow/lint/ArcanistLintWorkflow.php

diff --git a/src/lint/renderer/ArcanistLintJSONRenderer.php b/src/lint/renderer/ArcanistLintJSONRenderer.php
new file mode 100644 (file)
index 0000000..6607329
--- /dev/null
@@ -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";
+  }
+}
index 0fca7044d5fb0ff9b692e262a2987613f98ba368..dea9326006bfd31dacacad820beaeaa996d3a9e8 100644 (file)
  * @group lint
  */
 class ArcanistLintRenderer {
-
-  private $summaryMode;
-
-  public function setSummaryMode($mode) {
-    $this->summaryMode = $mode;
-  }
-
   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();
     $path = $result->getPath();
     $lines = explode("\n", $result->getData());
@@ -76,26 +61,6 @@ class ArcanistLintRenderer {
     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(
     ArcanistLintMessage $message,
     array $line_data) {
@@ -191,5 +156,4 @@ class ArcanistLintRenderer {
       $line,
       $data);
   }
-
 }
diff --git a/src/lint/renderer/ArcanistLintSummaryRenderer.php b/src/lint/renderer/ArcanistLintSummaryRenderer.php
new file mode 100644 (file)
index 0000000..b580e38
--- /dev/null
@@ -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);
+  }
+}
index e8872d7cf110e0ae155ba7ae1e32b097bcaf08f8..23f3b9606d5c186d7a4be395b23bd94327d48131 100644 (file)
@@ -13,3 +13,5 @@ phutil_require_module('phutil', 'utils');
 
 
 phutil_require_source('ArcanistLintRenderer.php');
+phutil_require_source('ArcanistLintSummaryRenderer.php');
+phutil_require_source('ArcanistLintJSONRenderer.php');
index a030a950da8f24fa772737c1712a89ab8efee3b0..17a6fe3ba736076d95e21d3a5f688c8d3db01375 100644 (file)
@@ -53,9 +53,11 @@ EOTEXT
         'help' =>
           "Show all lint warnings, not just those on changed lines."
       ),
-      'summary' => array(
+      'output' => array(
+        'param' => 'format',
         '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(
         'help' =>
@@ -193,10 +195,18 @@ EOTEXT
 
     $wrote_to_disk = false;
 
-    $renderer = new ArcanistLintRenderer();
-    if ($this->getArgument('summary')) {
-      $renderer->setSummaryMode(true);
+    switch ($this->getArgument('output')) {
+      case 'json':
+        $renderer = new ArcanistLintJSONRenderer();
+        break;
+      case 'summary':
+        $renderer = new ArcanistLintSummaryRenderer();
+        break;
+      default:
+        $renderer = new ArcanistLintRenderer();
+        break;
     }
+
     foreach ($results as $result) {
       if (!$result->getMessages()) {
         continue;