Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [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 | //! `aconfig` is a build time tool to manage build time configurations, such as feature flags. |
| 18 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 19 | use anyhow::Result; |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 20 | use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command}; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 21 | use std::fs; |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 22 | use std::io; |
| 23 | use std::io::Write; |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 24 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 25 | mod aconfig; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 26 | mod cache; |
| 27 | mod commands; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 28 | mod protos; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 29 | |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 30 | use crate::cache::Cache; |
| 31 | use commands::{Input, Source}; |
| 32 | |
| 33 | fn cli() -> Command { |
| 34 | Command::new("aconfig") |
| 35 | .subcommand_required(true) |
| 36 | .subcommand( |
| 37 | Command::new("create-cache") |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 38 | .arg( |
| 39 | Arg::new("build-id") |
| 40 | .long("build-id") |
| 41 | .value_parser(clap::value_parser!(u32)) |
| 42 | .required(true), |
| 43 | ) |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 44 | .arg(Arg::new("aconfig").long("aconfig").action(ArgAction::Append)) |
| 45 | .arg(Arg::new("override").long("override").action(ArgAction::Append)) |
| 46 | .arg(Arg::new("cache").long("cache").required(true)), |
| 47 | ) |
| 48 | .subcommand( |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 49 | Command::new("dump") |
| 50 | .arg(Arg::new("cache").long("cache").required(true)) |
| 51 | .arg( |
| 52 | Arg::new("format") |
| 53 | .long("format") |
| 54 | .value_parser(EnumValueParser::<commands::Format>::new()) |
| 55 | .default_value("text"), |
| 56 | ) |
| 57 | .arg(Arg::new("out").long("out").default_value("-")), |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 58 | ) |
| 59 | } |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 60 | |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 61 | fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> { |
| 62 | let mut opened_files = vec![]; |
| 63 | for path in matches.get_many::<String>(arg_name).unwrap_or_default() { |
| 64 | let file = Box::new(fs::File::open(path)?); |
| 65 | opened_files.push(Input { source: Source::File(path.to_string()), reader: file }); |
| 66 | } |
| 67 | Ok(opened_files) |
| 68 | } |
| 69 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 70 | fn main() -> Result<()> { |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 71 | let matches = cli().get_matches(); |
| 72 | match matches.subcommand() { |
| 73 | Some(("create-cache", sub_matches)) => { |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 74 | let build_id = *sub_matches.get_one::<u32>("build-id").unwrap(); |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 75 | let aconfigs = open_zero_or_more_files(sub_matches, "aconfig")?; |
| 76 | let overrides = open_zero_or_more_files(sub_matches, "override")?; |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 77 | let cache = commands::create_cache(build_id, aconfigs, overrides)?; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 78 | let path = sub_matches.get_one::<String>("cache").unwrap(); |
| 79 | let file = fs::File::create(path)?; |
| 80 | cache.write_to_writer(file)?; |
| 81 | } |
| 82 | Some(("dump", sub_matches)) => { |
| 83 | let path = sub_matches.get_one::<String>("cache").unwrap(); |
| 84 | let file = fs::File::open(path)?; |
| 85 | let cache = Cache::read_from_reader(file)?; |
| 86 | let format = sub_matches.get_one("format").unwrap(); |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 87 | let output = commands::dump_cache(cache, *format)?; |
| 88 | let path = sub_matches.get_one::<String>("out").unwrap(); |
| 89 | let mut file: Box<dyn Write> = if path == "-" { |
| 90 | Box::new(io::stdout()) |
| 91 | } else { |
| 92 | Box::new(fs::File::create(path)?) |
| 93 | }; |
| 94 | file.write_all(&output)?; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 95 | } |
| 96 | _ => unreachable!(), |
| 97 | } |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 98 | Ok(()) |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 99 | } |