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, |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 69 | pub is_read_write: bool, |
| 70 | pub method_name: String, |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 73 | fn create_class_element(package: &str, pf: &ProtoParsedFlag) -> ClassElement { |
| 74 | let device_config_flag = codegen::create_device_config_ident(package, pf.name()) |
| 75 | .expect("values checked at flag parse time"); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 76 | ClassElement { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 77 | default_value: if pf.state() == ProtoFlagState::ENABLED { |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 78 | "true".to_string() |
| 79 | } else { |
| 80 | "false".to_string() |
| 81 | }, |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 82 | device_config_namespace: pf.namespace().to_string(), |
Mårten Kongstad | 066575b | 2023-06-07 16:29:25 +0200 | [diff] [blame] | 83 | device_config_flag, |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 84 | is_read_write: pf.permission() == ProtoFlagPermission::READ_WRITE, |
| 85 | method_name: format_java_method_name(pf.name()), |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 89 | fn format_java_method_name(flag_name: &str) -> String { |
| 90 | flag_name |
| 91 | .split('_') |
| 92 | .filter(|&word| !word.is_empty()) |
| 93 | .enumerate() |
| 94 | .map(|(index, word)| { |
| 95 | if index == 0 { |
| 96 | word.to_ascii_lowercase() |
| 97 | } else { |
| 98 | word[0..1].to_ascii_uppercase() + &word[1..].to_ascii_lowercase() |
| 99 | } |
| 100 | }) |
| 101 | .collect::<Vec<String>>() |
| 102 | .join("") |
| 103 | } |
| 104 | |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 105 | #[cfg(test)] |
| 106 | mod tests { |
| 107 | use super::*; |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 108 | use std::collections::HashMap; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 109 | |
| 110 | #[test] |
| 111 | fn test_generate_java_code() { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 112 | let parsed_flags = crate::test::parse_test_flags(); |
| 113 | let generated_files = |
| 114 | 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] | 115 | let expect_flags_content = r#" |
| 116 | package com.android.aconfig.test; |
Joe Onorato | 0c4ef0f | 2023-05-13 11:30:11 -0700 | [diff] [blame] | 117 | public final class Flags { |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 118 | public static boolean disabledRo() { |
| 119 | return FEATURE_FLAGS.disabledRo(); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 120 | } |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 121 | public static boolean disabledRw() { |
| 122 | return FEATURE_FLAGS.disabledRw(); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 123 | } |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 124 | public static boolean enabledRo() { |
| 125 | return FEATURE_FLAGS.enabledRo(); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 126 | } |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 127 | public static boolean enabledRw() { |
| 128 | return FEATURE_FLAGS.enabledRw(); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 129 | } |
| 130 | private static FeatureFlags FEATURE_FLAGS = new FeatureFlagsImpl(); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 131 | } |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 132 | "#; |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 133 | let expected_featureflagsimpl_content = r#" |
| 134 | package com.android.aconfig.test; |
| 135 | import android.provider.DeviceConfig; |
| 136 | public final class FeatureFlagsImpl implements FeatureFlags { |
| 137 | @Override |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 138 | public boolean disabledRo() { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | @Override |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 142 | public boolean disabledRw() { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 143 | return DeviceConfig.getBoolean( |
| 144 | "aconfig_test", |
| 145 | "com.android.aconfig.test.disabled_rw", |
| 146 | false |
| 147 | ); |
| 148 | } |
| 149 | @Override |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 150 | public boolean enabledRo() { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | @Override |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 154 | public boolean enabledRw() { |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 155 | return DeviceConfig.getBoolean( |
| 156 | "aconfig_test", |
| 157 | "com.android.aconfig.test.enabled_rw", |
| 158 | true |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | "#; |
| 163 | let expected_featureflags_content = r#" |
| 164 | package com.android.aconfig.test; |
| 165 | public interface FeatureFlags { |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 166 | boolean disabledRo(); |
| 167 | boolean disabledRw(); |
| 168 | boolean enabledRo(); |
| 169 | boolean enabledRw(); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 170 | } |
| 171 | "#; |
| 172 | let mut file_set = HashMap::from([ |
| 173 | ("com/android/aconfig/test/Flags.java", expect_flags_content), |
| 174 | ("com/android/aconfig/test/FeatureFlagsImpl.java", expected_featureflagsimpl_content), |
| 175 | ("com/android/aconfig/test/FeatureFlags.java", expected_featureflags_content), |
| 176 | ]); |
| 177 | |
| 178 | for file in generated_files { |
| 179 | let file_path = file.path.to_str().unwrap(); |
| 180 | assert!(file_set.contains_key(file_path), "Cannot find {}", file_path); |
| 181 | assert_eq!( |
| 182 | None, |
| 183 | crate::test::first_significant_code_diff( |
| 184 | file_set.get(file_path).unwrap(), |
| 185 | &String::from_utf8(file.contents.clone()).unwrap() |
| 186 | ), |
| 187 | "File {} content is not correct", |
| 188 | file_path |
| 189 | ); |
| 190 | file_set.remove(file_path); |
| 191 | } |
| 192 | |
| 193 | assert!(file_set.is_empty()); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 194 | } |
Zhi Dou | af81e20 | 2023-06-14 20:38:20 +0000 | [diff] [blame] | 195 | |
| 196 | #[test] |
| 197 | fn test_format_java_method_name() { |
| 198 | let input = "____some_snake___name____"; |
| 199 | let expected = "someSnakeName"; |
| 200 | let formatted_name = format_java_method_name(input); |
| 201 | assert_eq!(expected, formatted_name); |
| 202 | } |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 203 | } |