-
Notifications
You must be signed in to change notification settings - Fork 17
/
buck.rs
1025 lines (934 loc) · 32.9 KB
/
buck.rs
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/
extern crate serde;
extern crate serde_json;
use std::borrow::Cow;
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::sync::Arc;
use anyhow::bail;
use anyhow::Result;
use eetf::Term;
use eetf::Term::Atom;
use elp_log::timeit;
use fxhash::FxHashMap;
use fxhash::FxHashSet;
use indexmap::indexset;
use itertools::Itertools;
use lazy_static::lazy_static;
use parking_lot::Mutex;
use paths::AbsPath;
use paths::AbsPathBuf;
use paths::RelPathBuf;
use serde::Deserialize;
use serde::Serialize;
use tempfile::NamedTempFile;
use crate::otp::Otp;
use crate::AppName;
use crate::AppType;
use crate::BuildInfoFile;
use crate::CommandProxy;
use crate::ElpConfig;
use crate::ProjectAppData;
use crate::ProjectModelError;
pub type TargetFullName = String;
lazy_static! {
static ref DIRS: Vec<RelPathBuf> = vec!["src", "test", "include"]
.into_iter()
.flat_map(|dir| dir.try_into())
.collect();
}
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Hash,
Ord,
PartialOrd,
Deserialize,
Default
)]
pub struct BuckConfig {
#[serde(skip_deserializing)]
/// Location of ELP_CONFIG_FILE this config was loaded from
config_path: Option<AbsPathBuf>,
#[serde(skip_deserializing)]
buck_root: Option<AbsPathBuf>,
pub enabled: bool,
pub deps_target: Option<String>,
pub build_deps: bool,
pub included_targets: Vec<String>,
#[serde(default)]
pub excluded_targets: Vec<String>,
source_root: Option<PathBuf>,
}
impl BuckConfig {
pub fn buck_command(&self) -> CommandProxy<'_> {
static BUCK_GLOBAL_LOCK: Mutex<()> = Mutex::new(());
let guard = BUCK_GLOBAL_LOCK.lock();
let mut cmd = Command::new("buck2");
cmd.arg("--isolation-dir");
cmd.arg("lsp");
cmd.current_dir(self.buck_root());
CommandProxy(guard, cmd)
}
pub fn buck_root(&self) -> &AbsPathBuf {
self.buck_root.as_ref().unwrap()
}
pub fn source_root(&self) -> Cow<AbsPathBuf> {
let buck_root = self.buck_root();
match self.source_root {
None => Cow::Borrowed(buck_root),
Some(ref dir) => Cow::Owned(buck_root.join(dir)),
}
}
pub(crate) fn make_config(path: &AbsPath, config: &mut ElpConfig) -> Result<()> {
let buck_conf = match &mut config.buck {
Some(conf) => conf,
None => return Ok(()),
};
if buck_conf.enabled {
//assign any file from buck monorepo in order to find root
buck_conf.buck_root = Some(path.parent().unwrap().to_path_buf());
buck_conf.config_path = Some(path.to_path_buf());
let root = find_root(buck_conf)?;
buck_conf.buck_root = Some(root);
for excluded in buck_conf.excluded_targets.iter_mut() {
let pat = "/...";
if excluded.ends_with(pat) {
excluded.truncate(excluded.len() - pat.len());
}
}
}
Ok(())
}
pub(crate) fn config_path(&self) -> &AbsPath {
self.config_path.as_ref().unwrap()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Deserialize)]
pub struct EqwalizerConfig {
#[serde(default = "eqwalizer_enable_all_default")]
pub enable_all: bool,
}
fn eqwalizer_enable_all_default() -> bool {
true
}
impl Default for EqwalizerConfig {
fn default() -> Self {
Self { enable_all: true }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct TargetInfo {
pub targets: FxHashMap<TargetFullName, Target>,
pub path_to_target_name: FxHashMap<AbsPathBuf, TargetFullName>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BuckProject {
pub target_info: TargetInfo,
// pub project_app_data: Vec<ProjectAppData>,
pub buck_conf: BuckConfig,
}
impl BuckProject {
pub fn load_from_config(
buck_conf: &BuckConfig,
) -> Result<(BuckProject, Vec<ProjectAppData>, BuildInfoFile, PathBuf), anyhow::Error> {
let target_info = load_buck_targets(buck_conf)?;
let otp_root = Otp::find_otp()?;
let project_app_data = targets_to_project_data(&target_info.targets, &otp_root);
let build_info_term = build_info(buck_conf, &project_app_data, &otp_root);
let build_info = save_build_info(build_info_term)?;
let project = BuckProject {
target_info,
buck_conf: buck_conf.clone(),
};
Ok((project, project_app_data, build_info, otp_root))
}
}
#[derive(Deserialize, Debug)]
struct BuckTarget {
name: String,
//Some if target is test
suite: Option<String>,
#[serde(default)]
srcs: Vec<String>,
#[serde(default)]
includes: Vec<String>,
#[serde(default)]
labels: FxHashSet<String>,
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Target {
//full-name, like waserver//erl/chatd:chatd
pub name: TargetFullName,
pub app_name: String,
pub dir: AbsPathBuf,
pub src_files: Vec<AbsPathBuf>,
pub include_files: Vec<AbsPathBuf>,
pub ebin: Option<AbsPathBuf>,
pub target_type: TargetType,
pub private_header: bool,
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
pub enum TargetType {
ErlangApp,
ErlangTest,
//rebar3's deps
ThirdParty,
ErlangTestUtils,
}
pub fn load_buck_targets(buck_config: &BuckConfig) -> Result<TargetInfo> {
let _timer = timeit!("loading info from buck");
let root = buck_config.buck_root();
let mut dep_path = if buck_config.build_deps {
build_third_party_targets(buck_config)?
} else {
FxHashMap::default()
};
let buck_targets = query_buck_targets(buck_config)?;
let mut target_info = TargetInfo::default();
for (name, target) in buck_targets {
let mut private_header = false;
let dir = match find_app_root(root, &name, &target) {
None => continue,
Some(dir) => dir,
};
let (src_files, include_files, target_type, private_header, ebin) =
if let Some(suite) = target.suite {
let src_file = buck_path_to_abs_path(root, &suite)?;
let src = vec![src_file.clone()];
target_info
.path_to_target_name
.insert(src_file, name.clone());
(src, vec![], TargetType::ErlangTest, false, None)
} else {
let target_type = compute_target_type(&name, &target);
let mut src_files = vec![];
for src in &target.srcs {
let src = buck_path_to_abs_path(root, src).unwrap();
if Some(OsStr::new("hrl")) == src.extension() {
private_header = true;
}
src_files.push(src);
}
let mut include_files = vec![];
for include in &target.includes {
let inc = buck_path_to_abs_path(root, include).unwrap();
include_files.push(inc);
}
let ebin = match target_type {
TargetType::ThirdParty if buck_config.build_deps => dep_path
.remove(&name)
.map(|dir| dir.join(PathBuf::from("ebin"))),
TargetType::ThirdParty => Some(dir.clone()),
_ => None,
};
(src_files, include_files, target_type, private_header, ebin)
};
let target = Target {
name: name.clone(),
app_name: target.name,
dir,
src_files,
include_files,
ebin,
target_type,
private_header,
};
target_info.targets.insert(name, target);
}
Ok(target_info)
}
fn compute_target_type(name: &TargetFullName, target: &BuckTarget) -> TargetType {
if name.contains("//third-party") {
TargetType::ThirdParty
} else {
let test_utils = target.labels.contains("test_utils");
let test_application = target.labels.contains("test_application");
let elp_enabled = target.labels.contains("elp_enabled");
match (elp_enabled, test_application, test_utils) {
(true, _, _) => TargetType::ErlangApp,
(false, false, false) => TargetType::ErlangApp,
(_, _, _) => TargetType::ErlangTestUtils,
}
}
}
/// finds buck root directory based on buck config, executing `buck2 root`
fn find_root(buck_config: &BuckConfig) -> Result<AbsPathBuf> {
let _timer = timeit!("loading root");
let output = match buck_config.buck_command().arg("root").output() {
Ok(out) => out,
Err(err) => {
log::error!("Err executing buck2 root: {:?}", err);
bail!(crate::ProjectModelError::MissingBuck(err))
}
};
if !output.status.success() {
if output.status.code() == Some(1)
&& String::from_utf8_lossy(&output.stderr)
.contains("not in a Buck project, are you missing a .buckconfig file?")
{
bail!(ProjectModelError::NotInBuckProject)
} else {
bail!(
"Failed to get buck2 root, error code: {:?}, stderr: {:?}",
output.status.code(),
String::from_utf8(output.stderr)
)
}
}
let path = String::from_utf8(output.stdout)?;
match AbsPathBuf::try_from(path.trim()) {
Ok(path) => Ok(path),
Err(path) => bail!("expected absolute path, got {}", path.display()),
}
}
fn query_buck_targets(buck_config: &BuckConfig) -> Result<FxHashMap<TargetFullName, BuckTarget>> {
let _timer = timeit!("load buck targets");
let mut kinds = String::new();
for target in &buck_config.included_targets {
if kinds.is_empty() {
kinds = format!(
"kind(erlang_test$, {}) + kind(erlang_app$, {})",
target, target
);
} else {
kinds.push_str(
format!(
"+ kind(erlang_test$, {}) + kind(erlang_app$, {})",
target, target
)
.as_str(),
);
}
}
if let Some(deps_target) = &buck_config.deps_target {
if kinds.is_empty() {
kinds = format!("kind(erlang_app$, {})", deps_target);
} else {
kinds.push_str(format!("+ kind(erlang_app$, {})", deps_target).as_str());
}
}
let output = buck_config
.buck_command()
.arg("uquery")
.arg("--config=client.id=elp")
.arg("--json")
.arg(kinds)
.arg("--output-attribute")
.arg("suite")
.arg("--output-attribute")
.arg("^includes")
.arg("--output-attribute")
.arg("srcs")
.arg("--output-attribute")
.arg("name")
.arg("--output-attribute")
.arg("labels")
.output()?;
if !output.status.success() {
bail!(
"Failed to load buck2 targets, error code: {:?}, stderr: {:?}",
output.status.code(),
String::from_utf8(output.stderr)
);
}
let string = String::from_utf8(output.stdout)?;
let result: FxHashMap<TargetFullName, BuckTarget> = serde_json::from_str(&string)?;
let result = result
.into_iter()
.filter(|(name, _)| {
!buck_config
.excluded_targets
.iter()
.any(|excluded| name.starts_with(excluded))
})
.filter(|(_, target)| {
target.suite.is_some() || !target.srcs.is_empty() || !target.includes.is_empty()
})
.collect();
Ok(result)
}
fn build_third_party_targets(
buck_config: &BuckConfig,
) -> Result<FxHashMap<TargetFullName, AbsPathBuf>> {
let deps_target = match &buck_config.deps_target {
None => return Ok(FxHashMap::default()),
Some(_) if !buck_config.build_deps => return Ok(FxHashMap::default()),
Some(target) => target,
};
let _timer = timeit!("building third party deps");
let output = buck_config
.buck_command()
.arg("build")
.arg("--config=client.id=elp")
.arg("--prefer-local")
.arg("--show-full-json-output")
.arg(deps_target)
.output()?;
if !output.status.success() {
bail!(
"Failed to get buck2 build target output, error code: {:?}, stderr: {:?}",
output.status.code(),
String::from_utf8(output.stderr)
);
}
let string = String::from_utf8(output.stdout)?;
let result: FxHashMap<String, String> = serde_json::from_str(&string)?;
// yaws_priv and piqi_priv are buck hacks, we get empty path for it
Ok(result
.into_iter()
.filter(|(_, v)| !v.is_empty())
.map(|(k, v)| (k, AbsPathBuf::assert(v.into())))
.collect())
}
/// Convert waserver//erl/util/thrift/src/thrift_compiler.erl to /Users/$USER/local/whatsapp/server/erl/util/thrift/src/thrift_compiler.erl
fn buck_path_to_abs_path(root: &AbsPath, target: &str) -> Result<AbsPathBuf> {
let mut split = target.split("//");
let _ = split.next(); //waserver or empty in case of //...
match split.next() {
None => bail!("couldn't find a path for target {:?}", target),
Some(path) => Ok(root.join(path)),
}
}
fn str_to_binary(s: &str) -> Term {
Term::Binary(s.as_bytes().into())
}
fn path_to_binary(path: impl AsRef<Path>) -> Term {
str_to_binary(path.as_ref().as_os_str().to_str().unwrap())
}
/// creates erlang term for an app in a format required by eqwalizer
pub fn build_info_app(project_data: &ProjectAppData, ebin: impl AsRef<Path>) -> Term {
let dir = path_to_binary(&project_data.dir);
let ebin = path_to_binary(ebin);
let extra_src_dirs = Term::List(
project_data
.extra_src_dirs
.iter()
.map(|s| str_to_binary(s.as_ref()))
.collect::<Vec<Term>>()
.into(),
);
let include_dirs = Term::List(
project_data
.include_dirs
.iter()
.map(path_to_binary)
.collect::<Vec<Term>>()
.into(),
);
let macros = Term::List(project_data.macros.clone().into());
let name = str_to_binary(&project_data.name.0);
let parse_transforms = Term::List(project_data.parse_transforms.clone().into());
let src_dirs = Term::List(
project_data
.abs_src_dirs
.iter()
.filter_map(|src| src.strip_prefix(project_data.dir.as_ref()))
.map(|path| path_to_binary(path.as_ref()))
.collect::<Vec<Term>>()
.into(),
);
Term::Map(
vec![
(Atom("dir".into()), dir),
(Atom("ebin".into()), ebin),
(Atom("extra_src_dirs".into()), extra_src_dirs),
(Atom("include_dirs".into()), include_dirs),
(Atom("macros".into()), macros),
(Atom("name".into()), name),
(Atom("parse_transforms".into()), parse_transforms),
(Atom("src_dirs".into()), src_dirs),
]
.into(),
)
}
pub fn build_info(config: &BuckConfig, project_apps: &[ProjectAppData], otp_root: &Path) -> Term {
let mut apps = vec![];
let mut deps = vec![];
let default_ebin = config.buck_root().join("buck-out/ebins");
for project in project_apps {
let ebin = match &project.ebin {
None => default_ebin.as_path(),
Some(ebin) => ebin.as_path(),
};
match project.app_type {
AppType::App => apps.push(build_info_app(project, ebin)),
AppType::Dep if !config.build_deps => apps.push(build_info_app(project, ebin)),
AppType::Dep => deps.push(build_info_app(project, ebin)),
_ => {}
};
}
let path = &config.source_root();
make_build_info(apps, deps, otp_root, path.as_ref())
}
pub fn make_build_info(
apps: Vec<Term>,
deps: Vec<Term>,
otp_root: impl AsRef<Path>,
source_root: impl AsRef<Path>,
) -> Term {
let apps = Term::List(apps.into());
let deps = Term::List(deps.into());
let otp_lib_dir = path_to_binary(otp_root);
let source_root = path_to_binary(source_root);
Term::Map(
vec![
(Atom("apps".into()), apps),
(Atom("deps".into()), deps),
(Atom("otp_lib_dir".into()), otp_lib_dir),
(Atom("source_root".into()), source_root),
]
.into(),
)
}
pub fn save_build_info(term: Term) -> Result<BuildInfoFile> {
let mut out_file = NamedTempFile::new()?;
term.encode(&mut out_file)?;
let build_info_path = out_file.into_temp_path();
Ok(BuildInfoFile(Arc::new(build_info_path)))
}
/// convert waserver//erl/chatd:chatd into abs path ~/local/whatsapp/server/erl/chatd
fn find_buck_file_base_target_dir(
root: &AbsPath,
target_name: &TargetFullName,
) -> Result<AbsPathBuf> {
let path: String = target_name.chars().take_while(|ch| *ch != ':').collect();
buck_path_to_abs_path(root, &path)
}
fn find_app_root(
root: &AbsPath,
target_name: &TargetFullName,
target: &BuckTarget,
) -> Option<AbsPathBuf> {
let dir_based_on_buck_file = find_buck_file_base_target_dir(root, target_name).ok()?;
let mut set = indexset![];
let paths = target
.srcs
.iter()
.chain(target.includes.iter())
.chain(target.suite.iter());
for path in paths {
if let Ok(path) = buck_path_to_abs_path(root, path) {
let parent = path.parent();
if let Some(parent) = parent {
if !set.contains(parent) {
set.insert(parent.to_path_buf());
}
}
}
}
for path in set {
if let Some(path) = examine_path(&path, dir_based_on_buck_file.as_path()) {
// We found an src/, test/, or include/ directory before the BUCK file,
// so we just return the parent of this directory.
return Some(path);
}
}
// Otherwise, we just return the directory containing the BUCK file,
Some(dir_based_on_buck_file)
}
fn examine_path(path: &AbsPath, dir_based_on_buck_file: &AbsPath) -> Option<AbsPathBuf> {
let mut parent_it = Some(path);
while let Some(parent_in_dirs) = parent_it {
for dir in DIRS.iter() {
if parent_in_dirs.ends_with(dir) {
return parent_in_dirs.parent().map(|p| p.to_path_buf());
}
}
//optimization to stop on BUCK file location and not go to the /
if dir_based_on_buck_file == parent_in_dirs {
break;
}
parent_it = parent_in_dirs.parent();
}
None
}
pub fn targets_to_project_data(
targets: &FxHashMap<TargetFullName, Target>,
otp_root: &Path,
) -> Vec<ProjectAppData> {
let it = targets
.values()
.sorted_by(|a, b| match (a.target_type, b.target_type) {
(TargetType::ErlangTest, TargetType::ErlangTest) => Ordering::Equal,
(TargetType::ErlangTest, _) => Ordering::Greater,
_ => Ordering::Equal,
});
let mut accs: FxHashMap<AbsPathBuf, ProjectAppDataAcc> = Default::default();
for target in it {
let mut target_dir = target.dir.clone();
let acc = accs.remove(&target_dir);
let mut acc = match acc {
Some(acc) => acc,
None => {
let search_first = match target.target_type {
TargetType::ErlangApp | TargetType::ThirdParty => false,
_ => true,
};
let mut result = None;
if search_first {
let dir = accs.keys().find(|dir| target.dir.starts_with(dir));
if let Some(dir) = dir {
let dir = dir.clone();
result = accs.remove(&dir);
target_dir = dir;
}
}
result.unwrap_or_else(ProjectAppDataAcc::new)
}
};
acc.add(target);
accs.insert(target_dir, acc);
}
let mut result: Vec<ProjectAppData> = vec![];
let mut global_inc: Vec<AbsPathBuf> = targets
.values()
.filter(|target| target.target_type != TargetType::ErlangTest)
.filter_map(|target| target.dir.parent().map(|p| p.to_path_buf()))
.collect();
global_inc.push(AbsPathBuf::assert(otp_root.to_path_buf()));
for (_, mut acc) in accs {
acc.add_global_includes(global_inc.clone());
result.push(acc.into());
}
result
}
#[derive(Debug)]
struct ProjectAppDataAcc {
pub name: Option<AppName>,
pub dir: Option<AbsPathBuf>,
pub ebin: Option<AbsPathBuf>,
pub abs_src_dirs: FxHashSet<AbsPathBuf>,
pub extra_src_dirs: FxHashSet<String>,
pub abs_extra_src_dirs: FxHashSet<AbsPathBuf>,
pub include_dirs: FxHashSet<String>,
pub macros: Vec<Term>,
pub app_type: Option<AppType>,
pub include_path: FxHashSet<AbsPathBuf>,
}
impl ProjectAppDataAcc {
fn new() -> ProjectAppDataAcc {
ProjectAppDataAcc {
name: None,
dir: None,
ebin: None,
extra_src_dirs: Default::default(),
abs_extra_src_dirs: Default::default(),
include_dirs: Default::default(),
macros: vec![],
app_type: None,
include_path: Default::default(),
abs_src_dirs: Default::default(),
}
}
fn set_ebin(&mut self, target: &Target) {
if let (Some(ebin), None) = (&target.ebin, &self.ebin) {
self.ebin = Some(ebin.clone());
}
}
fn set_app_type(&mut self, target: &Target) {
if self.app_type.is_none() {
let app_type = match target.target_type {
TargetType::ErlangApp => AppType::App,
TargetType::ErlangTest => AppType::App,
TargetType::ErlangTestUtils => AppType::App,
TargetType::ThirdParty => AppType::Dep,
};
self.app_type = Some(app_type)
}
}
fn set_name(&mut self, target: &Target) {
let dir_name = target
.dir
.file_name()
.map(|f| AppName(f.to_string_lossy().to_string()));
let target_name = Some(AppName(target.app_name.clone()));
if dir_name == target_name {
self.name = target_name;
} else {
match (target.target_type, &self.name) {
(TargetType::ErlangApp | TargetType::ThirdParty, None) => self.name = target_name,
(_, None) => self.name = dir_name,
_ => (),
};
}
}
fn set_dir(&mut self, target: &Target) {
if self.dir.is_none() {
self.dir = Some(target.dir.clone());
}
}
fn add_src(&mut self, target: &Target) {
let app_dir = match &self.dir {
None => return,
Some(dir) => dir,
};
match target.target_type {
TargetType::ErlangApp | TargetType::ThirdParty => {
let abs_src_dirs: FxHashSet<AbsPathBuf> = target
.src_files
.iter()
.filter_map(|src| src.parent())
.map(|dir| dir.to_path_buf())
.filter(|dir| dir.file_name() != Some(&OsStr::new("test")))
.collect();
self.abs_src_dirs.extend(abs_src_dirs);
}
TargetType::ErlangTest | TargetType::ErlangTestUtils => {
let abs_extra_dirs: FxHashSet<AbsPathBuf> = target
.src_files
.iter()
.filter_map(|extra| extra.parent())
.map(|extra| extra.to_path_buf())
.filter(|dir| dir.file_name() != Some(&OsStr::new("src")))
.collect();
let extra_src_dirs: Vec<String> = abs_extra_dirs
.iter()
.filter_map(|dir| dir.strip_prefix(app_dir))
.map(|dir| dir.as_ref().as_os_str().to_string_lossy().to_string())
.collect();
for abs_extra in &abs_extra_dirs {
self.add_parent_if_not_exist(abs_extra);
}
self.extra_src_dirs.extend(extra_src_dirs);
self.abs_extra_src_dirs.extend(abs_extra_dirs);
}
}
}
fn add_include_path(&mut self, target: &Target) {
//we need it because some applications, like erlclient has tree structure inside the /src
//and includes the files from top into the bottom files
//for example erlclient/src/wrapper_handlers/erlclient_web_device_login_cookie.erl includes the
//erlclient/src/erlclient.hrl
if target.private_header {
for src in &target.src_files {
self.add_parent_if_not_exist(src);
}
}
for inc in &target.include_files {
self.add_parent_if_not_exist(inc);
}
}
fn add_parent_if_not_exist(&mut self, path: &AbsPath) {
if let Some(parent) = path.parent() {
if !self.include_path.contains(parent) {
self.include_path.insert(parent.to_path_buf());
}
}
}
fn add_global_includes(&mut self, includes: Vec<AbsPathBuf>) {
self.include_path.extend(includes);
}
fn add_include(&mut self, target: &Target) {
let it = target
.include_files
.iter()
.filter_map(|inc| inc.parent())
//in case of .*_test_utils target it is possible to have *.hrl file in test dir
//which buck treats as include. This check prevents it in case we already added
//the folder as test folder
.filter(|inc| !self.abs_extra_src_dirs.contains(*inc))
.filter_map(|inc| inc.strip_prefix(&target.dir))
.map(|inc| inc.as_ref().to_string_lossy().to_string());
self.include_dirs.extend(it);
}
fn set_macro(&mut self, target: &Target) {
if target.target_type != TargetType::ThirdParty && self.macros.is_empty() {
self.macros.push(Atom("TEST".into()));
self.macros.push(Atom("COMMON_TEST".into()));
}
}
fn add(&mut self, target: &Target) {
self.set_name(target);
self.set_dir(target);
self.set_app_type(target);
self.set_ebin(target);
self.set_macro(target);
self.add_src(target);
self.add_include(target);
self.add_include_path(target);
}
}
impl From<ProjectAppDataAcc> for ProjectAppData {
fn from(acc: ProjectAppDataAcc) -> Self {
let dir = acc.dir.unwrap();
ProjectAppData {
name: acc.name.unwrap(),
dir: dir.clone(),
ebin: acc.ebin,
extra_src_dirs: acc.extra_src_dirs.into_iter().collect(),
include_dirs: acc
.include_dirs
.into_iter()
.map(|include| dir.join(include))
.collect(),
macros: acc.macros,
parse_transforms: vec![],
abs_src_dirs: acc.abs_src_dirs.into_iter().collect(),
app_type: acc.app_type.unwrap(),
// we sort to speed up parse-server
// local folders goes first, than common, like /erl
// TODO:AZ why only for buck?
include_path: acc
.include_path
.into_iter()
.sorted_by(|a, b| {
if a.starts_with(&dir) && !b.starts_with(&dir) {
return Ordering::Less;
}
if b.starts_with(&dir) && !a.starts_with(&dir) {
return Ordering::Greater;
}
let len1 = a.as_os_str().len();
let len2 = b.as_os_str().len();
len1.cmp(&len2)
})
.collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_fixture::FixtureWithProjectMeta;
#[test]
fn test_find_app_root_src() {
let spec = r#"
//- /app_a/src/app.erl
//- /app_a/BUCK
"#;
let dir = FixtureWithProjectMeta::gen_project(spec);
let root = AbsPath::assert(dir.path());
let target_name = "waserver//app_a:app_a".to_string();
let target = BuckTarget {
name: "app_a".to_string(),
suite: None,
srcs: vec!["waserver//app_a/src/app.erl".to_string()],
includes: vec![],
labels: FxHashSet::default(),
};
let actual = find_app_root(root, &target_name, &target);
let expected = Some(AbsPathBuf::assert(dir.path().join("app_a")));
assert_eq!(expected, actual)
}
#[test]
fn test_find_app_root_include() {
let spec = r#"
//- /app_a/include/app.hrl
//- /app_a/BUCK
"#;
let dir = FixtureWithProjectMeta::gen_project(spec);
let root = AbsPath::assert(dir.path());
let target_name = "waserver//app_a:app_a".to_string();
let target = BuckTarget {
name: "app_a".to_string(),
suite: None,
srcs: vec![],
includes: vec!["waserver//app_a/include/app.hrl".to_string()],
labels: FxHashSet::default(),
};
let actual = find_app_root(root, &target_name, &target);
let expected = Some(AbsPathBuf::assert(dir.path().join("app_a")));
assert_eq!(expected, actual)
}
#[test]
fn test_find_app_root_suite() {
let spec = r#"
//- /app_a/test/app_SUITE.erl
//- /app_a/BUCK
"#;
let dir = FixtureWithProjectMeta::gen_project(spec);
let root = AbsPath::assert(dir.path());
let target_name = "waserver//app_a:app_a".to_string();
let target = BuckTarget {
name: "app_a".to_string(),
suite: Some("waserver//app_a/test/app_SUITE.erl".to_string()),
srcs: vec![],
includes: vec![],
labels: FxHashSet::default(),
};
let actual = find_app_root(root, &target_name, &target);
let expected = Some(AbsPathBuf::assert(dir.path().join("app_a")));
assert_eq!(expected, actual)
}
#[test]
//aka wa_zippy case
fn test_find_app_root_multiple_src() {
let spec = r#"
//- /app_a/src/app.erl
//- /app_a/entity/entity.erl
//- /app_a/BUCK
"#;
let dir = FixtureWithProjectMeta::gen_project(spec);
let root = AbsPath::assert(dir.path());
let target_name = "waserver//app_a:app_a".to_string();
let target = BuckTarget {
name: "app_a".to_string(),
suite: None,
srcs: vec![
"waserver//app_a/entity/entity.erl".to_string(),
"waserver//app_a/src/app.erl".to_string(),
],
includes: vec![],
labels: FxHashSet::default(),
};
let actual = find_app_root(root, &target_name, &target);
let expected = Some(AbsPathBuf::assert(dir.path().join("app_a")));
assert_eq!(expected, actual)
}
#[test]
fn test_find_app_root_no_structure_buck_inside() {
let spec = r#"
//- /app_a/app.erl
//- /app_a/app.hrl
//- /app_a/BUCK
"#;
let dir = FixtureWithProjectMeta::gen_project(spec);
let root = AbsPath::assert(dir.path());
let target_name = "waserver//app_a:app_a".to_string();
let target = BuckTarget {
name: "app_a".to_string(),
suite: None,
srcs: vec!["waserver//app_a/app.erl".to_string()],
includes: vec!["waserver//app_a/app.hrl".to_string()],
labels: FxHashSet::default(),
};
let actual = find_app_root(root, &target_name, &target);
let expected = Some(AbsPathBuf::assert(dir.path().join("app_a")));
assert_eq!(expected, actual)