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 | |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame] | 22 | use crate::codegen; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 23 | use crate::commands::OutputFile; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 24 | use crate::protos::{ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag}; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 25 | |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 26 | pub fn generate_java_code<'a, I>(package: &str, parsed_flags_iter: I) -> Result<Vec<OutputFile>> |
| 27 | where |
| 28 | I: Iterator<Item = &'a ProtoParsedFlag>, |
| 29 | { |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame] | 30 | let class_elements: Vec<ClassElement> = |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 31 | parsed_flags_iter.map(|pf| create_class_element(package, pf)).collect(); |
| 32 | let is_read_write = class_elements.iter().any(|elem| elem.is_read_write); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 33 | let context = Context { package_name: package.to_string(), is_read_write, class_elements }; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 34 | let mut template = TinyTemplate::new(); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 35 | template.add_template("Flags.java", include_str!("../templates/Flags.java.template"))?; |
| 36 | template.add_template( |
| 37 | "FeatureFlagsImpl.java", |
| 38 | include_str!("../templates/FeatureFlagsImpl.java.template"), |
| 39 | )?; |
| 40 | template.add_template( |
| 41 | "FeatureFlags.java", |
| 42 | include_str!("../templates/FeatureFlags.java.template"), |
| 43 | )?; |
| 44 | |
| 45 | let path: PathBuf = package.split('.').collect(); |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 46 | ["Flags.java", "FeatureFlagsImpl.java", "FeatureFlags.java"] |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 47 | .iter() |
| 48 | .map(|file| { |
| 49 | Ok(OutputFile { |
| 50 | contents: template.render(file, &context)?.into(), |
| 51 | path: path.join(file), |
| 52 | }) |
| 53 | }) |
| 54 | .collect::<Result<Vec<OutputFile>>>() |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | #[derive(Serialize)] |
| 58 | struct Context { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 59 | pub package_name: String, |
| 60 | pub is_read_write: bool, |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 61 | pub class_elements: Vec<ClassElement>, |
| 62 | } |
| 63 | |
| 64 | #[derive(Serialize)] |
| 65 | struct ClassElement { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 66 | pub default_value: String, |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame] | 67 | pub device_config_namespace: String, |
| 68 | pub device_config_flag: String, |
Mårten Kongstad | a2e152a | 2023-06-19 16:11:33 +0200 | [diff] [blame^] | 69 | pub flag_name_constant_suffix: String, |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 70 | pub is_read_write: bool, |
| 71 | pub method_name: String, |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 74 | fn create_class_element(package: &str, pf: &ProtoParsedFlag) -> ClassElement { |
| 75 | let device_config_flag = codegen::create_device_config_ident(package, pf.name()) |
| 76 | .expect("values checked at flag parse time"); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 77 | ClassElement { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 78 | default_value: if pf.state() == ProtoFlagState::ENABLED { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 79 | "true".to_string() |
| 80 | } else { |
| 81 | "false".to_string() |
| 82 | }, |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 83 | device_config_namespace: pf.namespace().to_string(), |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame] | 84 | device_config_flag, |
Mårten Kongstad | a2e152a | 2023-06-19 16:11:33 +0200 | [diff] [blame^] | 85 | flag_name_constant_suffix: pf.name().to_ascii_uppercase(), |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 86 | is_read_write: pf.permission() == ProtoFlagPermission::READ_WRITE, |
| 87 | method_name: format_java_method_name(pf.name()), |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 91 | fn format_java_method_name(flag_name: &str) -> String { |
| 92 | flag_name |
| 93 | .split('_') |
| 94 | .filter(|&word| !word.is_empty()) |
| 95 | .enumerate() |
| 96 | .map(|(index, word)| { |
| 97 | if index == 0 { |
| 98 | word.to_ascii_lowercase() |
| 99 | } else { |
| 100 | word[0..1].to_ascii_uppercase() + &word[1..].to_ascii_lowercase() |
| 101 | } |
| 102 | }) |
| 103 | .collect::<Vec<String>>() |
| 104 | .join("") |
| 105 | } |
| 106 | |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 107 | #[cfg(test)] |
| 108 | mod tests { |
| 109 | use super::*; |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 110 | use std::collections::HashMap; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 111 | |
| 112 | #[test] |
| 113 | fn test_generate_java_code() { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 114 | let parsed_flags = crate::test::parse_test_flags(); |
| 115 | let generated_files = |
| 116 | generate_java_code(crate::test::TEST_PACKAGE, parsed_flags.parsed_flag.iter()).unwrap(); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 117 | let expect_flags_content = r#" |
| 118 | package com.android.aconfig.test; |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 119 | public final class Flags { |
Mårten Kongstad | a2e152a | 2023-06-19 16:11:33 +0200 | [diff] [blame^] | 120 | public static final String FLAG_DISABLED_RO = "com.android.aconfig.test.disabled_ro"; |
| 121 | public static final String FLAG_DISABLED_RW = "com.android.aconfig.test.disabled_rw"; |
| 122 | public static final String FLAG_ENABLED_RO = "com.android.aconfig.test.enabled_ro"; |
| 123 | public static final String FLAG_ENABLED_RW = "com.android.aconfig.test.enabled_rw"; |
| 124 | |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 125 | public static boolean disabledRo() { |
| 126 | return FEATURE_FLAGS.disabledRo(); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 127 | } |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 128 | public static boolean disabledRw() { |
| 129 | return FEATURE_FLAGS.disabledRw(); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 130 | } |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 131 | public static boolean enabledRo() { |
| 132 | return FEATURE_FLAGS.enabledRo(); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 133 | } |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 134 | public static boolean enabledRw() { |
| 135 | return FEATURE_FLAGS.enabledRw(); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 136 | } |
| 137 | private static FeatureFlags FEATURE_FLAGS = new FeatureFlagsImpl(); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 138 | } |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 139 | "#; |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 140 | let expected_featureflagsimpl_content = r#" |
| 141 | package com.android.aconfig.test; |
| 142 | import android.provider.DeviceConfig; |
| 143 | public final class FeatureFlagsImpl implements FeatureFlags { |
| 144 | @Override |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 145 | public boolean disabledRo() { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | @Override |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 149 | public boolean disabledRw() { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 150 | return DeviceConfig.getBoolean( |
| 151 | "aconfig_test", |
| 152 | "com.android.aconfig.test.disabled_rw", |
| 153 | false |
| 154 | ); |
| 155 | } |
| 156 | @Override |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 157 | public boolean enabledRo() { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | @Override |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 161 | public boolean enabledRw() { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 162 | return DeviceConfig.getBoolean( |
| 163 | "aconfig_test", |
| 164 | "com.android.aconfig.test.enabled_rw", |
| 165 | true |
| 166 | ); |
| 167 | } |
| 168 | } |
| 169 | "#; |
| 170 | let expected_featureflags_content = r#" |
| 171 | package com.android.aconfig.test; |
| 172 | public interface FeatureFlags { |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 173 | boolean disabledRo(); |
| 174 | boolean disabledRw(); |
| 175 | boolean enabledRo(); |
| 176 | boolean enabledRw(); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 177 | } |
| 178 | "#; |
| 179 | let mut file_set = HashMap::from([ |
| 180 | ("com/android/aconfig/test/Flags.java", expect_flags_content), |
| 181 | ("com/android/aconfig/test/FeatureFlagsImpl.java", expected_featureflagsimpl_content), |
| 182 | ("com/android/aconfig/test/FeatureFlags.java", expected_featureflags_content), |
| 183 | ]); |
| 184 | |
| 185 | for file in generated_files { |
| 186 | let file_path = file.path.to_str().unwrap(); |
| 187 | assert!(file_set.contains_key(file_path), "Cannot find {}", file_path); |
| 188 | assert_eq!( |
| 189 | None, |
| 190 | crate::test::first_significant_code_diff( |
| 191 | file_set.get(file_path).unwrap(), |
| 192 | &String::from_utf8(file.contents.clone()).unwrap() |
| 193 | ), |
| 194 | "File {} content is not correct", |
| 195 | file_path |
| 196 | ); |
| 197 | file_set.remove(file_path); |
| 198 | } |
| 199 | |
| 200 | assert!(file_set.is_empty()); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 201 | } |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 202 | |
| 203 | #[test] |
| 204 | fn test_format_java_method_name() { |
| 205 | let input = "____some_snake___name____"; |
| 206 | let expected = "someSnakeName"; |
| 207 | let formatted_name = format_java_method_name(input); |
| 208 | assert_eq!(expected, formatted_name); |
| 209 | } |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 210 | } |