Skip to content

Commit

Permalink
#223 Allow add new or existing project to solution without selecting …
Browse files Browse the repository at this point in the history
…a solution file in the tree view
  • Loading branch information
Fernando Escolar committed Oct 10, 2022
1 parent d5e8b6a commit 55dde95
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/SolutionExplorerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class SolutionExplorerCommands {
private readonly actionsRunner: ActionsRunner,
private readonly templateEngineCollection: TemplateEngineColletion,
private readonly eventAggregator: IEventAggregator) {
this.commands['addExistingProject'] = new cmds.AddExistingProjectCommand();
this.commands['addNewProject'] = new cmds.AddNewProjectCommand();
this.commands['addExistingProject'] = new cmds.AddExistingProjectCommand(provider);
this.commands['addNewProject'] = new cmds.AddNewProjectCommand(provider);
this.commands['addPackage'] = new cmds.AddPackageCommand();
this.commands['addProjectReference'] = new cmds.AddProjectReferenceCommand();
this.commands['addSolutionFile'] = new cmds.AddExistingFileToSolutionFolderCommand();
Expand Down
32 changes: 28 additions & 4 deletions src/commands/AddExistingProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,46 @@ import * as dialogs from "@extensions/dialogs";
import { ContextValues, TreeItem } from "@tree";
import { Action, AddExistingProject } from "@actions";
import { ActionsCommand } from "@commands";
import { SolutionExplorerProvider } from "@SolutionExplorerProvider";

export class AddExistingProjectCommand extends ActionsCommand {
constructor() {
constructor(private readonly provider: SolutionExplorerProvider) {
super('Add existing project');
}

public shouldRun(item: TreeItem): boolean {
return item && !!item.path && (item.contextValue === ContextValues.solution || item.contextValue === ContextValues.solution + '-cps');
return !item || (item && !!item.path && (item.contextValue === ContextValues.solution || item.contextValue === ContextValues.solution + '-cps'));
}

public async getActions(item: TreeItem): Promise<Action[]> {
const solution = await dialogs.selectOption('Select solution', this.getSolutions(item));
const projectPath = await dialogs.openProjectFile('Select a project file to add');
if (!item.path || !projectPath) {
if (!solution || !projectPath) {
return [];
}

return [ new AddExistingProject(item.path, projectPath) ];
return [ new AddExistingProject(solution, projectPath) ];
}

private getSolutions(item: TreeItem): dialogs.ItemsOrItemsResolver {
if (item && item.path) {
const result: { [id: string]: string } = {};
result[item.label] = item.path;
return result;
}

return async () => {
const result: { [id: string]: string } = {};
var children = await this.provider.getChildren();
if (!children) { return result; }

children.forEach(child => {
if (child && child.path) {
result[child.label] = child.path;
}
});

return result;
};
}
}
60 changes: 44 additions & 16 deletions src/commands/AddNewProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as dialogs from '@extensions/dialogs';
import { ContextValues, TreeItem } from "@tree";
import { Action, AddExistingProject, CreateProject } from '@actions';
import { ActionsCommand } from "@commands";
import { SolutionExplorerProvider } from '@SolutionExplorerProvider';

type ProjectType = { name: string, value: string, languages: string[] };

Expand Down Expand Up @@ -36,39 +37,66 @@ const PROJECT_TYPES: ProjectType[] = [
export class AddNewProjectCommand extends ActionsCommand {
private wizard: dialogs.Wizard | undefined;

constructor() {
constructor(private readonly provider: SolutionExplorerProvider) {
super('Add new project');
}

public shouldRun(item: TreeItem): boolean {
return item && !!item.path && (item.contextValue === ContextValues.solution || item.contextValue === ContextValues.solution + '-cps');
return !item || (item && !!item.path && (item.contextValue === ContextValues.solution || item.contextValue === ContextValues.solution + '-cps'));
}

public async getActions(item: TreeItem): Promise<Action[]> {
if (!item || ! item.path) { return []; }

this.loadProjectTemplates();
this.wizard = dialogs.wizard('Add new project')
.selectOption('Select solution', this.getSolutions(item))
.selectOption('Select project type', this.getProjectTypes())
.selectOption('Select language', () => this.getLanguages())
.getText('Project name')
.getText('Folder name', '', () => this.getCurrentProjectName());

const parameters = await this.wizard .run();
if (!parameters) { return []; }
if (!parameters || parameters.length < 5) { return []; }

const solution = parameters[0];
const projectType = parameters[1];
const language = parameters[2];
const projectName = parameters[3];
const folderName = parameters[4];
const workingpath = path.dirname(solution);

const workingpath = path.dirname(item.path);
let projectPath = path.join(workingpath, parameters[3], parameters[2]);
if (parameters[1] === 'C#') { projectPath += '.csproj'; }
if (parameters[1] === 'F#') { projectPath += '.fsproj'; }
if (parameters[1] === 'VB') { projectPath += '.vbproj'; }
let projectPath = path.join(workingpath, folderName, projectName);
if (language === 'C#') { projectPath += '.csproj'; }
if (language === 'F#') { projectPath += '.fsproj'; }
if (language === 'VB') { projectPath += '.vbproj'; }

return [
new CreateProject(parameters[0], parameters[1], parameters[2], parameters[3], workingpath),
new AddExistingProject(item.path, projectPath)
new CreateProject(projectType, language, projectName, folderName, workingpath),
new AddExistingProject(solution, projectPath)
];
}

private getSolutions(item: TreeItem): dialogs.ItemsOrItemsResolver {
if (item && item.path) {
const result: { [id: string]: string } = {};
result[item.label] = item.path;
return result;
}

return async () => {
const result: { [id: string]: string } = {};
var children = await this.provider.getChildren();
if (!children) { return result; }

children.forEach(child => {
if (child && child.path) {
result[child.label] = child.path;
}
});

return result;
};
}

private loadProjectTemplates(): void {
if (PROJECT_TYPES.length > 0) { return; }

Expand Down Expand Up @@ -106,8 +134,8 @@ export class AddNewProjectCommand extends ActionsCommand {

private getLanguages(): Promise<string[]> {
let result: string[] = [ 'C#' ];
if (this.wizard && this.wizard.context && this.wizard.context.results[0]) {
let selectedProject = this.wizard.context.results[0];
if (this.wizard && this.wizard.context && this.wizard.context.results[1]) {
let selectedProject = this.wizard.context.results[1];
let index = PROJECT_TYPES.findIndex(pt => pt.value === selectedProject);
if (index >= 0) {
result = PROJECT_TYPES[index].languages;
Expand All @@ -118,8 +146,8 @@ export class AddNewProjectCommand extends ActionsCommand {
}

private getCurrentProjectName(): Promise<string> {
if (this.wizard && this.wizard.context && this.wizard.context.results[2]) {
return Promise.resolve(this.wizard.context.results[2]);
if (this.wizard && this.wizard.context && this.wizard.context.results[3]) {
return Promise.resolve(this.wizard.context.results[3]);
}

return Promise.resolve("");
Expand Down

0 comments on commit 55dde95

Please sign in to comment.