Skip to content

Commit 0d0c0fc

Browse files
committed
Allow bsb to find bs-platform via env variables
1 parent 63bb690 commit 0d0c0fc

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

jscomp/bsb/bsb_pkg.ml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,65 @@ let resolve_bs_package ~cwd (package : t) =
101101
end;
102102
x
103103

104+
(* Some package managers will implement "postinstall" caches, that do not
105+
* keep their build artifacts in the local node_modules. Similar to
106+
* npm_config_prefix, bs_custom_resolution allows these to specify the
107+
* exact location of build cache, but on a per-package basis. Implemented as
108+
* environment lookup to avoid invasive changes to bsconfig and mandates. *)
109+
let custom_resolution =
110+
match Sys.getenv "bs_custom_resolution" with
111+
| exception Not_found -> false
112+
| "true" -> true
113+
| _ -> false
104114

115+
let regex_at = Str.regexp "@"
116+
let regex_unders = Str.regexp "_+"
117+
let regex_slash = Str.regexp "\\/"
118+
let regex_dot = Str.regexp "\\."
119+
let regex_hyphen = Str.regexp "-"
120+
let pkg_name_as_variable pkg =
121+
Bsb_pkg_types.to_string pkg
122+
|> Str.replace_first regex_at ""
123+
|> Str.global_replace regex_unders "\\0_"
124+
|> Str.global_replace regex_slash "__slash__"
125+
|> Str.global_replace regex_dot "__dot__"
126+
|> Str.global_replace regex_hyphen "_"
127+
128+
let resolve_bs_package ~cwd (pkg : t) =
129+
if custom_resolution then
130+
begin
131+
Bsb_log.info "@{<info>Using Custom Resolution@}@.";
132+
let custom_pkg_loc = pkg_name_as_variable pkg ^ "__install" in
133+
match Sys.getenv custom_pkg_loc with
134+
| exception Not_found ->
135+
begin
136+
Bsb_log.error
137+
"@{<error>Custom resolution of package %s does not exist in var %s @}@."
138+
(Bsb_pkg_types.to_string pkg)
139+
custom_pkg_loc;
140+
Bsb_exception.package_not_found ~pkg ~json:None
141+
end
142+
| path when not (Sys.file_exists path) ->
143+
begin
144+
Bsb_log.error
145+
"@{<error>Custom resolution of package %s does not exist on disk: %s=%s @}@."
146+
(Bsb_pkg_types.to_string pkg)
147+
custom_pkg_loc
148+
path;
149+
Bsb_exception.package_not_found ~pkg ~json:None
150+
end
151+
| path ->
152+
begin
153+
Bsb_log.info
154+
"@{<info>Custom Resolution of package %s in var %s found at %s@}@."
155+
(Bsb_pkg_types.to_string pkg)
156+
custom_pkg_loc
157+
path;
158+
path
159+
end
160+
end
161+
else
162+
resolve_bs_package ~cwd pkg
105163

106164

