@eagleoutice/flowr-dev
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
165 lines • 6.84 kB
JavaScript
"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";
/**
* Files below an `inst/` folder, which R installs verbatim into the package root (resources/scripts, e.g., `inst/REFERENCES.R`, `inst/CITATION`, `inst/extdata/...`).
* These are not part of the package namespace source, so tooling may want to treat them separately.
*/
FileRole["Install"] = "install";
/** 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";
/** Files describing a project's virtual/pinned package environment, e.g., `renv.lock`, `rv.lock` or `uvr.lock`. */
FileRole["VirtualEnv"] = "virtual-env";
/** A project manifest that is no `DESCRIPTION`, e.g. the `rproject.toml` of an rv project or the `uvr.toml` of a uvr project. */
FileRole["Manifest"] = "manifest";
/**
* R sources evaluated at startup, before any project code (`.Rprofile`, `Rprofile.site`).
* These commonly bootstrap a package manager, e.g. by sourcing `packrat/init.R` or `renv/activate.R`.
*/
FileRole["Startup"] = "startup";
/**
* Environment-variable definitions read at startup (`.Renviron`, `Renviron.site`). These are `KEY=value`
* files, not R source, so they are labeled but not parsed as R.
*/
FileRole["Environment"] = "environment";
/**
* 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;
onInvalidate = [];
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);
}
}
addOnInvalidate(callback) {
this.onInvalidate.push(callback);
}
removeOnInvalidate(callback) {
this.onInvalidate = this.onInvalidate.filter(cb => cb !== callback);
}
invalidate() {
const oldContent = this.contentCache;
this.contentCache = undefined;
for (const invalidator of this.onInvalidate) {
invalidator({ type: "single-file-invalidate" /* InvalidationEventType.SingleFileInvalidate */, oldContent, filePath: this.path() });
}
}
}
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;
}
/**
* Update the content of this inline file and invalidate the cache to trigger updates in the analysis.
* @see {@link FlowrFile#invalidate}
*/
updateInlineContent(newContent) {
this.contentStr = newContent;
this.invalidate();
}
}
exports.FlowrInlineTextFile = FlowrInlineTextFile;
//# sourceMappingURL=flowr-file.js.map