Skip to content

Commit

Permalink
Read import from csporj (#126)
Browse files Browse the repository at this point in the history
* Read import from csporj

* Added imports tests

---------

Co-authored-by: Sante Barbuto <s.barbuto@twinner.com>
  • Loading branch information
bard83 and Sante Barbuto committed Aug 10, 2023
1 parent d899304 commit 186eafc
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 58 deletions.
15 changes: 12 additions & 3 deletions src/project/csproj.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
export interface Csproj {
Project: Project | undefined
Project?: Project
}

export interface Project {
PropertyGroup: Array<PropertyGroup>
ItemGroup: Array<ItemGroup>
Import?: Array<Import>
}

export interface PropertyGroup {
RootNamespace: Array<string> | undefined
TargetFramework: Array<string> | undefined
RootNamespace?: Array<string>
TargetFramework?: Array<string>
ImplicitUsings?: Array<string>
}

Expand All @@ -21,7 +22,15 @@ export interface Using {
$?: UsingAttribute
}

export interface Import {
$: ImportAttribute
}

export interface UsingAttribute {
Include?: string
Remove?: string
}

export interface ImportAttribute {
Project: string
}
30 changes: 21 additions & 9 deletions src/project/csprojReader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Uri, workspace } from 'vscode';
import * as path from 'path';
import { Parser } from 'xml2js';
import { log } from '../util';

import { Csproj, PropertyGroup, Using } from './csproj';
import ProjectReader from './projectReader';
import FileHandler from '../io/fileHandler';


export default class CsprojReader extends ProjectReader {
Expand Down Expand Up @@ -103,19 +104,17 @@ export default class CsprojReader extends ProjectReader {
*
* @returns The content of this project file
*/
protected async getContent(): Promise<string> {
const document = await workspace.openTextDocument(Uri.file(this.filePath));

return document.getText();
protected async getContent(filePath?: string): Promise<string> {
return await FileHandler.read(filePath ?? this.filePath);
}

/**
* Retrieves and parses the content of this project file
*
* @returns The parsed xml content of this project file
*/
protected async getXmlContent(): Promise<Csproj> {
const content = await this.getContent();
protected async getXmlContent(filePath?: string): Promise<Csproj> {
const content = await this.getContent(filePath);

return await this.xmlParser.parseStringPromise(content);
}
Expand All @@ -128,7 +127,20 @@ export default class CsprojReader extends ProjectReader {
protected async getPropertyGroups(): Promise<PropertyGroup[] | undefined> {
const xmlContent = await this.getXmlContent();

return xmlContent?.Project?.PropertyGroup;
if (xmlContent?.Project?.PropertyGroup) {
return xmlContent?.Project?.PropertyGroup;
}

const importProject = xmlContent?.Project?.Import;
if (!importProject || importProject.length === 0) {
return undefined;
}

const cleanPath = importProject[0].$.Project.replace('\\', path.sep).replace('/', path.sep);
const projectPath = path.resolve(path.dirname(this.filePath), cleanPath);
const importXmlContent = await this.getXmlContent(projectPath);

return importXmlContent?.Project?.PropertyGroup;
}

/**
Expand Down Expand Up @@ -169,7 +181,7 @@ export default class CsprojReader extends ProjectReader {
return [];
}

const itemGroups = xmlContent.Project.ItemGroup.filter(g => g.Using !== undefined);
const itemGroups = xmlContent.Project.ItemGroup.filter(g => g.Using !== undefined);

const usings: Using[] = [];
itemGroups.forEach(g => {
Expand Down
Loading

0 comments on commit 186eafc

Please sign in to comment.