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 | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame^] | 20 | use clap::{builder::ArgAction, builder::EnumValueParser, Arg, Command}; |
| 21 | use std::fs; |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 22 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 23 | mod aconfig; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame^] | 24 | mod cache; |
| 25 | mod commands; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 26 | mod protos; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 27 | |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame^] | 28 | use crate::cache::Cache; |
| 29 | use commands::{Input, Source}; |
| 30 | |
| 31 | fn cli() -> Command { |
| 32 | Command::new("aconfig") |
| 33 | .subcommand_required(true) |
| 34 | .subcommand( |
| 35 | Command::new("create-cache") |
| 36 | .arg(Arg::new("aconfig").long("aconfig").action(ArgAction::Append)) |
| 37 | .arg(Arg::new("override").long("override").action(ArgAction::Append)) |
| 38 | .arg(Arg::new("cache").long("cache").required(true)), |
| 39 | ) |
| 40 | .subcommand( |
| 41 | Command::new("dump").arg(Arg::new("cache").long("cache").required(true)).arg( |
| 42 | Arg::new("format") |
| 43 | .long("format") |
| 44 | .value_parser(EnumValueParser::<commands::Format>::new()) |
| 45 | .default_value("text"), |
| 46 | ), |
| 47 | ) |
| 48 | } |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 49 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 50 | fn main() -> Result<()> { |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame^] | 51 | let matches = cli().get_matches(); |
| 52 | match matches.subcommand() { |
| 53 | Some(("create-cache", sub_matches)) => { |
| 54 | let mut aconfigs = vec![]; |
| 55 | for path in |
| 56 | sub_matches.get_many::<String>("aconfig").unwrap_or_default().collect::<Vec<_>>() |
| 57 | { |
| 58 | let file = Box::new(fs::File::open(path)?); |
| 59 | aconfigs.push(Input { source: Source::File(path.to_string()), reader: file }); |
| 60 | } |
| 61 | let mut overrides = vec![]; |
| 62 | for path in |
| 63 | sub_matches.get_many::<String>("override").unwrap_or_default().collect::<Vec<_>>() |
| 64 | { |
| 65 | let file = Box::new(fs::File::open(path)?); |
| 66 | overrides.push(Input { source: Source::File(path.to_string()), reader: file }); |
| 67 | } |
| 68 | let cache = commands::create_cache(aconfigs, overrides)?; |
| 69 | let path = sub_matches.get_one::<String>("cache").unwrap(); |
| 70 | let file = fs::File::create(path)?; |
| 71 | cache.write_to_writer(file)?; |
| 72 | } |
| 73 | Some(("dump", sub_matches)) => { |
| 74 | let path = sub_matches.get_one::<String>("cache").unwrap(); |
| 75 | let file = fs::File::open(path)?; |
| 76 | let cache = Cache::read_from_reader(file)?; |
| 77 | let format = sub_matches.get_one("format").unwrap(); |
| 78 | commands::dump_cache(cache, *format)?; |
| 79 | } |
| 80 | _ => unreachable!(), |
| 81 | } |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 82 | Ok(()) |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 83 | } |