Skip to content

Commit

Permalink
fix: Detecting namespace from path (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
KreativJos authored Apr 24, 2022
1 parent c0f7f70 commit 718ada8
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/namespaceDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@ export default class NamespaceDetector {
}

public async getNamespace(): Promise<string> {
let fullNamespace = await this.fromCsproj();
const projectReaders = new Array<ProjectReader>();
const csprojReader = await CsprojReader.createFromPath(this.filePath);

if (fullNamespace) return fullNamespace;
if (csprojReader) {
projectReaders.push(csprojReader);

fullNamespace = await this.fromProjectJson();
const fullNamespace = await this.getRootNamespace(csprojReader);

if (fullNamespace) return fullNamespace;
if (fullNamespace) return fullNamespace;
}

return await this.fromFilepath();
}
const projectJsonReader = await ProjectJsonReader.createFromPath(this.filePath);

private async fromCsproj(): Promise<string | undefined> {
const csprojReader = await CsprojReader.createFromPath(this.filePath);
if (projectJsonReader) {
projectReaders.push(projectJsonReader);

return await this.getRootNamespace(csprojReader);
}
const fullNamespace = await this.getRootNamespace(projectJsonReader);

private async fromProjectJson(): Promise<string | undefined> {
const projectJsonReader = await ProjectJsonReader.createFromPath(this.filePath);
if (fullNamespace) return fullNamespace;
}

return await this.getRootNamespace(projectJsonReader);
return await this.fromFilepath(projectReaders);
}

private async getRootNamespace(projectReader: ProjectReader | undefined): Promise<string | undefined> {
Expand All @@ -47,20 +48,24 @@ export default class NamespaceDetector {
return this.calculateFullNamespace(rootNamespace, path.dirname(projectReader.getFilePath()));
}

private async getRootPath(): Promise<string> {
const projectPath = await ProjectReader.findProjectPath(this.filePath);
private async getRootPath(projectReaders: Array<ProjectReader>): Promise<string> {
for (const projectReader of projectReaders) {
if (projectReader) {
const projectPath = projectReader.getFilePath();

if (projectPath) {
const projectPathSplit = projectPath.split(path.sep);
if (projectPath) {
const projectPathSplit = projectPath.split(path.sep);

return projectPathSplit.slice(0, projectPathSplit.length - 2).join(path.sep);
return projectPathSplit.slice(0, projectPathSplit.length - 2).join(path.sep);
}
}
}

return workspace.workspaceFolders && workspace.workspaceFolders.length ? workspace.workspaceFolders[0].uri.fsPath : '';
}

private async fromFilepath(): Promise<string> {
const rootPath = await this.getRootPath();
private async fromFilepath(projectReaders: Array<ProjectReader>): Promise<string> {
const rootPath = await this.getRootPath(projectReaders);
const namespaceWithLeadingDot = this.calculateFullNamespace('', rootPath);

return namespaceWithLeadingDot.slice(1);
Expand All @@ -69,7 +74,7 @@ export default class NamespaceDetector {
private calculateFullNamespace(rootNamespace: string, rootDirectory: string): string {
const filePathSegments: string[] = path.dirname(this.filePath).split(path.sep);
const rootDirSegments: string[] = rootDirectory.split(path.sep);

let fullNamespace = rootNamespace;

// Remove rootDirSegments from filePathSegments
Expand Down

0 comments on commit 718ada8

Please sign in to comment.