Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify Ruby files order through magic comments #3748

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -15,4 +15,6 @@ addons:
- gperf

env: MRUBY_CONFIG=travis_config.rb
script: "./minirake all test"
before_script:
- gem install minitest
script: "./minirake all test libtest"
5 changes: 5 additions & 0 deletions Rakefile
Expand Up @@ -117,6 +117,11 @@ task :all => depfiles do
end
end

desc "run ruby lib tests"
task :libtest do
sh "ruby -Ilib:test lib_test/*"
end

desc "run all mruby tests"
task :test => ["all"] do
MRuby.each_target do
Expand Down
47 changes: 47 additions & 0 deletions lib/mruby/build/rbfiles_sorter.rb
@@ -0,0 +1,47 @@
require 'tsort'

module MRuby
class RbfilesSorter
include TSort

TAG_RXP = /^#\s*require:\s*['"]([\w\.\/]+)["']\s*$/

def initialize(root)
@root = root
@files = Dir.glob("#{root}/**/*.rb").sort
@deps = {}
end

def sort
@files.each { |f| parse_deps(f) }
tsort
end

def tsort_each_child(node, &block)
@deps[node].each(&block)
end

def tsort_each_node(&block)
@deps.each_key(&block)
end

def parse_deps(file)
f = File.new(file)

deps_list = @deps[File.expand_path(file)] = []

f.each_line do |line|
# Skip blank lines
next if line =~ /^\s*$/
# All requires should be in the beginning of the file
break if line !~ TAG_RXP

dep = line.match(TAG_RXP)[1]

dep += ".rb" unless dep.end_with?(".rb")

deps_list << File.expand_path(dep, File.dirname(file))
end
end
end
end
4 changes: 3 additions & 1 deletion lib/mruby/gem.rb
Expand Up @@ -2,6 +2,7 @@
require 'forwardable'
require 'tsort'
require 'shellwords'
require 'mruby/build/rbfiles_sorter'

module MRuby
module Gem
Expand Down Expand Up @@ -58,7 +59,8 @@ def setup
end
@linker = LinkerConfig.new([], [], [], [], [])

@rbfiles = Dir.glob("#{@dir}/#{@mrblib_dir}/**/*.rb").sort
@rbfiles = RbfilesSorter.new("#{@dir}/#{@mrblib_dir}").sort

@objs = Dir.glob("#{@dir}/#{@objs_dir}/*.{c,cpp,cxx,cc,m,asm,s,S}").map do |f|
objfile(f.relative_path_from(@dir).to_s.pathmap("#{build_dir}/%X"))
end
Expand Down
28 changes: 28 additions & 0 deletions lib_test/rbfiles_sorter_test.rb
@@ -0,0 +1,28 @@
require "minitest/autorun"

require "mruby/build/rbfiles_sorter"

class TestRbfilesSorter < Minitest::Test
def test_sort
@sorter = MRuby::RbfilesSorter.new("#{__dir__}/support/rbfiles_sorter")
sorted = @sorter.sort

root = File.expand_path("#{__dir__}/support/rbfiles_sorter")

assert_equal "#{root}/stuff/y.rb", sorted[0]
assert_equal "#{root}/utils/x.rb", sorted[1]
assert_equal "#{root}/b.rb", sorted[2]
assert_equal "#{root}/a.rb", sorted[3]
end

def test_sort_without_requires
@sorter = MRuby::RbfilesSorter.new("#{__dir__}/support/rbfiles_sorter_2")
sorted = @sorter.sort

root = File.expand_path("#{__dir__}/support/rbfiles_sorter_2")

assert_equal "#{root}/0_x.rb", sorted[0]
assert_equal "#{root}/a.rb", sorted[1]
assert_equal "#{root}/c.rb", sorted[2]
end
end
6 changes: 6 additions & 0 deletions lib_test/support/rbfiles_sorter/a.rb
@@ -0,0 +1,6 @@

# require: "b"

# require: "utils/x.rb"

class A; end
5 changes: 5 additions & 0 deletions lib_test/support/rbfiles_sorter/b.rb
@@ -0,0 +1,5 @@
#require:'utils/x.rb'

module B; end

#require:'utils/y.rb'
1 change: 1 addition & 0 deletions lib_test/support/rbfiles_sorter/stuff/y.rb
@@ -0,0 +1 @@
module Y; end
3 changes: 3 additions & 0 deletions lib_test/support/rbfiles_sorter/utils/x.rb
@@ -0,0 +1,3 @@
# require: "../stuff/y.rb"

module X; end
Empty file.
Empty file.
Empty file.