Skip to content

Commit f22ce40

Browse files
authored
feat(plugin): support license file (#2196)
1 parent 1747813 commit f22ce40

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

src/project/context/flowr-analyzer-files-context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type RoleBasedFiles = {
4343
/* currently no special support */
4444
[FileRole.Vignette]: FlowrFileProvider[];
4545
[FileRole.Test]: FlowrFileProvider[];
46+
[FileRole.License]: FlowrFileProvider[];
4647
[FileRole.Source]: FlowrFileProvider[];
4748
[FileRole.Data]: FlowrFileProvider[];
4849
[FileRole.Other]: FlowrFileProvider[];

src/project/context/flowr-file.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export enum FileRole {
2626
Test = 'test',
2727
/** Data files, e.g., `R/sysdata.rda`, currently not specially supported. */
2828
Data = 'data',
29+
/** Signals separate license files, but please note, that DESCRIPTION files may contain license info too */
30+
License = 'license',
2931
/**
3032
* Catch-all for any file that provides usable R source code to incorporate into the analysis.
3133
* Please note, that the loading order/inclusion and even potential relevance of these source files
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { FlowrAnalyzerFilePlugin } from './flowr-analyzer-file-plugin';
2+
import { SemVer } from 'semver';
3+
import type { PathLike } from 'fs';
4+
import type { FlowrAnalyzerContext } from '../../context/flowr-analyzer-context';
5+
import type { FlowrFileProvider } from '../../context/flowr-file';
6+
import { FileRole } from '../../context/flowr-file';
7+
import {
8+
platformBasename
9+
} from '../../../dataflow/internal/process/functions/call/built-in/built-in-source';
10+
11+
const FileNamePattern = /license(\.md|\.txt)?$/i;
12+
13+
/**
14+
* This plugin provides supports for the identification of license files.
15+
*/
16+
export class FlowrAnalyzerLicenseFilePlugin extends FlowrAnalyzerFilePlugin {
17+
public readonly name = 'flowr-analyzer-license-files-plugin';
18+
public readonly description = 'This plugin provides support for loading license files.';
19+
public readonly version = new SemVer('0.1.0');
20+
private readonly pathPattern: RegExp;
21+
22+
/**
23+
* Creates a new instance of the TEST file plugin.
24+
* @param pathPattern - The pathPattern to identify TEST files, see {@link FileNamePattern} for the default pathPattern.
25+
*/
26+
constructor(pathPattern: RegExp = FileNamePattern) {
27+
super();
28+
this.pathPattern = pathPattern;
29+
}
30+
31+
public applies(file: PathLike): boolean {
32+
return this.pathPattern.test(platformBasename(file.toString()));
33+
}
34+
35+
/**
36+
* Processes the given file, assigning it the {@link FileRole.License} role.
37+
*/
38+
public process(_ctx: FlowrAnalyzerContext, file: FlowrFileProvider): FlowrFileProvider {
39+
file.assignRole(FileRole.License);
40+
return file;
41+
}
42+
}

src/project/plugins/flowr-analyzer-plugin-defaults.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { FlowrAnalyzerNamespaceFilesPlugin } from './file-plugins/flowr-analyzer
1313
import { FlowrAnalyzerNewsFilePlugin } from './file-plugins/flowr-analyzer-news-file-plugin';
1414
import { FlowrAnalyzerMetaVignetteFilesPlugin } from './file-plugins/flowr-analyzer-vignette-file-plugin';
1515
import { FlowrAnalyzerMetaTestFilesPlugin } from './file-plugins/flowr-analyzer-test-file-plugin';
16+
import { FlowrAnalyzerLicenseFilePlugin } from './file-plugins/flowr-analyzer-license-file-plugin';
1617

1718
/**
1819
* Provides the default set of Flowr Analyzer plugins.
@@ -26,6 +27,7 @@ export function FlowrAnalyzerPluginDefaults(): FlowrAnalyzerPlugin[] {
2627
new FlowrAnalyzerLoadingOrderDescriptionFilePlugin(),
2728
new FlowrAnalyzerRmdFilePlugin(),
2829
new FlowrAnalyzerQmdFilePlugin(),
30+
new FlowrAnalyzerLicenseFilePlugin(),
2931
new FlowrAnalyzerJupyterFilePlugin(),
3032
new FlowrAnalyzerNamespaceFilesPlugin(),
3133
new FlowrAnalyzerNewsFilePlugin()

src/project/plugins/plugin-registry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { FlowrAnalyzerNamespaceFilesPlugin } from './file-plugins/flowr-analyzer
1414
import { FlowrAnalyzerNewsFilePlugin } from './file-plugins/flowr-analyzer-news-file-plugin';
1515
import { FlowrAnalyzerMetaVignetteFilesPlugin } from './file-plugins/flowr-analyzer-vignette-file-plugin';
1616
import { FlowrAnalyzerMetaTestFilesPlugin } from './file-plugins/flowr-analyzer-test-file-plugin';
17+
import { FlowrAnalyzerLicenseFilePlugin } from './file-plugins/flowr-analyzer-license-file-plugin';
1718

1819
/**
1920
* The built-in Flowr Analyzer plugins that are always available.
@@ -28,7 +29,8 @@ export const BuiltInPlugins = [
2829
['file:qmd', FlowrAnalyzerQmdFilePlugin],
2930
['file:ipynb', FlowrAnalyzerJupyterFilePlugin],
3031
['file:namespace', FlowrAnalyzerNamespaceFilesPlugin],
31-
['file:news', FlowrAnalyzerNewsFilePlugin]
32+
['file:news', FlowrAnalyzerNewsFilePlugin],
33+
['file:license', FlowrAnalyzerLicenseFilePlugin]
3234
] as const satisfies [string, PluginProducer][];
3335

3436
export type BuiltInFlowrPluginName = typeof BuiltInPlugins[number][0];
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { assert, describe, test } from 'vitest';
2+
import { FlowrAnalyzerContext } from '../../../../src/project/context/flowr-analyzer-context';
3+
import { arraysGroupBy } from '../../../../src/util/collections/arrays';
4+
import { FileRole, FlowrInlineTextFile } from '../../../../src/project/context/flowr-file';
5+
import { defaultConfigOptions } from '../../../../src/config';
6+
import {
7+
FlowrAnalyzerLicenseFilePlugin
8+
} from '../../../../src/project/plugins/file-plugins/flowr-analyzer-license-file-plugin';
9+
10+
11+
describe('License-file', function() {
12+
const ctx = new FlowrAnalyzerContext(
13+
defaultConfigOptions,
14+
arraysGroupBy([
15+
new FlowrAnalyzerLicenseFilePlugin()
16+
], p => p.type)
17+
);
18+
19+
ctx.addFile(new FlowrInlineTextFile('LICENSE', 'Hey'));
20+
ctx.resolvePreAnalysis();
21+
22+
test('Get the License!', () => {
23+
const files = ctx.files.getFilesByRole(FileRole.License);
24+
assert.lengthOf(files, 1, 'There should be exactly one License file');
25+
26+
const content = files[0].content();
27+
assert.strictEqual(content, 'Hey', 'The content of the LICENSE file should match');
28+
});
29+
});

0 commit comments

Comments
 (0)