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
use std::io::prelude::*;
use std::fs::{self, File};
use std::collections::BTreeMap;
use std::vec::Vec;
use serde_json;
use std::path::{Path, PathBuf};
use super::{CliError, LalResult};
pub fn create_lal_subdir(pwd: &PathBuf) -> LalResult<()> {
let loc = pwd.join(".lal");
if !loc.is_dir() {
fs::create_dir(&loc)?
}
Ok(())
}
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone)]
pub struct ComponentConfiguration {
pub defaultConfig: String,
pub configurations: Vec<String>,
}
impl Default for ComponentConfiguration {
fn default() -> ComponentConfiguration {
ComponentConfiguration {
configurations: vec!["release".to_string()],
defaultConfig: "release".to_string(),
}
}
}
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Manifest {
pub name: String,
pub environment: String,
pub supportedEnvironments: Vec<String>,
pub components: BTreeMap<String, ComponentConfiguration>,
pub dependencies: BTreeMap<String, u32>,
pub devDependencies: BTreeMap<String, u32>,
#[serde(skip_serializing, skip_deserializing)]
location: String,
}
pub enum ManifestLocation {
RepoRoot,
LalSubfolder,
}
impl Default for ManifestLocation {
fn default() -> ManifestLocation { ManifestLocation::LalSubfolder }
}
impl ManifestLocation {
pub fn as_path(&self, pwd: &PathBuf) -> PathBuf {
match *self {
ManifestLocation::RepoRoot => pwd.join("manifest.json"),
ManifestLocation::LalSubfolder => pwd.join(".lal/manifest.json"),
}
}
pub fn identify(pwd: &PathBuf) -> LalResult<ManifestLocation> {
if ManifestLocation::LalSubfolder.as_path(pwd).exists() {
if ManifestLocation::RepoRoot.as_path(pwd).exists() {
warn!("manifest.json found in both .lal/ and current directory");
warn!("Using the default: .lal/manifest.json");
}
Ok(ManifestLocation::LalSubfolder)
} else if ManifestLocation::RepoRoot.as_path(pwd).exists() {
Ok(ManifestLocation::RepoRoot)
} else {
Err(CliError::MissingManifest)
}
}
}
impl Manifest {
pub fn new(name: &str, env: &str, location: PathBuf) -> Manifest {
let mut comps = BTreeMap::new();
comps.insert(name.into(), ComponentConfiguration::default());
Manifest {
name: name.into(),
components: comps,
environment: env.into(),
supportedEnvironments: vec![env.into()],
location: location.to_string_lossy().into(),
..Default::default()
}
}
pub fn all_dependencies(&self) -> BTreeMap<String, u32> {
let mut deps = self.dependencies.clone();
for (k, v) in &self.devDependencies {
deps.insert(k.clone(), *v);
}
deps
}
pub fn read() -> LalResult<Manifest> { Ok(Manifest::read_from(&Path::new(".").to_path_buf())?) }
pub fn read_from(pwd: &PathBuf) -> LalResult<Manifest> {
let mpath = ManifestLocation::identify(pwd)?.as_path(pwd);
trace!("Using manifest in {}", mpath.display());
let mut f = File::open(&mpath)?;
let mut data = String::new();
f.read_to_string(&mut data)?;
let mut res: Manifest = serde_json::from_str(&data)?;
res.location = mpath.to_string_lossy().into();
Ok(res)
}
pub fn write(&self) -> LalResult<()> {
let encoded = serde_json::to_string_pretty(self)?;
trace!("Writing manifest in {}", self.location);
let mut f = File::create(&self.location)?;
write!(f, "{}\n", encoded)?;
debug!("Wrote manifest in {}: \n{}", self.location, encoded);
Ok(())
}
pub fn verify(&self) -> LalResult<()> {
for (name, conf) in &self.components {
if &name.to_lowercase() != name {
return Err(CliError::InvalidComponentName(name.clone()));
}
debug!("Verifying component {}", name);
if !conf.configurations.contains(&conf.defaultConfig) {
let ename = format!("default configuration '{}' not found in configurations list",
conf.defaultConfig);
return Err(CliError::InvalidBuildConfiguration(ename));
}
}
for (name, _) in &self.dependencies {
if &name.to_lowercase() != name {
return Err(CliError::InvalidComponentName(name.clone()));
}
}
for (name, _) in &self.devDependencies {
if &name.to_lowercase() != name {
return Err(CliError::InvalidComponentName(name.clone()));
}
}
if self.supportedEnvironments.is_empty() {
return Err(CliError::NoSupportedEnvironments);
}
if !self.supportedEnvironments.iter().any(|x| x == &self.environment) {
return Err(CliError::UnsupportedEnvironment);
}
Ok(())
}
}