-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path_updateModules.R
More file actions
2094 lines (1740 loc) · 69 KB
/
_updateModules.R
File metadata and controls
2094 lines (1740 loc) · 69 KB
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
#!/usr/bin/env Rscript
# Enhanced Update Modules Script for ClinicoPathJamoviModule
#
# This script provides a comprehensive solution for distributing production-ready functions
# from the main ClinicoPathJamoviModule to specialized submodules with enhanced:
# - Error handling and validation
# - Security measures
# - Performance optimization
# - Testing integration
# - Backup and rollback capabilities
# - Configuration management
#
# Usage: Rscript _updateModules.R [config_file]
#
# Author: Enhanced version with enterprise-grade features
# Requires: R >= 4.0.0, yaml, future, digest packages
# run /Applications/jamovi.app/Contents/MacOS/jamovi
# Command line argument handling
args <- commandArgs(trailingOnly = TRUE)
config_file <- if (length(args) > 0) args[1] else "_updateModules_config.yaml"
cat("🚀 Starting Enhanced Module Update Process\n")
cat("Configuration file:", config_file, "\n")
# Get script directory and set working directory
script_dir <- tryCatch({
# Try to get script directory from command line execution
dirname(normalizePath(sys.frame(1)$ofile))
}, error = function(e) {
# Fallback for interactive execution
getwd()
})
setwd(script_dir)
# Source utility functions with enhanced validation
utility_file <- "_updateModules_utils.R"
if (!file.exists(utility_file)) {
warning("⚠️ _updateModules_utils.R not found in current directory: ", getwd())
cat("Looking for _updateModules_utils.R in script directory...\n")
# Try to find in script directory
script_utility <- file.path(script_dir, utility_file)
if (file.exists(script_utility)) {
cat("✅ Found _updateModules_utils.R in script directory\n")
utility_file <- script_utility
} else {
stop("❌ _updateModules_utils.R not found. Please ensure all required files are present.")
}
}
# Source with error handling
tryCatch({
source(utility_file)
cat("✅ Successfully loaded module utilities\n")
}, error = function(e) {
stop("❌ Failed to load _updateModules_utils.R: ", e$message)
})
# Load and validate configuration
cat("\n📋 Loading configuration...\n")
config <- load_config(config_file)
config <- validate_config(config)
# Extract configuration values with backward compatibility
global <- config$global
modes <- config$modes
modules_config <- config$modules
required_packages <- config$required_packages %||% c("xfun", "fs", "jmvtools", "devtools", "purrr", "yaml", "digest")
# Handle simplified top-level configuration (new format) or nested format (old format)
new_version <- config$new_version %||% global$new_version
new_date <- config$new_date %||% global$new_date
main_repo_dir <- global$base_repo_dir %||% "/Users/serdarbalci/Documents/GitHub/ClinicoPathJamoviModule"
# Operation modes (simplified format first, then nested format)
quick <- config$quick %||% modes$quick %||% FALSE
check <- modes$check %||% FALSE
extended <- modes$extended %||% TRUE
webpage <- modes$webpage %||% FALSE
commit_modules <- modes$commit_modules %||% FALSE
WIP <- modes$WIP %||% FALSE
TEST <- config$TEST %||% modes$TEST %||% FALSE
# File copying control modes
copy_vignettes <- modes$copy_vignettes %||% TRUE
copy_data_files <- modes$copy_data_files %||% TRUE
copy_test_files <- modes$copy_test_files %||% TRUE
copy_r_files <- modes$copy_r_files %||% TRUE
# NAMESPACE-DESCRIPTION synchronization modes
sync_namespace_description <- modes$sync_namespace_description %||% FALSE
namespace_sync_dry_run <- modes$namespace_sync_dry_run %||% FALSE
# Module-specific flags (using simplified top-level toggles)
meddecide_module <- config$meddecide %||% modes$meddecide %||% FALSE
jjstatsplot_module <- config$jjstatsplot %||% modes$jjstatsplot %||% FALSE
jsurvival_module <- config$jsurvival %||% modes$jsurvival %||% FALSE
ClinicoPathDescriptives_module <- config$ClinicoPathDescriptives %||% modes$ClinicoPathDescriptives %||% FALSE
OncoPath_module <- config$OncoPath %||% modes$OncoPath %||% FALSE
# Hardcoded module configurations (simplified for maintainability)
module_patterns <- list(
meddecide = list(
pattern = "menuGroup: meddecide$",
pattern_wip = "menuGroup: meddecide",
data_files = c("histopathology.rda", "roc_analysis_test_data.RData", "cancer_biomarker_data.csv",
"cardiac_troponin_data.csv", "sepsis_biomarker_data.csv", "thyroid_function_data.csv",
"bayesdca_test_data.rda", "breast_cancer_data.rda", "breast_diagnostic_styles.rda",
"lymphoma_diagnostic_styles.rda", "dca_test_data.csv", "thyroid_function_data.rda")
),
jjstatsplot = list(
pattern = "menuGroup: JJStatsPlot$",
pattern_wip = "menuGroup: JJStatsPlot",
data_files = c("histopathology.rda", "groupsummary_financial_data.rda", "groupsummary_simple.rda",
"categorical_quality_data.rda", paste0("hullplot_", c("clinical", "customer", "experimental", "quality", "survey"), "_data.rda"),
paste0("jggstats_", c("clinical", "educational", "experimental", "financial", "marketing",
"medical", "pharmaceutical", "psychological", "quality", "survey"), "_data.rda"))
),
jsurvival = list(
pattern = "menuGroup: Survival$",
pattern_wip = "menuGroup: Survival",
data_files = c("histopathology.rda", "melanoma.rda", "data_longitudinal.rda",
paste0("stagemigration_", c("lung_cancer", "breast_cancer", "colorectal_cancer", "small_sample",
"large_performance", "problematic", "combined", "summary_stats"), ".rda"))
),
ClinicoPathDescriptives = list(
pattern = "menuGroup: Exploration$",
pattern_wip = "menuGroup: Exploration",
data_files = c("histopathology.rda")
),
OncoPath = list(
pattern = "menuGroup: OncoPath$",
pattern_wip = "menuGroup: OncoPath",
data_files = c("histopathology.rda")
)
)
# Set enabled status for modules based on toggles and add hardcoded configurations
if (meddecide_module) {
modules_config$meddecide$enabled <- TRUE
modules_config$meddecide <- c(modules_config$meddecide, module_patterns$meddecide)
cat("🔧 meddecide enabled\n")
}
if (jjstatsplot_module) {
modules_config$jjstatsplot$enabled <- TRUE
modules_config$jjstatsplot <- c(modules_config$jjstatsplot, module_patterns$jjstatsplot)
cat("🔧 jjstatsplot enabled\n")
}
if (jsurvival_module) {
modules_config$jsurvival$enabled <- TRUE
modules_config$jsurvival <- c(modules_config$jsurvival, module_patterns$jsurvival)
cat("🔧 jsurvival enabled\n")
}
if (ClinicoPathDescriptives_module) {
modules_config$ClinicoPathDescriptives$enabled <- TRUE
modules_config$ClinicoPathDescriptives <- c(modules_config$ClinicoPathDescriptives, module_patterns$ClinicoPathDescriptives)
cat("🔧 ClinicoPathDescriptives enabled\n")
}
if (OncoPath_module) {
modules_config$OncoPath$enabled <- TRUE
modules_config$OncoPath <- c(modules_config$OncoPath, module_patterns$OncoPath)
cat("🔧 OncoPath enabled\n")
}
# Apply WIP mode overrides
if (WIP) {
quick <- FALSE
check <- FALSE
extended <- TRUE
webpage <- FALSE
commit_modules <- FALSE
cat("🔧 WIP mode enabled - using sandbox environment\n")
}
# Apply TEST mode overrides
if (TEST) {
quick <- FALSE
check <- FALSE
extended <- TRUE
webpage <- FALSE
commit_modules <- FALSE
cat("🧪 TEST mode enabled - creating standalone JamoviTest module\n")
# Enable JamoviTest module when TEST mode is active
modules_config$JamoviTest$enabled <- TRUE
}
# Load required packages with validation
cat("\n📦 Loading required packages...\n")
load_required_packages(required_packages)
# Setup parallel processing if enabled
parallel_enabled <- setup_parallel_processing(
enabled = config$performance$parallel_processing %||% FALSE,
max_workers = config$performance$max_workers %||% 4
)
# Validate main repository directory
main_repo_dir <- validate_path(main_repo_dir, dirname(main_repo_dir), "main repository")
setwd(main_repo_dir)
# Clean old backups if backup is enabled
if (config$backup$enabled %||% TRUE) {
cat("\n🧹 Cleaning old backups...\n")
clean_old_backups(
backup_base_dir = config$backup$backup_location %||% "backups",
retention_days = config$backup$retention_days %||% 30
)
}
# Quick mode handling
if (quick) {
cat("⚡ Quick mode enabled - performing fast installation\n")
with_error_handling({
devtools::install(quick = TRUE, reload = TRUE, quiet = FALSE,
upgrade = FALSE, build_vignettes = FALSE, keep_source = TRUE)
}, "quick installation")
cat("✅ Quick mode completed successfully\n")
quit("no", status = 0)
}
# Extract module directories from configuration with validation
module_dirs <- list()
module_validation_failed <- FALSE
cat("\n📁 Validating module directories...\n")
for (module_name in names(modules_config)) {
# Check if module is enabled via top-level toggles or old enabled property
module_enabled <- FALSE
if (module_name == "meddecide" && meddecide_module) module_enabled <- TRUE
if (module_name == "jjstatsplot" && jjstatsplot_module) module_enabled <- TRUE
if (module_name == "jsurvival" && jsurvival_module) module_enabled <- TRUE
if (module_name == "ClinicoPathDescriptives" && ClinicoPathDescriptives_module) module_enabled <- TRUE
if (module_name == "OncoPath" && OncoPath_module) module_enabled <- TRUE
if (module_name == "JamoviTest" && (TEST || (!is.null(modules_config[[module_name]]$enabled) && modules_config[[module_name]]$enabled))) module_enabled <- TRUE
if (module_enabled) {
module_dir <- modules_config[[module_name]]$directory
# Validate directory exists
if (!dir.exists(module_dir)) {
warning("⚠️ Module directory does not exist: ", module_dir, " for ", module_name)
module_validation_failed <- TRUE
next
}
module_dirs[[module_name]] <- module_dir
cat(" ✅", module_name, ":", module_dir, "\n")
} else {
cat(" ⏭️", module_name, ": disabled\n")
}
}
if (module_validation_failed && !WIP && !TEST) {
stop("❌ Some module directories are invalid. Check configuration or enable WIP/TEST mode.")
}
# Handle JamoviTest module creation in TEST mode
if (TEST && modules_config$JamoviTest$enabled) {
test_dir <- modules_config$JamoviTest$directory
cat("\n🧪 Setting up JamoviTest module...\n")
# Create JamoviTest directory if it doesn't exist
if (!dir.exists(test_dir)) {
cat(" 📁 Creating new JamoviTest module:", test_dir, "\n")
with_error_handling({
jmvtools::create(path = test_dir)
}, "creating JamoviTest module", continue_on_error = FALSE)
cat(" ✅ JamoviTest module created successfully\n")
} else {
cat(" ♻️ Using existing JamoviTest module:", test_dir, "\n")
# Remove NAMESPACE file for clean build
namespace_file <- file.path(test_dir, "NAMESPACE")
if (file.exists(namespace_file)) {
cat(" 🧹 Removing NAMESPACE file for clean build...\n")
file.remove(namespace_file)
}
# Remove any .jmo files
jmo_files <- list.files(test_dir, pattern = "\\.jmo$", full.names = TRUE)
if (length(jmo_files) > 0) {
cat(" 🧹 Removing", length(jmo_files), ".jmo file(s)...\n")
file.remove(jmo_files)
}
# Clean only the R and jamovi directories to refresh functions
r_dir <- file.path(test_dir, "R")
jamovi_dir <- file.path(test_dir, "jamovi")
if (dir.exists(r_dir)) {
cat(" 🧹 Cleaning R directory for fresh functions...\n")
# Remove all .b.R files but keep other R files
r_files <- list.files(r_dir, pattern = "\\.b\\.R$", full.names = TRUE)
if (length(r_files) > 0) {
file.remove(r_files)
}
}
if (dir.exists(jamovi_dir)) {
cat(" 🧹 Cleaning jamovi directory for fresh function definitions...\n")
# Remove ALL yaml files including 0000.yaml for clean rebuild
yaml_files <- list.files(jamovi_dir, pattern = "\\.yaml$", full.names = TRUE)
if (length(yaml_files) > 0) {
file.remove(yaml_files)
}
}
}
# Add to module_dirs
module_dirs$JamoviTest <- test_dir
}
# Legacy variable assignments for backward compatibility (using config values)
jjstatsplot_dir <- module_dirs$jjstatsplot %||% modules_config$jjstatsplot$directory
meddecide_dir <- module_dirs$meddecide %||% modules_config$meddecide$directory
jsurvival_dir <- module_dirs$jsurvival %||% modules_config$jsurvival$directory
ClinicoPathDescriptives_dir <- module_dirs$ClinicoPathDescriptives %||% modules_config$ClinicoPathDescriptives$directory
OncoPath_dir <- module_dirs$OncoPath %||% modules_config$OncoPath$directory
# Enhanced WIP mode with backup and validation (TEST mode uses standalone JamoviTest only)
if (WIP) {
mode_name <- "WIP (Work-In-Progress)"
mode_suffix <- "-WIP"
cat("\n🔧 Setting up", mode_name, "environment...\n")
wip_setup_success <- TRUE
for (module_name in names(module_dirs)) {
original_dir <- module_dirs[[module_name]]
wip_dir <- paste0(original_dir, mode_suffix)
# Validate original directory exists
if (!dir.exists(original_dir)) {
warning("⚠️ Original module directory does not exist: ", original_dir)
wip_setup_success <- FALSE
next
}
cat("🔧 Setting up", mode_name, "environment for", module_name, "\n")
# Delete existing directory if it exists
if (dir.exists(wip_dir)) {
cat(" 🗑️ Removing existing", mode_name, "directory:", wip_dir, "\n")
with_error_handling({
fs::dir_delete(wip_dir)
}, paste("removing existing", mode_name, "directory for", module_name), continue_on_error = TRUE)
}
# Create backup of original directory
backup_path <- create_backup(original_dir, "wip_backups")
if (is.null(backup_path)) {
warning("⚠️ Failed to create backup for ", module_name, ", skipping", mode_name, "setup")
wip_setup_success <- FALSE
next
}
# Copy original to directory
cat(" 📁 Creating", mode_name, "copy:", wip_dir, "\n")
copy_result <- with_error_handling({
fs::dir_copy(path = original_dir, new_path = wip_dir, overwrite = TRUE)
}, paste("creating", mode_name, "directory for", module_name), continue_on_error = TRUE)
if (!copy_result$success) {
wip_setup_success <- FALSE
next
}
# Update module directory reference
module_dirs[[module_name]] <- wip_dir
cat(" ✅", mode_name, "environment ready for", module_name, "\n")
}
# Update legacy variables for WIP
jjstatsplot_dir <- module_dirs$jjstatsplot %||% jjstatsplot_dir
meddecide_dir <- module_dirs$meddecide %||% meddecide_dir
jsurvival_dir <- module_dirs$jsurvival %||% jsurvival_dir
ClinicoPathDescriptives_dir <- module_dirs$ClinicoPathDescriptives %||% ClinicoPathDescriptives_dir
OncoPath_dir <- module_dirs$OncoPath %||% OncoPath_dir
if (!wip_setup_success) {
stop("❌", mode_name, "setup failed for one or more modules. Check warnings above.")
}
cat("✅", mode_name, "environment setup completed successfully\n")
}
# Enhanced function to update DESCRIPTION files with validation
update_description_files <- function(paths, version, date) {
cat("\n📝 Updating DESCRIPTION files...\n")
version_pattern <- "Version:.*$"
date_pattern <- "Date:.*$"
version_replacement <- paste0("Version: ", version)
date_replacement <- paste0("Date: ", date)
updated_count <- 0
failed_count <- 0
for (path in paths) {
if (!file.exists(path)) {
warning("⚠️ DESCRIPTION file not found: ", path)
failed_count <- failed_count + 1
next
}
# Perform updates with error handling
update_result <- with_error_handling({
xfun::gsub_files(files = path,
pattern = version_pattern,
replacement = version_replacement)
xfun::gsub_files(files = path,
pattern = date_pattern,
replacement = date_replacement)
}, paste("updating DESCRIPTION file", path), continue_on_error = TRUE)
if (update_result$success) {
updated_count <- updated_count + 1
cat(" ✅ Updated:", basename(dirname(path)), "\n")
} else {
failed_count <- failed_count + 1
}
}
cat("📝 DESCRIPTION update summary:", updated_count, "updated,", failed_count, "failed\n")
if (failed_count > 0 && updated_count == 0) {
stop("❌ All DESCRIPTION file updates failed")
}
}
# Enhanced function to update YAML files with validation
update_yaml_0000_files <- function(paths, version, date) {
cat("\n📝 Updating 0000.yaml files...\n")
version_pattern <- "version:.*$"
date_pattern <- "date:.*$"
version_replacement <- paste0("version: ", version)
date_replacement <- paste0("date: '", date, "'")
updated_count <- 0
failed_count <- 0
for (path in paths) {
if (!file.exists(path)) {
warning("⚠️ YAML file not found: ", path)
failed_count <- failed_count + 1
next
}
update_result <- with_error_handling({
xfun::gsub_files(files = path,
pattern = version_pattern,
replacement = version_replacement)
xfun::gsub_files(files = path,
pattern = date_pattern,
replacement = date_replacement)
}, paste("updating 0000.yaml file", path), continue_on_error = TRUE)
if (update_result$success) {
updated_count <- updated_count + 1
cat(" ✅ Updated:", basename(dirname(path)), "\n")
} else {
failed_count <- failed_count + 1
}
}
cat("📝 YAML 0000 update summary:", updated_count, "updated,", failed_count, "failed\n")
}
# Enhanced function to update analysis YAML files
update_yaml_a_files <- function(paths, version) {
cat("\n📝 Updating analysis .a.yaml files...\n")
version_pattern <- "version:.*$"
valid_version <- paste(strsplit(version, "\\.")[[1]][1:3], collapse = ".")
version_replacement <- paste0("version: '", valid_version, "'")
updated_count <- 0
failed_count <- 0
for (path in paths) {
if (!file.exists(path)) {
failed_count <- failed_count + 1
next
}
update_result <- with_error_handling({
xfun::gsub_files(files = path,
pattern = version_pattern,
replacement = version_replacement)
}, paste("updating analysis YAML file", basename(path)), continue_on_error = TRUE)
if (update_result$success) {
updated_count <- updated_count + 1
} else {
failed_count <- failed_count + 1
}
}
cat("📝 Analysis YAML update summary:", updated_count, "updated,", failed_count, "failed\n")
}
# Enhanced function to copy module files with comprehensive validation ----
copy_module_files <- function(module_names, source_dir, dest_dir, file_extensions) {
if (length(module_names) == 0) {
cat(" ⏭️ No modules to copy\n")
return(list(copied = 0, skipped = 0, failed = 0))
}
if (!dir.exists(source_dir)) {
warning("⚠️ Source directory does not exist: ", source_dir)
return(list(copied = 0, skipped = 0, failed = length(module_names) * length(file_extensions)))
}
if (!dir.exists(dest_dir)) {
cat(" 📁 Creating destination directory: ", dest_dir, "\n")
dir.create(dest_dir, recursive = TRUE)
}
copied_count <- 0
failed_count <- 0
for (module_name in module_names) {
for (ext in file_extensions) {
source_path <- file.path(source_dir, paste0(module_name, ext))
dest_path <- file.path(dest_dir, paste0(module_name, ext))
if (!file.exists(source_path)) {
warning("⚠️ Source file not found: ", source_path)
failed_count <- failed_count + 1
next
}
tryCatch({
fs::file_copy(path = source_path, new_path = dest_path, overwrite = TRUE)
cat(" ✅ Copied: ", paste0(module_name, ext), "\n")
copied_count <- copied_count + 1
}, error = function(e) {
warning("⚠️ Failed to copy ", source_path, ": ", e$message)
failed_count <- failed_count + 1
})
}
}
return(list(copied = copied_count, skipped = 0, failed = failed_count))
}
# Enhanced function to copy module files with validation and performance
copy_module_files_enhanced <- function(module_names, source_dir, dest_dir, file_extensions, module_type = "unknown") {
cat("\n📁 Copying", module_type, "module files...\n")
if (length(module_names) == 0) {
cat(" ⏭️ No", module_type, "modules to process\n")
return(list(copied = 0, skipped = 0, failed = 0))
}
if (!dir.exists(source_dir)) {
warning("⚠️ Source directory does not exist: ", source_dir)
return(list(copied = 0, skipped = 0, failed = length(module_names) * length(file_extensions)))
}
if (!dir.exists(dest_dir)) {
cat(" 📁 Creating destination directory:", dest_dir, "\n")
tryCatch({
dir.create(dest_dir, recursive = TRUE)
}, error = function(e) {
stop("❌ Failed to create directory ", dest_dir, ": ", e$message)
})
}
copied_count <- 0
skipped_count <- 0
failed_count <- 0
use_incremental <- config$performance$incremental_updates %||% TRUE
verify_integrity <- config$security$verify_checksums %||% TRUE
for (module_name in module_names) {
for (ext in file_extensions) {
source_path <- file.path(source_dir, paste0(module_name, ext))
dest_path <- file.path(dest_dir, paste0(module_name, ext))
if (!file.exists(source_path)) {
warning("⚠️ Source file not found: ", source_path)
failed_count <- failed_count + 1
next
}
# Check if incremental update is possible
if (use_incremental && verify_file_integrity(source_path, dest_path)) {
skipped_count <- skipped_count + 1
next
}
# Copy file with validation
copy_result <- with_error_handling({
# Validate file size before copying
if (!validate_file_size(source_path, config$security$max_file_size_mb %||% 100)) {
stop("File too large: ", source_path)
}
fs::file_copy(path = source_path, new_path = dest_path, overwrite = TRUE)
# Verify integrity after copy if enabled
if (verify_integrity && !verify_file_integrity(source_path, dest_path)) {
stop("File integrity check failed after copy")
}
}, paste("copying", basename(source_path)), continue_on_error = TRUE)
if (copy_result$success) {
copied_count <- copied_count + 1
cat(" ✅ Copied:", paste0(module_name, ext), "\n")
} else {
failed_count <- failed_count + 1
}
}
}
cat("📁", module_type, "module file summary:", copied_count, "copied,",
skipped_count, "skipped,", failed_count, "failed\n")
if (failed_count > 0 && copied_count == 0) {
warning("⚠️ All ", module_type, " module files failed to copy")
}
return(list(copied = copied_count, skipped = skipped_count, failed = failed_count))
}
# Enhanced copy function for JavaScript and HTML files - detects files for any function
copy_jamovi_assets <- function(module_names, source_base_dir, dest_base_dir, module_type = "unknown") {
cat("\n🎯 Copying", module_type, "JavaScript and HTML assets...\n")
if (length(module_names) == 0) {
cat(" ⏭️ No", module_type, "modules to process\n")
return(list(copied = 0, failed = 0))
}
copied_count <- 0
failed_count <- 0
# JavaScript files from jamovi/js/
js_source_dir <- file.path(source_base_dir, "jamovi", "js")
js_dest_dir <- file.path(dest_base_dir, "jamovi", "js")
if (dir.exists(js_source_dir)) {
if (!dir.exists(js_dest_dir)) {
dir.create(js_dest_dir, recursive = TRUE)
}
# Enhanced detection: find ALL JavaScript files related to our modules
copied_files <- character(0) # Track already copied files to avoid duplicates
for (module_name in module_names) {
# Multiple patterns to catch various JavaScript file naming conventions:
# 1. Direct match: moduleName.js, moduleName.events.js
# 2. Prefix match: moduleName*.js (catches events, helpers, etc.)
# 3. Exact event files: moduleName.events.js, moduleName.handlers.js, etc.
js_patterns <- c(
paste0("^", module_name, "\\.js$"), # moduleName.js
paste0("^", module_name, "\\.events\\.js$"), # moduleName.events.js
paste0("^", module_name, "\\.handlers\\.js$"), # moduleName.handlers.js
paste0("^", module_name, "\\.helpers\\.js$"), # moduleName.helpers.js
paste0("^", module_name, ".*\\.js$") # moduleName*.js (catch-all)
)
for (pattern in js_patterns) {
js_files <- list.files(js_source_dir, pattern = pattern, full.names = TRUE)
for (js_file in js_files) {
dest_file <- file.path(js_dest_dir, basename(js_file))
# Skip if we already copied this file (avoid duplicates from multiple patterns)
if (basename(js_file) %in% copied_files) {
next
}
copy_result <- tryCatch({
file.copy(js_file, dest_file, overwrite = TRUE)
cat(" ✅ Copied JS:", basename(js_file), "for", module_name, "\n")
copied_files <- c(copied_files, basename(js_file))
copied_count <- copied_count + 1
TRUE
}, error = function(e) {
warning("⚠️ Failed to copy JS file ", basename(js_file), ": ", e$message)
failed_count <- failed_count + 1
FALSE
})
}
}
}
} else {
cat(" ℹ️ No JavaScript source directory found:", js_source_dir, "\n")
}
# HTML files from jamovi/html/ (less common but included for completeness)
html_source_dir <- file.path(source_base_dir, "jamovi", "html")
html_dest_dir <- file.path(dest_base_dir, "jamovi", "html")
if (dir.exists(html_source_dir)) {
if (!dir.exists(html_dest_dir)) {
dir.create(html_dest_dir, recursive = TRUE)
}
# Enhanced detection for HTML files
copied_html_files <- character(0) # Track already copied HTML files
for (module_name in module_names) {
html_patterns <- c(
paste0("^", module_name, "\\.html$"),
paste0("^", module_name, ".*\\.html$")
)
for (pattern in html_patterns) {
html_files <- list.files(html_source_dir, pattern = pattern, full.names = TRUE)
for (html_file in html_files) {
dest_file <- file.path(html_dest_dir, basename(html_file))
# Skip if we already copied this file (avoid duplicates from multiple patterns)
if (basename(html_file) %in% copied_html_files) {
next
}
copy_result <- tryCatch({
file.copy(html_file, dest_file, overwrite = TRUE)
cat(" ✅ Copied HTML:", basename(html_file), "for", module_name, "\n")
copied_html_files <- c(copied_html_files, basename(html_file))
copied_count <- copied_count + 1
TRUE
}, error = function(e) {
warning("⚠️ Failed to copy HTML file ", basename(html_file), ": ", e$message)
failed_count <- failed_count + 1
FALSE
})
}
}
}
} else {
cat(" ℹ️ No HTML source directory found:", html_source_dir, "\n")
}
if (copied_count > 0) {
cat(" ✅", copied_count, "asset files copied successfully\n")
} else if (failed_count == 0) {
cat(" ℹ️ No JavaScript/HTML assets found for", module_type, "modules\n")
}
return(list(copied = copied_count, failed = failed_count))
}
# Copy 00refs.yaml to module jamovi folder
copy_refs_yaml <- function(dest_base_dir, module_type = "unknown") {
cat("\n📚 Copying references file (00refs.yaml) to", module_type, "module...\n")
source_file <- file.path(main_repo_dir, "jamovi", "00refs.yaml")
dest_dir <- file.path(dest_base_dir, "jamovi")
dest_file <- file.path(dest_dir, "00refs.yaml")
if (!file.exists(source_file)) {
warning("⚠️ Source 00refs.yaml not found: ", source_file)
return(list(copied = 0, failed = 1))
}
if (!dir.exists(dest_dir)) {
cat(" 📁 Creating destination jamovi directory: ", dest_dir, "\n")
dir.create(dest_dir, recursive = TRUE)
}
tryCatch({
file.copy(source_file, dest_file, overwrite = TRUE)
cat(" ✅ Copied 00refs.yaml to", module_type, "module\n")
return(list(copied = 1, failed = 0))
}, error = function(e) {
warning("⚠️ Failed to copy 00refs.yaml: ", e$message)
return(list(copied = 0, failed = 1))
})
}
# Enhanced Git commit function with comprehensive validation
commit_repo_enhanced <- function(repo_dir, commit_message, validate_repo = TRUE, dry_run = FALSE) {
if (!dir.exists(repo_dir)) {
warning("⚠️ Repository directory does not exist: ", repo_dir)
return(FALSE)
}
old_wd <- getwd()
on.exit(setwd(old_wd))
tryCatch({
setwd(repo_dir)
repo_name <- basename(repo_dir)
# Validate it's a git repository
if (validate_repo && !dir.exists(".git")) {
warning("⚠️ Not a git repository: ", repo_dir)
return(FALSE)
}
# Check if there are changes to commit
status_result <- system("git status --porcelain", intern = TRUE, ignore.stderr = TRUE)
if (length(status_result) == 0) {
cat(" ℹ️ No changes to commit in:", repo_name, "\n")
return(TRUE)
}
cat(" 📝 Found", length(status_result), "changed files in:", repo_name, "\n")
if (dry_run) {
cat(" 📋 Dry run mode - would commit:", repo_name, "\n")
return(TRUE)
}
# Add all changes
add_result <- system("git add -A", intern = TRUE, ignore.stderr = TRUE)
if (add_result != 0) {
warning("⚠️ Git add failed for ", repo_name)
return(FALSE)
}
# Commit with message
escaped_message <- gsub('"', '\\"', commit_message)
commit_cmd <- sprintf('git commit -m "%s"', escaped_message)
commit_result <- system(commit_cmd, intern = TRUE, ignore.stderr = TRUE)
if (commit_result == 0 || any(grepl("nothing to commit", commit_result))) {
cat(" ✅ Committed changes in:", repo_name, "\n")
return(TRUE)
} else {
warning("⚠️ Git commit returned non-zero status for ", repo_name)
return(FALSE)
}
}, error = function(e) {
warning("⚠️ Git commit failed for ", basename(repo_dir), ": ", e$message)
return(FALSE)
})
}
# Function to commit changes in a Git repository ----
commit_repo <- function(repo_dir, commit_message) {
old_wd <- getwd()
setwd(repo_dir)
system("git add -A")
system(sprintf("git commit -m \"%s\"", commit_message))
setwd(old_wd)
}
# Function to replace ClinicoPath references with module-specific names ----
replace_clinicopath_with_module <- function(base_dir, module_name) {
if (!dir.exists(base_dir)) {
message("Directory does not exist: ", base_dir)
return()
}
# Get all R, Rmd, qmd, and md files in the directory recursively
files <- list.files(
path = base_dir,
pattern = "\\.(R|Rmd|rmd|qmd|md)$",
full.names = TRUE,
recursive = TRUE
)
if (length(files) > 0) {
cat(" 📝 Updating", length(files), "file(s) with package references...\n")
# Perform replacements
xfun::gsub_files(
files = files,
pattern = "library\\(ClinicoPath\\)",
replacement = paste0("library(", module_name, ")")
)
xfun::gsub_files(
files = files,
pattern = "ClinicoPath::",
replacement = paste0(module_name, "::")
)
cat(" ✅ Package references updated successfully\n")
} else {
cat(" ℹ️ No files found to update\n")
}
}
# Enhanced configuration-based asset copying ----
if (!WIP && !TEST) {
cat("\n📁 Copying assets with configuration-based logic...\n")
for (module_name in names(modules_config)) {
# Skip disabled modules - use top-level configuration flags
module_enabled <- FALSE
if (module_name == "meddecide" && meddecide_module) module_enabled <- TRUE
if (module_name == "jjstatsplot" && jjstatsplot_module) module_enabled <- TRUE
if (module_name == "jsurvival" && jsurvival_module) module_enabled <- TRUE
if (module_name == "ClinicoPathDescriptives" && ClinicoPathDescriptives_module) module_enabled <- TRUE
if (module_name == "OncoPath" && OncoPath_module) module_enabled <- TRUE
if (module_name == "JamoviTest" && TEST) module_enabled <- TRUE
if (!module_enabled) next
module_cfg <- modules_config[[module_name]]
module_dir <- module_dirs[[module_name]]
cat("📁 Processing assets for", module_name, "\n")
# Copy data files
if (copy_data_files && length(module_cfg$data_files) > 0) {
cat(" 📁 Copying", module_name, "data files...\n")
data_dir <- file.path(module_dir, "data")
if (!dir.exists(data_dir)) {
dir.create(data_dir, recursive = TRUE)
}
data_sources <- file.path(main_repo_dir, "data", module_cfg$data_files)
tryCatch({
for (data_file in module_cfg$data_files) {
source_path <- file.path(main_repo_dir, "data", data_file)
if (file.exists(source_path)) {
fs::file_copy(source_path, file.path(data_dir, data_file), overwrite = TRUE)
} else {
warning("⚠️ Data file not found: ", source_path)
}
}
}, error = function(e) {
warning("⚠️ Error copying data files for ", module_name, ": ", e$message)
})
} else if (!copy_data_files) {
cat(" ⏭️ Skipping", module_name, "data files (copy_data_files: false)\n")
}
# Copy R files
if (copy_r_files && length(module_cfg$r_files) > 0) {
cat(" 📁 Copying", module_name, "R files...\n")
r_dir <- file.path(module_dir, "R")
if (!dir.exists(r_dir)) {
dir.create(r_dir, recursive = TRUE)
}
tryCatch({
for (r_file in module_cfg$r_files) {
source_path <- file.path(main_repo_dir, "R", r_file)
if (file.exists(source_path)) {
fs::file_copy(source_path, file.path(r_dir, r_file), overwrite = TRUE)
} else {
warning("⚠️ R file not found: ", source_path)
}
}
}, error = function(e) {
warning("⚠️ Error copying R files for ", module_name, ": ", e$message)
})
} else if (!copy_r_files) {
cat(" ⏭️ Skipping", module_name, "R files (copy_r_files: false)\n")
}
# Copy test files
if (copy_test_files && length(module_cfg$test_files) > 0) {
cat(" 📁 Copying", module_name, "test files...\n")
test_dir <- file.path(module_dir, "tests/testthat")
if (!dir.exists(test_dir)) {
dir.create(test_dir, recursive = TRUE)
}
tryCatch({
for (test_file in module_cfg$test_files) {
source_path <- file.path(main_repo_dir, "tests/testthat", test_file)
if (file.exists(source_path)) {
fs::file_copy(source_path, file.path(test_dir, test_file), overwrite = TRUE)
} else {
warning("⚠️ Test file not found: ", source_path)
}
}
}, error = function(e) {
warning("⚠️ Error copying test files for ", module_name, ": ", e$message)
})
} else if (!copy_test_files) {
cat(" ⏭️ Skipping", module_name, "test files (copy_test_files: false)\n")
}
# Copy vignette files (folder-based copying)
if (copy_vignettes && (config$vignette_folders$copy_settings$use_folder_based %||% TRUE)) {
cat(" 📄 Copying", module_name, "vignette files (folder-based)...\n")
vignette_dir <- file.path(module_dir, "vignettes")
# Clean existing vignettes folder for fresh copy
if (dir.exists(vignette_dir)) {
cat(" 🧹 Cleaning existing vignettes folder...\n")
tryCatch({
fs::dir_delete(vignette_dir)