Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | use anyhow::Result; |
| 18 | use serde::Serialize; |
| 19 | use tinytemplate::TinyTemplate; |
| 20 | |
| 21 | use crate::aconfig::{FlagState, Permission}; |
| 22 | use crate::cache::{Cache, Item}; |
| 23 | |
| 24 | pub struct GeneratedFile { |
| 25 | pub file_content: String, |
| 26 | pub file_name: String, |
| 27 | } |
| 28 | |
| 29 | pub fn generate_java_code(cache: &Cache) -> Result<GeneratedFile> { |
| 30 | let class_elements: Vec<ClassElement> = cache.iter().map(create_class_element).collect(); |
| 31 | let readwrite = class_elements.iter().any(|item| item.readwrite); |
| 32 | let namespace = uppercase_first_letter( |
| 33 | cache.iter().find(|item| !item.namespace.is_empty()).unwrap().namespace.as_str(), |
| 34 | ); |
| 35 | let context = Context { namespace: namespace.clone(), readwrite, class_elements }; |
| 36 | let mut template = TinyTemplate::new(); |
| 37 | template.add_template("java_code_gen", include_str!("../templates/java.template"))?; |
| 38 | let file_content = template.render("java_code_gen", &context)?; |
| 39 | Ok(GeneratedFile { file_content, file_name: format!("{}.java", namespace) }) |
| 40 | } |
| 41 | |
| 42 | #[derive(Serialize)] |
| 43 | struct Context { |
| 44 | pub namespace: String, |
| 45 | pub readwrite: bool, |
| 46 | pub class_elements: Vec<ClassElement>, |
| 47 | } |
| 48 | |
| 49 | #[derive(Serialize)] |
| 50 | struct ClassElement { |
| 51 | pub method_name: String, |
| 52 | pub readwrite: bool, |
| 53 | pub default_value: String, |
| 54 | pub feature_name: String, |
| 55 | pub flag_name: String, |
| 56 | } |
| 57 | |
| 58 | fn create_class_element(item: &Item) -> ClassElement { |
| 59 | ClassElement { |
| 60 | method_name: item.name.clone(), |
| 61 | readwrite: item.permission == Permission::ReadWrite, |
| 62 | default_value: if item.state == FlagState::Enabled { |
| 63 | "true".to_string() |
| 64 | } else { |
| 65 | "false".to_string() |
| 66 | }, |
| 67 | feature_name: item.name.clone(), |
| 68 | flag_name: item.name.clone(), |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn uppercase_first_letter(s: &str) -> String { |
| 73 | s.chars() |
| 74 | .enumerate() |
| 75 | .map( |
| 76 | |(index, ch)| { |
| 77 | if index == 0 { |
| 78 | ch.to_ascii_uppercase() |
| 79 | } else { |
| 80 | ch.to_ascii_lowercase() |
| 81 | } |
| 82 | }, |
| 83 | ) |
| 84 | .collect() |
| 85 | } |
| 86 | |
| 87 | #[cfg(test)] |
| 88 | mod tests { |
| 89 | use super::*; |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame^] | 90 | use crate::aconfig::{FlagDeclaration, FlagValue}; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 91 | use crate::commands::Source; |
| 92 | |
| 93 | #[test] |
| 94 | fn test_generate_java_code() { |
| 95 | let namespace = "TeSTFlaG"; |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame^] | 96 | let mut cache = Cache::new(namespace.to_string()); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 97 | cache |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame^] | 98 | .add_flag_declaration( |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 99 | Source::File("test.txt".to_string()), |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame^] | 100 | FlagDeclaration { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 101 | name: "test".to_string(), |
| 102 | description: "buildtime enable".to_string(), |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 103 | }, |
| 104 | ) |
| 105 | .unwrap(); |
| 106 | cache |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame^] | 107 | .add_flag_declaration( |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 108 | Source::File("test2.txt".to_string()), |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame^] | 109 | FlagDeclaration { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 110 | name: "test2".to_string(), |
| 111 | description: "runtime disable".to_string(), |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame^] | 112 | }, |
| 113 | ) |
| 114 | .unwrap(); |
| 115 | cache |
| 116 | .add_flag_value( |
| 117 | Source::Memory, |
| 118 | FlagValue { |
| 119 | namespace: namespace.to_string(), |
| 120 | name: "test".to_string(), |
| 121 | state: FlagState::Disabled, |
| 122 | permission: Permission::ReadOnly, |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 123 | }, |
| 124 | ) |
| 125 | .unwrap(); |
| 126 | let expect_content = "package com.android.aconfig; |
| 127 | |
| 128 | import android.provider.DeviceConfig; |
| 129 | |
| 130 | public final class Testflag { |
| 131 | |
| 132 | public static boolean test() { |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame^] | 133 | return false; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public static boolean test2() { |
| 137 | return DeviceConfig.getBoolean( |
| 138 | \"Testflag\", |
| 139 | \"test2__test2\", |
| 140 | false |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | "; |
| 146 | let expected_file_name = format!("{}.java", uppercase_first_letter(namespace)); |
| 147 | let generated_file = generate_java_code(&cache).unwrap(); |
| 148 | assert_eq!(expected_file_name, generated_file.file_name); |
| 149 | assert_eq!(expect_content.replace(' ', ""), generated_file.file_content.replace(' ', "")); |
| 150 | } |
| 151 | } |