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