1use crate::{
2 cmd::{cache::CacheSubcommands, generate::GenerateSubcommands, watch},
3 opts::{Forge, ForgeSubcommand},
4};
5use clap::{CommandFactory, Parser};
6use clap_complete::generate;
7use eyre::Result;
8use foundry_cli::utils;
9use foundry_common::shell;
10use foundry_evm::inspectors::cheatcodes::{ForgeContext, set_execution_context};
11
12pub fn run() -> Result<()> {
14 setup()?;
15
16 let args = Forge::parse();
17 args.global.init()?;
18
19 run_command(args)
20}
21
22pub fn setup() -> Result<()> {
24 utils::common_setup();
25 utils::subscriber();
26
27 Ok(())
28}
29
30pub fn run_command(args: Forge) -> Result<()> {
32 let context = match &args.cmd {
34 ForgeSubcommand::Test(_) => ForgeContext::Test,
35 ForgeSubcommand::Coverage(_) => ForgeContext::Coverage,
36 ForgeSubcommand::Snapshot(_) => ForgeContext::Snapshot,
37 ForgeSubcommand::Script(cmd) => {
38 if cmd.broadcast {
39 ForgeContext::ScriptBroadcast
40 } else if cmd.resume {
41 ForgeContext::ScriptResume
42 } else {
43 ForgeContext::ScriptDryRun
44 }
45 }
46 _ => ForgeContext::Unknown,
47 };
48 set_execution_context(context);
49
50 let global = &args.global;
51
52 match args.cmd {
54 ForgeSubcommand::Test(cmd) => {
55 if cmd.is_watch() {
56 global.block_on(watch::watch_test(cmd))
57 } else {
58 let silent = cmd.junit || shell::is_json();
59 let outcome = global.block_on(cmd.run())?;
60 outcome.ensure_ok(silent)
61 }
62 }
63 ForgeSubcommand::Script(cmd) => global.block_on(cmd.run_script()),
64 ForgeSubcommand::Coverage(cmd) => {
65 if cmd.is_watch() {
66 global.block_on(watch::watch_coverage(cmd))
67 } else {
68 global.block_on(cmd.run())
69 }
70 }
71 ForgeSubcommand::Bind(cmd) => cmd.run(),
72 ForgeSubcommand::Build(cmd) => {
73 if cmd.is_watch() {
74 global.block_on(watch::watch_build(cmd))
75 } else {
76 global.block_on(cmd.run()).map(drop)
77 }
78 }
79 ForgeSubcommand::VerifyContract(args) => global.block_on(args.run()),
80 ForgeSubcommand::VerifyCheck(args) => global.block_on(args.run()),
81 ForgeSubcommand::VerifyBytecode(cmd) => global.block_on(cmd.run()),
82 ForgeSubcommand::Clone(cmd) => global.block_on(cmd.run()),
83 ForgeSubcommand::Cache(cmd) => match cmd.sub {
84 CacheSubcommands::Clean(cmd) => cmd.run(),
85 CacheSubcommands::Ls(cmd) => cmd.run(),
86 },
87 ForgeSubcommand::Create(cmd) => global.block_on(cmd.run()),
88 ForgeSubcommand::Update(cmd) => cmd.run(),
89 ForgeSubcommand::Install(cmd) => global.block_on(cmd.run()),
90 ForgeSubcommand::Remove(cmd) => cmd.run(),
91 ForgeSubcommand::Remappings(cmd) => cmd.run(),
92 ForgeSubcommand::Init(cmd) => global.block_on(cmd.run()),
93 ForgeSubcommand::Completions { shell } => {
94 generate(shell, &mut Forge::command(), "forge", &mut std::io::stdout());
95 Ok(())
96 }
97 ForgeSubcommand::Clean { root } => {
98 let config = utils::load_config_with_root(root.as_deref())?;
99 let project = config.project()?;
100 config.cleanup(&project)?;
101 Ok(())
102 }
103 ForgeSubcommand::Snapshot(cmd) => {
104 if cmd.is_watch() {
105 global.block_on(watch::watch_gas_snapshot(cmd))
106 } else {
107 global.block_on(cmd.run())
108 }
109 }
110 ForgeSubcommand::Fmt(cmd) => {
111 if cmd.is_watch() {
112 global.block_on(watch::watch_fmt(cmd))
113 } else {
114 cmd.run()
115 }
116 }
117 ForgeSubcommand::Config(cmd) => cmd.run(),
118 ForgeSubcommand::Flatten(cmd) => cmd.run(),
119 ForgeSubcommand::Inspect(cmd) => cmd.run(),
120 ForgeSubcommand::Tree(cmd) => cmd.run(),
121 ForgeSubcommand::Geiger(cmd) => cmd.run(),
122 ForgeSubcommand::Doc(cmd) => {
123 if cmd.is_watch() {
124 global.block_on(watch::watch_doc(cmd))
125 } else {
126 global.block_on(cmd.run())?;
127 Ok(())
128 }
129 }
130 ForgeSubcommand::Selectors { command } => global.block_on(command.run()),
131 ForgeSubcommand::Generate(cmd) => match cmd.sub {
132 GenerateSubcommands::Test(cmd) => cmd.run(),
133 },
134 ForgeSubcommand::Compiler(cmd) => cmd.run(),
135 ForgeSubcommand::Soldeer(cmd) => global.block_on(cmd.run()),
136 ForgeSubcommand::Eip712(cmd) => cmd.run(),
137 ForgeSubcommand::BindJson(cmd) => cmd.run(),
138 ForgeSubcommand::Lint(cmd) => cmd.run(),
139 }
140}