-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlicenses.rs
274 lines (249 loc) · 10.7 KB
/
licenses.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
// Copyright (C) 2017 Christopher R. Field.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::stored_path::{StoredPath, StoredPathBuf};
use crate::Error;
use crate::Result;
use crate::Template;
use crate::LICENSE_FILE_NAME;
use crate::RTF_FILE_EXTENSION;
use camino::Utf8Path;
use camino::Utf8PathBuf;
use log::trace;
use log::warn;
use std::str::FromStr;
use cargo_metadata::Package;
/// License info
#[derive(Clone, Debug)]
pub struct Licenses {
/// The license for the actual source code
///
/// This likely will become/contain a Vec at some point,
/// since dual MIT/Apache wants to have two license files!
pub source: Option<License>,
/// The end-user license (EULA) that must be agreed to when installing
pub end_user: Option<License>,
}
/// A license file/item
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct License {
/// Path to the file, relative to package's root dir.
///
/// So generated files would be "wix\License.rtf"
/// and the typical LICENSE file would just be "LICENSE-MIT".
///
/// Conveniently this means we don't need to do any special handling
/// to rewrite/relativize a path we get out of a Cargo.toml.
///
/// It can also be an absolute path if the user passed this via CLI
/// and doesn't care about persistence/portability.
pub stored_path: StoredPathBuf,
/// File name to use for the license when installed to the user's system
///
/// If None, the source file name is used.
pub name: Option<String>,
/// This file needs to be generated, write it to the given path
/// using the given Template.
pub generate: Option<(Utf8PathBuf, Template)>,
}
impl License {
/// Create a License entry with just the StoredPath
fn from_stored_path(path: &StoredPath) -> Self {
Self {
name: None,
stored_path: path.to_owned(),
generate: None,
}
}
}
impl Licenses {
/// Get license/eula info for a package
pub fn new(
dest_dir: Option<&Utf8Path>,
license_path: Option<&StoredPath>,
eula_path: Option<&StoredPath>,
package: &Package,
) -> Result<Self> {
let source_license = Self::find_source_license(dest_dir, license_path, package)?;
let end_user_license =
Self::find_end_user_license(eula_path, package, source_license.as_ref())?;
Ok(Self {
source: source_license,
end_user: end_user_license,
})
}
/// Find the source-license for a package
fn find_source_license(
dest_dir: Option<&Utf8Path>,
path: Option<&StoredPath>,
package: &Package,
) -> Result<Option<License>> {
trace!("finding source license for {}", package.name);
// If explicitly passed, use that
if let Some(path) = path {
trace!("explicit source-license path passed as argument, using that");
return Ok(Some(License::from_stored_path(path)));
}
let package_dir = package.manifest_path.parent().expect("non-root Cargo.toml");
let dest_dir = dest_dir
.map(|p| p.to_owned())
.unwrap_or_else(|| package_dir.join(crate::WIX));
// If there's [package.manifest.wix].license, respect that
if let Some(license) = package
.metadata
.get("wix")
.and_then(|w| w.as_object())
.and_then(|t| t.get("license"))
{
trace!("[package.manifest.wix].license is specified");
if let Some(license_enabled) = license.as_bool() {
// If the user sets `eula = false`, disable the eula
// (= true just falls through to the auto-detection stuff below)
if !license_enabled {
trace!("[package.manifest.wix].license is false, disabling license support");
return Ok(None);
} else {
trace!("[package.manifest.wix].license is true, continuing to auto-detect");
}
} else if let Some(path) = license.as_str() {
// If the user sets `license = "path/to/license"`, use that
trace!("[package.manifest.wix].license is a path, using that");
if package_dir.join(path).exists() {
return Ok(Some(License::from_stored_path(StoredPath::new(path))));
} else {
return Err(Error::Generic(format!(
r#"{} specifies package.metadata.wix.license="{}" in its Cargo.toml, but no such file exists."#,
package.name, path,
)));
}
} else {
// Don't accept anything else
trace!("[package.manifest.wix].license is an invalid type");
return Err(Error::Generic(format!(
"{}'s [package.metadata.wix].license must be a bool or a path",
package.name
)));
}
}
// First try Cargo's license_file field
if let Some(path) = &package.license_file {
trace!("Cargo.toml license_file is specified, using that");
// NOTE: this join will mishandle an absolute path stored in the Cargo.toml
// but there's very little justification for such a thing, so it's fine.
if package_dir.join(path).exists() {
// This is already a path relative to the Cargo.toml, so it can be used verbatim
return Ok(Some(License::from_stored_path(
&StoredPathBuf::from_utf8_path(path),
)));
} else {
return Err(Error::Generic(format!(
r#"{} specifies license-file="{}" in its Cargo.toml, but no such file exists."#,
package.name, path,
)));
}
}
// Next try Cargo's license field
if let Some(name) = package.license.clone() {
trace!("Cargo.toml license is specified");
// If there's a template for this license, generate it
if let Ok(generate) = Template::from_str(&name) {
trace!("Found a matching template, generating that");
let file_name = format!("{LICENSE_FILE_NAME}.{RTF_FILE_EXTENSION}");
let dest_file = dest_dir.join(file_name);
let rel_file = dest_file.strip_prefix(package_dir).unwrap_or(&dest_file);
let stored_path = StoredPathBuf::from_utf8_path(rel_file);
return Ok(Some(License {
name: None,
stored_path,
generate: Some((dest_file, generate)),
}));
} else {
trace!("No matching template, ignoring license");
}
}
trace!("No source-license found");
Ok(None)
}
/// Find the eula for a package
fn find_end_user_license(
path: Option<&StoredPath>,
package: &Package,
source_license: Option<&License>,
) -> Result<Option<License>> {
trace!("finding end-user-license for {}", package.name);
// If explicitly passed, use that
if let Some(path) = path {
trace!("explicit end-user-license path passed as argument, using that");
return Ok(Some(License::from_stored_path(path)));
}
let package_dir = package.manifest_path.parent().expect("non-root Cargo.toml");
// If there's [package.manifest.wix].eula, respect that
if let Some(eula) = package
.metadata
.get("wix")
.and_then(|w| w.as_object())
.and_then(|t| t.get("eula"))
{
trace!("[package.manifest.wix].eula is specified");
if let Some(eula_enabled) = eula.as_bool() {
// If the user sets `eula = false`, disable the eula
// (= true just falls through to the auto-detection stuff below)
if !eula_enabled {
trace!("[package.manifest.wix].eula is false, disabling license support");
return Ok(None);
} else {
trace!("[package.manifest.wix].eula is true, continuing to auto-detect");
}
} else if let Some(path) = eula.as_str() {
// If the user sets `eula = "path/to/license"`, use that
trace!("[package.manifest.wix].eula is a path, using that");
if package_dir.join(path).exists() {
return Ok(Some(License::from_stored_path(StoredPath::new(path))));
} else {
return Err(Error::Generic(format!(
r#"{} specifies package.metadata.wix.eula="{}" in its Cargo.toml, but no such file exists."#,
package.name, path,
)));
}
} else {
// Don't accept anything else
trace!("[package.manifest.wix].eula is an invalid type");
return Err(Error::Generic(format!(
"{}'s [package.metadata.wix].eula must be a bool or a path",
package.name
)));
}
}
// Try to use the source-license, if it has a path and is RTF,
if let Some(license) = source_license {
trace!("source-license is defined");
let path = &license.stored_path;
if path.extension() == Some(RTF_FILE_EXTENSION) {
trace!("using path from license");
return Ok(Some(License::from_stored_path(path)));
}
}
trace!("No end-user-license found");
warn!(
"Could not find your project's EULA. The license agreement dialog will be excluded \
from the installer. You can add one by either:
* Setting 'package.license' to a recognized value (MIT, Apache-2.0, or GPL-3.0)
* Setting 'package.license-file', 'package.metadata.wix.license', or 'package.metadata.wix.eula' \
to point to an RTF file
* Passing an RTF file with --license or --eula to the cargo-wix CLI
* Editing the generated WiX Source (wxs) with a text editor
To supress this warning, set 'package.metadata.wix.eula = false'"
);
Ok(None)
}
}