@atomist/sdm-pack-sloc
Version:
Extension Pack for an Atomist SDM to integrate code metrics
86 lines • 3.77 kB
JavaScript
;
/*
* Copyright © 2018 Atomist, 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.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const slocReport_1 = require("./slocReport");
/**
* Inspection that reports on languages used in a project
* @param {Project} p
* @param {SdmContext} ci
* @return {Promise<LanguagesReport>}
* @constructor
*/
exports.SlocInspection = (p, ci) => __awaiter(this, void 0, void 0, function* () {
const result = yield slocReport_1.reportForLanguages(p);
yield ci.addressChannels(reportForProject({ repoId: p.id, result }));
return result;
});
/**
* Command to display lines of code in current project or projects
* to Slack, across understood languages.
* Also displays aggregate data.
*/
exports.SlocCommand = {
name: "sloc",
inspection: exports.SlocInspection,
intent: ["compute sloc", "sloc"],
onInspectionResults: (results, ci) => __awaiter(this, void 0, void 0, function* () {
// Aggregate the results
// We'll have all languages for each project
const languages = results[0].result.languagesScanned;
// Totals for all languages
const consolidated = languages.map(language => {
const stats = slocReport_1.consolidate(language, results.map(r => r.result.languageReports.find(l => l.language === language).stats));
return {
language,
stats,
};
}).filter(s => s.stats.total > 0);
let message = `Totals across ${results.length} projects:\n`;
const sorted = _.sortBy(consolidated, s => -s.stats.total);
for (const langStats of sorted) {
message += `* ${formatLanguageStats(langStats)}\n`;
}
yield ci.context.messageClient.respond(message);
}),
};
/**
* Format a string for the report for the project
* @param {CodeInspectionResult<LanguagesReport>} result
* @return {string}
*/
function reportForProject(result) {
return `Project \`${result.repoId.owner}:${result.repoId.repo}\`: ${result.repoId.url}\n` +
result.result.relevantLanguageReports.map(formatLanguageReport).map(s => "* " + s).join("\n");
}
function formatLanguageReport(report) {
return formatLanguageStats(report) +
`, ${Number(report.fileReports.length).toLocaleString()} \`.${report.language.extensions[0]}\` files`;
}
function formatLanguageStats(report) {
return `*${report.language.name}*: ${Number(report.stats.total).toLocaleString()} loc, ` +
`${Number(report.stats.comment).toLocaleString()} in comments`;
}
//# sourceMappingURL=slocCommand.js.map