diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..9ad0ca1 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,20 @@ +--- +engines: + luacheck: + enabled: true + exclude_paths: + - Dockerfile + - ".*.yml" + rubocop: + enabled: true + bundler-audit: + enabled: true + reek: + enabled: true +ratings: + paths: + - "**/*.lua" + - bin/* +exclude_paths: +- lib/test.lua +- "**/.*.swp" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..922d289 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM alpine:3.5 + +ARG LUA_VERSION=5.3 +ARG LUACHECK_VERSION=0.19.1 +WORKDIR /tmp +COPY Gemfile* /tmp/ +RUN adduser -D -H -h /code -u 9000 -s /bin/false app \ + && apk add --no-cache luarocks${LUA_VERSION} ruby-bundler ruby-json icu-libs zlib \ + lua${LUA_VERSION}-dev build-base curl ruby-dev icu-dev zlib-dev cmake \ + && luarocks-${LUA_VERSION} install luacheck ${LUACHECK_VERSION} \ + && luarocks-${LUA_VERSION} install lua-cjson \ + && BUNDLE_SILENCE_ROOT_WARNING=1 bundle install --system \ + && apk del build-base curl lua${LUA_VERSION}-dev ruby-dev zlib-dev icu-dev cmake \ + && ln -s $(which lua${LUA_VERSION}) /usr/local/bin/lua + +COPY bin /usr/local/bin/ +COPY lib /usr/local/share/lua/${LUA_VERSION}/ +USER app +VOLUME /code + +WORKDIR /code + +COPY engine.json / + +CMD ["engine-luacheck"] diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..ab2efb6 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' do + gem 'github-linguist' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..dd1e77b --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,23 @@ +GEM + remote: https://rubygems.org/ + specs: + charlock_holmes (0.7.3) + escape_utils (1.1.1) + github-linguist (5.0.8) + charlock_holmes (~> 0.7.3) + escape_utils (~> 1.1.0) + mime-types (>= 1.19) + rugged (>= 0.25.1) + mime-types (3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2016.0521) + rugged (0.25.1.1) + +PLATFORMS + ruby + +DEPENDENCIES + github-linguist! + +BUNDLED WITH + 1.14.5 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..aebf184 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2017 Michal Cichra + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..536c336 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +IMAGE_NAME ?= codeclimate/codeclimate-luacheck + +build: + docker build . -t $(IMAGE_NAME) +test: + codeclimate analyze --dev + +bash: USER = 9000 +bash: + docker run -it --user $(USER) --rm --volume $(PWD):/code:ro $(IMAGE_NAME) sh + +local: export LUA_PATH = lib/?.lua +local: export CONFIG_FILE = config.json +local: + @bin/engine-lua-files + +codeclimate: export CODECLIMATE_DEBUG = 1 +codeclimate: + codeclimate analyze --dev diff --git a/README.md b/README.md new file mode 100644 index 0000000..4bd8cce --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Code Climate luacheck Engine + +CodeClimate Engine that runs [luacheck](https://github.com/mpeterv/luacheck/) on Lua files. +Uses [linguist](https://github.com/github/linguist) to detect Lua files. + +## Testing + +```shell +make build test +``` + +## TODO + +- [ ] expose luacheck options via to codeclimate +- [ ] fingerprinting +- [ ] detect categories diff --git a/bin/engine-luacheck b/bin/engine-luacheck new file mode 100755 index 0000000..7e71349 --- /dev/null +++ b/bin/engine-luacheck @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby + +require 'linguist' +require 'find' +require 'json' + +config_file = ENV['CONFIG_FILE'] || '/config.json' +config = JSON.parse(File.read(config_file)) + +include_paths = config.fetch('include_paths', %w(.)) + +Lua = Linguist::Language.find_by_name('Lua') + +files = [] + +Find.find(*include_paths) do |path| + next unless FileTest.file?(path) + + blob = Linguist::FileBlob.new(path) + + case blob.language + when Lua + files << path + end +end + +IO.popen(['luacheck', '--formatter', 'codeclimate', *files ]) do |io| + while (line = io.gets) + STDOUT.print line + end +end diff --git a/config.json b/config.json new file mode 100644 index 0000000..2624cac --- /dev/null +++ b/config.json @@ -0,0 +1 @@ +{"enabled":true,"exclude_paths":["Dockerfile","bin/engine-lua-files","*.yml"],"include_paths":[".codeclimate.yml","bin/engine-luacheck","config.json","engine.json","Gemfile","Gemfile.lock","lib/codeclimate.lua","Makefile","README.md"]} diff --git a/engine.json b/engine.json new file mode 100644 index 0000000..5db68e0 --- /dev/null +++ b/engine.json @@ -0,0 +1,11 @@ +{ + "name": "luacheck", + "description": "Luacheck is a static analyzer and a linter for Lua. Luacheck detects various issues such as usage of undefined global variables, unused variables and values, accessing uninitialized variables, unreachable code and more.", + "maintainer": { + "name": "Michal Cichra", + "email": "michal.cichra@gmail.com" + }, + "languages" : ["Lua"], + "version": "da5a2077", + "spec_version": "0.0.1" +} diff --git a/lib/codeclimate.lua b/lib/codeclimate.lua new file mode 100644 index 0000000..0ac2e9f --- /dev/null +++ b/lib/codeclimate.lua @@ -0,0 +1,59 @@ +local format = require 'luacheck.format' +local cjson = require 'cjson' + +local function Positions(line, column, end_column) + return { + begin = { + line = line, + column = column + }, + ['end'] = { + line = line, + column = end_column + } + } +end + +local function OtherLocations(file, warning) + if warning.prev_line then + return { + { + path = file, + positions = Positions(warning.line, warning.prev_column, warning.prev_column + warning.end_column - warning.column) + } + } + end +end + +local function Location(file, warning) + return { + path = file, + positions = Positions(warning.line, warning.column, warning.end_column) + + } +end + +local function Issue(file, warning) + return { + type = 'issue', + check_name = warning.code, + description = format.get_message(warning), + categories = { 'Style' }, + location = Location(file, warning), + other_locations = OtherLocations(file, warning), + } +end +return function(report, file_names) + -- CodeClimate formatter does not support any options for now + local issues = {} + + for i, file_report in ipairs(report) do + for _, warning in ipairs(file_report) do + local issue = Issue(file_names[i], warning) + io.stdout:write(cjson.encode(issue),string.char(0), "\n") + table.insert(issues, issue) + end + end + + return '' +end