Skip to content

Commit

Permalink
Improves Gem dependency handling.
Browse files Browse the repository at this point in the history
Gem dependencies are now managed by bundler and
loaded in a central place.

Implements #111.
Also contains changes pertaining to #93.
  • Loading branch information
reitzig committed Feb 25, 2018
1 parent 8111f83 commit 1603968
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 106 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.idea/
Gemfile.lock
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'ruby-progressbar'
gem 'parallel'
gem 'listen'
25 changes: 25 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
GEM
remote: https://rubygems.org/
specs:
ffi (1.9.18)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
parallel (1.12.0)
rb-fsevent (0.10.2)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
ruby-progressbar (1.8.3)
ruby_dep (1.5.0)

PLATFORMS
ruby

DEPENDENCIES
listen
parallel
ruby-progressbar

BUNDLED WITH
1.15.4
4 changes: 3 additions & 1 deletion constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
# along with ltx2any. If not, see <http://www.gnu.org/licenses/>.

# TODO: move to a properties file?

NAME = 'ltx2any'.freeze
VERSION = '0.9b'.freeze
YEAR = '2018'.freeze
AUTHOR = 'Raphael Reitzig'.freeze
TMPSUFFIX = '_tmp'.freeze
HASHFILE = '.hashes'.freeze # relative to tmp directory
# TODO: move this constant to HashManager?

WORKDIR = Dir.pwd.freeze
BASEDIR = File.expand_path(__dir__).freeze
LIBDIR = 'lib'.freeze
EXTDIR = 'extensions'.freeze
ENGDIR = 'engines'.freeze
Expand Down
2 changes: 0 additions & 2 deletions lib/CliHelp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with ltx2any. If not, see <http://www.gnu.org/licenses/>.

require 'singleton'

class CliHelp
include Singleton

Expand Down
110 changes: 53 additions & 57 deletions lib/DependencyManager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with ltx2any. If not, see <http://www.gnu.org/licenses/>.

require 'rubygems'

class DependencyManager
private
@@dependencies = []
Expand All @@ -40,6 +38,7 @@ def self.more_recent(v1, v2)
def self.which(cmd)
raise ArgumentError.new("Argument not a string: #{cmd.inspect}") unless cmd.is_a?(String)
return nil if cmd.empty?

case RbConfig::CONFIG['host_os']
when /cygwin/
exts = nil
Expand All @@ -49,40 +48,40 @@ def self.which(cmd)
else
exts = nil
end
if cmd[File::SEPARATOR] or (File::ALT_SEPARATOR and cmd[File::ALT_SEPARATOR])
if cmd[File::SEPARATOR] || (File::ALT_SEPARATOR && cmd[File::ALT_SEPARATOR])
if exts
ext = File.extname(cmd)
if not ext.empty? and exts.any?{ |e| e.casecmp(ext).zero? } \
and File.file?(cmd) and File.executable?(cmd)
if (not ext.empty?) && exts.any?{ |e| e.casecmp(ext).zero? } \
&& File.file?(cmd) && File.executable?(cmd)
return File.absolute_path(cmd)
end
exts.each do |ext|
exe = "#{cmd}#{ext}"
return File.absolute_path(exe) if File.file?(exe) and File.executable?(exe)
return File.absolute_path(exe) if File.file?(exe) && File.executable?(exe)
end
else
return File.absolute_path(cmd) if File.file?(cmd) and File.executable?(cmd)
return File.absolute_path(cmd) if File.file?(cmd) && File.executable?(cmd)
end
else
paths = ENV['PATH']
paths = paths ? paths.split(File::PATH_SEPARATOR).select{ |e| File.directory?(e) } : []
if exts
ext = File.extname(cmd)
has_valid_ext = (not ext.empty? and exts.any?{ |e| e.casecmp(ext).zero? })
has_valid_ext = ((not ext.empty?) && exts.any?{ |e| e.casecmp(ext).zero? })
paths.unshift('.').each do |path|
if has_valid_ext
exe = File.join(path, "#{cmd}")
return File.absolute_path(exe) if File.file?(exe) and File.executable?(exe)
return File.absolute_path(exe) if File.file?(exe) && File.executable?(exe)
end
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return File.absolute_path(exe) if File.file?(exe) and File.executable?(exe)
return File.absolute_path(exe) if File.file?(exe) && File.executable?(exe)
end
end
else
paths.each do |path|
exe = File.join(path, cmd)
return File.absolute_path(exe) if File.file?(exe) and File.executable?(exe)
return File.absolute_path(exe) if File.file?(exe) && File.executable?(exe)
end
end
end
Expand All @@ -91,28 +90,26 @@ def self.which(cmd)

public


def self.add(dep)
if dep.kind_of?(Dependency)

def self.add(dep)
raise "Illegal parameter #{dep.to_s}" unless dep.is_a?(Dependency)

@@dependencies.push(dep)
else
raise "Illegal parameter #{dep.to_s}"
end
end


def self.list(type: :all, source: :all, relevance: :all)
@@dependencies.select { |d|
(type == :all || d.type == type || (type.kind_of?(Array) && type.include?(d.type)))\
&& (source == :all || d.source == source || (d.source.kind_of?(Array) && d.source.include?(source)))\
&& (relevance == :all || d.relevance == relevance || (relevance.kind_of?(Array) && relevance.include?(d.relevance)))
}
end


