Skip to content

Commit

Permalink
Move rbfiles_sorter to lib/; add libtest task
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Aug 8, 2017
1 parent 05112dd commit e41ee66
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -15,4 +15,4 @@ addons:
- gperf

env: MRUBY_CONFIG=travis_config.rb
script: "./minirake all test"
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
2 changes: 1 addition & 1 deletion lib/mruby/gem.rb
Expand Up @@ -2,7 +2,7 @@
require 'forwardable'
require 'tsort'
require 'shellwords'
require_relative 'utils/rbfiles_sorter'
require 'mruby/build/rbfiles_sorter'

module MRuby
module Gem
Expand Down
@@ -1,13 +1,13 @@
require "minitest/autorun"

require_relative "../utils/rbfiles_sorter"
require "mruby/build/rbfiles_sorter"

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

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

assert_equal "#{root}/stuff/y.rb", sorted[0]
assert_equal "#{root}/utils/x.rb", sorted[1]
Expand All @@ -16,10 +16,10 @@ def test_sort
end

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

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

assert_equal "#{root}/0_x.rb", sorted[0]
assert_equal "#{root}/a.rb", sorted[1]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 0 additions & 45 deletions tasks/utils/rbfiles_sorter.rb

This file was deleted.

0 comments on commit e41ee66

Please sign in to comment.