blob: e1e916637a6149d585a0d4a1f600d3a97e8477dd [file] [log] [blame]
Mårten Kongstad867a3492023-04-25 15:06:30 +02001/*
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//! `aconfig` is a build time tool to manage build time configurations, such as feature flags.
18
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +020019use anyhow::{anyhow, ensure, Result};
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020020use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command};
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020021use core::any::Any;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020022use std::fs;
Mårten Kongstada1029092023-05-08 11:51:59 +020023use std::io;
24use std::io::Write;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +020025use std::path::{Path, PathBuf};
Mårten Kongstad867a3492023-04-25 15:06:30 +020026
Mårten Kongstadbb520722023-04-26 13:16:41 +020027mod aconfig;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020028mod cache;
Zhi Doueb744892023-05-10 04:02:33 +000029mod codegen_java;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020030mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020031mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020032
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020033use crate::cache::Cache;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020034use commands::{DumpFormat, Input, OutputFile, Source};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020035
36fn cli() -> Command {
37 Command::new("aconfig")
38 .subcommand_required(true)
39 .subcommand(
40 Command::new("create-cache")
Mårten Kongstad30950782023-05-09 13:31:29 +020041 .arg(Arg::new("namespace").long("namespace").required(true))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020042 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
43 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020044 .arg(Arg::new("cache").long("cache").required(true)),
45 )
46 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000047 Command::new("create-java-lib")
48 .arg(Arg::new("cache").long("cache").required(true))
49 .arg(Arg::new("out").long("out").required(true)),
50 )
51 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +020052 Command::new("dump")
53 .arg(Arg::new("cache").long("cache").required(true))
54 .arg(
55 Arg::new("format")
56 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +020057 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +020058 .default_value("text"),
59 )
60 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020061 )
62}
Mårten Kongstad867a3492023-04-25 15:06:30 +020063
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020064fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
65where
66 T: Any + Clone + Send + Sync + 'static,
67{
68 matches
69 .get_one::<T>(arg_name)
70 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
71}
72
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020073fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
74 let mut opened_files = vec![];
75 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
76 let file = Box::new(fs::File::open(path)?);
77 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
78 }
79 Ok(opened_files)
80}
81
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +020082fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
83 ensure!(
84 root.is_dir(),
85 "output directory {} does not exist or is not a directory",
86 root.display()
87 );
88 let path = root.join(output_file.path.clone());
89 let parent = path
90 .parent()
91 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
92 fs::create_dir_all(parent)?;
93 let mut file = fs::File::create(path)?;
94 file.write_all(&output_file.contents)?;
95 Ok(())
96}
97
Mårten Kongstadbb520722023-04-26 13:16:41 +020098fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020099 let matches = cli().get_matches();
100 match matches.subcommand() {
101 Some(("create-cache", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200102 let namespace = get_required_arg::<String>(sub_matches, "namespace")?;
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200103 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
104 let values = open_zero_or_more_files(sub_matches, "values")?;
105 let cache = commands::create_cache(namespace, declarations, values)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200106 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200107 let file = fs::File::create(path)?;
108 cache.write_to_writer(file)?;
109 }
Zhi Doueb744892023-05-10 04:02:33 +0000110 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200111 let path = get_required_arg::<String>(sub_matches, "cache")?;
Zhi Doueb744892023-05-10 04:02:33 +0000112 let file = fs::File::open(path)?;
113 let cache = Cache::read_from_reader(file)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200114 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
115 let generated_file = commands::generate_code(&cache)?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200116 write_output_file_realtive_to_dir(&dir, &generated_file)?;
Zhi Doueb744892023-05-10 04:02:33 +0000117 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200118 Some(("dump", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200119 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200120 let file = fs::File::open(path)?;
121 let cache = Cache::read_from_reader(file)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200122 let format = get_required_arg::<DumpFormat>(sub_matches, "format")?;
Mårten Kongstada1029092023-05-08 11:51:59 +0200123 let output = commands::dump_cache(cache, *format)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200124 let path = get_required_arg::<String>(sub_matches, "out")?;
125 let mut file: Box<dyn Write> = if *path == "-" {
Mårten Kongstada1029092023-05-08 11:51:59 +0200126 Box::new(io::stdout())
127 } else {
128 Box::new(fs::File::create(path)?)
129 };
130 file.write_all(&output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200131 }
132 _ => unreachable!(),
133 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200134 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200135}