11# frozen_string_literal: true
22
3- require " bundler/gem_tasks"
4- require " rubygems/package_task"
5- require " rake/extensiontask"
6- require " rake_compiler_dock"
7- require " yaml"
8-
9- cross_platforms = [
10- " aarch64-linux-gnu" ,
11- " x86_64-linux-gnu" ,
12- " arm64-darwin" ,
13- " x86_64-darwin"
3+ require ' bundler/gem_tasks'
4+ require ' rubygems/package_task'
5+ require ' rake/extensiontask'
6+ require ' rake_compiler_dock'
7+ require ' yaml'
8+
9+ cross_platforms = %w [
10+ aarch64-linux-gnu
11+ x86_64-linux-gnu
12+ arm64-darwin
13+ x86_64-darwin
1414]
1515
16- RakeCompilerDock . set_ruby_cc_version ( " ~> 3.1" )
16+ RakeCompilerDock . set_ruby_cc_version ( ' ~> 3.1' )
1717
1818# Gem::PackageTask.new(CHDB_SPEC).define # packaged_tarball version of the gem for platform=ruby
19- task "package" => cross_platforms . map { |p | "gem:#{ p } " } # "package" task for all the native platforms
20-
21- module CHDBDependency
22- class << self
23- def setup
24- dependencies = YAML . load_file ( File . join ( __dir__ , ".." , "dependencies.yml" ) , symbolize_names : true )
25- chdb_info = dependencies [ :chdb ]
26- version = chdb_info [ :version ]
27-
28- cross_platforms . each do |platform |
29- platform_key = platform . gsub ( /-/ , '_' ) . to_sym
30- next unless chdb_info [ :platforms ] [ platform_key ]
31-
32- download_and_extract ( platform , version )
33- end
34- end
35-
36- private
37-
38- def download_and_extract ( platform , version )
39- file_name = case platform
40- when 'aarch64-linux-gnu' then 'linux-aarch64-libchdb.tar.gz'
41- when 'x86_64-linux-gnu' then 'linux-x86_64-libchdb.tar.gz'
42- when 'arm64-darwin' then 'macos-arm64-libchdb.tar.gz'
43- when 'x86_64-darwin' then 'macos-x86_64-libchdb.tar.gz'
44- end
45-
46- url = "https://github.com/chdb-io/chdb/releases/download/v#{ version } /#{ file_name } "
47-
48- archive_dir = File . join ( "ports" , "archives" )
49- FileUtils . mkdir_p ( archive_dir )
50-
51- tarball = File . join ( archive_dir , name )
52- unless File . exist? ( tarball )
53- puts "Downloading #{ name } ..."
54- URI . open ( url ) do |remote |
55- IO . copy_stream ( remote , tarball )
56- end
57- end
58-
59- tmp_dir = File . join ( archive_dir , "tmp_chdb" )
60- FileUtils . rm_rf ( tmp_dir )
61- FileUtils . mkdir_p ( tmp_dir )
62-
63- system ( "tar xzf #{ tarball } -C #{ tmp_dir } " )
64-
65- ext_chdb_path = File . expand_path ( "ext/chdb" , __dir__ )
66- [ %w[ include *.h ] , %w[ lib *.so ] , %w[ lib *.dylib ] ] . each do |( src_dir , pattern ) |
67- src = File . join ( tmp_dir , src_dir , pattern )
68- dest = File . join ( ext_chdb_path , src_dir )
69- FileUtils . mkdir_p ( dest )
70- FileUtils . cp_r ( Dir . glob ( src ) , dest , remove_destination : true )
71- end
72-
73- # 清理临时目录
74- FileUtils . rm_rf ( tmp_dir )
75- end
76- end
77- end
19+ # 'package' task for all the native platforms
20+ task 'package' => cross_platforms . map { |p | "gem:#{ p } " }
7821
7922def gem_build_path
80- File . join ( " pkg" , CHDB_SPEC . full_name )
23+ File . join ( ' pkg' , CHDB_SPEC . full_name )
8124end
8225
8326def add_file_to_gem ( relative_source_path )
84- if relative_source_path . nil? || !File . exist? ( relative_source_path )
85- raise "Cannot find file '#{ relative_source_path } '"
86- end
27+ raise "Cannot find file '#{ relative_source_path } '" if relative_source_path . nil? || !File . exist? ( relative_source_path )
8728
8829 dest_path = File . join ( gem_build_path , relative_source_path )
8930 dest_dir = File . dirname ( dest_path )
@@ -96,72 +37,71 @@ def add_file_to_gem(relative_source_path)
9637end
9738
9839task gem_build_path do
99- dependencies = YAML . load_file ( File . join ( __dir__ , ".." , " dependencies.yml" ) , symbolize_names : true )
40+ dependencies = YAML . load_file ( File . join ( __dir__ , '..' , ' dependencies.yml' ) , symbolize_names : true )
10041 sqlite_tarball = File . basename ( dependencies [ :sqlite3 ] [ :files ] . first [ :url ] )
101- archive = Dir . glob ( File . join ( " ports" , " archives" , sqlite_tarball ) ) . first
42+ archive = Dir . glob ( File . join ( ' ports' , ' archives' , sqlite_tarball ) ) . first
10243 add_file_to_gem ( archive )
10344end
10445
105- Rake ::ExtensionTask . new ( " chdb_native" , CHDB_SPEC ) do |ext |
106- ext . ext_dir = " ext/chdb"
107- ext . lib_dir = " lib/chdb"
46+ Rake ::ExtensionTask . new ( ' chdb_native' , CHDB_SPEC ) do |ext |
47+ ext . ext_dir = ' ext/chdb'
48+ ext . lib_dir = ' lib/chdb'
10849 ext . cross_compile = true
10950 ext . cross_platform = cross_platforms
110- ext . cross_config_options << "--enable-cross-build" # so extconf.rb knows we're cross-compiling
111-
112- ext . prerequisites << :download_chdb_deps
51+ # so extconf.rb knows we're cross-compiling
52+ ext . cross_config_options << '--enable-cross-build'
11353end
11454
115- namespace " gem" do
55+ namespace ' gem' do
11656 cross_platforms . each do |platform |
11757 desc "build native gem for #{ platform } "
11858 task platform do
119- RakeCompilerDock . sh ( <<~EOF , platform : platform , verbose : true )
59+ RakeCompilerDock . sh ( <<~COMMAND_END , platform : platform , verbose : true )
12060 gem install bundler --no-document &&
12161 bundle &&
12262 bundle exec rake gem:#{ platform } :buildit
123- EOF
63+ COMMAND_END
12464 end
12565
12666 namespace platform do
12767 # this runs in the rake-compiler-dock docker container
128- task " buildit" do
68+ task ' buildit' do
12969 # use Task#invoke because the pkg/*gem task is defined at runtime
13070 Rake ::Task [ "native:#{ platform } " ] . invoke
13171 Rake ::Task [ "pkg/#{ CHDB_SPEC . full_name } -#{ Gem ::Platform . new ( platform ) } .gem" ] . invoke
13272 end
13373 end
13474 end
13575
136- desc " build native gem for all platforms"
137- task " all" => [ cross_platforms , " gem" ] . flatten
76+ desc ' build native gem for all platforms'
77+ task ' all' => [ cross_platforms , ' gem' ] . flatten
13878end
13979
140- desc " Temporarily set VERSION to a unique timestamp"
141- task " set-version-to-timestamp" do
80+ desc ' Temporarily set VERSION to a unique timestamp'
81+ task ' set-version-to-timestamp' do
14282 # this task is used by bin/test-gem-build
14383 # to test building, packaging, and installing a precompiled gem
14484 version_constant_re = /^\s *VERSION\s *=\s *["'](.*)["']$/
14585
146- version_file_path = File . join ( __dir__ , " ../lib/chdb/version.rb" )
86+ version_file_path = File . join ( __dir__ , ' ../lib/chdb/version.rb' )
14787 version_file_contents = File . read ( version_file_path )
14888
14989 current_version_string = version_constant_re . match ( version_file_contents ) [ 1 ]
15090 current_version = Gem ::Version . new ( current_version_string )
15191
152- fake_version = Gem ::Version . new ( format ( "%s.test.%s" , current_version . bump , Time . now . strftime ( "%Y.%m%d.%H%M" ) ) )
92+ bumped_version = current_version . bump
93+ timestamp = Time . now . strftime ( '%Y.%m%d.%H%M' )
94+ fake_version = Gem ::Version . new ( format ( '%<bumped_version>s.test.%<timestamp>s' ,
95+ bumped_version : bumped_version ,
96+ timestamp : timestamp ) )
15397
15498 unless version_file_contents . gsub! ( version_constant_re , " VERSION = \" #{ fake_version } \" " )
155- raise ( " Could not hack the VERSION constant" )
99+ raise ( ' Could not hack the VERSION constant' )
156100 end
157101
158102 File . write ( version_file_path , version_file_contents )
159103
160104 puts "NOTE: wrote version as \" #{ fake_version } \" "
161105end
162106
163- CLEAN . add ( "{ext,lib}/**/*.{o,so}" , "pkg" , "ext/chdb/{include,lib}" )
164-
165- task :download_chdb_deps do
166- CHDBDependency . setup
167- end
107+ CLEAN . add ( '{ext,lib}/**/*.{o,so}' , 'pkg' , 'ext/chdb/{include,lib}' )
0 commit comments