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; |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 19 | use std::path::PathBuf; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 20 | use tinytemplate::TinyTemplate; |
| 21 | |
| 22 | use crate::aconfig::{FlagState, Permission}; |
| 23 | use crate::cache::{Cache, Item}; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 24 | use crate::commands::OutputFile; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 25 | |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 26 | pub fn generate_java_code(cache: &Cache) -> Result<OutputFile> { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 27 | let class_elements: Vec<ClassElement> = cache.iter().map(create_class_element).collect(); |
| 28 | let readwrite = class_elements.iter().any(|item| item.readwrite); |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 29 | let namespace = cache.namespace(); |
| 30 | let context = Context { namespace: namespace.to_string(), readwrite, class_elements }; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 31 | let mut template = TinyTemplate::new(); |
| 32 | template.add_template("java_code_gen", include_str!("../templates/java.template"))?; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 33 | let contents = template.render("java_code_gen", &context)?; |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 34 | let mut path: PathBuf = namespace.split('.').collect(); |
| 35 | // TODO: Allow customization of the java class name |
| 36 | path.push("Flags.java"); |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 37 | Ok(OutputFile { contents: contents.into(), path }) |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | #[derive(Serialize)] |
| 41 | struct Context { |
| 42 | pub namespace: String, |
| 43 | pub readwrite: bool, |
| 44 | pub class_elements: Vec<ClassElement>, |
| 45 | } |
| 46 | |
| 47 | #[derive(Serialize)] |
| 48 | struct ClassElement { |
| 49 | pub method_name: String, |
| 50 | pub readwrite: bool, |
| 51 | pub default_value: String, |
| 52 | pub feature_name: String, |
| 53 | pub flag_name: String, |
| 54 | } |
| 55 | |
| 56 | fn create_class_element(item: &Item) -> ClassElement { |
| 57 | ClassElement { |
| 58 | method_name: item.name.clone(), |
| 59 | readwrite: item.permission == Permission::ReadWrite, |
| 60 | default_value: if item.state == FlagState::Enabled { |
| 61 | "true".to_string() |
| 62 | } else { |
| 63 | "false".to_string() |
| 64 | }, |
| 65 | feature_name: item.name.clone(), |
| 66 | flag_name: item.name.clone(), |
| 67 | } |
| 68 | } |
| 69 | |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 70 | #[cfg(test)] |
| 71 | mod tests { |
| 72 | use super::*; |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 73 | use crate::aconfig::{FlagDeclaration, FlagValue}; |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame^] | 74 | use crate::cache::CacheBuilder; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 75 | use crate::commands::Source; |
| 76 | |
| 77 | #[test] |
| 78 | fn test_generate_java_code() { |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 79 | let namespace = "com.example"; |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame^] | 80 | let mut builder = CacheBuilder::new(namespace.to_string()).unwrap(); |
| 81 | builder |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 82 | .add_flag_declaration( |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 83 | Source::File("test.txt".to_string()), |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 84 | FlagDeclaration { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 85 | name: "test".to_string(), |
| 86 | description: "buildtime enable".to_string(), |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 87 | }, |
| 88 | ) |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame^] | 89 | .unwrap() |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 90 | .add_flag_declaration( |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 91 | Source::File("test2.txt".to_string()), |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 92 | FlagDeclaration { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 93 | name: "test2".to_string(), |
| 94 | description: "runtime disable".to_string(), |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 95 | }, |
| 96 | ) |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame^] | 97 | .unwrap() |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 98 | .add_flag_value( |
| 99 | Source::Memory, |
| 100 | FlagValue { |
| 101 | namespace: namespace.to_string(), |
| 102 | name: "test".to_string(), |
| 103 | state: FlagState::Disabled, |
| 104 | permission: Permission::ReadOnly, |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 105 | }, |
| 106 | ) |
| 107 | .unwrap(); |
Mårten Kongstad | 2f95444 | 2023-05-17 16:51:16 +0200 | [diff] [blame^] | 108 | let cache = builder.build(); |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 109 | let expect_content = r#"package com.example; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 110 | |
| 111 | import android.provider.DeviceConfig; |
| 112 | |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 113 | public final class Flags { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 114 | |
| 115 | public static boolean test() { |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 116 | return false; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public static boolean test2() { |
| 120 | return DeviceConfig.getBoolean( |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 121 | "com.example", |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 122 | "test2__test2", |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 123 | false |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | } |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 128 | "#; |
| 129 | let file = generate_java_code(&cache).unwrap(); |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 130 | assert_eq!("com/example/Flags.java", file.path.to_str().unwrap()); |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 131 | assert_eq!( |
| 132 | expect_content.replace(' ', ""), |
| 133 | String::from_utf8(file.contents).unwrap().replace(' ', "") |
| 134 | ); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 135 | } |
| 136 | } |