UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

124 lines 4.87 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowrInlineTextFile = exports.FlowrTextFile = exports.FlowrFile = exports.FileRole = void 0; const fs_1 = __importDefault(require("fs")); /** * Some files have a special meaning in R projects, e.g., the `DESCRIPTION` file in R packages. * This list may be extended in the future and reflects files that the {@link FlowrAnalyzer} can do something interesting with. * If you add an interesting file that is only part of your plugin infrastructure, please use the `other` role. */ var FileRole; (function (FileRole) { /** The `DESCRIPTION` file in R packages, this is the only currently supported special file. */ FileRole["Description"] = "description"; /** The `NAMESPACE` file in R packages, currently not specially supported. */ FileRole["Namespace"] = "namespace"; /** The `NEWS` file in R packages */ FileRole["News"] = "news"; /** Vignette files, e.g., R Markdown files in the `vignettes/` folder */ FileRole["Vignette"] = "vignette"; /** Test source files, e.g., files in the `tests/` folder */ FileRole["Test"] = "test"; /** Data files, e.g., `R/sysdata.rda`, currently not specially supported. */ FileRole["Data"] = "data"; /** Signals separate license files, but please note, that DESCRIPTION files may contain license info too */ FileRole["License"] = "license"; /** * Catch-all for any file that provides usable R source code to incorporate into the analysis. * Please note, that the loading order/inclusion and even potential relevance of these source files * is determined by the loading order plugins (cf. {@link PluginType.LoadingOrder}) * in the {@link FlowrAnalyzerLoadingOrderContext}. */ FileRole["Source"] = "source"; /** Other special files that are not specifically supported by flowR but may be interesting for some analyses. */ FileRole["Other"] = "other"; })(FileRole || (exports.FileRole = FileRole = {})); /** * A basic implementation of the {@link FlowrFileProvider} interface that caches the content after the first load (i.e., updates on disk are ignored). * * See {@link FlowrTextFile} for a text-file specific implementation and {@link FlowrInlineTextFile} for inline text files. */ class FlowrFile { contentCache; filePath; _roles; static INLINE_PATH = '@inline'; constructor(filePath, roles) { this.filePath = filePath; this._roles = roles ? Array.from(roles) : undefined; } get roles() { return this._roles; } path() { return this.filePath.toString(); } content() { if (this.contentCache === undefined) { this.contentCache = this.loadContent(); } return this.contentCache; } /** * Allows to overwrite the content cache. * @protected */ setContent(content) { this.contentCache = content; } assignRole(role) { if (this._roles === undefined) { this._roles = [role]; } else if (this._roles.includes(role)) { // already assigned return; } else { this._roles.push(role); } } /** * Creates a {@link FlowrFile} from a given {@link RParseRequest}. * @see {@link FlowrTextFile} * @see {@link FlowrInlineTextFile} */ static fromRequest(request) { if (request.request === 'file') { return new FlowrTextFile(request.content); } else { return new FlowrInlineTextFile(FlowrFile.INLINE_PATH, request.content); } } } exports.FlowrFile = FlowrFile; /** * A basic implementation of the {@link FlowrFileProvider} interface for text files that caches the content after the first load (i.e., updates on disk are ignored). */ class FlowrTextFile extends FlowrFile { loadContent() { return fs_1.default.readFileSync(this.filePath, 'utf8'); } } exports.FlowrTextFile = FlowrTextFile; /** * A basic implementation of the {@link FlowrFileProvider} interface for (constant) inline text files. * This is also useful for "special" files like the `DESCRIPTION` file in R packages that you want to pass in directly. * These will be handled by the {@link FlowrAnalyzerDescriptionFilePlugin} (e.g., by using the {@link FlowrDescriptionFile#from} method decorator). */ class FlowrInlineTextFile extends FlowrFile { contentStr; constructor(path, content) { super(path); this.contentStr = content; } loadContent() { return this.contentStr; } } exports.FlowrInlineTextFile = FlowrInlineTextFile; //# sourceMappingURL=flowr-file.js.map