def self.to_s
@@dependencies.map { |d| d.to_s }.join("\n")
end


def self.list(type: :all, source: :all, relevance: :all)
@@dependencies.select { |d|
(type == :all || d.type == type || (type.is_a?(Array) && type.include?(d.type)))\
&& (source == :all || d.source == source || (d.source.is_a?(Array) && d.source.include?(source)))\
&& (relevance == :all || d.relevance == relevance || (relevance.is_a?(Array) && relevance.include?(d.relevance)))
}
end


def self.to_s
@@dependencies.map { |d| d.to_s }.join("\n")
end
end


Expand All @@ -123,66 +120,65 @@ class MissingDependencyError < StandardError; end

class Dependency
def initialize(name, type, source, relevance, reason = '', version = nil)
if ![:gem, :binary, :file].include?(type)
unless [:binary, :file].include?(type)
raise "Illegal dependency type #{type.to_s}"
elsif source != :core && (!source.kind_of?(Array) || ![:core, :extension, :engine, :logwriter].include?(source[0]) )
end

if source != :core && (!source.is_a?(Array) || ![:core, :extension, :engine, :logwriter].include?(source[0]) )
raise "Illegal source #{source.to_s}"
elsif ![:recommended, :essential].include?(relevance)
end

unless [:recommended, :essential].include?(relevance)
raise "Illegal relevance #{relevance.to_s}"
end
if type != :gem && !(version.nil? || version.empty?)

unless version.nil? || version.empty?
# Should not happen in production
# TODO: add version command to dependency?
puts 'Developer warning: versions of binaries and files are not checked!'
end

@name = name
@type = type
@source = source.kind_of?(Array) ? source : [source, '']
@source = source.is_a?(Array) ? source : [source, '']
@relevance = relevance
@reason = reason
@version = version
@available = nil

DependencyManager.add(self)
end

public

def available?
if @available.nil?
@available = case @type
when :gem
begin
gem "#{@name}"
require @name
true
rescue Gem::LoadError
false
end
@available =
case @type
when :binary
DependencyManager.which(@name) != nil
when :file
File.exists?(@name)
when :file
File.exist?(@name)
else
# Should not happen in production
raise "Illegal dependency type #{@type}"
end
end

@available
end

def to_s
"#{@type} #{@name} is #{@relevance} for #{@source.join(' ').strip}"
end

attr_reader :name, :type, :source, :relevance, :reason, :version

def relevance=(value)
if ![:recommended, :essential].include?(value)
unless [:recommended, :essential].include?(value)
raise "Illegal relevance #{value.to_s}"
end

@relevance = value
end
end
20 changes: 11 additions & 9 deletions lib/Extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with ltx2any. If not, see <http://www.gnu.org/licenses/>.

Dependency.new('parallel', :gem, [:core, 'Extension'], :recommended, 'Faster execution for some extensions', '>=1.9.0')

class Extension
@@list = {}
@@dependencies = DependencyManager.list(source: [:core, 'Extension'])
Expand Down Expand Up @@ -71,11 +69,6 @@ def each(hash, options = {}, &block)

# Wrap execution of many items
def self.execute_parts(jobs, when_done, &block)
if @@dependencies.all?(&:available?)
require 'system'
require 'parallel'
end

Parallel.map(jobs, finish: ->(_, _, _) { when_done.call }) do |job|
begin
block.call(job)
Expand All @@ -102,7 +95,13 @@ def self.run_all(time, output, log)
dependencies = DependencyManager.list(source: [:extension, e.name], relevance: :essential)
if dependencies.all?(&:available?)
progress, stop = output.start("#{e.name} running", e.job_size)
r = e.exec(time, progress)
r = begin
e.exec(time, progress)
rescue NotImplementedError
{ success: true,
messages: ['No execution code, need to overwrite!'],
log: 'No execution code, need to overwrite!' }
end
stop.call(r[:success] ? :success : :error)
log.add_messages(e.name, :extension, r[:messages], r[:log])
else
Expand All @@ -114,16 +113,19 @@ def self.run_all(time, output, log)

public

# @abstract
def do?(time)
false
end

# @abstract
def job_size
1
end

# @abstract
def exec(time, progress)
{ success: true, messages: ['No execution code, need to overwrite!'], log: 'No execution code, need to overwrite!' }
raise NotImplementedError
end

def to_s
Expand Down
4 changes: 0 additions & 4 deletions lib/FileListener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with ltx2any. If not, see <http://www.gnu.org/licenses/>.

require 'singleton'

Dependency.new('listen', :gem, [:core, 'FileListener'], :recommended, 'Listening to files for automatic recompilation.', '>=3.1.5')

ParameterManager.instance.addParameter(Parameter.new(
:daemon, 'd', Boolean, false, 'Re-compile automatically when files change.'))
ParameterManager.instance.addParameter(Parameter.new(
Expand Down
3 changes: 0 additions & 3 deletions lib/HashManager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with ltx2any. If not, see <http://www.gnu.org/licenses/>.

require 'digest'
require 'singleton'

# TODO: Document
class HashManager
include Singleton
Expand Down
2 changes: 0 additions & 2 deletions lib/Log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with ltx2any. If not, see <http://www.gnu.org/licenses/>.

require "#{File.dirname(__FILE__)}/LogMessage.rb"

# TODO: Document
class Log
def initialize
Expand Down
Loading

0 comments on commit 1603968

Please sign in to comment.