@@ -51,6 +51,84 @@ def vendor_gem_cache(pkg)
51
51
JS ::VERSION
52
52
end
53
53
54
+ def build_ruby ( pkg , base_dir , pkg_dir , wasi_sdk , clean : false )
55
+ build_command = npm_pkg_build_command ( pkg )
56
+ # Skip if the package does not require building ruby
57
+ return unless build_command
58
+
59
+ js_gem_version = vendor_gem_cache ( pkg )
60
+
61
+ env = {
62
+ # Share ./build and ./rubies in the same workspace
63
+ "RUBY_WASM_ROOT" => base_dir
64
+ }
65
+ cwd = base_dir
66
+ if gemfile_path = pkg [ :gemfile ]
67
+ cwd = File . dirname ( gemfile_path )
68
+ else
69
+ # Explicitly disable rubygems integration since Bundler finds
70
+ # Gemfile in the repo root directory.
71
+ build_command . push "--disable-gems"
72
+ end
73
+ dist_dir = File . join ( pkg_dir , "dist" )
74
+ mkdir_p dist_dir
75
+ if pkg [ :target ] . start_with? ( "wasm32-unknown-wasi" )
76
+ if pkg [ :enable_component_model ]
77
+ component_path = File . join ( pkg_dir , "tmp" , "ruby.component.wasm" )
78
+ FileUtils . mkdir_p ( File . dirname ( component_path ) )
79
+
80
+ # Remove js gem from the ./bundle directory to force Bundler to re-install it
81
+ rm_rf FileList [ File . join ( pkg_dir , "bundle" , "**" , "js-#{ js_gem_version } " ) ]
82
+
83
+ Dir . chdir ( cwd ) do
84
+ sh env . merge ( "RUBY_WASM_EXPERIMENTAL_DYNAMIC_LINKING" => "1" ) ,
85
+ *build_command , *( clean ? [ "--clean" ] : [ ] ) ,
86
+ "-o" , component_path
87
+ end
88
+ sh "npx" , "jco" , "transpile" ,
89
+ "--no-wasi-shim" , "--instantiation" , "--valid-lifting-optimization" ,
90
+ component_path , "-o" , File . join ( dist_dir , "component" )
91
+ # ./component/package.json is required to be an ES module
92
+ File . write ( File . join ( dist_dir , "component" , "package.json" ) , '{ "type": "module" }' )
93
+ else
94
+ Dir . chdir ( cwd ) do
95
+ # uninstall js gem to re-install just-built js gem
96
+ sh "gem" , "uninstall" , "js" , "-v" , js_gem_version , "--force"
97
+ # install gems including js gem
98
+ sh "bundle" , "install"
99
+
100
+ sh env ,
101
+ "bundle" , "exec" ,
102
+ *build_command ,
103
+ "--no-stdlib" , "--remake" ,
104
+ *( clean ? [ "--clean" ] : [ ] ) ,
105
+ "-o" ,
106
+ File . join ( dist_dir , "ruby.wasm" )
107
+ sh env ,
108
+ "bundle" , "exec" ,
109
+ *build_command ,
110
+ "-o" ,
111
+ File . join ( dist_dir , "ruby.debug+stdlib.wasm" )
112
+ end
113
+
114
+ sh wasi_sdk . wasm_opt ,
115
+ "--strip-debug" ,
116
+ File . join ( dist_dir , "ruby.wasm" ) ,
117
+ "-o" ,
118
+ File . join ( dist_dir , "ruby.wasm" )
119
+ sh wasi_sdk . wasm_opt ,
120
+ "--strip-debug" ,
121
+ File . join ( dist_dir , "ruby.debug+stdlib.wasm" ) ,
122
+ "-o" ,
123
+ File . join ( dist_dir , "ruby+stdlib.wasm" )
124
+ end
125
+ elsif pkg [ :target ] == "wasm32-unknown-emscripten"
126
+ Dir . chdir ( cwd || base_dir ) do
127
+ sh env , *build_command , "-o" , "/dev/null"
128
+ end
129
+ end
130
+ end
131
+
54
132
namespace :npm do
55
133
NPM_PACKAGES . each do |pkg |
56
134
base_dir = Dir . pwd
@@ -59,86 +137,19 @@ namespace :npm do
59
137
namespace pkg [ :name ] do
60
138
desc "Build ruby for npm package #{ pkg [ :name ] } "
61
139
task "ruby" do
62
- build_command = npm_pkg_build_command ( pkg )
63
- # Skip if the package does not require building ruby
64
- next unless build_command
65
-
66
- js_gem_version = vendor_gem_cache ( pkg )
67
-
68
- env = {
69
- # Share ./build and ./rubies in the same workspace
70
- "RUBY_WASM_ROOT" => base_dir
71
- }
72
- cwd = base_dir
73
- if gemfile_path = pkg [ :gemfile ]
74
- cwd = File . dirname ( gemfile_path )
75
- else
76
- # Explicitly disable rubygems integration since Bundler finds
77
- # Gemfile in the repo root directory.
78
- build_command . push "--disable-gems"
79
- end
80
- dist_dir = File . join ( pkg_dir , "dist" )
81
- mkdir_p dist_dir
82
- if pkg [ :target ] . start_with? ( "wasm32-unknown-wasi" )
83
- if pkg [ :enable_component_model ]
84
- component_path = File . join ( pkg_dir , "tmp" , "ruby.component.wasm" )
85
- FileUtils . mkdir_p ( File . dirname ( component_path ) )
86
-
87
- # Remove js gem from the ./bundle directory to force Bundler to re-install it
88
- rm_rf FileList [ File . join ( pkg_dir , "bundle" , "**" , "js-#{ js_gem_version } " ) ]
89
-
90
- Dir . chdir ( cwd ) do
91
- sh env . merge ( "RUBY_WASM_EXPERIMENTAL_DYNAMIC_LINKING" => "1" ) ,
92
- *build_command , "-o" , component_path
93
- end
94
- sh "npx" , "jco" , "transpile" ,
95
- "--no-wasi-shim" , "--instantiation" , "--valid-lifting-optimization" ,
96
- component_path , "-o" , File . join ( dist_dir , "component" )
97
- # ./component/package.json is required to be an ES module
98
- File . write ( File . join ( dist_dir , "component" , "package.json" ) , '{ "type": "module" }' )
99
- else
100
- Dir . chdir ( cwd ) do
101
- # uninstall js gem to re-install just-built js gem
102
- sh "gem" , "uninstall" , "js" , "-v" , js_gem_version , "--force"
103
- # install gems including js gem
104
- sh "bundle" , "install"
105
-
106
- sh env ,
107
- "bundle" , "exec" ,
108
- *build_command ,
109
- "--no-stdlib" , "--remake" ,
110
- "-o" ,
111
- File . join ( dist_dir , "ruby.wasm" )
112
- sh env ,
113
- "bundle" , "exec" ,
114
- *build_command ,
115
- "-o" ,
116
- File . join ( dist_dir , "ruby.debug+stdlib.wasm" )
117
- end
118
-
119
- sh wasi_sdk . wasm_opt ,
120
- "--strip-debug" ,
121
- File . join ( dist_dir , "ruby.wasm" ) ,
122
- "-o" ,
123
- File . join ( dist_dir , "ruby.wasm" )
124
- sh wasi_sdk . wasm_opt ,
125
- "--strip-debug" ,
126
- File . join ( dist_dir , "ruby.debug+stdlib.wasm" ) ,
127
- "-o" ,
128
- File . join ( dist_dir , "ruby+stdlib.wasm" )
129
- end
130
- elsif pkg [ :target ] == "wasm32-unknown-emscripten"
131
- Dir . chdir ( cwd || base_dir ) do
132
- sh env , *build_command , "-o" , "/dev/null"
133
- end
134
- end
140
+ build_ruby ( pkg , base_dir , pkg_dir , wasi_sdk )
135
141
end
136
142
137
143
desc "Build npm package #{ pkg [ :name ] } "
138
144
task "build" => [ "ruby" ] do
139
145
sh tools , "npm run build" , chdir : pkg_dir
140
146
end
141
147
148
+ desc "Clean and build npm package #{ pkg [ :name ] } "
149
+ task "clean-build" do
150
+ build_ruby ( pkg , base_dir , pkg_dir , wasi_sdk , clean : true )
151
+ end
152
+
142
153
desc "Check npm package #{ pkg [ :name ] } "
143
154
task "check" do
144
155
sh "npm test" , chdir : pkg_dir
0 commit comments