blob: f29186ae1b369d11ac3a6c0a4007082a0b4c6321 [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 Kongstad09c28d12023-05-04 13:29:26 +020039 .arg(
40 Arg::new("build-id")
41 .long("build-id")
42 .value_parser(clap::value_parser!(u32))
43 .required(true),
44 )
Mårten Kongstad30950782023-05-09 13:31:29 +020045 .arg(Arg::new("namespace").long("namespace").required(true))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020046 .arg(Arg::new("aconfig").long("aconfig").action(ArgAction::Append))
47 .arg(Arg::new("override").long("override").action(ArgAction::Append))
48 .arg(Arg::new("cache").long("cache").required(true)),
49 )
50 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000051 Command::new("create-java-lib")
52 .arg(Arg::new("cache").long("cache").required(true))
53 .arg(Arg::new("out").long("out").required(true)),
54 )
55 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +020056 Command::new("dump")
57 .arg(Arg::new("cache").long("cache").required(true))
58 .arg(
59 Arg::new("format")
60 .long("format")
61 .value_parser(EnumValueParser::<commands::Format>::new())
62 .default_value("text"),
63 )
64 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020065 )
66}
Mårten Kongstad867a3492023-04-25 15:06:30 +020067
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020068fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
69 let mut opened_files = vec![];
70 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
71 let file = Box::new(fs::File::open(path)?);
72 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
73 }
74 Ok(opened_files)
75}
76
Mårten Kongstadbb520722023-04-26 13:16:41 +020077fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020078 let matches = cli().get_matches();
79 match matches.subcommand() {
80 Some(("create-cache", sub_matches)) => {
Mårten Kongstad09c28d12023-05-04 13:29:26 +020081 let build_id = *sub_matches.get_one::<u32>("build-id").unwrap();
Mårten Kongstad30950782023-05-09 13:31:29 +020082 let namespace = sub_matches.get_one::<String>("namespace").unwrap();
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020083 let aconfigs = open_zero_or_more_files(sub_matches, "aconfig")?;
84 let overrides = open_zero_or_more_files(sub_matches, "override")?;
Mårten Kongstad30950782023-05-09 13:31:29 +020085 let cache = commands::create_cache(build_id, namespace, aconfigs, overrides)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020086 let path = sub_matches.get_one::<String>("cache").unwrap();
87 let file = fs::File::create(path)?;
88 cache.write_to_writer(file)?;
89 }
Zhi Doueb744892023-05-10 04:02:33 +000090 Some(("create-java-lib", sub_matches)) => {
91 let path = sub_matches.get_one::<String>("cache").unwrap();
92 let file = fs::File::open(path)?;
93 let cache = Cache::read_from_reader(file)?;
94 let out = sub_matches.get_one::<String>("out").unwrap();
95 let generated_file = commands::generate_code(&cache).unwrap();
96 fs::write(
97 format!("{}/{}", out, generated_file.file_name),
98 generated_file.file_content,
99 )?;
100 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200101 Some(("dump", sub_matches)) => {
102 let path = sub_matches.get_one::<String>("cache").unwrap();
103 let file = fs::File::open(path)?;
104 let cache = Cache::read_from_reader(file)?;
105 let format = sub_matches.get_one("format").unwrap();
Mårten Kongstada1029092023-05-08 11:51:59 +0200106 let output = commands::dump_cache(cache, *format)?;
107 let path = sub_matches.get_one::<String>("out").unwrap();
108 let mut file: Box<dyn Write> = if path == "-" {
109 Box::new(io::stdout())
110 } else {
111 Box::new(fs::File::create(path)?)
112 };
113 file.write_all(&output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200114 }
115 _ => unreachable!(),
116 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200117 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200118}