blob: b9ce25902a45617bd22e500d8856755a4ab2d431 [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 Kongstadd42eeeb2023-05-12 10:01:00 +020019use anyhow::{anyhow, ensure, 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 Kongstadd42eeeb2023-05-12 10:01:00 +020024use std::path::{Path, PathBuf};
Mårten Kongstad867a3492023-04-25 15:06:30 +020025
Mårten Kongstadbb520722023-04-26 13:16:41 +020026mod aconfig;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020027mod cache;
Zhi Doueb744892023-05-10 04:02:33 +000028mod codegen_java;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020029mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020030mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020031
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020032use crate::cache::Cache;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +020033use commands::{Input, OutputFile, Source};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020034
35fn cli() -> Command {
36 Command::new("aconfig")
37 .subcommand_required(true)
38 .subcommand(
39 Command::new("create-cache")
Mårten Kongstad30950782023-05-09 13:31:29 +020040 .arg(Arg::new("namespace").long("namespace").required(true))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020041 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
42 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020043 .arg(Arg::new("cache").long("cache").required(true)),
44 )
45 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000046 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 Kongstada1029092023-05-08 11:51:59 +020051 Command::new("dump")
52 .arg(Arg::new("cache").long("cache").required(true))
53 .arg(
54 Arg::new("format")
55 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +020056 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +020057 .default_value("text"),
58 )
59 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020060 )
61}
Mårten Kongstad867a3492023-04-25 15:06:30 +020062
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020063fn 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 Kongstadd42eeeb2023-05-12 10:01:00 +020072fn 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 Kongstadbb520722023-04-26 13:16:41 +020088fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020089 let matches = cli().get_matches();
90 match matches.subcommand() {
91 Some(("create-cache", sub_matches)) => {
Mårten Kongstad30950782023-05-09 13:31:29 +020092 let namespace = sub_matches.get_one::<String>("namespace").unwrap();
Mårten Kongstadfa23d292023-05-11 14:47:02 +020093 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 Kongstad4d2b4b02023-04-27 16:05:58 +020096 let path = sub_matches.get_one::<String>("cache").unwrap();
97 let file = fs::File::create(path)?;
98 cache.write_to_writer(file)?;
99 }
Zhi Doueb744892023-05-10 04:02:33 +0000100 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 Kongstadd42eeeb2023-05-12 10:01:00 +0200104 let dir = PathBuf::from(sub_matches.get_one::<String>("out").unwrap());
Zhi Doueb744892023-05-10 04:02:33 +0000105 let generated_file = commands::generate_code(&cache).unwrap();
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200106 write_output_file_realtive_to_dir(&dir, &generated_file)?;
Zhi Doueb744892023-05-10 04:02:33 +0000107 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200108 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 Kongstada1029092023-05-08 11:51:59 +0200113 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 Kongstad4d2b4b02023-04-27 16:05:58 +0200121 }
122 _ => unreachable!(),
123 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200124 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200125}