blob: 4abcb90868651439c4a0987008d54fdc6fcd7f2c [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 Kongstadbb520722023-04-26 13:16:41 +020019use anyhow::Result;
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020020use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020021use std::fs;
Mårten Kongstada1029092023-05-08 11:51:59 +020022use std::io;
23use std::io::Write;
Mårten Kongstad867a3492023-04-25 15:06:30 +020024
Mårten Kongstadbb520722023-04-26 13:16:41 +020025mod aconfig;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020026mod cache;
Zhi Doueb744892023-05-10 04:02:33 +000027mod codegen_java;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020028mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020029mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020030
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020031use crate::cache::Cache;
32use commands::{Input, Source};
33
34fn cli() -> Command {
35 Command::new("aconfig")
36 .subcommand_required(true)
37 .subcommand(
38 Command::new("create-cache")
Mårten Kongstad30950782023-05-09 13:31:29 +020039 .arg(Arg::new("namespace").long("namespace").required(true))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020040 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
41 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020042 .arg(Arg::new("cache").long("cache").required(true)),
43 )
44 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000045 Command::new("create-java-lib")
46 .arg(Arg::new("cache").long("cache").required(true))
47 .arg(Arg::new("out").long("out").required(true)),
48 )
49 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +020050 Command::new("dump")
51 .arg(Arg::new("cache").long("cache").required(true))
52 .arg(
53 Arg::new("format")
54 .long("format")
55 .value_parser(EnumValueParser::<commands::Format>::new())
56 .default_value("text"),
57 )
58 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020059 )
60}
Mårten Kongstad867a3492023-04-25 15:06:30 +020061
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020062fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
63 let mut opened_files = vec![];
64 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
65 let file = Box::new(fs::File::open(path)?);
66 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
67 }
68 Ok(opened_files)
69}
70
Mårten Kongstadbb520722023-04-26 13:16:41 +020071fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020072 let matches = cli().get_matches();
73 match matches.subcommand() {
74 Some(("create-cache", sub_matches)) => {
Mårten Kongstad30950782023-05-09 13:31:29 +020075 let namespace = sub_matches.get_one::<String>("namespace").unwrap();
Mårten Kongstadfa23d292023-05-11 14:47:02 +020076 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
77 let values = open_zero_or_more_files(sub_matches, "values")?;
78 let cache = commands::create_cache(namespace, declarations, values)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020079 let path = sub_matches.get_one::<String>("cache").unwrap();
80 let file = fs::File::create(path)?;
81 cache.write_to_writer(file)?;
82 }
Zhi Doueb744892023-05-10 04:02:33 +000083 Some(("create-java-lib", sub_matches)) => {
84 let path = sub_matches.get_one::<String>("cache").unwrap();
85 let file = fs::File::open(path)?;
86 let cache = Cache::read_from_reader(file)?;
87 let out = sub_matches.get_one::<String>("out").unwrap();
88 let generated_file = commands::generate_code(&cache).unwrap();
89 fs::write(
90 format!("{}/{}", out, generated_file.file_name),
91 generated_file.file_content,
92 )?;
93 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020094 Some(("dump", sub_matches)) => {
95 let path = sub_matches.get_one::<String>("cache").unwrap();
96 let file = fs::File::open(path)?;
97 let cache = Cache::read_from_reader(file)?;
98 let format = sub_matches.get_one("format").unwrap();
Mårten Kongstada1029092023-05-08 11:51:59 +020099 let output = commands::dump_cache(cache, *format)?;
100 let path = sub_matches.get_one::<String>("out").unwrap();
101 let mut file: Box<dyn Write> = if path == "-" {
102 Box::new(io::stdout())
103 } else {
104 Box::new(fs::File::create(path)?)
105 };
106 file.write_all(&output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200107 }
108 _ => unreachable!(),
109 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200110 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200111}