/
ScionManager.java
927 lines (811 loc) · 36.8 KB
/
ScionManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
package net.sf.eclipsefp.haskell.ui.internal.scion;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.List;
import java.util.Stack;
import net.sf.eclipsefp.haskell.browser.BrowserPlugin;
import net.sf.eclipsefp.haskell.browser.Database;
import net.sf.eclipsefp.haskell.browser.items.HoogleStatus;
import net.sf.eclipsefp.haskell.buildwrapper.BWFacade;
import net.sf.eclipsefp.haskell.buildwrapper.BuildWrapperPlugin;
import net.sf.eclipsefp.haskell.buildwrapper.JobFacade;
import net.sf.eclipsefp.haskell.buildwrapper.types.BuildOptions;
import net.sf.eclipsefp.haskell.core.cabal.CabalImplementationManager;
import net.sf.eclipsefp.haskell.core.cabal.CabalPackageVersion;
import net.sf.eclipsefp.haskell.core.cabalmodel.CabalSyntax;
import net.sf.eclipsefp.haskell.core.cabalmodel.PackageDescription;
import net.sf.eclipsefp.haskell.core.cabalmodel.PackageDescriptionLoader;
import net.sf.eclipsefp.haskell.core.cabalmodel.PackageDescriptionStanza;
import net.sf.eclipsefp.haskell.core.cabalmodel.RealValuePosition;
import net.sf.eclipsefp.haskell.core.code.ModuleCreationInfo;
import net.sf.eclipsefp.haskell.core.partitioned.runner.AlexRunner;
import net.sf.eclipsefp.haskell.core.partitioned.runner.HappyRunner;
import net.sf.eclipsefp.haskell.core.partitioned.runner.UuagcRunner;
import net.sf.eclipsefp.haskell.core.project.HaskellNature;
import net.sf.eclipsefp.haskell.hlint.HLintPlugin;
import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
import net.sf.eclipsefp.haskell.ui.console.HaskellConsole;
import net.sf.eclipsefp.haskell.ui.internal.preferences.IPreferenceConstants;
import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
import net.sf.eclipsefp.haskell.ui.util.CabalFileChangeListener;
import net.sf.eclipsefp.haskell.util.FileUtil;
import net.sf.eclipsefp.haskell.util.ProcessRunner;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProvider;
/**
* Manages helper executables
*
* This class manages buildwrapper, scion-browser, etc. It gets the path from the preferences and does the initialization
*
* This works by listening for resource changes.
*/
public class ScionManager implements IResourceChangeListener {
/** Current executable path string */
private IPath buildWrapperExecutablePath;
/** Current browser executable path string */
private IPath browserExecutablePath;
/** Haskell console low water mark */
private int hConLowWater;
/** Haskell console high water mark */
private int hConHighWater;
private final static String MINIMUM_BUILDWRAPPER="0.6.5";
private final static String MINIMUM_SCIONBROWSER="0.2.12";
public ScionManager() {
// The interesting stuff is done in the start() method
buildWrapperExecutablePath = null;
browserExecutablePath = null;
hConLowWater = HaskellConsole.HASKELL_CONSOLE_LOW_WATER_MARK;
hConHighWater = HaskellConsole.HASKELL_CONSOLE_HIGH_WATER_MARK;
}
public static String getExecutablePath(final String preference,final String exeName,final boolean strict){
IPreferenceStore preferenceStore = HaskellUIPlugin.getDefault().getPreferenceStore();
String exe = preferenceStore.getString( preference );
if (exe!=null && exe.length()>0){
File f=new File(exe);
if(f.exists()){
return f.getAbsolutePath();
}
}
File exeF=FileUtil.findExecutableInPath( exeName );
if (exeF!=null){
HaskellUIPlugin.getDefault().getPreferenceStore().setValue(preference,exeF.getAbsolutePath());
return exeF.getAbsolutePath();
}
return strict?null:exeName;
}
public void start() {
IWorkspace workSpace = ResourcesPlugin.getWorkspace();
IPreferenceStore preferenceStore = HaskellUIPlugin.getDefault().getPreferenceStore();
// Capture preferences as currently stored:
hConLowWater = preferenceStore.getInt( IPreferenceConstants.HASKELL_CONSOLE_LOW_WATER_MARK );
if (hConLowWater == 0) {
hConLowWater = HaskellConsole.HASKELL_CONSOLE_LOW_WATER_MARK;
}
hConHighWater = preferenceStore.getInt( IPreferenceConstants.HASKELL_CONSOLE_HIGH_WATER_MARK );
if (hConHighWater == 0) {
hConHighWater = HaskellConsole.HASKELL_CONSOLE_HIGH_WATER_MARK;
}
/*final String serverExecutable = preferenceStore.getString( IPreferenceConstants.BUILDWRAPPER_EXECUTABLE );
if (serverExecutable.length() > 0) {
buildWrapperExecutablePath = new Path(serverExecutable);
if (!buildWrapperExecutablePath.toFile().exists()){
buildWrapperExecutablePath=null;
}
}
// look in path
if (buildWrapperExecutablePath==null){
File f=FileUtil.findExecutableInPath( "buildwrapper" );
if (f!=null){
buildWrapperExecutablePath=new Path(f.getAbsolutePath());
// set preference
HaskellUIPlugin.getDefault().getPreferenceStore().setValue(IPreferenceConstants.BUILDWRAPPER_EXECUTABLE,f.getAbsolutePath());
}
}*/
String serverExecutable =getExecutablePath( IPreferenceConstants.BUILDWRAPPER_EXECUTABLE, "buildwrapper",true );
if (serverExecutable!=null){
buildWrapperExecutablePath = new Path(serverExecutable);
}
// final String browserExecutable = preferenceStore.getString( IPreferenceConstants.SCION_BROWSER_SERVER_EXECUTABLE );
// if (browserExecutable.length() > 0) {
// browserExecutablePath = new Path(browserExecutable);
// if (!browserExecutablePath.toFile().exists()){
// browserExecutablePath=null;
// }
//
// }
// // look in path
// if (browserExecutablePath==null){
// File f=FileUtil.findExecutableInPath( "scion-browser" );
// if (f!=null){
// browserExecutablePath=new Path(f.getAbsolutePath());
// // set preference
// HaskellUIPlugin.getDefault().getPreferenceStore().setValue(IPreferenceConstants.SCION_BROWSER_SERVER_EXECUTABLE,f.getAbsolutePath());
// }
// }
serverExecutable =getExecutablePath( IPreferenceConstants.SCION_BROWSER_SERVER_EXECUTABLE, "scion-browser",true );
if (serverExecutable!=null){
browserExecutablePath = new Path(serverExecutable);
}
// can't install if no cabal executable
if ((buildWrapperExecutablePath==null || browserExecutablePath==null) && CabalImplementationManager.getCabalExecutable()!=null){
boolean ignore=HaskellUIPlugin.getDefault().getPreferenceStore().getBoolean( IPreferenceConstants.IGNORE_MISSING_EXECUTABLE );
if (!ignore){
final Display display = HaskellUIPlugin.getStandardDisplay();
display.asyncExec( new Runnable() {
@Override
public void run() {
Shell parent = display.getActiveShell();
InstallExecutableDialog ied=new InstallExecutableDialog(parent , buildWrapperExecutablePath==null, MINIMUM_BUILDWRAPPER, browserExecutablePath==null, MINIMUM_SCIONBROWSER );
ied.open();
}
});
}
}
boolean doBuildWrapperSetup=true;
boolean doBrowserSetup=true;
boolean ignore=HaskellUIPlugin.getDefault().getPreferenceStore().getBoolean( IPreferenceConstants.IGNORE_TOOOLD_EXECUTABLE );
if (!ignore){
final boolean buildwrapperVersionOK=buildWrapperExecutablePath==null || checkVersion( buildWrapperExecutablePath, MINIMUM_BUILDWRAPPER ,true);
final boolean browserVersionOK=browserExecutablePath==null || checkVersion( browserExecutablePath, MINIMUM_SCIONBROWSER,false );
doBuildWrapperSetup=buildwrapperVersionOK; // do not launch if too old
doBrowserSetup=browserVersionOK;// do not launch if too old
if (!buildwrapperVersionOK || !browserVersionOK){
final Display display = HaskellUIPlugin.getStandardDisplay();
display.asyncExec( new Runnable() {
@Override
public void run() {
Shell parent = display.getActiveShell();
InstallOutdatedExecutableDialog ied =
new InstallOutdatedExecutableDialog(parent,
!buildwrapperVersionOK, MINIMUM_BUILDWRAPPER, getVersion(buildWrapperExecutablePath,true), buildWrapperExecutablePath.toOSString(),
!browserVersionOK, MINIMUM_SCIONBROWSER, getVersion(browserExecutablePath, false), browserExecutablePath.toOSString() );
ied.open();
}
});
}
}
if (doBuildWrapperSetup){
buildWrapperFactorySetup();
}
// Set up the output logging console for the shared Browser
HaskellConsole cBrowser = new HaskellConsole( UITexts.sharedBrowserInstance_console );
BrowserPlugin.setSharedLogStream( cBrowser.createOutputWriter() );
cBrowser.setWaterMarks( hConLowWater, hConHighWater );
if (doBrowserSetup){
// ensure class is loaded
BrowserLocalDatabaseRebuildJob.class.toString();
BrowserLocalDatabaseRebuildJobListener.class.toString();
browserSetup();
}
HLintPlugin.setHlintPath( preferenceStore.getString( IPreferenceConstants.HLINT_EXECUTABLE ));
AlexRunner.setFullPath( preferenceStore.getString( IPreferenceConstants.ALEX_EXECUTABLE ) );
HappyRunner.setFullPath( preferenceStore.getString( IPreferenceConstants.HAPPY_EXECUTABLE ) );
UuagcRunner.setFullPath( preferenceStore.getString( IPreferenceConstants.UUAGC_EXECUTABLE ) );
// Sit and listen to the preference store changes
preferenceStore.addPropertyChangeListener( new ExecutablesPropertiesListener() );
try {
workSpace.getRoot().accept( new UpdateResourceVisitor() );
} catch( CoreException ex ) {
HaskellUIPlugin.log( UITexts.scion_delta_error, ex );
}
workSpace.addResourceChangeListener( this, IResourceChangeEvent.POST_CHANGE );
workSpace.addResourceChangeListener( new FileDeletionListener(), IResourceChangeEvent.PRE_BUILD );
// POST_BUILD is similar to POST_CHANGE but the workspace tree is not locked, which is useful for some listeners
workSpace.addResourceChangeListener( new CabalFileResourceChangeListener(), IResourceChangeEvent.POST_BUILD );
workSpace.addResourceChangeListener( new ProjectDeletionListener(), IResourceChangeEvent.PRE_DELETE);
}
/**
* Handle ScionPP preference changes.
*
* @param forceRebuildServer If using the built-in scion-server, should it be rebuilt?
* @param forceRebuildServer If using the built-in scion-browser, should it be rebuilt?
*/
public void handlePreferenceChanges() {
handlePreferenceChangesServer( );
handlePreferenceChangesBrowser( );
}
public void handlePreferenceChangesServer() {
IPreferenceStore preferenceStore = HaskellUIPlugin.getDefault().getPreferenceStore();
final String newServerExecutable = preferenceStore.getString( IPreferenceConstants.BUILDWRAPPER_EXECUTABLE );
IPath newServerExecutablePath = new Path(newServerExecutable);
buildWrapperExecutablePath = newServerExecutablePath;
buildWrapperFactorySetup();
}
public void handlePreferenceChangesBrowser() {
IPreferenceStore preferenceStore = HaskellUIPlugin.getDefault().getPreferenceStore();
//boolean newUseBuiltIn = preferenceStore.getBoolean( IPreferenceConstants.SCION_BROWSER_SERVER_BUILTIN );
final String newServerExecutable = preferenceStore.getString( IPreferenceConstants.SCION_BROWSER_SERVER_EXECUTABLE );
IPath newServerExecutablePath = new Path(newServerExecutable);
// Did something change?
if (newServerExecutablePath.equals( browserExecutablePath )) {
return;
}
// Yup, something changed, so shut down the instances...
BrowserPlugin.getSharedInstance().stop();
// Switch over to the null instance factory
BrowserPlugin.useNullSharedInstance();
// And update...
browserExecutablePath = newServerExecutablePath;
browserSetup();
}
/**
* setup buildwrapper
*/
private synchronized void buildWrapperFactorySetup(){
IPreferenceStore preferenceStore = HaskellUIPlugin.getDefault().getPreferenceStore();
boolean verbose = preferenceStore.getBoolean( IPreferenceConstants.VERBOSE_INTERACTION );
BuildWrapperPlugin.logAnswers=verbose;
if ( buildWrapperExecutablePath != null && buildWrapperExecutablePath.toFile().exists() ) {
try {
/** we get the dreaded message about mismatch cabal versions if the buildwrapper cabal library is not the same as the cabal library used to build the cabal executable **/
List<String> ls=ProcessRunner.getExecutableAndCabalVersion( buildWrapperExecutablePath.toOSString(),true);
if (ls!=null && ls.size()>1){
String cabalVersion=ls.get( 1 );
if (CabalImplementationManager.getCabalLibraryVersion()!=null && !CabalImplementationManager.getCabalLibraryVersion().toString().equals( cabalVersion )){
String msg=NLS.bind( UITexts.buildWrapperCabalVersionMismatch, new Object[]{CabalImplementationManager.getCabalLibraryVersion().toString(),cabalVersion,CabalImplementationManager.getCabalExecutable()} );
HaskellUIPlugin.log( msg, IStatus.ERROR );
}
}
} catch (IOException ioe){
HaskellUIPlugin.log(UITexts.error_getVersion, ioe);
}
BuildWrapperPlugin.setBwPath( buildWrapperExecutablePath.toOSString() );
} else {
BuildWrapperPlugin.setBwPath(null);
buildWrapperExecutablePath = null;
}
}
public static String getVersion(final IPath path,final boolean wait){
if (path!=null){
try {
return ProcessRunner.getExecutableVersion(path.toOSString(),wait);
} catch (IOException ioe){
HaskellUIPlugin.log(UITexts.error_getVersion, ioe);
}
}
return null;
}
public static boolean checkVersion(final IPath path,final String minimal,final boolean wait){
String currentVersion=getVersion(path, wait);
if (currentVersion==null) {
return false;
} else {
return CabalPackageVersion.compare( currentVersion, minimal )>=0;
}
}
private synchronized void browserSetup() {
final Display display = Display.getDefault();
if ( browserExecutablePath != null && browserExecutablePath.toFile().exists() ) {
IPreferenceStore preferenceStore = HaskellUIPlugin.getDefault().getPreferenceStore();
boolean verbose = preferenceStore.getBoolean( IPreferenceConstants.BROWSER_VERBOSE_INTERACTION );
BrowserPlugin.changeSharedInstance( browserExecutablePath ,verbose );
display.asyncExec( new Runnable() {
@Override
public void run() {
Job builder = new BrowserLocalDatabaseRebuildJob(UITexts.scionBrowserRebuildingDatabase);
//builder.setRule( ResourcesPlugin.getWorkspace().getRoot() );
builder.setPriority( Job.DECORATE );
builder.schedule();
}
} );
} else {
browserExecutablePath = null;
BrowserPlugin.useNullSharedInstance();
}
}
void loadHackageDatabase() {
final Display display = Display.getDefault();
final IPreferenceStore preferenceStore = HaskellUIPlugin.getDefault().getPreferenceStore();
boolean questionWasAnswered = preferenceStore.getBoolean( IPreferenceConstants.SCION_BROWSER_HACKAGE_QUESTION_ANSWERED );
if( !questionWasAnswered ) {
display.asyncExec( new Runnable() {
@Override
public void run() {
// needs ui thread
final Shell parentShell = display.getActiveShell();
boolean result = MessageDialog.openQuestion( parentShell,
UITexts.scionBrowserUseHackage_QuestionNew_title,
UITexts.scionBrowserUseHackage_QuestionNew_label );
preferenceStore.setValue( IPreferenceConstants.SCION_BROWSER_HACKAGE_QUESTION_ANSWERED, true );
preferenceStore.setValue( IPreferenceConstants.SCION_BROWSER_USE_HACKAGE, result );
}
} );
}
if (preferenceStore.getBoolean( IPreferenceConstants.SCION_BROWSER_USE_HACKAGE )) {
boolean rebuild = !questionWasAnswered || !BrowserPlugin.getHackageDatabasePath().toFile().exists();
if (!rebuild) {
/* Check time of the Hackage database */
long timeDiff = BrowserPlugin.getHackageDatabasePath().toFile().lastModified() - (new Date()).getTime();
/* We ask to rebuild if more than one week passed since last update */
boolean askRebuild = timeDiff > 7 /* days */ * 24 /* h/day */ * 3600 /* s/h */ * 1000 /* ms/s */;
if (askRebuild) {
final Stack<Boolean> response = new Stack<Boolean>();
display.asyncExec( new Runnable() {
@Override
public void run() {
// needs ui thread
final Shell parentShell = display.getActiveShell();
boolean result = MessageDialog.openQuestion( parentShell,
UITexts.scionBrowserUseHackage_QuestionUpdate_title,
UITexts.scionBrowserUseHackage_QuestionUpdate_label );
response.push(result);
}
} );
rebuild = response.pop();
}
}
/* Execute build job */
final boolean doRebuild = rebuild;
display.asyncExec( new Runnable() {
@Override
public void run() {
Job builder = new BrowserHackageDatabaseRebuildJob(
UITexts.scionBrowserRebuildingDatabase, doRebuild );
// builder.setRule( ResourcesPlugin.getWorkspace().getRoot() );
builder.setPriority( Job.DECORATE );
builder.schedule();
}
} );
} else {
preloadPrelude();
checkHoogleDataIsPresent();
}
}
void preloadPrelude() {
try {
BrowserPlugin.getSharedInstance().getDeclarations(Database.ALL, "Prelude" );
} catch (Exception e) {
// Do nothing
}
}
void checkHoogleDataIsPresent() {
final IPreferenceStore preferenceStore = HaskellUIPlugin.getDefault().getPreferenceStore();
String extraHooglePath = preferenceStore.getString( IPreferenceConstants.SCION_BROWSER_EXTRA_HOOGLE_PATH );
checkHoogleDataIsPresent(extraHooglePath);
}
void checkHoogleDataIsPresent(final String extraHooglePath) {
boolean rebuild = false;
try {
// Set extra paths if needed
if (extraHooglePath != null && extraHooglePath.length() > 0) {
BrowserPlugin.getSharedInstance().setExtraHooglePath( extraHooglePath );
}
rebuild = HoogleStatus.ERROR.equals(BrowserPlugin.getSharedInstance().checkHoogle());
} catch( Exception e ) {
// ignore
}
if( rebuild ) {
// There is no "fmap", we don't have a database
final Display display = Display.getDefault();
display.asyncExec( new Runnable() {
@Override
public void run() {
// needs ui thread
Shell parentShell = display.getActiveShell();
if( MessageDialog
.openQuestion(
parentShell,
UITexts.hoogle_dataNotPresent_title,
UITexts.hoogle_dataNotPresent_message ) ) {
display.asyncExec( new Runnable() {
@Override
public void run() {
Job builder = new HoogleDownloadDataJob(
UITexts.hoogle_downloadingData );
// no need to stop all other operations
//builder.setRule( ResourcesPlugin.getWorkspace().getRoot() );
builder.setPriority( Job.DECORATE );
builder.schedule();
}
} );
}
}
} );
}
}
/**
* Detects when a file is deleted and updates the Cabal file accordingly (remove the module).
* If the removed file is the cabal file, stop the underlying scion-server.
*
* @author JP Moresmau
*/
private class FileDeletionListener implements IResourceChangeListener {
@Override
public void resourceChanged( final IResourceChangeEvent event ) {
try {
event.getDelta().accept( new IResourceDeltaVisitor() {
@Override
public boolean visit( final IResourceDelta delta )
throws CoreException {
if( delta.getKind() == IResourceDelta.REMOVED ) {
if( delta.getResource() instanceof IFile){
IFile f = ( IFile )delta.getResource();
if (!f.getProject().isOpen()){
return false;
}
IFile cabalF = BuildWrapperPlugin.getCabalFile( f.getProject() );
if(FileUtil.hasHaskellExtension( f ) && f.getProject().isOpen()) {
// System.out.println(delta.getFullPath());
PackageDescription pd = PackageDescriptionLoader.load( cabalF );
ModuleCreationInfo info = new ModuleCreationInfo( f );
if (info.getSourceContainer()!=null){
List<PackageDescriptionStanza> lpds = pd.getStanzasBySourceDir().get( info.getSourceContainer().getProjectRelativePath().toOSString() );
if (lpds!=null && lpds.size()>0){
String qn = info.getQualifiedModuleName();
IDocumentProvider prov = new TextFileDocumentProvider();
prov.connect( cabalF );
try {
IDocument doc = prov.getDocument( cabalF );
for( PackageDescriptionStanza pds: lpds ) {
pds=pd.getSameStanza(pds);
RealValuePosition rvp = pds.removeFromPropertyList( CabalSyntax.FIELD_EXPOSED_MODULES, qn );
if (rvp!=null){
rvp.updateDocument( doc );
pd=PackageDescriptionLoader.load( doc.get() );
pds=pd.getSameStanza(pds);
}
rvp = pds.removeFromPropertyList( CabalSyntax.FIELD_OTHER_MODULES, qn );
if (rvp!=null){
rvp.updateDocument( doc );
pd=PackageDescriptionLoader.load( doc.get() );
}
}
prov.saveDocument( null, cabalF, doc, true );
} finally {
prov.disconnect( cabalF );
}
}
}
/* final ScionInstance si = ScionPlugin.getScionInstance( f );
if (si != null) {
BuildOptions buildOptions=new BuildOptions().setOutput(false).setRecompile(true);
si.buildProject( buildOptions);
}*/
JobFacade fa=BuildWrapperPlugin.getJobFacade( f.getProject() );
if (fa!=null){
fa.build( new BuildOptions().setOutput(false) );
}
return false;
} else if (f.equals( cabalF )){
stopInstance( f );
return false;
}
}
}
return true;
}
} );
} catch( CoreException ex ) {
HaskellUIPlugin.log( UITexts.scion_delta_error, ex );
}
}
}
/**
* <p>detects when a haskell project is deleted and stops the corresponding scion server</p>
*
* @author JP Moresmau
*/
public class ProjectDeletionListener implements IResourceChangeListener{
@Override
public void resourceChanged( final IResourceChangeEvent event ) {
if (event.getResource() instanceof IProject){
stopInstance( event.getResource() );
}
}
}
/** */
public class ExecutablesPropertiesListener implements IPropertyChangeListener {
@Override
public void propertyChange( final PropertyChangeEvent event ) {
if( event.getProperty().equals( IPreferenceConstants.BUILDWRAPPER_EXECUTABLE ) ) {
if( event.getNewValue() instanceof String ) {
buildWrapperExecutablePath=new Path((String)event.getNewValue());
buildWrapperFactorySetup();
}
} else if (event.getProperty().equals( IPreferenceConstants.SCION_BROWSER_SERVER_EXECUTABLE)) {
if (event.getNewValue() instanceof String) {
browserExecutablePath = new Path((String)event.getNewValue());
browserSetup();
}
} else if (event.getProperty().equals( IPreferenceConstants.SCION_BROWSER_EXTRA_HOOGLE_PATH)) {
if (event.getNewValue() instanceof String) {
checkHoogleDataIsPresent((String)event.getNewValue());
}
} else if (event.getProperty().equals(IPreferenceConstants.HLINT_EXECUTABLE)){
if (event.getNewValue() instanceof String || event.getNewValue()==null){
HLintPlugin.setHlintPath( (String)event.getNewValue() );
}
} else if (event.getProperty().equals(IPreferenceConstants.ALEX_EXECUTABLE)){
if (event.getNewValue() instanceof String || event.getNewValue()==null){
AlexRunner.setFullPath( (String)event.getNewValue() );
}
} else if (event.getProperty().equals(IPreferenceConstants.HAPPY_EXECUTABLE)){
if (event.getNewValue() instanceof String || event.getNewValue()==null){
HappyRunner.setFullPath( (String)event.getNewValue() );
}
} else if (event.getProperty().equals(IPreferenceConstants.UUAGC_EXECUTABLE)){
if (event.getNewValue() instanceof String || event.getNewValue()==null){
UuagcRunner.setFullPath( (String)event.getNewValue() );
}
} else if (event.getProperty().equals(IPreferenceConstants.VERBOSE_INTERACTION)){
boolean verbose = ((Boolean)event.getNewValue()).booleanValue();
BuildWrapperPlugin.logAnswers=verbose;
}
}
}
/** */
public class UpdateResourceVisitor implements IResourceVisitor {
@Override
public boolean visit( final IResource resource ) throws CoreException {
return updateForResource( resource );
}
}
/** */
public class CabalFileResourceChangeListener implements IResourceChangeListener {
@Override
public void resourceChanged( final IResourceChangeEvent event ) {
try {
event.getDelta().accept( new IResourceDeltaVisitor() {
@Override
public boolean visit( final IResourceDelta delta ) {
if( delta.getKind() == IResourceDelta.CHANGED && (delta.getFlags() & IResourceDelta.CONTENT)>0) {
if( delta.getResource() instanceof IFile ) {
IFile f = ( IFile )delta.getResource();
IFile cabalF = BuildWrapperPlugin.getCabalFile( f.getProject() );
if( f.equals( cabalF ) ) {
BWFacade bwf=BuildWrapperPlugin.getFacade( f.getProject() );
if (bwf!=null){
bwf.cabalFileChanged();
}
for (CabalFileChangeListener l:CabalFileChangeListenerManager.getListeners()){
l.cabalFileChanged( f );
}
}
return false;
}
}
return true;
}
} );
} catch( CoreException ex ) {
HaskellUIPlugin.log( UITexts.scion_delta_error, ex );
}
}
}
/** Preference change listener when EclipseFP core preferences change */
// public class CorePreferencesChangeListener implements IPreferenceChangeListener {
// public void preferenceChange( final PreferenceChangeEvent event ) {
// String key = event.getKey();
// //if( ICorePreferenceNames.HS_IMPLEMENTATIONS.equals( key )
// // || ICorePreferenceNames.SELECTED_HS_IMPLEMENTATION.equals( key ) ) {
// if (CabalImplementationManager.DEFAULT_CABAL_IMPLEMENTATION.equals( key ) || ICorePreferenceNames.SELECTED_HS_IMPLEMENTATION.equals( key )){
// //if (useBuiltIn){
// handlePreferenceChanges();
// //}
// } /*else {
// HaskellUIPlugin.log("Core preference changed: ".concat( key ), IStatus.INFO);
// }*/
// }
// }
public void stop() {
ResourcesPlugin.getWorkspace().removeResourceChangeListener( this );
// ScionPlugin.stopAllInstances();
}
/**
* Called after a resource in the workspace was changed. It finds all projects
* that were opened/closed, and starts/stops Scion instances accordingly.
*/
@Override
public void resourceChanged( final IResourceChangeEvent event ) {
try {
event.getDelta().accept( new IResourceDeltaVisitor() {
@Override
public boolean visit( final IResourceDelta delta ) throws CoreException {
return updateForResource( delta.getResource() );
}
} );
} catch( CoreException ex ) {
HaskellUIPlugin.log( UITexts.scion_delta_error, ex );
}
}
private boolean updateForResource( final IResource resource )
throws CoreException {
if( resource instanceof IProject ) {
IProject project = ( IProject ) resource;
if( project.isOpen()
&& project.hasNature( HaskellNature.NATURE_ID ) ) {
startInstance( project );
}
if( !project.isOpen() ) {
// we cannot check the nature of closed projects, but if it's in
// instances, stop it
stopInstance( project );
}
return true; // projects can't be children of other projects, can they?
}
return true;
}
/**
* Starts and returns a new Scion instance for the given project. Does not add
* the instance to the instances map.
*/
private synchronized void startInstance( final IProject project ) {
if (BuildWrapperPlugin.getFacade( project )==null && CabalImplementationManager.getCabalExecutable()!=null){
HaskellConsole cbw=getBWHaskellConsole( project );
Writer outStreamBw = cbw.createOutputWriter();
BuildWrapperPlugin.createFacade(project, CabalImplementationManager.getCabalExecutable(), outStreamBw );
}
}
/**
* Stops the Scion instance for the given project. Does not remove the
* instance from the instances map.
*/
private void stopInstance( final IProject project ) {
if( project != null) {
BuildWrapperPlugin.removeFacade( project );
//if ( ScionPlugin.terminateScionInstance( project ) ) {
IConsoleManager mgr = ConsolePlugin.getDefault().getConsoleManager();
String name = bwconsoleName( project);
for( IConsole c: mgr.getConsoles() ) {
if( c.getName().equals( name ) ) {
mgr.removeConsoles( new IConsole[] { c } );
break;
}
}
//}
}
}
private void stopInstance(final IResource res){
if (res != null && res.getProject() != null) {
stopInstance( res.getProject() );
}
}
private final String bwconsoleName ( final IProject project ) {
String projectName = project != null ? project.getName() : UITexts.noproject;
return NLS.bind( UITexts.bw_console_title, projectName );
}
private HaskellConsole getBWHaskellConsole(final IProject project) {
final String consoleName = bwconsoleName(project);
final IConsoleManager mgr = ConsolePlugin.getDefault().getConsoleManager();
for( IConsole c: mgr.getConsoles() ) {
if( c.getName().equals( consoleName ) ) {
return (HaskellConsole) c;
}
}
HaskellConsole hCon = new HaskellConsole( consoleName );
hCon.setWaterMarks( hConLowWater, hConHighWater );
return hCon;
}
private class BrowserLocalDatabaseRebuildJobListener extends JobChangeAdapter{
@Override
public void done( final IJobChangeEvent event ) {
if (event.getResult().isOK()) {
loadHackageDatabase();
} else {
Display.getDefault().syncExec( new Runnable() {
@Override
public void run() {
MessageDialog.openError( Display.getDefault().getActiveShell(),
UITexts.scionBrowserRebuildingDatabaseError_title,
UITexts.scionBrowserRebuildingDatabaseError_message );
}
} );
}
super.done( event );
}
}
/** Specialized Job class that manages rebuilding the Browser database.
* Based in the work of B. Scott Michel.
*
* @author B. Alejandro Serrano
*/
public class BrowserLocalDatabaseRebuildJob extends Job {
IStatus status;
public BrowserLocalDatabaseRebuildJob(final String jobTitle) {
super(jobTitle);
// If the build failed, there will be some indication of why it failed.
addJobChangeListener( new BrowserLocalDatabaseRebuildJobListener());
}
@Override
protected IStatus run( final IProgressMonitor monitor ) {
monitor.beginTask( UITexts.scionBrowserRebuildingDatabase, IProgressMonitor.UNKNOWN );
status = BrowserPlugin.loadLocalDatabase( true );
monitor.done();
return status;
}
}
/** Specialized Job class that manages loading the Hackage part of Browser database.
* Based in the work of B. Scott Michel.
*
* @author B. Alejandro Serrano
*/
public class BrowserHackageDatabaseRebuildJob extends Job {
IStatus status;
boolean rebuild;
public BrowserHackageDatabaseRebuildJob(final String jobTitle, final boolean rebuild) {
super(jobTitle);
this.rebuild = rebuild;
// If the build failed, there will be some indication of why it failed.
addJobChangeListener( new JobChangeAdapter() {
@Override
public void done( final IJobChangeEvent event ) {
if (event.getResult().isOK()) {
preloadPrelude();
checkHoogleDataIsPresent();
} else {
Display.getDefault().syncExec( new Runnable() {
@Override
public void run() {
MessageDialog.openError( Display.getDefault().getActiveShell(),
UITexts.scionBrowserRebuildingDatabaseError_title,
UITexts.scionBrowserRebuildingDatabaseError_message );
}
} );
}
super.done( event );
}
});
}
@Override
protected IStatus run( final IProgressMonitor monitor ) {
monitor.beginTask( UITexts.scionBrowserRebuildingDatabase, IProgressMonitor.UNKNOWN );
status = BrowserPlugin.loadHackageDatabase( rebuild );
monitor.done();
return status;
}
}
/** Specialized Job class that manages downloading Hoogle data.
* Based in the work of B. Scott Michel.
*
* @author B. Alejandro Serrano
*/
public class HoogleDownloadDataJob extends Job {
public HoogleDownloadDataJob( final String jobTitle ) {
super( jobTitle );
}
@Override
protected IStatus run( final IProgressMonitor monitor ) {
monitor.beginTask( UITexts.hoogle_downloadingData, IProgressMonitor.UNKNOWN );
try {
BrowserPlugin.getSharedInstance().downloadHoogleData();
BrowserPlugin.getSharedInstance().checkHoogle();
} catch( Exception e ) {
// Do nothing if fails
}
monitor.done();
return Status.OK_STATUS;
}
}
}