Page MenuHomeSealhub

ApacheThriftGeneratedLinter.php
No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None

ApacheThriftGeneratedLinter.php

<?php
/**
* Copyright 2016 Pinterest, 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.
*/
/**
* Lints code generated by the Apache Thrift compiler.
*/
final class ApacheThriftGeneratedLinter extends ArcanistLinter {
const LINT_VERSION = 1;
private $requiredVersion = null;
private $requiredVersionSuffix = false;
private $requiredVersionOperator = '==';
public function getInfoName() {
return 'Apache Thrift Generated Linter';
}
public function getInfoDescription() {
return pht('Validates generated Thrift files');
}
public function getInfoURI() {
return 'https://thrift.apache.org/';
}
public function getLinterName() {
return 'THRIFT-GEN';
}
public function getLinterConfigurationName() {
return 'thrift-gen';
}
public function getLinterConfigurationOptions() {
$options = array(
'thrift-gen.version' => array(
'type' => 'optional string',
'help' => pht(
'Specify a version requirement for the Thrift compiler that was '.
'used to generate these files. The version number may be prefixed '.
'with <, <=, >, >=, or = to specify the version comparison '.
'operator (default: =).'),
),
);
return $options + parent::getLinterConfigurationOptions();
}
public function setLinterConfigurationValue($key, $value) {
switch ($key) {
case 'thrift-gen.version':
$matches = null;
if (preg_match('/^([<>]=?|=)\s*(.*)$/', $value, $matches)) {
$this->requiredVersionOperator = $matches[1];
$this->requiredVersion = $matches[2];
if ($this->requiredVersionOperator === '=') {
$this->requiredVersionOperator = '==';
}
} else {
$this->requiredVersion = $value;
}
$this->requiredVersionSuffix = strpos($this->requiredVersion, '-');
return;
}
return parent::setLinterConfigurationValue($key, $value);
}
public function getLintSeverityMap() {
return array(
self::LINT_VERSION => ArcanistLintSeverity::SEVERITY_ERROR,
);
}
public function getLintNameMap() {
return array(
self::LINT_VERSION => pht('Unsupported Thrift version'),
);
}
private function lintVersion(array $lines) {
// TODO: Support more patterns beyond this possibly Python-specific one.
$regex = "/Autogenerated by Thrift Compiler \((?P<version>.*)\)/";
foreach ($lines as $lineno => $line) {
$matches = array();
if (preg_match($regex, $line, $matches, PREG_OFFSET_CAPTURE)) {
list($version, $offset) = $matches['version'];
// If the captured version contains contains a suffix but the required
// version does not, remove the suffix before performing the version
// comparison. This gives us the flexibility of enforcing versions
// across a range of custom compiler builds while also allowing us to
// explicitly require a specific custom version should we need one.
if ($this->requiredVersionSuffix === false) {
$suffix = strpos($version, '-');
if ($suffix !== false) {
$version = substr($version, 0, $suffix);
}
}
$unsupported = !version_compare(
$version,
$this->requiredVersion,
$this->requiredVersionOperator);
if ($unsupported) {
$this->raiseLintAtLine(
$lineno + 1,
$offset + 1,
self::LINT_VERSION,
pht(
'This file was generated by an unsupported Thrift version (%s). '.
'Please regenerate it using Thrift version %s (%s).',
$version, $this->requiredVersion, $this->requiredVersionOperator),
$version);
}
return;
}
}
}
public function lintPath($path) {
$lines = phutil_split_lines($this->getData($path), false);
if ($this->requiredVersion && $this->isMessageEnabled(self::LINT_VERSION)) {
$this->lintVersion($lines);
}
}
}

File Metadata

Mime Type
text/x-php
Expires
Sat, Nov 28, 03:19 (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1209523
Default Alt Text
ApacheThriftGeneratedLinter.php (4 KB)

Event Timeline