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_request! {
57 pub enum Request {
58 LocalPath(PathBuf),
60 Version(String),
62 Get(WriteVar<ProtocPackage>),
64 }
65}
66
67new_flow_node!(struct Node);
68
69impl FlowNode for Node {
70 type Request = Request;
71
72 fn imports(ctx: &mut ImportCtx<'_>) {
73 ctx.import::<crate::install_dist_pkg::Node>();
74 ctx.import::<crate::download_gh_release::Node>();
75 ctx.import::<crate::cache::Node>();
76 }
77
78 fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
79 let mut version = None;
80 let mut local_path = None;
81 let mut get_reqs = Vec::new();
82
83 for req in requests {
84 match req {
85 Request::LocalPath(path) => {
86 same_across_all_reqs("LocalPath", &mut local_path, path)?
87 }
88 Request::Version(v) => same_across_all_reqs("Version", &mut version, v)?,
89 Request::Get(v) => get_reqs.push(v),
90 }
91 }
92
93 if version.is_some() && local_path.is_some() {
94 anyhow::bail!("Cannot specify both Version and LocalPath requests");
95 }
96
97 if version.is_none() && local_path.is_none() {
98 anyhow::bail!("Must specify a Version or LocalPath request");
99 }
100
101 if get_reqs.is_empty() {
104 return Ok(());
105 }
106
107 if let Some(local_path) = local_path {
108 ctx.emit_rust_step("use local protoc", |ctx| {
109 let get_reqs = get_reqs.claim(ctx);
110 let local_path = local_path.clone();
111 move |rt| {
112 log::info!("using protoc from base path {}", local_path.display());
113
114 let pkg = resolve_protoc_from_dir(rt, &local_path, false)?;
117 rt.write_all(get_reqs, &pkg);
118
119 Ok(())
120 }
121 });
122
123 return Ok(());
124 }
125
126 let version = version.expect("local requests handled above");
127
128 let tag = format!("v{version}");
129 let file_name = format!(
130 "protoc-{}-{}.zip",
131 version,
132 match (ctx.platform(), ctx.arch()) {
133 (FlowPlatform::Windows, _) => "win64",
136 (FlowPlatform::Linux(_), FlowArch::X86_64) => "linux-x86_64",
137 (FlowPlatform::Linux(_), FlowArch::Aarch64) => "linux-aarch_64",
138 (FlowPlatform::MacOs, FlowArch::X86_64) => "osx-x86_64",
139 (FlowPlatform::MacOs, FlowArch::Aarch64) => "osx-aarch_64",
140 (platform, arch) => anyhow::bail!("unsupported platform {platform} {arch}"),
141 }
142 );
143
144 let protoc_zip = ctx.reqv(|v| crate::download_gh_release::Request {
145 repo_owner: "protocolbuffers".into(),
146 repo_name: "protobuf".into(),
147 needs_auth: false,
148 tag: tag.clone(),
149 file_name: file_name.clone(),
150 path: v,
151 });
152
153 let extract_zip_deps = crate::_util::extract::extract_zip_if_new_deps(ctx);
154 ctx.emit_rust_step("unpack protoc", |ctx| {
155 let extract_zip_deps = extract_zip_deps.clone().claim(ctx);
156 let get_reqs = get_reqs.claim(ctx);
157 let protoc_zip = protoc_zip.claim(ctx);
158 move |rt| {
159 let protoc_zip = rt.read(protoc_zip);
160
161 let extract_dir = crate::_util::extract::extract_zip_if_new(
162 rt,
163 extract_zip_deps,
164 &protoc_zip,
165 &tag,
166 )?;
167
168 let pkg = resolve_protoc_from_dir(rt, &extract_dir, true)?;
169 rt.write_all(get_reqs, &pkg);
170
171 Ok(())
172 }
173 });
174
175 Ok(())
176 }
177}