1use std::path::Path;
2
3pub const WRAPPER_PROPERTIES_PATH: &str = "gradle/wrapper/gradle-wrapper.properties";
4
5pub fn create_dir_write_file<P: AsRef<Path>, R: ?Sized>(
6 overwrite: bool,
7 path: P,
8 content: &mut R,
9) -> std::io::Result<bool>
10where
11 R: std::io::Read,
12{
13 let path = path.as_ref();
14 if !overwrite && path.exists() {
15 return Ok(false);
16 }
17 if let Some(dir) = path.parent() {
18 std::fs::create_dir_all(dir)?;
19 }
20 let mut file = std::fs::File::create(path)?;
21 std::io::copy(content, &mut file)?;
22 Ok(true)
23}
24fn wrapper_properties_content(version: &str) -> String {
25 format!(
26 r#"distributionBase=GRADLE_USER_HOME
27distributionPath=wrapper/dists
28distributionUrl=https\://services.gradle.org/distributions/gradle-{}-bin.zip
29zipStoreBase=GRADLE_USER_HOME
30zipStorePath=wrapper/dists"#,
31 version
32 )
33}
34
35pub fn create_dir_write_wrapper_properties<P: AsRef<Path>>(
36 path: P,
37 version: &str,
38) -> std::io::Result<()> {
39 create_dir_write_file(
40 true,
41 path,
42 &mut std::io::Cursor::new(wrapper_properties_content(version)),
43 )
44 .map(|_| ())
45}