lib/jarbler/config.rb in jarbler-0.1.8 vs lib/jarbler/config.rb in jarbler-0.2.0

- old
+ new

@@ -1,11 +1,11 @@ require 'rubygems' require 'json' module Jarbler class Config - attr_accessor :jar_name, :includes, :excludes, :jruby_version, :executable, :executable_params + attr_accessor :jar_name, :includes, :excludes, :jruby_version, :executable, :executable_params, :compile_ruby_files CONFIG_FILE = 'config/jarble.rb' # create instance of Config class with defaults or from config file # Should be called from rails/ruby root directory def self.create @@ -17,14 +17,19 @@ unless config.class == Jarbler::Config Jarbler.debug "No valid config provided in #{CONFIG_FILE}! Using defaults." config = Config.new end config.define_jruby_version + # Replace .rb with .class if compile_ruby_files is true + config.executable = config.executable.sub(/\.rb$/, '.class') if config.compile_ruby_files + + config.validate_values config end def initialize + @compile_ruby_files = false @jar_name = File.basename(Dir.pwd) + '.jar' @includes = %w(app bin config config.ru db Gemfile Gemfile.lock lib log public Rakefile script vendor tmp) @excludes = %w(tmp/cache tmp/pids tmp/sockets vendor/bundle vendor/cache vendor/ruby) @jruby_version = nil # determined automatically at runtime @executable = 'bin/rails' @@ -34,10 +39,14 @@ end # Generate the template config file based on default values def create_config_file write_config_file("\ +# Compile the ruby files of the project to Java .class files with JRuby's ahead-of-time compiler +# the original ruby files are not included in the jar file, so source code is not visible +# config.compile_ruby_files = #{compile_ruby_files} + # Name of the generated jar file # config.jar_name = '#{jar_name}' # Application directories or files to include in the jar file # config.includes = #{includes} @@ -45,13 +54,13 @@ # Application directories or files to exclude from the jar file # config.excludes = #{excludes} # config.excludes << 'additional' -# Use certail jRuby version +# Use certain JRuby version # if not set (nil) then the version defined in .ruby-version -# if not jRuby version defined here or in .ruby-version then the latest available jRuby version is used +# if not JRuby version defined here or in .ruby-version then the latest available JRuby version is used # config.jruby_version = '9.2.3.0' # config.jruby_version = nil # The Ruby executable file to run, e.g. 'bin/rails' or 'bin/rake' # config.executable = '#{executable}' @@ -81,19 +90,19 @@ file.write("end\n") end puts "Jarbler: Created config file #{Dir.pwd}/#{CONFIG_FILE}" end - # define jRuby version if not set in config file + # define JRuby version if not set in config file def define_jruby_version unless @jruby_version # not defined in config file if File.exist?('.ruby-version') # read the file RAILS_ROOT/.ruby-version starting from char at position 6 to the end of the line self.jruby_version = File.read('.ruby-version')[6..20].strip - debug "jRuby version from .ruby-version file: #{jruby_version}" + debug "JRuby version from .ruby-version file: #{jruby_version}" else - # no .ruby-version file, use jRuby version of the latest Gem + # no .ruby-version file, use JRuby version of the latest Gem # Fetch the gem specification from Rubygems.org # search for the gem and get the JSON response response = Gem::SpecFetcher.fetcher.search_for_dependency(Gem::Dependency.new('jruby-jars')) # extract the versions from the response self.jruby_version = response&.first&.first&.first&.version&.to_s @@ -103,15 +112,25 @@ #lines = `#{command}` #raise "Command \"#{command}\" failed with return code #{$?} and output:\n#{lines}" unless $?.success? #jruby_jars_line = lines.match(/^jruby-jars \((.*)\)/) #raise "No jruby-jars gem found in rubygems.org!" unless jruby_jars_line #self.jruby_version = /\((.*?)\)/.match(jruby_jars_line.to_s)[1] - debug "jRuby version from latest jruby-jars gem: #{jruby_version}" + debug "JRuby version from latest jruby-jars gem: #{jruby_version}" end end end def debug(msg) puts msg if ENV['DEBUG'] + end + + def validate_values + raise "Invalid config value for jar name: #{jar_name}" unless jar_name =~ /\w+/ + raise "Invalid config value for executable: #{executable}" unless executable =~ /\w+/ + raise "Invalid config value for executable params: #{executable_params}" unless executable_params.is_a?(Array) + raise "Invalid config value for includes: #{includes}" unless includes.is_a?(Array) + raise "Invalid config value for excludes: #{excludes}" unless excludes.is_a?(Array) + raise "Invalid config value for compile_ruby_files: #{compile_ruby_files}" unless [true, false].include?(compile_ruby_files) + raise "compile_ruby_files = true is supported only with JRuby! Current runtime is '#{RUBY_ENGINE}'" if compile_ruby_files && (defined?(RUBY_ENGINE) && RUBY_ENGINE != 'jruby') end end end \ No newline at end of file