Merge "Use the namepace as the java package name in aconfig and call the class "Flags"" am: 7833547153
am: 6ee16caf99
am: 31268dd2da
am: a4440161f7
Original change: https://android-review.googlesource.com/c/platform/build/+/2588271 Change-Id: Idd288c2eb41877c12ddcb8c3192154887f932352 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
commit
48a66bf49e
2 changed files with 13 additions and 25 deletions
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use std::path::PathBuf;
|
||||||
use tinytemplate::TinyTemplate;
|
use tinytemplate::TinyTemplate;
|
||||||
|
|
||||||
use crate::aconfig::{FlagState, Permission};
|
use crate::aconfig::{FlagState, Permission};
|
||||||
|
@ -25,12 +26,14 @@ use crate::commands::OutputFile;
|
||||||
pub fn generate_java_code(cache: &Cache) -> Result<OutputFile> {
|
pub fn generate_java_code(cache: &Cache) -> Result<OutputFile> {
|
||||||
let class_elements: Vec<ClassElement> = cache.iter().map(create_class_element).collect();
|
let class_elements: Vec<ClassElement> = cache.iter().map(create_class_element).collect();
|
||||||
let readwrite = class_elements.iter().any(|item| item.readwrite);
|
let readwrite = class_elements.iter().any(|item| item.readwrite);
|
||||||
let namespace = uppercase_first_letter(cache.namespace());
|
let namespace = cache.namespace();
|
||||||
let context = Context { namespace: namespace.clone(), readwrite, class_elements };
|
let context = Context { namespace: namespace.to_string(), readwrite, class_elements };
|
||||||
let mut template = TinyTemplate::new();
|
let mut template = TinyTemplate::new();
|
||||||
template.add_template("java_code_gen", include_str!("../templates/java.template"))?;
|
template.add_template("java_code_gen", include_str!("../templates/java.template"))?;
|
||||||
let contents = template.render("java_code_gen", &context)?;
|
let contents = template.render("java_code_gen", &context)?;
|
||||||
let path = ["com", "android", "internal", "aconfig", &(namespace + ".java")].iter().collect();
|
let mut path: PathBuf = namespace.split('.').collect();
|
||||||
|
// TODO: Allow customization of the java class name
|
||||||
|
path.push("Flags.java");
|
||||||
Ok(OutputFile { contents: contents.into(), path })
|
Ok(OutputFile { contents: contents.into(), path })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,21 +67,6 @@ fn create_class_element(item: &Item) -> ClassElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uppercase_first_letter(s: &str) -> String {
|
|
||||||
s.chars()
|
|
||||||
.enumerate()
|
|
||||||
.map(
|
|
||||||
|(index, ch)| {
|
|
||||||
if index == 0 {
|
|
||||||
ch.to_ascii_uppercase()
|
|
||||||
} else {
|
|
||||||
ch.to_ascii_lowercase()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -87,7 +75,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_generate_java_code() {
|
fn test_generate_java_code() {
|
||||||
let namespace = "TeSTFlaG";
|
let namespace = "com.example";
|
||||||
let mut cache = Cache::new(namespace.to_string()).unwrap();
|
let mut cache = Cache::new(namespace.to_string()).unwrap();
|
||||||
cache
|
cache
|
||||||
.add_flag_declaration(
|
.add_flag_declaration(
|
||||||
|
@ -118,11 +106,11 @@ mod tests {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let expect_content = r#"package com.android.internal.aconfig;
|
let expect_content = r#"package com.example;
|
||||||
|
|
||||||
import android.provider.DeviceConfig;
|
import android.provider.DeviceConfig;
|
||||||
|
|
||||||
public final class Testflag {
|
public final class Flags {
|
||||||
|
|
||||||
public static boolean test() {
|
public static boolean test() {
|
||||||
return false;
|
return false;
|
||||||
|
@ -130,7 +118,7 @@ mod tests {
|
||||||
|
|
||||||
public static boolean test2() {
|
public static boolean test2() {
|
||||||
return DeviceConfig.getBoolean(
|
return DeviceConfig.getBoolean(
|
||||||
"Testflag",
|
"com.example",
|
||||||
"test2__test2",
|
"test2__test2",
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
@ -139,7 +127,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
let file = generate_java_code(&cache).unwrap();
|
let file = generate_java_code(&cache).unwrap();
|
||||||
assert_eq!("com/android/internal/aconfig/Testflag.java", file.path.to_str().unwrap());
|
assert_eq!("com/example/Flags.java", file.path.to_str().unwrap());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expect_content.replace(' ', ""),
|
expect_content.replace(' ', ""),
|
||||||
String::from_utf8(file.contents).unwrap().replace(' ', "")
|
String::from_utf8(file.contents).unwrap().replace(' ', "")
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.android.internal.aconfig;
|
package {namespace};
|
||||||
{{ if readwrite }}
|
{{ if readwrite }}
|
||||||
import android.provider.DeviceConfig;
|
import android.provider.DeviceConfig;
|
||||||
{{ endif }}
|
{{ endif }}
|
||||||
public final class {namespace} \{
|
public final class Flags \{
|
||||||
{{ for item in class_elements}}
|
{{ for item in class_elements}}
|
||||||
public static boolean {item.method_name}() \{
|
public static boolean {item.method_name}() \{
|
||||||
{{ if item.readwrite- }}
|
{{ if item.readwrite- }}
|
||||||
|
|
Loading…
Reference in a new issue