|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use clap::{Parser, ValueEnum}; |
| 4 | +use common::{cmd::Cmd, logger, spinner::Spinner}; |
| 5 | +use config::EcosystemConfig; |
| 6 | +use strum::EnumIter; |
| 7 | +use xshell::{cmd, Shell}; |
| 8 | + |
| 9 | +use crate::messages::{ |
| 10 | + MSG_BUILDING_CONTRACTS, MSG_BUILDING_CONTRACTS_SUCCESS, MSG_BUILDING_L1_CONTRACTS_SPINNER, |
| 11 | + MSG_BUILDING_L2_CONTRACTS_SPINNER, MSG_BUILDING_SYSTEM_CONTRACTS_SPINNER, |
| 12 | + MSG_BUILD_L1_CONTRACTS_HELP, MSG_BUILD_L2_CONTRACTS_HELP, MSG_BUILD_SYSTEM_CONTRACTS_HELP, |
| 13 | + MSG_CONTRACTS_DEPS_SPINNER, MSG_NOTHING_TO_BUILD_MSG, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(Debug, Parser)] |
| 17 | +pub struct ContractsArgs { |
| 18 | + #[clap(long, alias = "l1", help = MSG_BUILD_L1_CONTRACTS_HELP, default_missing_value = "true", num_args = 0..=1)] |
| 19 | + pub l1_contracts: Option<bool>, |
| 20 | + #[clap(long, alias = "l2", help = MSG_BUILD_L2_CONTRACTS_HELP, default_missing_value = "true", num_args = 0..=1)] |
| 21 | + pub l2_contracts: Option<bool>, |
| 22 | + #[clap(long, alias = "sc", help = MSG_BUILD_SYSTEM_CONTRACTS_HELP, default_missing_value = "true", num_args = 0..=1)] |
| 23 | + pub system_contracts: Option<bool>, |
| 24 | +} |
| 25 | + |
| 26 | +impl ContractsArgs { |
| 27 | + fn contracts(&self) -> Vec<ContractType> { |
| 28 | + if self.l1_contracts.is_none() |
| 29 | + && self.l2_contracts.is_none() |
| 30 | + && self.system_contracts.is_none() |
| 31 | + { |
| 32 | + return vec![ |
| 33 | + ContractType::L1, |
| 34 | + ContractType::L2, |
| 35 | + ContractType::SystemContracts, |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + let mut contracts = vec![]; |
| 40 | + |
| 41 | + if self.l1_contracts.unwrap_or(false) { |
| 42 | + contracts.push(ContractType::L1); |
| 43 | + } |
| 44 | + if self.l2_contracts.unwrap_or(false) { |
| 45 | + contracts.push(ContractType::L2); |
| 46 | + } |
| 47 | + if self.system_contracts.unwrap_or(false) { |
| 48 | + contracts.push(ContractType::SystemContracts); |
| 49 | + } |
| 50 | + |
| 51 | + contracts |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, ValueEnum, EnumIter, strum::Display, PartialEq, Eq, Clone, Copy)] |
| 56 | +#[strum(serialize_all = "lowercase")] |
| 57 | +pub enum ContractType { |
| 58 | + L1, |
| 59 | + L2, |
| 60 | + SystemContracts, |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(Debug)] |
| 64 | +struct ContractBuilder { |
| 65 | + dir: PathBuf, |
| 66 | + cmd: String, |
| 67 | + msg: String, |
| 68 | +} |
| 69 | + |
| 70 | +impl ContractBuilder { |
| 71 | + fn new(ecosystem: &EcosystemConfig, contract_type: ContractType) -> Self { |
| 72 | + match contract_type { |
| 73 | + ContractType::L1 => Self { |
| 74 | + dir: ecosystem.path_to_foundry(), |
| 75 | + cmd: "forge build".to_string(), |
| 76 | + msg: MSG_BUILDING_L1_CONTRACTS_SPINNER.to_string(), |
| 77 | + }, |
| 78 | + ContractType::L2 => Self { |
| 79 | + dir: ecosystem.link_to_code.clone(), |
| 80 | + cmd: "yarn l2-contracts build".to_string(), |
| 81 | + msg: MSG_BUILDING_L2_CONTRACTS_SPINNER.to_string(), |
| 82 | + }, |
| 83 | + ContractType::SystemContracts => Self { |
| 84 | + dir: ecosystem.link_to_code.join("contracts"), |
| 85 | + cmd: "yarn sc build".to_string(), |
| 86 | + msg: MSG_BUILDING_SYSTEM_CONTRACTS_SPINNER.to_string(), |
| 87 | + }, |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + fn build(&self, shell: &Shell) -> anyhow::Result<()> { |
| 92 | + let spinner = Spinner::new(&self.msg); |
| 93 | + let _dir_guard = shell.push_dir(&self.dir); |
| 94 | + |
| 95 | + let mut args = self.cmd.split_whitespace().collect::<Vec<_>>(); |
| 96 | + let command = args.remove(0); // It's safe to unwrap here because we know that the vec is not empty |
| 97 | + let mut cmd = cmd!(shell, "{command}"); |
| 98 | + |
| 99 | + for arg in args { |
| 100 | + cmd = cmd.arg(arg); |
| 101 | + } |
| 102 | + |
| 103 | + Cmd::new(cmd).run()?; |
| 104 | + |
| 105 | + spinner.finish(); |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +pub fn run(shell: &Shell, args: ContractsArgs) -> anyhow::Result<()> { |
| 111 | + let contracts = args.contracts(); |
| 112 | + if contracts.is_empty() { |
| 113 | + logger::outro(MSG_NOTHING_TO_BUILD_MSG); |
| 114 | + return Ok(()); |
| 115 | + } |
| 116 | + |
| 117 | + logger::info(MSG_BUILDING_CONTRACTS); |
| 118 | + |
| 119 | + let ecosystem = EcosystemConfig::from_file(shell)?; |
| 120 | + let link_to_code = ecosystem.link_to_code.clone(); |
| 121 | + |
| 122 | + let spinner = Spinner::new(MSG_CONTRACTS_DEPS_SPINNER); |
| 123 | + let _dir_guard = shell.push_dir(&link_to_code); |
| 124 | + Cmd::new(cmd!(shell, "yarn install")).run()?; |
| 125 | + spinner.finish(); |
| 126 | + |
| 127 | + contracts |
| 128 | + .iter() |
| 129 | + .map(|contract| ContractBuilder::new(&ecosystem, *contract)) |
| 130 | + .try_for_each(|builder| builder.build(shell))?; |
| 131 | + |
| 132 | + logger::outro(MSG_BUILDING_CONTRACTS_SUCCESS); |
| 133 | + |
| 134 | + Ok(()) |
| 135 | +} |
0 commit comments