Skip to content

Commit

Permalink
read slnx format #309
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandoescolar committed Nov 24, 2024
1 parent fa6e8d5 commit fedd5fe
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/OmnisharpIntegrationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface EventStream {
const CSHARP_EXTENSION_ID = 'ms-dotnettools.csharp';
const SELECT_SOLUTION_EVENT_TYPE = 4;
const SOLUTION_EXTENSION = '.sln';
const SOLUTIONX_EXTENSION = '.slnx';

export class OmnisharpIntegrationService extends vscode.Disposable {

Expand Down Expand Up @@ -72,6 +73,10 @@ export class OmnisharpIntegrationService extends vscode.Disposable {
const e = new SolutionSelected(solutionPath);
this.eventAggregator.publish(e);
}
if (solutionPath.toLocaleLowerCase().endsWith(SOLUTIONX_EXTENSION)) {
const e = new SolutionSelected(solutionPath);
this.eventAggregator.publish(e);
}
}
}
}
8 changes: 4 additions & 4 deletions src/SolutionFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class SolutionFinder {

public isWorkspaceSolutionFile(filePath: string): boolean {
return this.workspaceRoots.indexOf(path.dirname(filePath)) >= 0
&& filePath.endsWith('.sln');
&& (filePath.endsWith('.sln') || filePath.endsWith('.slnx'));
}

public async findSolutions(): Promise<FoundPath[]> {
Expand All @@ -35,7 +35,7 @@ export class SolutionFinder {

if (config.getOpenSolutionsInRootFolder()) {
for (let i = 0; i < this.workspaceRoots.length; i++) {
const paths = await Utilities.searchFilesInDir(this.workspaceRoots[i], '.sln');
const paths = await Utilities.searchFilesInDir(this.workspaceRoots[i], ['.sln', '.slnx']);
paths.forEach(p => solutionPaths.push({ root: this.workspaceRoots[i], sln: p }));
}
}
Expand All @@ -44,15 +44,15 @@ export class SolutionFinder {
let altFolders = config.getAlternativeSolutionFolders();
for (let i = 0; i < altFolders.length; i++) {
for (let j = 0; j < this.workspaceRoots.length; j++) {
const paths = await Utilities.searchFilesInDir(path.join(this.workspaceRoots[j], altFolders[i]), '.sln');
const paths = await Utilities.searchFilesInDir(path.join(this.workspaceRoots[j], altFolders[i]), ['.sln', '.slnx']);
paths.forEach(p => solutionPaths.push({ root: this.workspaceRoots[j], sln: p }));
}
}
}

if (config.getOpenSolutionsInFoldersAndSubfolders()) {
for (let i = 0; i < this.workspaceRoots.length; i++) {
const paths = await Utilities.searchFilesInDir(this.workspaceRoots[i], '.sln', true);
const paths = await Utilities.searchFilesInDir(this.workspaceRoots[i], ['.sln', '.slnx'], true);
paths.forEach(p => solutionPaths.push({ root: this.workspaceRoots[i], sln: p }));
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/Solutions/SolutionFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "@extensions/fs";
import { Solution } from './model';
import { SlnLoader } from "./SlnLoader";
import { SlnxSolution } from "./slnx/Slnx";

export class SolutionFactory {
public static async load(path: string): Promise<Solution> {
Expand All @@ -12,6 +13,12 @@ export class SolutionFactory {
return SlnLoader.load(path);
}

if (path.endsWith(".slnx")) {
const s = new SlnxSolution();
await s.load(path);
return s;
}

return new Solution();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Solutions/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SolutionObject {
}

class SolutionParentObject extends SolutionObject {
protected readonly items: SolutionItem[] = [];
protected items: SolutionItem[] = [];

public addItem(item: SolutionItem): void {
this.items.push(item);
Expand Down
65 changes: 65 additions & 0 deletions src/core/Solutions/slnx/Slnx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as path from "@extensions/path";
import * as fs from "@extensions/fs";
import * as xml from "@extensions/xml";
import { Solution, SolutionFolder, SolutionItem, SolutionProject, SolutionProjectType, SolutionType } from "../model";
import { v4 as uuidv4 } from "uuid";

export class SlnxSolution extends Solution {
private document: xml.XmlElement | undefined;

constructor() {
super();
this.type = SolutionType.Slnx;
}

public async load(filepath: string): Promise<void> {
const content = await fs.readFile(filepath);
this.document = await xml.parseToJson(content);
this.fullPath = filepath;
this.folderPath = path.dirname(filepath);
this.name = path.basename(filepath);
this.refresh();
}

public refresh(): void {
if (!this.document) {
return;
}

this.items = [];
if (this.document.elements.length === 1) {
this.document.elements[0].elements.forEach((child: { name: string; }) => {
if (child.name === 'Folder') {
this.addItem(this.createFolder(child));
} else if (child.name === 'Project') {
this.addItem(this.createProject(child));
}
});
}
}

private createProject(child: any) : SolutionItem {
const project = new SolutionProject(uuidv4());
const projectPath = child.attributes.Path.replace(/\\/g, path.sep).trim();
project.fullPath = path.join(this.folderPath, projectPath);
project.name = path.basename(projectPath, path.extname(projectPath));
project.type = SolutionProjectType.default;
return project;
}

private createFolder(child: any) {
const folder = new SolutionFolder(uuidv4());
folder.name = child.attributes.Name.replace(/^[\/\\]+|[\/\\]+$/g, '');
folder.fullPath = path.join(this.folderPath, folder.name);

child.elements.forEach((child: { name: string; }) => {
if (child.name === 'Project') {
folder.addItem(this.createProject(child))
} else if (child.name === 'Folder') {
folder.addItem(this.createFolder(child));
}
});

return folder;
}
}
14 changes: 9 additions & 5 deletions src/core/Utilities/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from "@extensions/path";
import * as fs from "@extensions/fs";
import { DirectorySearchResult } from "./DirectorySearchResult";

export async function searchFilesInDir(startPath:string, extension: string, recursive: boolean = false) : Promise<string[]> {
export async function searchFilesInDir(startPath:string, extensions: string[], recursive: boolean = false) : Promise<string[]> {
if (!(await fs.exists(startPath))) {
return [];
}
Expand All @@ -11,14 +11,18 @@ export async function searchFilesInDir(startPath:string, extension: string, recu
let files = await fs.readdir(startPath);
for (let i = 0; i < files.length; i++) {
let filename = path.join(startPath, files[i]);
if (filename.endsWith(extension)) {
result.push(filename);
}
extensions.some(ext => {
if (filename.endsWith(ext)) {
result.push(filename);
return true;
}
return false;
});

if (recursive) {
let isDirectory = await fs.isDirectory(filename);
if (isDirectory) {
let subresult = await searchFilesInDir(filename, extension, recursive);
let subresult = await searchFilesInDir(filename, extensions, recursive);
result = result.concat(subresult);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/dialogs/openSolutionFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function openSolutionFile(label: string): Promise<string | undefine
openLabel: label,
canSelectFolders: false,
canSelectMany: false,
filters: { ['Solution files']: [ 'sln' ] }
filters: { ['Solution files']: [ 'sln', 'slnx' ] }
});

if (uris !== undefined && uris.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/language/decorators/CodeDecoratorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from "vscode";
import { ICodeDecorator } from "./ICodeDecorator";

export class CodeDecoratorController {
private timeout: NodeJS.Timer | undefined = undefined;
private timeout: NodeJS.Timer | number | undefined = undefined;

constructor(private readonly context: vscode.ExtensionContext, private readonly decorators: ICodeDecorator[]) {
}
Expand Down Expand Up @@ -41,7 +41,7 @@ export class CodeDecoratorController {

private triggerUpdateDecorations(activeEditor: vscode.TextEditor, throttle = false) {
if (this.timeout) {
clearTimeout(this.timeout);
clearTimeout(this.timeout as number);
this.timeout = undefined;
}

Expand Down

0 comments on commit fedd5fe

Please sign in to comment.