FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/session/projects/SessionProjects.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
projects
/
SessionProjects.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1032 lines (879 loc) · 33.7 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
projects
/
SessionProjects.cpp
Copy path
File metadata and controls
1032 lines (879 loc) · 33.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SessionProjects.cpp
*
* Copyright (C) 2009-19 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
session/projects/SessionProjects.hpp
>
#
include
<
core/Exec.hpp
>
#
include
<
core/FileSerializer.hpp
>
#
include
<
core/http/URL.hpp
>
#
include
<
core/r_util/RSessionContext.hpp
>
#
include
<
session/SessionModuleContext.hpp
>
#
include
<
session/SessionProjectTemplate.hpp
>
#
include
<
session/SessionScopes.hpp
>
#
include
<
session/prefs/UserPrefs.hpp
>
#
include
<
r/RExec.hpp
>
#
include
<
r/RRoutines.hpp
>
#
include
<
r/session/RSessionUtils.hpp
>
#
include
"
SessionProjectFirstRun.hpp
"
#
include
"
SessionProjectsInternal.hpp
"
using
namespace
rstudio
::core
;
namespace
rstudio
{
namespace
session
{
namespace
projects
{
namespace
{
ProjectContext s_projectContext;
void
onSuspend
(Settings*)
{
//
on suspend write out current project path as the one to use
//
on resume. we read this back in initialize (rather than in
//
the onResume handler) because we need it very early in the
//
processes lifetime and onResume happens too late
projects::ProjectsSettings
(
options
().
userScratchPath
()).
setNextSessionProject
(s_projectContext.
file
().
getAbsolutePath
());
}
void
onResume
(
const
Settings&) {}
Error
validateProjectPath
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
std::string projectFile;
Error error =
json::readParams
(request.
params
, &projectFile);
if
(error)
return
error;
FilePath projectFilePath =
module_context::resolveAliasedPath
(projectFile);
if
(projectFilePath.
exists
())
{
//
ensure that the project directory and project file are writeable
bool
writeable =
true
;
//
TODO: how to handle on Windows?
#
ifndef
_WIN32
error =
core::system::isFileWriteable
(projectFilePath, &writeable);
if
(error)
return
error;
if
(writeable)
{
error =
core::system::isFileWriteable
(projectFilePath.
getParent
(), &writeable);
if
(error)
return
error;
}
#
endif
pResponse->
setResult
(writeable);
}
else
{
pResponse->
setResult
(
false
);
}
return
Success
();
}
Error
getProjectFilePath
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
std::string projectIdParam;
Error error =
json::readParams
(request.
params
, &projectIdParam);
if
(error)
return
error;
//
project dir from id
core::r_util::ProjectId
projectId
(projectIdParam);
std::string project =
session::projectIdToProject
(
options
().
userScratchPath
(),
FilePath
(
options
().
getOverlayOption
(
kSessionSharedStoragePath
)),
projectId);
//
resolve to project file
FilePath projectPath =
module_context::resolveAliasedPath
(project);
if
(!project.
empty
() && projectPath.
exists
())
{
FilePath projectFile =
r_util::projectFromDirectory
(projectPath);
if
(projectFile.
exists
())
pResponse->
setResult
(
module_context::createAliasedPath
(projectFile));
else
pResponse->
setResult
(
"
"
);
}
else
{
pResponse->
setResult
(
"
"
);
}
return
Success
();
}
bool
findProjectFile
(
const
std::string& path, std::string* pResult)
{
//
Tries to find an EXISTING .Rproj file in the input path. If found, sets the
//
path in pResult and return true. Otherwise returns false.
//
//
If input specifies an existing .Rproj file, e.g. (/home/gary/foo/existing.Rproj),
//
sets that as the result and returns true.
//
//
Otherwise, looks for an .Rproj file in the specified path as follows:
//
//
1. If it finds an .Rproj with same name as folder, returns it:
//
/home/gary/foo -> /home/gary/foo/foo.Rproj
//
//
2. If it finds a single .Rproj in the folder, returns it:
//
//
3. If there are multiple .Rproj's in the folder, returns the most recently modified.
//
//
4. If none are found, returns false.
std::string folder = path;
boost::algorithm::trim
(folder);
FilePath projectFilePath =
module_context::resolveAliasedPath
(folder);
if
(!projectFilePath.
exists
())
{
return
false
;
}
if
(!projectFilePath.
isDirectory
())
{
//
handle being passed full path to an existing .Rproj file
if
(projectFilePath.
getExtensionLowerCase
() ==
"
.rproj
"
)
{
*pResult = folder;
return
true
;
}
else
{
return
false
;
}
}
FilePath resultPath =
r_util::projectFromDirectory
(projectFilePath);
*pResult =
module_context::createAliasedPath
(resultPath);
return
resultPath.
exists
();
}
//
Find an existing .Rproj in a folder, return its full path. Returns empty
//
string if none found.
Error
findProjectInFolder
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
std::string folder;
Error error =
json::readParams
(request.
params
, &folder);
if
(error)
return
error;
std::string result;
findProjectFile
(folder, &result);
pResponse->
setResult
(result);
return
Success
();
}
Error
getNewProjectContext
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
json::Object contextJson;
contextJson[
"
rcpp_available
"
] =
module_context::isPackageInstalled
(
"
Rcpp
"
);
contextJson[
"
packrat_available
"
] =
module_context::packratContext
().
available
&&
module_context::canBuildCpp
();
contextJson[
"
working_directory
"
] =
module_context::createAliasedPath
(
r::session::utils::safeCurrentPath
());
pResponse->
setResult
(contextJson);
return
Success
();
}
Error
initializeProjectFromTemplate
(
const
FilePath& projectFilePath,
const
json::Value& projectTemplateOptions)
{
using
namespace
modules
::projects::templates
;
using
namespace
r
::exec
;
Error error;
json::Object descriptionJson;
json::Object inputsJson;
error =
json::readObject
(projectTemplateOptions.
getObject
(),
"
description
"
, &descriptionJson,
"
inputs
"
, &inputsJson);
if
(error)
return
error;
FilePath projectPath = projectFilePath.
getParent
();
return
r::exec::RFunction
(
"
.rs.initializeProjectFromTemplate
"
)
.
addParam
(
string_utils::utf8ToSystem
(projectFilePath.
getAbsolutePath
()))
.
addParam
(
string_utils::utf8ToSystem
(projectPath.
getAbsolutePath
()))
.
addParam
(descriptionJson)
.
addParam
(inputsJson)
.
call
();
}
Error
createProject
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
//
read params
std::string projectFile;
json::Value newPackageJson;
json::Value newShinyAppJson;
json::Value projectTemplateOptions;
Error error =
json::readParams
(request.
params
,
&projectFile,
&newPackageJson,
&newShinyAppJson,
&projectTemplateOptions);
if
(error)
return
error;
FilePath projectFilePath =
module_context::resolveAliasedPath
(projectFile);
//
Shiny application
if
(!newShinyAppJson.
isNull
())
{
//
error if the shiny app dir already exists
FilePath appDir = projectFilePath.
getParent
();
if
(appDir.
exists
())
return
core::fileExistsError
(
ERROR_LOCATION
);
//
now create it
Error error = appDir.
ensureDirectory
();
if
(error)
return
error;
//
copy app.R into the project
FilePath shinyDir =
session::options
().
rResourcesPath
()
.
completeChildPath
(
"
templates/shiny
"
);
error = shinyDir.
completeChildPath
(
"
app.R
"
).
copy
(appDir.
completeChildPath
(
"
app.R
"
));
if
(error)
LOG_ERROR
(error);
//
add first run actions for the source files
addFirstRunDoc
(projectFilePath,
"
app.R
"
);
std::string existingProjectFilePath;
if
(!
findProjectFile
(projectFilePath.
getParent
().
getAbsolutePath
(), &existingProjectFilePath))
{
//
create the project file
return
r_util::writeProjectFile
(projectFilePath,
ProjectContext::buildDefaults
(),
ProjectContext::defaultConfig
());
}
else
{
pResponse->
setResult
(existingProjectFilePath);
return
Success
();
}
}
//
if we have a custom project template, call that first
if
(!projectTemplateOptions.
isNull
() &&
json::isType<json::Object>(projectTemplateOptions))
{
Error error =
initializeProjectFromTemplate
(projectFilePath, projectTemplateOptions);
if
(error)
return
error;
}
//
default project scaffolding
error = projectFilePath.
getParent
().
ensureDirectory
();
if
(error)
return
error;
std::string existingProjectFilePath;
if
(!
findProjectFile
(projectFilePath.
getParent
().
getAbsolutePath
(), &existingProjectFilePath))
{
//
create the project file
error =
r_util::writeProjectFile
(projectFilePath,
ProjectContext::buildDefaults
(),
ProjectContext::defaultConfig
());
if
(error)
return
error;
return
Success
();
}
else
{
pResponse->
setResult
(existingProjectFilePath);
return
Success
();
}
}
Error
createProjectFile
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
using
namespace
projects
;
std::string projDir;
Error error =
json::readParams
(request.
params
, &projDir);
if
(error)
return
error;
//
Resolve the path and ensure it exists
FilePath projDirPath =
module_context::resolveAliasedPath
(projDir);
FilePath projFilePath;
if
(!projDirPath.
exists
())
{
return
pathNotFoundError
(projDir,
ERROR_LOCATION
);
}
//
Check for an existing project file in the directory
projFilePath =
r_util::projectFromDirectory
(projDirPath);
if
(projFilePath.
isEmpty
())
{
//
We didn't find a project file, so we need to make one. Use the name of the project
//
directory as the filename.
projFilePath = projDirPath.
completePath
(projDirPath.
getFilename
() +
"
.Rproj
"
);
error =
r_util::writeProjectFile
(
projFilePath,
ProjectContext::buildDefaults
(),
ProjectContext::defaultConfig
());
if
(error)
LOG_ERROR
(error);
}
//
Return the name of the discovered and/or created .Rproj filename
pResponse->
setResult
(
module_context::createAliasedPath
(projFilePath));
return
Success
();
}
json::Object
projectConfigJson
(
const
r_util::RProjectConfig& config)
{
json::Object configJson;
configJson[
"
version
"
] = config.
version
;
json::Object rVersionJson;
rVersionJson[
"
number
"
] = config.
rVersion
.
number
;
rVersionJson[
"
arch
"
] = config.
rVersion
.
arch
;
configJson[
"
r_version
"
] = rVersionJson;
configJson[
"
restore_workspace
"
] = config.
restoreWorkspace
;
configJson[
"
save_workspace
"
] = config.
saveWorkspace
;
configJson[
"
always_save_history
"
] = config.
alwaysSaveHistory
;
configJson[
"
enable_code_indexing
"
] = config.
enableCodeIndexing
;
configJson[
"
use_spaces_for_tab
"
] = config.
useSpacesForTab
;
configJson[
"
num_spaces_for_tab
"
] = config.
numSpacesForTab
;
configJson[
"
auto_append_newline
"
] = config.
autoAppendNewline
;
configJson[
"
strip_trailing_whitespace
"
] = config.
stripTrailingWhitespace
;
configJson[
"
line_endings
"
] = config.
lineEndings
;
configJson[
"
default_encoding
"
] = config.
encoding
;
configJson[
"
default_sweave_engine
"
] = config.
defaultSweaveEngine
;
configJson[
"
default_latex_program
"
] = config.
defaultLatexProgram
;
configJson[
"
root_document
"
] = config.
rootDocument
;
configJson[
"
build_type
"
] = config.
buildType
;
configJson[
"
package_use_devtools
"
] = config.
packageUseDevtools
;
configJson[
"
package_path
"
] = config.
packagePath
;
configJson[
"
package_install_args
"
] = config.
packageInstallArgs
;
configJson[
"
package_build_args
"
] = config.
packageBuildArgs
;
configJson[
"
package_build_binary_args
"
] = config.
packageBuildBinaryArgs
;
configJson[
"
package_check_args
"
] = config.
packageCheckArgs
;
configJson[
"
package_roxygenize
"
] = config.
packageRoxygenize
;
configJson[
"
makefile_path
"
] = config.
makefilePath
;
configJson[
"
website_path
"
] = config.
websitePath
;
configJson[
"
custom_script_path
"
] = config.
customScriptPath
;
configJson[
"
tutorial_path
"
] = config.
tutorialPath
;
configJson[
"
quit_child_processes_on_exit
"
] = config.
quitChildProcessesOnExit
;
configJson[
"
disable_execute_rprofile
"
] = config.
disableExecuteRprofile
;
return
configJson;
}
json::Object
projectBuildOptionsJson
()
{
RProjectBuildOptions buildOptions;
Error error = s_projectContext.
readBuildOptions
(&buildOptions);
if
(error)
LOG_ERROR
(error);
json::Object buildOptionsJson;
buildOptionsJson[
"
makefile_args
"
] = buildOptions.
makefileArgs
;
buildOptionsJson[
"
preview_website
"
] = buildOptions.
previewWebsite
;
buildOptionsJson[
"
live_preview_website
"
] = buildOptions.
livePreviewWebsite
;
buildOptionsJson[
"
website_output_format
"
] = buildOptions.
websiteOutputFormat
;
json::Object autoRoxJson;
autoRoxJson[
"
run_on_check
"
] = buildOptions.
autoRoxygenizeForCheck
;
autoRoxJson[
"
run_on_package_builds
"
] =
buildOptions.
autoRoxygenizeForBuildPackage
;
autoRoxJson[
"
run_on_build_and_reload
"
] =
buildOptions.
autoRoxygenizeForBuildAndReload
;
buildOptionsJson[
"
auto_roxygenize_options
"
] = autoRoxJson;
return
buildOptionsJson;
}
json::Object
projectVcsOptionsJson
()
{
RProjectVcsOptions vcsOptions;
Error error = s_projectContext.
readVcsOptions
(&vcsOptions);
if
(error)
LOG_ERROR
(error);
json::Object vcsOptionsJson;
vcsOptionsJson[
"
active_vcs_override
"
] = vcsOptions.
vcsOverride
;
return
vcsOptionsJson;
}
json::Object
projectVcsContextJson
()
{
module_context::VcsContext vcsContext =
module_context::vcsContext
(
s_projectContext.
directory
());
json::Object contextJson;
contextJson[
"
detected_vcs
"
] = vcsContext.
detectedVcs
;
json::Array applicableJson;
for
(
const
std::string& vcs : vcsContext.
applicableVcs
)
{
applicableJson.
push_back
(
json::Value
(vcs));
}
contextJson[
"
applicable_vcs
"
] = applicableJson;
contextJson[
"
svn_repository_root
"
] = vcsContext.
svnRepositoryRoot
;
contextJson[
"
git_remote_origin_url
"
] = vcsContext.
gitRemoteOriginUrl
;
return
contextJson;
}
json::Object
projectBuildContextJson
()
{
using
namespace
module_context
;
json::Object contextJson;
contextJson[
"
roxygen2_installed
"
] =
isMinimumRoxygenInstalled
();
contextJson[
"
devtools_installed
"
] =
isMinimumDevtoolsInstalled
();
contextJson[
"
website_output_formats
"
] =
websiteOutputFormatsJson
();
return
contextJson;
}
void
setProjectConfig
(
const
r_util::RProjectConfig& config)
{
//
set it
s_projectContext.
setConfig
(config);
//
sync underlying R setting
module_context::syncRSaveAction
();
}
void
syncProjectFileChanges
()
{
//
read project file config
bool
providedDefaults;
std::string userErrMsg;
r_util::RProjectConfig config;
Error error =
r_util::readProjectFile
(s_projectContext.
file
(),
ProjectContext::defaultConfig
(),
ProjectContext::buildDefaults
(),
&config,
&providedDefaults,
&userErrMsg);
if
(error)
{
LOG_ERROR
(error);
return
;
}
//
set config
setProjectConfig
(config);
}
Error
readProjectOptions
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
//
read the latest config from disk
syncProjectFileChanges
();
//
get project config json
json::Object configJson =
projectConfigJson
(s_projectContext.
config
());
//
create project options json
json::Object optionsJson;
optionsJson[
"
config
"
] = configJson;
optionsJson[
"
vcs_options
"
] =
projectVcsOptionsJson
();
optionsJson[
"
vcs_context
"
] =
projectVcsContextJson
();
optionsJson[
"
build_options
"
] =
projectBuildOptionsJson
();
optionsJson[
"
build_context
"
] =
projectBuildContextJson
();
optionsJson[
"
packrat_options
"
] =
module_context::packratOptionsAsJson
();
optionsJson[
"
packrat_context
"
] =
module_context::packratContextAsJson
();
optionsJson[
"
renv_options
"
] =
module_context::renvOptionsAsJson
();
optionsJson[
"
renv_context
"
] =
module_context::renvContextAsJson
();
pResponse->
setResult
(optionsJson);
return
Success
();
}
Error
rProjectBuildOptionsFromJson
(
const
json::Object& optionsJson,
RProjectBuildOptions* pOptions)
{
json::Object autoRoxJson;
Error error =
json::readObject
(
optionsJson,
"
makefile_args
"
, &(pOptions->
makefileArgs
),
"
preview_website
"
, &(pOptions->
previewWebsite
),
"
live_preview_website
"
, &(pOptions->
livePreviewWebsite
),
"
website_output_format
"
, &(pOptions->
websiteOutputFormat
),
"
auto_roxygenize_options
"
, &autoRoxJson);
if
(error)
return
error;
return
json::readObject
(
autoRoxJson,
"
run_on_check
"
, &(pOptions->
autoRoxygenizeForCheck
),
"
run_on_package_builds
"
, &(pOptions->
autoRoxygenizeForBuildPackage
),
"
run_on_build_and_reload
"
, &(pOptions->
autoRoxygenizeForBuildAndReload
));
}
Error
rProjectVcsOptionsFromJson
(
const
json::Object& optionsJson,
RProjectVcsOptions* pOptions)
{
return
json::readObject
(
optionsJson,
"
active_vcs_override
"
, &(pOptions->
vcsOverride
));
}
Error
writeProjectOptions
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse*
/*
pResponse
*/
)
{
//
get the project config, vcs options, and build options
json::Object configJson, vcsOptionsJson, buildOptionsJson;
Error error =
json::readObjectParam
(request.
params
,
0
,
"
config
"
, &configJson,
"
vcs_options
"
, &vcsOptionsJson,
"
build_options
"
, &buildOptionsJson);
if
(error)
return
error;
//
read the config
r_util::RProjectConfig config;
error =
json::readObject
(
configJson,
"
version
"
, &(config.
version
),
"
restore_workspace
"
, &(config.
restoreWorkspace
),
"
save_workspace
"
, &(config.
saveWorkspace
),
"
always_save_history
"
, &(config.
alwaysSaveHistory
),
"
enable_code_indexing
"
, &(config.
enableCodeIndexing
),
"
use_spaces_for_tab
"
, &(config.
useSpacesForTab
),
"
num_spaces_for_tab
"
, &(config.
numSpacesForTab
),
"
default_encoding
"
, &(config.
encoding
),
"
default_sweave_engine
"
, &(config.
defaultSweaveEngine
),
"
default_latex_program
"
, &(config.
defaultLatexProgram
),
"
root_document
"
, &(config.
rootDocument
),
"
quit_child_processes_on_exit
"
, &(config.
quitChildProcessesOnExit
));
if
(error)
return
error;
//
get the default_open_files options
//
this is currently not writeable by the UI
//
so we need to make sure we persist the current value
bool
providedDefaults;
std::string userErrMsg;
r_util::RProjectConfig existingConfig;
error =
r_util::readProjectFile
(s_projectContext.
file
(),
ProjectContext::defaultConfig
(),
ProjectContext::buildDefaults
(),
&existingConfig,
&providedDefaults,
&userErrMsg);
if
(error)
{
LOG_ERROR
(error);
}
else
{
if
(!existingConfig.
defaultOpenDocs
.
empty
())
{
config.
defaultOpenDocs
= existingConfig.
defaultOpenDocs
;
}
if
(!existingConfig.
defaultTutorial
.
empty
())
{
config.
defaultTutorial
= existingConfig.
defaultTutorial
;
}
}
error =
json::readObject
(
configJson,
"
auto_append_newline
"
, &(config.
autoAppendNewline
),
"
strip_trailing_whitespace
"
, &(config.
stripTrailingWhitespace
),
"
line_endings
"
, &(config.
lineEndings
));
if
(error)
return
error;
error =
json::readObject
(
configJson,
"
build_type
"
, &(config.
buildType
),
"
package_use_devtools
"
, &(config.
packageUseDevtools
),
"
package_path
"
, &(config.
packagePath
),
"
package_install_args
"
, &(config.
packageInstallArgs
),
"
package_build_args
"
, &(config.
packageBuildArgs
),
"
package_build_binary_args
"
, &(config.
packageBuildBinaryArgs
),
"
package_check_args
"
, &(config.
packageCheckArgs
),
"
package_roxygenize
"
, &(config.
packageRoxygenize
),
"
makefile_path
"
, &(config.
makefilePath
),
"
website_path
"
, &(config.
websitePath
),
"
custom_script_path
"
, &(config.
customScriptPath
),
"
tutorial_path
"
, &(config.
tutorialPath
));
if
(error)
return
error;
error =
json::readObject
(configJson,
"
disable_execute_rprofile
"
, &(config.
disableExecuteRprofile
));
if
(error)
return
error;
//
read the r version info
json::Object rVersionJson;
error =
json::readObject
(configJson,
"
r_version
"
, &rVersionJson);
if
(error)
return
error;
error =
json::readObject
(rVersionJson,
"
number
"
, &(config.
rVersion
.
number
),
"
arch
"
, &(config.
rVersion
.
arch
));
if
(error)
return
error;
//
read the vcs options
RProjectVcsOptions vcsOptions;
error =
rProjectVcsOptionsFromJson
(vcsOptionsJson, &vcsOptions);
if
(error)
return
error;
//
read the build options
RProjectBuildOptions buildOptions;
error =
rProjectBuildOptionsFromJson
(buildOptionsJson, &buildOptions);
if
(error)
return
error;
//
write the config
error =
r_util::writeProjectFile
(s_projectContext.
file
(),
ProjectContext::buildDefaults
(),
config);
if
(error)
return
error;
//
set the config
setProjectConfig
(config);
//
write the vcs options
error = s_projectContext.
writeVcsOptions
(vcsOptions);
if
(error)
LOG_ERROR
(error);
//
write the build options
error = s_projectContext.
writeBuildOptions
(buildOptions);
if
(error)
LOG_ERROR
(error);
return
Success
();
}
Error
writeProjectVcsOptions
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse*
/*
pResponse
*/
)
{
//
read the vcs options
json::Object vcsOptionsJson;
Error error =
json::readParam
(request.
params
,
0
, &vcsOptionsJson);
if
(error)
return
error;
RProjectVcsOptions vcsOptions;
error =
rProjectVcsOptionsFromJson
(vcsOptionsJson, &vcsOptions);
if
(error)
return
error;
//
write the vcs options
error = s_projectContext.
writeVcsOptions
(vcsOptions);
if
(error)
LOG_ERROR
(error);
return
Success
();
}
void
onQuit
()
{
projects::ProjectsSettings
(
options
().
userScratchPath
()).
setLastProjectPath
(s_projectContext.
file
());
}
void
onFilesChanged
(
const
std::vector<core::system::FileChangeEvent>& events)
{
for
(
const
core::system::FileChangeEvent& event : events)
{
//
if the project file changed then sync its changes
if
(event.
fileInfo
().
absolutePath
() ==
s_projectContext.
file
().
getAbsolutePath
())
{
//
update project context
syncProjectFileChanges
();
//
fire event to client
json::Object dataJson;
dataJson[
"
name
"
] =
kUserPrefsProjectLayer
;
dataJson[
"
values
"
] = s_projectContext.
uiPrefs
();
ClientEvent
event
(client_events::
kUserPrefsChanged
, dataJson);
module_context::enqueClientEvent
(event);
break
;
}
}
}
void
onMonitoringDisabled
()
{
//
NOTE: if monitoring is disabled then we can't sync changes to the
//
project file -- we could poll for this however since it is only
//
a convenience to have these synced we don't do this
}
FilePath
resolveProjectSwitch
(
const
std::string& projectPath)
{
FilePath projectFilePath;
//
clear any initial context settings which may be leftover
//
by a re-instantiation of rsession by desktop
session::options
().
clearInitialContextSettings
();
//
check for special "none" value (used for close project)
if
(projectPath ==
kProjectNone
)
{
projectFilePath =
FilePath
();
//
flush the last project path so restarts won't put us back into
//
project context (see case 4015)
projects::ProjectsSettings
(
options
().
userScratchPath
()).
setLastProjectPath
(
FilePath
());
}
else
{
projectFilePath =
module_context::resolveAliasedPath
(projectPath);
}
return
projectFilePath;
}
}
//
anonymous namespace
void
onClientInit
(
const
json::Object& errorToSend)
{
//
enque the error
if
(!errorToSend.
isEmpty
())
{
ClientEvent
event
(client_events::
kOpenProjectError
, errorToSend);
module_context::enqueClientEvent
(event);
}
}
void
startup
(
const
std::string& firstProjectPath)
{
//
register suspend handler
using
namespace
module_context
;
addSuspendHandler
(
SuspendHandler
(
boost::bind
(onSuspend, _2), onResume));
//
determine project file path
FilePath projectFilePath;
//
alias some project context data
projects::ProjectsSettings
projSettings
(
options
().
userScratchPath
());
std::string nextSessionProject = projSettings.
nextSessionProject
();
if
(!firstProjectPath.
empty
())
nextSessionProject = firstProjectPath;
std::string switchToProject = projSettings.
switchToProjectPath
();
FilePath lastProjectPath = projSettings.
lastProjectPath
();
//
check for explicit project none scope
if
(
session::options
().
sessionScope
().
isProjectNone
() ||
session::options
().
initialProjectPath
().
getAbsolutePath
() ==
kProjectNone
)
{
projectFilePath =
resolveProjectSwitch
(
kProjectNone
);
}
//
check for explicit request for a project (file association or url based)
else
if
(!
session::options
().
initialProjectPath
().
isEmpty
())
{
projectFilePath =
session::options
().
initialProjectPath
();
}
//
see if there is a project path hard-wired for the next session
//
(this would be used for resuming of a suspended session)
else
if
(!nextSessionProject.
empty
())
{
//
reset next session project path so its a one shot deal
projSettings.
setNextSessionProject
(
"
"
);
projectFilePath =
resolveProjectSwitch
(nextSessionProject);
}
//
see if this is a project switch
else
if
(!switchToProject.
empty
())
{
projectFilePath =
resolveProjectSwitch
(switchToProject);
}
//
check for other working dir override (implies a launch of a file
//
but not of a project). this code path is here to prevent
//
the next code path from executing
else
if
(!
session::options
().
initialWorkingDirOverride
().
isEmpty
())
{
projectFilePath =
FilePath
();
}
//
check for restore last project
else
if
(
prefs::userPrefs
().
restoreLastProject
() &&
!lastProjectPath.
isEmpty
())
{
//
get last project path
projectFilePath = lastProjectPath;
//
reset it to empty so that we only attempt to load the "lastProject"
//
a single time (this will be reset to the path below after we
//
clear the s_projectContext.initialize)
projSettings.
setLastProjectPath
(
FilePath
());
}
//
else no active project for this session
else
{
projectFilePath =
FilePath
();
}
//
if we have a project file path then try to initialize the
//
project context (show a warning to the user if we can't)
if
(!projectFilePath.
isEmpty
())
{
std::string userErrMsg;
Error error = s_projectContext.
startup
(projectFilePath, &userErrMsg);
if
(error)
{
//
log the error
error.
addProperty
(
"
project-file
"
, projectFilePath.
getAbsolutePath
());
error.
addProperty
(
"
user-msg
"
, userErrMsg);
LOG_ERROR
(error);
json::Object openProjectError;
openProjectError[
"
project
"
] =
module_context::createAliasedPath
(
projectFilePath);
openProjectError[
"
message
"
] = userErrMsg;
module_context::events
().
onClientInit
.
connect
(
boost::bind
(onClientInit, openProjectError));
}
}
//
add default open docs if specified in the project
//
and the project has never been opened before
std::string defaultOpenDocs =
projectContext
().
config
().
defaultOpenDocs
;
if
(!defaultOpenDocs.
empty
() &&
projects::projectContext
().
isNewProject
())
{
std::vector<std::string> docs;
boost::algorithm::split
(docs, defaultOpenDocs,
boost::is_any_of
(
"
:
"
));
for
(std::string& doc : docs)
{
boost::algorithm::trim
(doc);
FilePath docPath =
projectContext
().
directory
().
completePath
(doc);
if
(docPath.
exists
())
{
addFirstRunDoc
(projectFilePath, doc);
}
}
}
}
SEXP
rs_writeProjectFile
(
SEXP
projectFilePathSEXP)
{
std::string absolutePath =
r::sexp::asString
(projectFilePathSEXP);
FilePath
projectFilePath
(absolutePath);
Error error =
r_util::writeProjectFile
(
projectFilePath,
ProjectContext::buildDefaults
(),
ProjectContext::defaultConfig
());
r::sexp::Protect protect;
return
error ?
r::sexp::create
(
false
, &protect) :
r::sexp::create
(
true
, &protect);
}
SEXP
rs_addFirstRunDoc
(
SEXP
projectFileAbsolutePathSEXP,
SEXP
docRelativePathsSEXP)
{
std::string projectFileAbsolutePath =
r::sexp::asString
(projectFileAbsolutePathSEXP);
const
FilePath
projectFilePath
(projectFileAbsolutePath);
std::vector<std::string> docRelativePaths;
bool
success =
r::sexp::fillVectorString
(docRelativePathsSEXP, &docRelativePaths);
if
(!success)
return
R_NilValue;
for
(
const
std::string& path : docRelativePaths)
{
addFirstRunDoc
(projectFilePath, path);
}
return
R_NilValue;
}
SEXP
rs_requestOpenProject
(
SEXP
projectFileSEXP,
SEXP
newSessionSEXP)
{
std::string projectFile =
r::sexp::asString
(projectFileSEXP);
bool
newSession =
r::sexp::asLogical
(newSessionSEXP);
//
opening projects in a new session is only supported in desktop, RSP
if
(newSession &&
options
().
programMode
() ==
kSessionProgramModeServer
&&
!
options
().
multiSession
())
{
newSession =
false
;
}
json::Object data;
data[
"
project_file
"
] = projectFile;
data[
"
new_session
"
] = newSession;
ClientEvent
event
(client_events::
kRequestOpenProject
, data);
module_context::enqueClientEvent
(event);
return
R_NilValue;
}
Error
initialize
()
{
//
register R methods
RS_REGISTER_CALL_METHOD
(rs_writeProjectFile,
1
);
RS_REGISTER_CALL_METHOD
(rs_addFirstRunDoc,
2
);
RS_REGISTER_CALL_METHOD
(rs_requestOpenProject,
2
);
//
call project-context initialize
Error error = s_projectContext.
initialize
();
if
(error)
return
error;
//
subscribe to file_monitor for project file changes
projects::FileMonitorCallbacks cb;
cb.
onFilesChanged
= onFilesChanged;
cb.
onMonitoringDisabled
= onMonitoringDisabled;
s_projectContext.
subscribeToFileMonitor
(
"
"
, cb);
//
subscribe to quit for setting last project path
module_context::events
().
onQuit
.
connect
(onQuit);
//
reset switch to project path so it's a one shot deal; we only do this after successful init so
//
that we can retry a project switch if it doesn't get off the ground the first time
projects::ProjectsSettings
projSettings
(
options
().
userScratchPath
());
if
(!projSettings.
switchToProjectPath
().
empty
())
{
projSettings.
setSwitchToProjectPath
(
"
"
);
}
using
boost::bind;
using
namespace
module_context
;
ExecBlock initBlock ;
initBlock.
addFunctions
()
(
bind
(registerRpcMethod,
"
validate_project_path
"
, validateProjectPath))
(
bind
(registerRpcMethod,
"
get_new_project_context
"
, getNewProjectContext))
(
bind
(registerRpcMethod,
"
get_project_file_path
"
, getProjectFilePath))
(
bind
(registerRpcMethod,
"
create_project
"
, createProject))
(
bind
(registerRpcMethod,
"
create_project_file
"
, createProjectFile))
(
bind
(registerRpcMethod,
"
read_project_options
"
, readProjectOptions))
(
bind
(registerRpcMethod,
"
write_project_options
"
, writeProjectOptions))
(
bind
(registerRpcMethod,
"
write_project_vcs_options
"
, writeProjectVcsOptions))
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL