Skip to content

Commit

Permalink
feat: add ruby language detector
Browse files Browse the repository at this point in the history
  • Loading branch information
Kroustille committed Oct 21, 2019
1 parent 340ce63 commit 59a430f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/detectors/Ruby/RubyLanguageDetector.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { FileInspector } from '../../inspectors/FileInspector';
import { ProgrammingLanguage } from '../../model';
import { FileSystemService } from '../../services/FileSystemService';
import * as nodePath from 'path';
import { DirectoryJSON } from 'memfs/lib/volume';
import { RubyLanguageDetector } from './RubyLanguageDetector';

describe('RubyLanguageDetector', () => {
let detector: RubyLanguageDetector;
let virtualFileSystemService: FileSystemService;

beforeEach(() => {
virtualFileSystemService = new FileSystemService({ isVirtual: true });

const fileInspector = new FileInspector(virtualFileSystemService, '/');
detector = new RubyLanguageDetector(fileInspector);
});

afterEach(async () => {
virtualFileSystemService.clearFileSystem();
});

it('detects ruby correctly via gemspec file', async () => {
const structure: DirectoryJSON = {
'/scan.gemspec': '...',
};

virtualFileSystemService.setFileSystem(structure);

const langAtPath = await detector.detectLanguage();
expect(langAtPath.length).toEqual(1);
expect(langAtPath[0].language).toEqual(ProgrammingLanguage.Ruby);
expect(langAtPath[0].path).toEqual(nodePath.sep);
});

it("detects it's not a ruby file", async () => {
const structure: DirectoryJSON = {
'/src/index.none': '...',
};

virtualFileSystemService.setFileSystem(structure);

const langAtPath = await detector.detectLanguage();
expect(langAtPath.length).toEqual(0);
expect(langAtPath).toEqual([]);
});

it('detects ruby correctly via rb file', async () => {
const structure: DirectoryJSON = {
'/index.rb': '...',
};

virtualFileSystemService.setFileSystem(structure);

const langAtPath = await detector.detectLanguage();
expect(langAtPath.length).toEqual(1);
expect(langAtPath[0].language).toEqual(ProgrammingLanguage.Ruby);
expect(langAtPath[0].path).toEqual(nodePath.sep);
});
});
37 changes: 37 additions & 0 deletions src/detectors/Ruby/RubyLanguageDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ILanguageDetector } from '../ILanguageDetector';
import { injectable, inject } from 'inversify';
import { LanguageAtPath, ProgrammingLanguage } from '../../model';
import { IFileInspector } from '../../inspectors/IFileInspector';
import { Types } from '../../types';
import { fileNameRegExp, fileExtensionRegExp, sharedSubpath } from '../utils';
import { uniq } from 'lodash';
import * as nodePath from 'path';

@injectable()
export class RubyLanguageDetector implements ILanguageDetector {
private fileInspector: IFileInspector;
constructor(@inject(Types.IFileInspector) fileInspector: IFileInspector) {
this.fileInspector = fileInspector;
}

async detectLanguage(): Promise<LanguageAtPath[]> {
const result: LanguageAtPath[] = [];
const packageFiles = await this.fileInspector.scanFor(fileExtensionRegExp(['gemspec']), '/');

if(packageFiles.length > 0) {
for (const path of packageFiles.map((file) => nodePath.dirname(file.path))) {
result.push({ language: ProgrammingLanguage.Ruby, path });
}
} else {
const rubyFiles = await this.fileInspector.scanFor(fileExtensionRegExp(['rb']), '/');
if (rubyFiles.length === 0) {
return result;
}
const dirsWithProjects = uniq(rubyFiles.map((f) => nodePath.dirname(f.path)));
// Get the shared subpath
const commonPath = sharedSubpath(dirsWithProjects);
result.push({ language: ProgrammingLanguage.Ruby, path: commonPath });
}
return result;
}
}

0 comments on commit 59a430f

Please sign in to comment.