@@ -87,9 +87,9 @@ func main() {
87
87
}
88
88
}
89
89
90
- err = configureXmx (alias )
90
+ err = configureVMOptions (alias )
91
91
if err != nil {
92
- log .WithError (err ).Error ("failed to configure backend Xmx " )
92
+ log .WithError (err ).Error ("failed to configure vmoptions " )
93
93
}
94
94
go run (wsInfo , alias )
95
95
@@ -307,19 +307,65 @@ func handleSignal(projectPath string) {
307
307
log .Info ("asked IDE to terminate" )
308
308
}
309
309
310
- func configureXmx (alias string ) error {
311
- xmx := os . Getenv ( strings . ToUpper ( alias ) + "_XMX" )
312
- if xmx == "" {
313
- return nil
310
+ func configureVMOptions (alias string ) error {
311
+ idePrefix := alias
312
+ if alias == "intellij " {
313
+ idePrefix = "idea"
314
314
}
315
- launcherPath := "/ide-desktop/backend/plugins/remote-dev-server/bin/launcher.sh"
316
- content , err := ioutil .ReadFile (launcherPath )
315
+ // [idea64|goland64|pycharm64|phpstorm64].vmoptions
316
+ path := fmt .Sprintf ("/ide-desktop/backend/bin/%s64.vmoptions" , idePrefix )
317
+ content , err := ioutil .ReadFile (path )
317
318
if err != nil {
318
319
return err
319
320
}
320
- // by default remote dev already set -Xmx2048m, see /ide-desktop/backend/plugins/remote-dev-server/bin/launcher.sh
321
- newContent := strings .Replace (string (content ), "-Xmx2048m" , "-Xmx" + xmx , 1 )
322
- return ioutil .WriteFile (launcherPath , []byte (newContent ), 0 )
321
+ newContent := updateVMOptions (alias , string (content ))
322
+ return ioutil .WriteFile (path , []byte (newContent ), 0 )
323
+ }
324
+
325
+ // deduplicateVMOption append new VMOptions onto old VMOptions and remove any duplicated leftmost options
326
+ func deduplicateVMOption (oldLines []string , newLines []string , predicate func (l , r string ) bool ) []string {
327
+ var result []string
328
+ var merged = append (oldLines , newLines ... )
329
+ for i , left := range merged {
330
+ for _ , right := range merged [i + 1 :] {
331
+ if predicate (left , right ) {
332
+ left = ""
333
+ break
334
+ }
335
+ }
336
+ if left != "" {
337
+ result = append (result , left )
338
+ }
339
+ }
340
+ return result
341
+ }
342
+
343
+ func updateVMOptions (alias string , content string ) string {
344
+ // inspired by how intellij platform merge the VMOptions
345
+ // https://github.com/JetBrains/intellij-community/blob/master/platform/platform-impl/src/com/intellij/openapi/application/ConfigImportHelper.java#L1115
346
+ filterFunc := func (l , r string ) bool {
347
+ isEqual := l == r
348
+ isXmx := strings .HasPrefix (l , "-Xmx" ) && strings .HasPrefix (r , "-Xmx" )
349
+ isXms := strings .HasPrefix (l , "-Xms" ) && strings .HasPrefix (r , "-Xms" )
350
+ isXss := strings .HasPrefix (l , "-Xss" ) && strings .HasPrefix (r , "-Xss" )
351
+ isXXOptions := strings .HasPrefix (l , "-XX:" ) && strings .HasPrefix (r , "-XX:" ) &&
352
+ strings .Split (l , "=" )[0 ] == strings .Split (r , "=" )[0 ]
353
+ return isEqual || isXmx || isXms || isXss || isXXOptions
354
+ }
355
+ // original vmoptions (inherited from $JETBRAINS_IDE_HOME/bin/idea64.vmoptions)
356
+ ideaVMOptionsLines := strings .Fields (content )
357
+ // Gitpod's default customization
358
+ gitpodVMOptions := []string {"-Dgtw.disable.exit.dialog=true" }
359
+ vmoptions := deduplicateVMOption (ideaVMOptionsLines , gitpodVMOptions , filterFunc )
360
+
361
+ // user-defined vmoptions
362
+ userVMOptionsVar := os .Getenv (strings .ToUpper (alias ) + "_VMOPTIONS" )
363
+ userVMOptions := strings .Fields (userVMOptionsVar )
364
+ if len (userVMOptions ) > 0 {
365
+ vmoptions = deduplicateVMOption (vmoptions , userVMOptions , filterFunc )
366
+ }
367
+ // vmoptions file should end with a newline
368
+ return strings .Join (vmoptions , "\n " ) + "\n "
323
369
}
324
370
325
371
/**
0 commit comments