88import { OmnisharpServer } from '../omnisharpServer' ;
99import * as serverUtils from '../omnisharpUtils' ;
1010import findLaunchTargets from '../launchTargetFinder' ;
11- import { runInTerminal } from 'run-in-terminal' ;
11+ import * as cp from 'child_process' ;
12+ import * as fs from 'fs-extra-promise' ;
1213import * as path from 'path' ;
14+ import * as protocol from '../protocol' ;
1315import * as vscode from 'vscode' ;
1416
15- const isWindows = process . platform === 'win32' ;
17+ let channel = vscode . window . createOutputChannel ( '.NET' ) ;
1618
1719export default function registerCommands ( server : OmnisharpServer , extensionPath : string ) {
1820 let d1 = vscode . commands . registerCommand ( 'o.restart' , ( ) => server . restart ( ) ) ;
@@ -57,6 +59,26 @@ interface Command {
5759 execute ( ) : Thenable < any > ;
5860}
5961
62+ function projectsToCommands ( projects : protocol . DotNetProject [ ] ) : Promise < Command > [ ] {
63+ return projects . map ( project => {
64+ let projectDirectory = project . Path ;
65+
66+ return fs . lstatAsync ( projectDirectory ) . then ( stats => {
67+ if ( stats . isFile ( ) ) {
68+ projectDirectory = path . dirname ( projectDirectory ) ;
69+ }
70+
71+ return {
72+ label : `dotnet restore - (${ project . Name || path . basename ( project . Path ) } )` ,
73+ description : projectDirectory ,
74+ execute ( ) {
75+ return runDotnetRestore ( projectDirectory ) ;
76+ }
77+ } ;
78+ } ) ;
79+ } ) ;
80+ }
81+
6082export function dotnetRestoreAllProjects ( server : OmnisharpServer ) {
6183
6284 if ( ! server . isRunning ( ) ) {
@@ -65,23 +87,15 @@ export function dotnetRestoreAllProjects(server: OmnisharpServer) {
6587
6688 return serverUtils . requestWorkspaceInformation ( server ) . then ( info => {
6789
68- let commands : Command [ ] = [ ] ;
90+ if ( ! ( 'DotNet in info' ) || info . DotNet . Projects . length < 1 ) {
91+ return Promise . reject ( "No .NET Core projects found" ) ;
92+ }
93+
94+ let commandPromises = projectsToCommands ( info . DotNet . Projects ) ;
6995
70- if ( 'DotNet' in info && info . DotNet . Projects . length > 0 ) {
71- for ( let project of info . DotNet . Projects ) {
72- commands . push ( {
73- label : `dotnet restor - (${ project . Name || path . basename ( project . Path ) } )` ,
74- description : path . dirname ( project . Path ) ,
75- execute ( ) {
76- return runInTerminal ( 'dotnet' , [ 'restore' ] , {
77- cwd : path . dirname ( project . Path )
78- } ) ;
79- }
80- } ) ;
81- }
82- }
83-
84- return vscode . window . showQuickPick ( commands ) . then ( command => {
96+ return Promise . all ( commandPromises ) . then ( commands => {
97+ return vscode . window . showQuickPick ( commands ) ;
98+ } ) . then ( command => {
8599 if ( command ) {
86100 return command . execute ( ) ;
87101 }
@@ -91,17 +105,42 @@ export function dotnetRestoreAllProjects(server: OmnisharpServer) {
91105
92106export function dotnetRestoreForProject ( server : OmnisharpServer , fileName : string ) {
93107
108+ if ( ! server . isRunning ( ) ) {
109+ return Promise . reject ( 'OmniSharp server is not running.' ) ;
110+ }
111+
94112 return serverUtils . requestWorkspaceInformation ( server ) . then ( info => {
95- if ( 'DotNet' in info && info . DotNet . Projects . length > 0 ) {
96- for ( let project of info . DotNet . Projects ) {
97- if ( project . Path === path . dirname ( fileName ) ) {
98- return runInTerminal ( 'dotnet' , [ 'restore' , fileName ] , {
99- cwd : path . dirname ( project . Path )
100- } ) ;
101- }
102- }
103- }
104-
105- return Promise . reject ( `Failed to execute restore, try to run 'dotnet restore' manually for ${ fileName } .` ) ;
113+
114+ if ( ! ( 'DotNet in info' ) || info . DotNet . Projects . length < 1 ) {
115+ return Promise . reject ( "No .NET Core projects found" ) ;
116+ }
117+
118+ let directory = path . dirname ( fileName ) ;
119+
120+ for ( let project of info . DotNet . Projects ) {
121+ if ( project . Path === directory ) {
122+ return runDotnetRestore ( directory , fileName ) ;
123+ }
124+ }
125+ } ) ;
126+ }
127+
128+ function runDotnetRestore ( cwd : string , fileName ?: string ) {
129+ return new Promise < cp . ChildProcess > ( ( resolve , reject ) => {
130+ channel . clear ( ) ;
131+ channel . show ( ) ;
132+
133+ let cmd = 'dotnet restore' ;
134+ if ( fileName ) {
135+ cmd = `${ cmd } "${ fileName } "`
136+ }
137+
138+ return cp . exec ( cmd , { cwd : cwd , env : process . env } , ( err , stdout , stderr ) => {
139+ channel . append ( stdout . toString ( ) ) ;
140+ channel . append ( stderr . toString ( ) ) ;
141+ if ( err ) {
142+ channel . append ( 'ERROR: ' + err ) ;
143+ }
144+ } ) ;
106145 } ) ;
107146}
0 commit comments