@@ -18,17 +18,31 @@ let extensionContext: ExtensionContext
18
18
let outputChannel : vscode . OutputChannel
19
19
export let client : LanguageClient
20
20
21
+ const sbtVersion = "1.2.3"
22
+ const sbtArtifact = `org.scala-sbt:sbt-launch:${ sbtVersion } `
23
+ const workspaceRoot = `${ vscode . workspace . rootPath } `
24
+ const disableDottyIDEFile = path . join ( workspaceRoot , ".dotty-ide-disabled" )
25
+ const sbtProjectDir = path . join ( workspaceRoot , "project" )
26
+ const sbtPluginFile = path . join ( sbtProjectDir , "dotty-plugin.sbt" )
27
+ const sbtBuildPropertiesFile = path . join ( sbtProjectDir , "build.properties" )
28
+ const sbtBuildSbtFile = path . join ( workspaceRoot , "build.sbt" )
29
+ const languageServerArtifactFile = path . join ( workspaceRoot , ".dotty-ide-artifact" )
30
+
31
+ function isUnconfiguredProject ( ) {
32
+ return ! ( fs . existsSync ( disableDottyIDEFile )
33
+ || fs . existsSync ( sbtPluginFile )
34
+ || fs . existsSync ( sbtBuildPropertiesFile )
35
+ || fs . existsSync ( sbtBuildSbtFile )
36
+ )
37
+ }
38
+
21
39
export function activate ( context : ExtensionContext ) {
22
40
extensionContext = context
23
41
outputChannel = vscode . window . createOutputChannel ( "Dotty" ) ;
24
42
25
- const sbtArtifact = "org.scala-sbt:sbt-launch:1.2.3"
26
- const buildSbtFile = `${ vscode . workspace . rootPath } /build.sbt`
27
- const dottyPluginSbtFile = path . join ( extensionContext . extensionPath , './out/dotty-plugin.sbt' )
28
- const disableDottyIDEFile = `${ vscode . workspace . rootPath } /.dotty-ide-disabled`
29
- const languageServerArtifactFile = `${ vscode . workspace . rootPath } /.dotty-ide-artifact`
30
- const languageServerDefaultConfigFile = path . join ( extensionContext . extensionPath , './out/default-dotty-ide-config' )
31
- const coursierPath = path . join ( extensionContext . extensionPath , './out/coursier' ) ;
43
+ const coursierPath = path . join ( extensionContext . extensionPath , "out" , "coursier" ) ;
44
+ const dottyPluginSbtFileSource = path . join ( extensionContext . extensionPath , "out" , "dotty-plugin.sbt" )
45
+ const buildSbtFileSource = path . join ( extensionContext . extensionPath , "out" , "build.sbt" )
32
46
33
47
vscode . workspace . onWillSaveTextDocument ( worksheet . prepareWorksheet )
34
48
vscode . workspace . onDidSaveTextDocument ( document => {
@@ -43,15 +57,15 @@ export function activate(context: ExtensionContext) {
43
57
} )
44
58
45
59
if ( process . env [ 'DLS_DEV_MODE' ] ) {
46
- const portFile = ` ${ vscode . workspace . rootPath } /. dotty-ide-dev-port`
60
+ const portFile = path . join ( workspaceRoot , ". dotty-ide-dev-port" )
47
61
fs . readFile ( portFile , ( err , port ) => {
48
62
if ( err ) {
49
63
outputChannel . appendLine ( `Unable to parse ${ portFile } ` )
50
64
throw err
51
65
}
52
66
53
67
run ( {
54
- module : context . asAbsolutePath ( ' out/ src/ passthrough-server.js' ) ,
68
+ module : context . asAbsolutePath ( path . join ( " out" , " src" , " passthrough-server.js" ) ) ,
55
69
args : [ port . toString ( ) ]
56
70
} , false )
57
71
} )
@@ -61,20 +75,14 @@ export function activate(context: ExtensionContext) {
61
75
// otherwise, try propose to start it if there's no build.sbt
62
76
if ( fs . existsSync ( languageServerArtifactFile ) ) {
63
77
runLanguageServer ( coursierPath , languageServerArtifactFile )
64
- } else if ( ! fs . existsSync ( disableDottyIDEFile ) && ! fs . existsSync ( buildSbtFile ) ) {
78
+ } else if ( isUnconfiguredProject ( ) ) {
65
79
vscode . window . showInformationMessage (
66
80
"This looks like an unconfigured Scala project. Would you like to start the Dotty IDE?" ,
67
81
"Yes" , "No"
68
82
) . then ( choice => {
69
83
if ( choice == "Yes" ) {
70
- fs . readFile ( languageServerDefaultConfigFile , ( err , data ) => {
71
- if ( err ) throw err
72
- else {
73
- const languageServerScalaVersion = data . toString ( ) . trim ( )
74
- fetchAndConfigure ( coursierPath , sbtArtifact , languageServerScalaVersion , dottyPluginSbtFile ) . then ( ( ) => {
75
- runLanguageServer ( coursierPath , languageServerArtifactFile )
76
- } )
77
- }
84
+ fetchAndConfigure ( coursierPath , sbtArtifact , buildSbtFileSource , dottyPluginSbtFileSource ) . then ( ( ) => {
85
+ runLanguageServer ( coursierPath , languageServerArtifactFile )
78
86
} )
79
87
} else {
80
88
fs . appendFile ( disableDottyIDEFile , "" , _ => { } )
@@ -101,17 +109,17 @@ function runLanguageServer(coursierPath: string, languageServerArtifactFile: str
101
109
} )
102
110
}
103
111
104
- function fetchAndConfigure ( coursierPath : string , sbtArtifact : string , languageServerScalaVersion : string , dottyPluginSbtFile : string ) {
112
+ function fetchAndConfigure ( coursierPath : string , sbtArtifact : string , buildSbtFileSource : string , dottyPluginSbtFileSource : string ) {
105
113
return fetchWithCoursier ( coursierPath , sbtArtifact ) . then ( ( sbtClasspath ) => {
106
- return configureIDE ( sbtClasspath , languageServerScalaVersion , dottyPluginSbtFile )
114
+ return configureIDE ( sbtClasspath , buildSbtFileSource , dottyPluginSbtFileSource )
107
115
} )
108
116
}
109
117
110
118
function fetchWithCoursier ( coursierPath : string , artifact : string , extra : string [ ] = [ ] ) {
111
119
return vscode . window . withProgress ( {
112
120
location : vscode . ProgressLocation . Window ,
113
121
title : `Fetching ${ artifact } `
114
- } , ( progress ) => {
122
+ } , _ => {
115
123
const args = [
116
124
"-jar" , coursierPath ,
117
125
"fetch" ,
@@ -142,21 +150,27 @@ function fetchWithCoursier(coursierPath: string, artifact: string, extra: string
142
150
} )
143
151
}
144
152
145
- function configureIDE ( sbtClasspath : string , languageServerScalaVersion : string , dottyPluginSbtFile : string ) {
153
+ function configureIDE ( sbtClasspath : string ,
154
+ buildSbtFileSource : string ,
155
+ dottyPluginSbtFileSource : string ) {
156
+
146
157
return vscode . window . withProgress ( {
147
158
location : vscode . ProgressLocation . Window ,
148
159
title : 'Configuring the IDE for Dotty...'
149
- } , ( progress ) => {
160
+ } , _ => {
161
+
162
+ // Bootstrap an sbt build
163
+ fs . mkdirSync ( sbtProjectDir )
164
+ fs . appendFileSync ( sbtBuildPropertiesFile , `sbt.version=${ sbtVersion } ` )
165
+ fs . copyFileSync ( buildSbtFileSource , sbtBuildSbtFile )
166
+ fs . copyFileSync ( dottyPluginSbtFileSource , path . join ( sbtProjectDir , "plugins.sbt" ) )
150
167
151
- // Run sbt to configure the IDE. If the `DottyPlugin` is not present, dynamically load it and
152
- // eventually run `configureIDE`.
168
+ // Run sbt to configure the IDE.
153
169
const sbtPromise =
154
170
cpp . spawn ( "java" , [
155
171
"-Dsbt.log.noformat=true" ,
156
172
"-classpath" , sbtClasspath ,
157
173
"xsbt.boot.Boot" ,
158
- `--addPluginSbtFile=${ dottyPluginSbtFile } ` ,
159
- `set every scalaVersion := "${ languageServerScalaVersion } "` ,
160
174
"configureIDE"
161
175
] )
162
176
@@ -182,7 +196,7 @@ function configureIDE(sbtClasspath: string, languageServerScalaVersion: string,
182
196
}
183
197
} )
184
198
185
- return sbtPromise
199
+ return sbtPromise
186
200
} )
187
201
}
188
202
0 commit comments