Skip to content

Commit e15751b

Browse files
committed
Use DSL v2 in workspace project
Signed-off-by: Patrick Luca Fazzi <patrick91@live.it>
1 parent 1e362af commit e15751b

File tree

3 files changed

+60
-37
lines changed

3 files changed

+60
-37
lines changed

examples/workspace_project/tests/src/architecture_rules_test.rs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
use rust_arkitect::dsl::{ArchitecturalRules, Arkitect, Project};
1+
use rust_arkitect::dsl_v2::architectural_rules::ArchitecturalRules;
2+
use rust_arkitect::dsl_v2::arkitect::Arkitect;
3+
use rust_arkitect::dsl_v2::project::Project;
24

35
#[test]
46
fn test_vertical_slices_architecture_rules() {
57
Arkitect::init_logger();
68

79
#[rustfmt::skip]
810
let rules = ArchitecturalRules::define()
9-
.component("Contracts")
10-
.located_at("contracts")
11+
.rules_for_crate("contracts")
1112
.must_not_depend_on_anything()
1213

13-
.component("Conversion")
14-
.located_at("conversion")
15-
.may_depend_on(&["Contracts"])
14+
.rules_for_crate("conversion")
15+
.allow_dependencies_on(&["contracts"])
1616

17-
.component("PolicyManagement")
18-
.located_at("policy_management")
19-
.may_depend_on(&["Contracts"])
17+
.rules_for_crate("policy_management")
18+
.allow_dependencies_on(&["contracts"])
2019

21-
.component("Application")
22-
.located_at("application")
23-
.may_depend_on(&["Conversion", "PolicyManagement"])
20+
.rules_for_crate("application")
21+
.allow_dependencies_on(&["conversion", "policy_management"])
2422

25-
.finalize();
23+
.build();
2624

2725
let project = Project::new();
2826

@@ -39,18 +37,16 @@ fn test_mvc_architecture_rules() {
3937

4038
#[rustfmt::skip]
4139
let rules = ArchitecturalRules::define()
42-
.component("Model")
43-
.located_at("crate::policy_management::model")
40+
.rules_for_module("crate::policy_management::model")
4441
.must_not_depend_on_anything()
4542

46-
.component("Repository")
47-
.located_at("crate::policy_management::repository")
48-
.may_depend_on(&["Model"])
43+
.rules_for_module("crate::policy_management::repository")
44+
.allow_dependencies_on(&["crate::policy_management::model"])
4945

50-
.component("Controller")
51-
.located_at("crate::policy_management::controller")
52-
.may_depend_on(&["Repository", "Model"])
53-
.finalize();
46+
.rules_for_module("crate::policy_management::controller")
47+
.allow_dependencies_on(&["crate::policy_management::repository", "crate::policy_management::model"])
48+
49+
.build();
5450

5551
let result = Arkitect::ensure_that(project).complies_with(rules);
5652

@@ -61,23 +57,19 @@ fn test_mvc_architecture_rules() {
6157
fn test_three_tier_architecture() {
6258
Arkitect::init_logger();
6359

64-
let project =Project::new();
60+
let project = Project::new();
6561

6662
#[rustfmt::skip]
6763
let rules = ArchitecturalRules::define()
68-
.component("Application")
69-
.located_at("crate::conversion::application")
70-
.may_depend_on(&["Domain"])
64+
.rules_for_module("crate::conversion::application")
65+
.allow_dependencies_on(&["crate::conversion::domain"])
7166

72-
.component("Domain")
73-
.located_at("crate::conversion::domain")
67+
.rules_for_module("crate::conversion::domain")
7468
.must_not_depend_on_anything()
7569

76-
.component("Infrastructure")
77-
.located_at("crate::conversion::infrastructure")
78-
.may_depend_on(&["Domain", "Application"])
79-
80-
.finalize();
70+
.rules_for_module("crate::conversion::infrastructure")
71+
.allow_dependencies_on(&["crate::conversion::domain", "crate::conversion::application"])
72+
.build();
8173

8274
let result = Arkitect::ensure_that(project).complies_with(rules);
8375

src/dsl_v2/project.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::env;
2-
use std::path::Path;
1+
use std::path::{Path, PathBuf};
2+
use std::{env, fs};
33

44
pub struct Project {
55
pub project_root: String,
@@ -16,11 +16,40 @@ impl Project {
1616
let cargo_manifest_dir =
1717
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is not set");
1818

19+
let crate_path = Path::new(&cargo_manifest_dir);
20+
21+
if Self::is_workspace_root(crate_path) {
22+
return Project {
23+
project_root: cargo_manifest_dir,
24+
};
25+
}
26+
27+
if let Some(parent_path) = crate_path.parent() {
28+
if Self::is_workspace_root(parent_path) {
29+
return Project {
30+
project_root: parent_path.to_string_lossy().into_owned(),
31+
};
32+
}
33+
}
34+
1935
Project {
2036
project_root: cargo_manifest_dir,
2137
}
2238
}
2339

40+
fn is_workspace_root(path: &Path) -> bool {
41+
let cargo_toml = path.join("Cargo.toml");
42+
if !cargo_toml.exists() {
43+
return false;
44+
}
45+
46+
if let Ok(contents) = fs::read_to_string(cargo_toml) {
47+
return contents.contains("[workspace]");
48+
}
49+
50+
false
51+
}
52+
2453
/// Creates a Project from a path relative to the given file.
2554
pub fn from_relative_path(current_file: &str, relative_path: &str) -> Project {
2655
let current_dir = Path::new(current_file)

src/engine.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::rules::rule::Rule;
22
use ansi_term::Color::RGB;
33
use ansi_term::Style;
4-
use log::{debug, error, info};
4+
use log::{debug, error, info, log};
55
use std::fs;
66
use std::path::{Path, PathBuf};
77
use toml::Value;
@@ -23,8 +23,10 @@ impl<'a> Engine<'a> {
2323

2424
pub(crate) fn get_violations(mut self) -> Vec<String> {
2525
if is_workspace(self.absolute_path).is_ok() {
26+
info!("Workspace found: {}", self.absolute_path);
2627
self.validate_workspace(self.absolute_path);
2728
} else if is_crate(self.absolute_path).is_ok() {
29+
info!("Crate found: {}", self.absolute_path);
2830
self.validate_dir(self.absolute_path);
2931
} else {
3032
panic!(
@@ -94,7 +96,7 @@ impl<'a> Engine<'a> {
9496
.and_then(|p| p.to_str().map(String::from))
9597
.unwrap_or_else(|| "Unknown file".to_string());
9698

97-
info!("🛠️Applying rules to {}", bold.paint(absolute_file_name));
99+
info!("🛠Applying rules to {}", bold.paint(absolute_file_name));
98100
for rule in self.rules {
99101
if rule.is_applicable(file_name) {
100102
debug!("🟢 Rule {} applied", rule);

0 commit comments

Comments
 (0)