flowey_lib_common/
resolve_protoc.rs1use flowey::node::prelude::*;
7
8#[derive(Serialize, Deserialize)]
9pub struct ProtocPackage {
10 pub protoc_bin: PathBuf,
11 pub include_dir: PathBuf,
12}
13
14fn resolve_protoc_from_dir(
17 rt: &mut RustRuntimeServices<'_>,
18 base_dir: &Path,
19 make_executable: bool,
20) -> anyhow::Result<ProtocPackage> {
21 let protoc_bin = base_dir
22 .join("bin")
23 .join(rt.platform().binary("protoc"))
24 .absolute()?;
25
26 if !protoc_bin.exists() {
27 anyhow::bail!("protoc binary not found at {}", protoc_bin.display())
28 }
29
30 let protoc_bin_executable = protoc_bin.is_executable()?;
31 if !protoc_bin_executable && !make_executable {
32 anyhow::bail!(
33 "protoc binary at {} is not executable",
34 protoc_bin.display()
35 );
36 }
37
38 if make_executable {
39 protoc_bin.make_executable()?;
40 }
41
42 let include_dir = base_dir.join("include").absolute()?;
43 if !include_dir.exists() {
44 anyhow::bail!(
45 "protoc include directory not found at {}",
46 include_dir.display()
47 )
48 }
49
50 Ok(ProtocPackage {
51 protoc_bin,
52 include_dir,
53 })
54}
55
56flowey_config! {
57 pub struct Config {
59 pub version: Option<String>,
61 pub local_path: Option<ConfigVar<PathBuf>>,
63 }
64}
65
66flowey_request! {
67 pub enum Request {
68 Get(WriteVar<ProtocPackage>),
70 }
71}
72
73new_flow_node_with_config!(struct Node);
74
75impl FlowNodeWithConfig for Node {
76 type Request = Request;
77 type Config = Config;
78
79 fn imports(ctx: &mut ImportCtx<'_>) {
80 ctx.import::<crate::install_dist_pkg::Node>();
81 ctx.import::<crate::download_gh_release::Node>();
82 ctx.import::<crate::cache::Node>();
83 }
84
85 fn emit(
86 config: Config,
87 requests: Vec<Self::Request>,
88 ctx: &mut NodeCtx<'_>,
89 ) -> anyhow::Result<()> {
90 let version = config.version;
91 let local_path = config.local_path;
92 let mut get_reqs = Vec::new();
93
94 for req in requests {
95 match req {
96 Request::Get(v) => get_reqs.push(v),
97 }
98 }
99
100 if version.is_some() && local_path.is_some() {
101 anyhow::bail!("Cannot specify both version and local_path config");
102 }
103
104 if version.is_none() && local_path.is_none() {
105 anyhow::bail!("Must specify a version or local_path config");
106 }
107
108 if get_reqs.is_empty() {
111 return Ok(());
112 }
113
114 if let Some(local_path) = local_path {
115 ctx.emit_rust_step("use local protoc", |ctx| {
116 let get_reqs = get_reqs.claim(ctx);
117 let local_path = local_path.claim(ctx);
118 move |rt| {
119 let local_path = rt.read(local_path);
120 log::info!("using protoc from base path {}", local_path.display());
121
122 let pkg = resolve_protoc_from_dir(rt, &local_path, false)?;
125 rt.write_all(get_reqs, &pkg);
126
127 Ok(())
128 }
129 });
130
131 return Ok(());
132 }
133
134 let version = version.expect("local requests handled above");
135
136 let tag = format!("v{version}");
137 let file_name = format!(
138 "protoc-{}-{}.zip",
139 version,
140 match (ctx.platform(), ctx.arch()) {
141 (FlowPlatform::Windows, _) => "win64",
144 (FlowPlatform::Linux(_), FlowArch::X86_64) => "linux-x86_64",
145 (FlowPlatform::Linux(_), FlowArch::Aarch64) => "linux-aarch_64",
146 (FlowPlatform::MacOs, FlowArch::X86_64) => "osx-x86_64",
147 (FlowPlatform::MacOs, FlowArch::Aarch64) => "osx-aarch_64",
148 (platform, arch) => anyhow::bail!("unsupported platform {platform} {arch}"),
149 }
150 );
151
152 let protoc_zip = ctx.reqv(|v| crate::download_gh_release::Request {
153 repo_owner: "protocolbuffers".into(),
154 repo_name: "protobuf".into(),
155 needs_auth: false,
156 tag: tag.clone(),
157 file_name: file_name.clone(),
158 path: v,
159 });
160
161 let extract_zip_deps = crate::_util::extract::extract_zip_if_new_deps(ctx);
162 ctx.emit_rust_step("unpack protoc", |ctx| {
163 let extract_zip_deps = extract_zip_deps.clone().claim(ctx);
164 let get_reqs = get_reqs.claim(ctx);
165 let protoc_zip = protoc_zip.claim(ctx);
166 move |rt| {
167 let protoc_zip = rt.read(protoc_zip);
168
169 let extract_dir = crate::_util::extract::extract_zip_if_new(
170 rt,
171 extract_zip_deps,
172 &protoc_zip,
173 &tag,
174 )?;
175
176 let pkg = resolve_protoc_from_dir(rt, &extract_dir, true)?;
177 rt.write_all(get_reqs, &pkg);
178
179 Ok(())
180 }
181 });
182
183 Ok(())
184 }
185}