@eagleoutice/flowr-dev
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
101 lines • 5.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlowrAnalyzerRbuildignoreProjectDiscoveryPlugin = exports.FlowrAnalyzerGitignoreProjectDiscoveryPlugin = exports.FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin = exports.IgnoreFileKind = void 0;
const flowr_analyzer_project_discovery_plugin_1 = require("./flowr-analyzer-project-discovery-plugin");
const semver_1 = require("semver");
const retriever_1 = require("../../../r-bridge/retriever");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const glob_1 = require("../../../util/glob");
/**
* The ignore files a {@link FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin} can respect.
* They differ in their pattern language, see {@link ignoreMatcher}.
*/
var IgnoreFileKind;
(function (IgnoreFileKind) {
/** `.gitignore`, gitignore-style globs */
IgnoreFileKind["Gitignore"] = "gitignore";
/** `.Rbuildignore`, one (Perl-compatible) regular expression per line, as used by `R CMD build` */
IgnoreFileKind["Rbuildignore"] = "rbuildignore";
})(IgnoreFileKind || (exports.IgnoreFileKind = IgnoreFileKind = {}));
const ignoreFileNames = {
[IgnoreFileKind.Gitignore]: '.gitignore',
[IgnoreFileKind.Rbuildignore]: '.Rbuildignore'
};
/** Reads the ignore file of `kind` at `root` and returns a matcher for paths relative to `root`, if it exists. */
function ignoreMatcher(kind, root) {
const file = path_1.default.join(root, ignoreFileNames[kind]);
if (!fs_1.default.existsSync(file)) {
return undefined;
}
const content = fs_1.default.readFileSync(file, 'utf-8');
if (kind === IgnoreFileKind.Rbuildignore) {
return (0, glob_1.rbuildignoreMatcher)(content);
}
const ig = (0, glob_1.loadIgnore)()().add(content);
return relativePath => ig.ignores(relativePath);
}
/**
* Decorator around any {@link FlowrAnalyzerProjectDiscoveryPlugin} that filters the discovered files
* by the ignore files found at the project root. Ignore files that do not exist are skipped, so with
* none of them present the inner plugin's results are returned unchanged.
*
* Use {@link FlowrAnalyzerGitignoreProjectDiscoveryPlugin} (`'project-discovery:gitignore'`),
* {@link FlowrAnalyzerRbuildignoreProjectDiscoveryPlugin} (`'project-discovery:rbuildignore'`), or
* `'project-discovery:ignore-files'` for both. As every variant wraps an inner discovery plugin, they
* can also be nested to combine them with a custom discovery plugin.
*/
class FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin extends flowr_analyzer_project_discovery_plugin_1.FlowrAnalyzerProjectDiscoveryPlugin {
name = 'flowr-analyzer-ignore-file-project-discovery-plugin';
description = 'Wraps a project discovery plugin and filters results by .gitignore and .Rbuildignore rules.';
version = new semver_1.SemVer('0.1.0');
inner;
kinds;
/**
* @param kinds - the ignore files to respect
* @param inner - the discovery plugin providing the files to filter
*/
constructor(kinds = [IgnoreFileKind.Gitignore, IgnoreFileKind.Rbuildignore], inner = flowr_analyzer_project_discovery_plugin_1.FlowrAnalyzerProjectDiscoveryPlugin.defaultPlugin()) {
super();
this.kinds = kinds;
this.inner = inner;
}
process(context, args) {
const matchers = this.kinds.map(k => ignoreMatcher(k, args.content)).filter(m => m !== undefined);
const inner = this.inner.processor(context, args);
if (matchers.length === 0) {
return inner;
}
return inner.filter(r => {
const filePath = (0, retriever_1.isParseRequest)(r) && r.request === 'file' ? r.content : !(0, retriever_1.isParseRequest)(r) ? r.path() : undefined;
if (filePath === undefined) {
return true;
}
const relative = path_1.default.relative(args.content, filePath);
return relative === '' || !matchers.some(m => m(relative));
});
}
}
exports.FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin = FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin;
/** Filters the discovered files by the `.gitignore` at the project root, see {@link FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin}. */
class FlowrAnalyzerGitignoreProjectDiscoveryPlugin extends FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin {
name = 'flowr-analyzer-gitignore-project-discovery-plugin';
description = 'Wraps a project discovery plugin and filters results by .gitignore rules.';
constructor(inner = flowr_analyzer_project_discovery_plugin_1.FlowrAnalyzerProjectDiscoveryPlugin.defaultPlugin()) {
super([IgnoreFileKind.Gitignore], inner);
}
}
exports.FlowrAnalyzerGitignoreProjectDiscoveryPlugin = FlowrAnalyzerGitignoreProjectDiscoveryPlugin;
/** Filters the discovered files by the `.Rbuildignore` at the package root, see {@link FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin}. */
class FlowrAnalyzerRbuildignoreProjectDiscoveryPlugin extends FlowrAnalyzerIgnoreFileProjectDiscoveryPlugin {
name = 'flowr-analyzer-rbuildignore-project-discovery-plugin';
description = 'Wraps a project discovery plugin and filters results by .Rbuildignore rules.';
constructor(inner = flowr_analyzer_project_discovery_plugin_1.FlowrAnalyzerProjectDiscoveryPlugin.defaultPlugin()) {
super([IgnoreFileKind.Rbuildignore], inner);
}
}
exports.FlowrAnalyzerRbuildignoreProjectDiscoveryPlugin = FlowrAnalyzerRbuildignoreProjectDiscoveryPlugin;
//# sourceMappingURL=flowr-analyzer-ignore-file-project-discovery-plugin.js.map