107165
(** The package does not need to be a bspackage

lib/4.02.3/bsb.ml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8567,6 +8567,65 @@ let resolve_bs_package ~cwd (package : t) =
85678567
end;
85688568
x
85698569

8570+
(* Some package managers will implement "postinstall" caches, that do not
8571+
* keep their build artifacts in the local node_modules. Similar to
8572+
* npm_config_prefix, bs_custom_resolution allows these to specify the
8573+
* exact location of build cache, but on a per-package basis. Implemented as
8574+
* environment lookup to avoid invasive changes to bsconfig and mandates. *)
8575+
let custom_resolution =
8576+
match Sys.getenv "bs_custom_resolution" with
8577+
| exception Not_found -> false
8578+
| "true" -> true
8579+
| _ -> false
8580+
8581+
let regex_at = Str.regexp "@"
8582+
let regex_unders = Str.regexp "_+"
8583+
let regex_slash = Str.regexp "\\/"
8584+
let regex_dot = Str.regexp "\\."
8585+
let regex_hyphen = Str.regexp "-"
8586+
let pkg_name_as_variable pkg =
8587+
Bsb_pkg_types.to_string pkg
8588+
|> Str.replace_first regex_at ""
8589+
|> Str.global_replace regex_unders "\\0_"
8590+
|> Str.global_replace regex_slash "__slash__"
8591+
|> Str.global_replace regex_dot "__dot__"
8592+
|> Str.global_replace regex_hyphen "_"
8593+
8594+
let resolve_bs_package ~cwd (pkg : t) =
8595+
if custom_resolution then
8596+
begin
8597+
Bsb_log.info "@{<info>Using Custom Resolution@}@.";
8598+
let custom_pkg_loc = pkg_name_as_variable pkg ^ "__install" in
8599+
match Sys.getenv custom_pkg_loc with
8600+
| exception Not_found ->
8601+
begin
8602+
Bsb_log.error
8603+
"@{<error>Custom resolution of package %s does not exist in var %s @}@."
8604+
(Bsb_pkg_types.to_string pkg)
8605+
custom_pkg_loc;
8606+
Bsb_exception.package_not_found ~pkg ~json:None
8607+
end
8608+
| path when not (Sys.file_exists path) ->
8609+
begin
8610+
Bsb_log.error
8611+
"@{<error>Custom resolution of package %s does not exist on disk: %s=%s @}@."
8612+
(Bsb_pkg_types.to_string pkg)
8613+
custom_pkg_loc
8614+
path;
8615+
Bsb_exception.package_not_found ~pkg ~json:None
8616+
end
8617+
| path ->
8618+
begin
8619+
Bsb_log.info
8620+
"@{<info>Custom Resolution of package %s in var %s found at %s@}@."
8621+
(Bsb_pkg_types.to_string pkg)
8622+
custom_pkg_loc
8623+
path;
8624+
path
8625+
end
8626+
end
8627+
else
8628+
resolve_bs_package ~cwd pkg
85708629

85718630

85728631

lib/4.06.1/bsb.ml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8537,6 +8537,65 @@ let resolve_bs_package ~cwd (package : t) =
85378537
end;
85388538
x
85398539

8540+
(* Some package managers will implement "postinstall" caches, that do not
8541+
* keep their build artifacts in the local node_modules. Similar to
8542+
* npm_config_prefix, bs_custom_resolution allows these to specify the
8543+
* exact location of build cache, but on a per-package basis. Implemented as
8544+
* environment lookup to avoid invasive changes to bsconfig and mandates. *)
8545+
let custom_resolution =
8546+
match Sys.getenv "bs_custom_resolution" with
8547+
| exception Not_found -> false
8548+
| "true" -> true
8549+
| _ -> false
8550+
8551+
let regex_at = Str.regexp "@"
8552+
let regex_unders = Str.regexp "_+"
8553+
let regex_slash = Str.regexp "\\/"
8554+
let regex_dot = Str.regexp "\\."
8555+
let regex_hyphen = Str.regexp "-"
8556+
let pkg_name_as_variable pkg =
8557+
Bsb_pkg_types.to_string pkg
8558+
|> Str.replace_first regex_at ""
8559+
|> Str.global_replace regex_unders "\\0_"
8560+
|> Str.global_replace regex_slash "__slash__"
8561+
|> Str.global_replace regex_dot "__dot__"
8562+
|> Str.global_replace regex_hyphen "_"
8563+
8564+
let resolve_bs_package ~cwd (pkg : t) =
8565+
if custom_resolution then
8566+
begin
8567+
Bsb_log.info "@{<info>Using Custom Resolution@}@.";
8568+
let custom_pkg_loc = pkg_name_as_variable pkg ^ "__install" in
8569+
match Sys.getenv custom_pkg_loc with
8570+
| exception Not_found ->
8571+
begin
8572+
Bsb_log.error
8573+
"@{<error>Custom resolution of package %s does not exist in var %s @}@."
8574+
(Bsb_pkg_types.to_string pkg)
8575+
custom_pkg_loc;
8576+
Bsb_exception.package_not_found ~pkg ~json:None
8577+
end
8578+
| path when not (Sys.file_exists path) ->
8579+
begin
8580+
Bsb_log.error
8581+
"@{<error>Custom resolution of package %s does not exist on disk: %s=%s @}@."
8582+
(Bsb_pkg_types.to_string pkg)
8583+
custom_pkg_loc
8584+
path;
8585+
Bsb_exception.package_not_found ~pkg ~json:None
8586+
end
8587+
| path ->
8588+
begin
8589+
Bsb_log.info
8590+
"@{<info>Custom Resolution of package %s in var %s found at %s@}@."
8591+
(Bsb_pkg_types.to_string pkg)
8592+
custom_pkg_loc
8593+
path;
8594+
path
8595+
end
8596+
end
8597+
else
8598+
resolve_bs_package ~cwd pkg
85408599

85418600

85428601

0 commit comments

Comments
 (0)