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 | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame^] | 19 | use anyhow::{anyhow, ensure, 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 | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame^] | 24 | use std::path::{Path, PathBuf}; |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 25 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 26 | mod aconfig; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 27 | mod cache; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 28 | mod codegen_java; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 29 | mod commands; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 30 | mod protos; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 31 | |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 32 | use crate::cache::Cache; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame^] | 33 | use commands::{Input, OutputFile, Source}; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 34 | |
| 35 | fn cli() -> Command { |
| 36 | Command::new("aconfig") |
| 37 | .subcommand_required(true) |
| 38 | .subcommand( |
| 39 | Command::new("create-cache") |
Mårten Kongstad | 3095078 | 2023-05-09 13:31:29 +0200 | [diff] [blame] | 40 | .arg(Arg::new("namespace").long("namespace").required(true)) |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 41 | .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append)) |
| 42 | .arg(Arg::new("values").long("values").action(ArgAction::Append)) |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 43 | .arg(Arg::new("cache").long("cache").required(true)), |
| 44 | ) |
| 45 | .subcommand( |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 46 | Command::new("create-java-lib") |
| 47 | .arg(Arg::new("cache").long("cache").required(true)) |
| 48 | .arg(Arg::new("out").long("out").required(true)), |
| 49 | ) |
| 50 | .subcommand( |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 51 | Command::new("dump") |
| 52 | .arg(Arg::new("cache").long("cache").required(true)) |
| 53 | .arg( |
| 54 | Arg::new("format") |
| 55 | .long("format") |
| 56 | .value_parser(EnumValueParser::<commands::Format>::new()) |
| 57 | .default_value("text"), |
| 58 | ) |
| 59 | .arg(Arg::new("out").long("out").default_value("-")), |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 60 | ) |
| 61 | } |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 62 | |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 63 | fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> { |
| 64 | let mut opened_files = vec![]; |
| 65 | for path in matches.get_many::<String>(arg_name).unwrap_or_default() { |
| 66 | let file = Box::new(fs::File::open(path)?); |
| 67 | opened_files.push(Input { source: Source::File(path.to_string()), reader: file }); |
| 68 | } |
| 69 | Ok(opened_files) |
| 70 | } |
| 71 | |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame^] | 72 | fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> { |
| 73 | ensure!( |
| 74 | root.is_dir(), |
| 75 | "output directory {} does not exist or is not a directory", |
| 76 | root.display() |
| 77 | ); |
| 78 | let path = root.join(output_file.path.clone()); |
| 79 | let parent = path |
| 80 | .parent() |
| 81 | .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?; |
| 82 | fs::create_dir_all(parent)?; |
| 83 | let mut file = fs::File::create(path)?; |
| 84 | file.write_all(&output_file.contents)?; |
| 85 | Ok(()) |
| 86 | } |
| 87 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 88 | fn main() -> Result<()> { |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 89 | let matches = cli().get_matches(); |
| 90 | match matches.subcommand() { |
| 91 | Some(("create-cache", sub_matches)) => { |
Mårten Kongstad | 3095078 | 2023-05-09 13:31:29 +0200 | [diff] [blame] | 92 | let namespace = sub_matches.get_one::<String>("namespace").unwrap(); |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 93 | let declarations = open_zero_or_more_files(sub_matches, "declarations")?; |
| 94 | let values = open_zero_or_more_files(sub_matches, "values")?; |
| 95 | let cache = commands::create_cache(namespace, declarations, values)?; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 96 | let path = sub_matches.get_one::<String>("cache").unwrap(); |
| 97 | let file = fs::File::create(path)?; |
| 98 | cache.write_to_writer(file)?; |
| 99 | } |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 100 | Some(("create-java-lib", sub_matches)) => { |
| 101 | let path = sub_matches.get_one::<String>("cache").unwrap(); |
| 102 | let file = fs::File::open(path)?; |
| 103 | let cache = Cache::read_from_reader(file)?; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame^] | 104 | let dir = PathBuf::from(sub_matches.get_one::<String>("out").unwrap()); |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 105 | let generated_file = commands::generate_code(&cache).unwrap(); |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame^] | 106 | write_output_file_realtive_to_dir(&dir, &generated_file)?; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 107 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 108 | Some(("dump", sub_matches)) => { |
| 109 | let path = sub_matches.get_one::<String>("cache").unwrap(); |
| 110 | let file = fs::File::open(path)?; |
| 111 | let cache = Cache::read_from_reader(file)?; |
| 112 | let format = sub_matches.get_one("format").unwrap(); |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 113 | let output = commands::dump_cache(cache, *format)?; |
| 114 | let path = sub_matches.get_one::<String>("out").unwrap(); |
| 115 | let mut file: Box<dyn Write> = if path == "-" { |
| 116 | Box::new(io::stdout()) |
| 117 | } else { |
| 118 | Box::new(fs::File::create(path)?) |
| 119 | }; |
| 120 | file.write_all(&output)?; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 121 | } |
| 122 | _ => unreachable!(), |
| 123 | } |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 124 | Ok(()) |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 125 | } |