Skip to content

Commit

Permalink
rubocop is the law 🚔
Browse files Browse the repository at this point in the history
  • Loading branch information
mguymon committed Nov 15, 2015
1 parent 6a5b8ad commit 40bf494
Show file tree
Hide file tree
Showing 24 changed files with 537 additions and 747 deletions.
5 changes: 2 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in lock_jar.gemspec
gemspec


gem "codeclimate-test-reporter", group: :test, require: nil
gem 'codeclimate-test-reporter', group: :test, require: nil

group :development do
gem 'guard-rspec', :require => false
gem 'guard-rspec', require: false
gem 'pry'
gem 'yard'
gem 'rubocop'
Expand Down
5 changes: 2 additions & 3 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { 'spec' }
end

28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ JRuby is natively supported. Ruby 1.9.3 and 2.1 uses [Rjb](http://rjb.rubyforge.
A Jarfile is a simple file using a Ruby DSL for defining a project's dependencies using the following
methods:

* **local_repo( path )**: Set the local Maven repository, this were dependencies are downloaded to.
* **remote_repo( url )**: Add additional url of remote Maven repository.
* **group( groups )**: Set the group for nested jar or pom. A single or Array of groups can be set.
* **jar( notations, opts = {} )**: Add Jar dependency in artifact notation, artifact:group:version as the bare minimum. A single or Array of notations can be passed. Default group is _default_, can be specified by setting _opts = { :group => ['group_name'] }_
* **local( path )**: Add a local path to a Jar
* **pom( pom_path, opts = {} )**: Add a local Maven pom, default is to load dependencies for `runtime` and `compile` scopes. To select the scopes to be loaded from the pom, set the _opts = { :scopes => ['test'] }_
* **local_repo(path)**: Set the local Maven repository, this were dependencies are downloaded to.
* **remote_repo(url)**: Add additional url of remote Maven repository.
* **group(groups)**: Set the group for nested jar or pom. A single or Array of groups can be set.
* **jar(notations, opts = {})**: Add Jar dependency in artifact notation, artifact:group:version as the bare minimum. A single or Array of notations can be passed. Default group is _default_, can be specified by setting _opts = { :group => ['group_name'] }_
* **local(path)**: Add a local path to a Jar
* **pom(pom_path, opts = {})**: Add a local Maven pom, default is to load dependencies for `runtime` and `compile` scopes. To select the scopes to be loaded from the pom, set the _opts = { :scopes => ['test'] }_
* **without_default_maven_repo**: Do not use the default maven repo.

#### Example Jarfile
Expand All @@ -56,7 +56,7 @@ methods:

### Resolving dependencies

**LockJar.lock( *args )**: Using a Jarfile, creates a lock file. Depending on the type of arg, a different configuration is set.
**LockJar.lock(*args)**: Using a Jarfile, creates a lock file. Depending on the type of arg, a different configuration is set.
* _[String]_ will set the Jarfile path, e.g. `'/somewhere/Jarfile.different'`. Default jarfile is `'Jarfile'`
* _[Hash]_ will set the options, e.g. `{ :local_repo => 'path' }`
* **:download** _[Boolean]_ if true, will download jars to local repo. Defaults to true.
Expand Down Expand Up @@ -193,7 +193,7 @@ You can skip the _Jarfile_ and _Jarfile.lock_ to directly play with dependencies

Since you skipped the locking part, mostly likely you will need to resolve the dependences in the block, just pass the _:resolve => true_ option to enable dependency resolution (also works for _LockJar.list_).

LockJar.load( :resolve => true ) do
LockJar.load(:resolve => true) do
jar 'org.eclipse.jetty:example-jetty-embedded:jar:8.1.2.v20120308'
end

Expand Down Expand Up @@ -234,9 +234,9 @@ Rakefile with default to install Jars using LockJar:
require 'lock_jar'

# get jarfile relative the gem dir
lockfile = File.expand_path( "../Jarfile.lock", __FILE__ )
lockfile = File.expand_path("../Jarfile.lock", __FILE__)

LockJar.install( lockfile )
LockJar.install(lockfile)
end

#### Work around for Rakefile default
Expand All @@ -253,21 +253,21 @@ Instead of rely in a Rakefile to install Jars when the Gem is installed, Jars ca
Ruby needs to be called before calling `LockJar.load`. Only Jars that are missing are downloaded.

#get jarfile relative the gem dir
lockfile = File.expand_path( "../Jarfile.lock", __FILE__ )
lockfile = File.expand_path("../Jarfile.lock", __FILE__)

# Download any missing Jars
LockJar.install( lockfile )
LockJar.install(lockfile)

### Loading

With the Jars installed, loading the classpath for the Gem is simple.
As part of the load process for the Gem (an entry file that is required, etc) use the following:

#get jarfile relative the gem dir
lockfile = File.expand_path( "../Jarfile.lock", __FILE__ )
lockfile = File.expand_path("../Jarfile.lock", __FILE__)

# Loads the ClassPath with Jars from the lockfile
LockJar.load( lockfile )
LockJar.load(lockfile)

See also [loading Jars into a custom ClassLoader](https://github.com/mguymon/lock_jar/wiki/ClassLoader).

Expand Down
8 changes: 3 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
require 'bundler'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
require 'rdoc/task'

begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
$stderr.puts 'Run `bundle install` to install missing gems'
exit e.status_code
end


RSpec::Core::RakeTask.new
RuboCop::RakeTask.new

Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

version = File.exist?('VERSION') ? File.read('VERSION') : ''
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "lockjar #{version}"
rdoc.rdoc_files.include('README*')
Expand Down
82 changes: 45 additions & 37 deletions lib/lock_jar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# License for the specific language governing permissions and limitations under
# the License.

require "yaml"
require 'rubygems'
require 'yaml'
require 'lock_jar/resolver'
require 'lock_jar/runtime'
require 'lock_jar/version'
Expand All @@ -27,63 +26,72 @@
# @author Michael Guymon
#
module LockJar

class << self
#
# Override LockJar configuration
#
def config( opts )
Runtime.instance.resolver( opts )
def config(opts)
Runtime.instance.resolver(opts)
end

def install( *args, &blk )
def install(*args, &blk)
lockfile, groups, opts = extract_args :lockfile, args, &blk
Runtime.instance.install( lockfile, groups, opts, &blk )
Runtime.instance.install(lockfile, groups, opts, &blk)
end


# Lists all dependencies as notations for groups from the Jarfile.lock. Depending on the type of arg, a different configuration is set.
# Lists all dependencies as notations for groups from the Jarfile.lock.
# Depending on the type of arg, a different configuration is set.
#
# * An arg of a String will set the Jarfile.lock, e.g. 'Better.lock'. Default lock file is *Jarfile.lock*.
# * An arg of an Array will set the groups, e.g. ['development','test']. Defaults group is *default*
# * An arg of a String will set the Jarfile.lock, e.g. 'Better.lock'.
# Default lock file is *Jarfile.lock*.
# * An arg of an Array will set the groups, e.g. ['development','test'].
# Defaults group is *default*
# * An arg of a Hash will set the options, e.g. { :local_repo => 'path' }
# * :local_repo [String] sets the local repo path
# * :local_paths [Boolean] to true converts the notations to paths to jars in the local repo path
# * :resolve [Boolean] to true will make transitive dependences resolve before loading to classpath
# * :local_paths [Boolean] to true converts the notations to paths to jars
# in the local repo path
# * :resolve [Boolean] to true will make transitive dependences resolve
# before loading to classpath
#
# A block can be passed in, overriding values from a Jarfile.lock.
#
# @return [Array] of jar and mapped path
def list( *args, &blk )
def list(*args, &blk)
lockfile, groups, opts = extract_args :lockfile, args, &blk
Runtime.instance.list( lockfile, groups, opts, &blk )
Runtime.instance.list(lockfile, groups, opts, &blk)
end

# LockJar.load(*args): Loads all dependencies to the classpath for groups from the Jarfile.lock. Depending on the type of arg, a different configuration is set.
# * An arg of a String will set the Jarfile.lock, e.g. 'Better.lock'. Default lock file is *Jarfile.lock*.
# * An arg of an Array will set the groups, e.g. ['development','test'].Defaults group is *default*.
# LockJar.load(*args): Loads all dependencies to the classpath for groups
# from the Jarfile.lock. Depending on the type of arg, a different configuration is set.
# * An arg of a String will set the Jarfile.lock, e.g. 'Better.lock'.
# Default lock file is *Jarfile.lock*.
# * An arg of an Array will set the groups, e.g. ['development','test'].
# Defaults group is *default*.
# * An arg of a Hash will set the options, e.g. { :local_repo => 'path' }
# * :local_repo [String] sets the local repo path
# * :resolve [Boolean] to true will make transitive dependences resolve before loading to classpath
# * :resolve [Boolean] to true will make transitive dependences resolve
# before loading to classpath
# * :disable [Boolean] to true will disable any additional calls to load and lock
#
# A block can be passed in, overriding values from a Jarfile.lock.
#
# @return [Array] of absolute paths of jars and mapped paths loaded into claspath
def load( *args, &blk )
def load(*args, &blk)
if Runtime.instance.opts.nil? || !Runtime.instance.opts[:disable]
lockfile, groups, opts = extract_args :lockfile, args, &blk
lockfile, groups, opts = extract_args(:lockfile, args, &blk)
Runtime.instance.load(lockfile, groups, opts, &blk)
else
puts "LockJar#load has been disabled"
puts 'LockJar#load has been disabled'
end
end

# Lock a Jarfile and generate a Jarfile.lock.
#
# LockJar.lock accepts an Array for parameters. Depending on the type of arg, a different configuration is set.
# LockJar.lock accepts an Array for parameters. Depending on the type of
# arg, a different configuration is set.
#
# * An arg of a String will set the Jarfile, e.g. 'Jarfile.different'. Default Jarfile is *Jarfile*.
# * An arg of a String will set the Jarfile, e.g. 'Jarfile.different'.
# Default Jarfile is *Jarfile*.
# * An arg of a Hash will set the options, e.g. { :local_repo => 'path' }
# * :download_artifacts if true, will download jars to local repo. Defaults to true.
# * :local_repo sets the local repo path
Expand All @@ -93,12 +101,12 @@ def load( *args, &blk )
# A block can be passed in, overriding values from a Jarfile.
#
# @return [Hash] Lock data
def lock( *args, &blk )
def lock(*args, &blk)
if Runtime.instance.opts.nil? || !Runtime.instance.opts[:disable]
jarfile, groups, opts = extract_args :jarfile, args, &blk
jarfile, _, opts = extract_args(:jarfile, args, &blk)
Runtime.instance.lock(jarfile, opts, &blk)
else
puts "LockJar#lock has been disabled"
puts 'LockJar#lock has been disabled'
end
end

Expand All @@ -107,25 +115,25 @@ def lock( *args, &blk )
#
# @param [String] lockfile path to lockfile
# @return [Hash] Lock Data
def read( lockfile )
LockJar::Domain::Lockfile.read( lockfile )
def read(lockfile)
LockJar::Domain::Lockfile.read(lockfile)
end

# Add a Jarfile to be included when LockJar.lock_registered_jarfiles is called.
#
# @param [String] jarfile path to register
# @return [Array] All registered jarfiles
def register_jarfile(jarfile, spec = nil)
fail "Jarfile not found: #{ jarfile }" unless File.exists? jarfile
fail "Jarfile not found: #{jarfile}" unless File.exist? jarfile
registered_jarfiles[jarfile] = spec
end

def reset_registered_jarfiles
@@registered_jarfiles = {}
@registered_jarfiles = {}
end

def registered_jarfiles
@@registered_jarfiles ||= {}
@registered_jarfiles ||= {}
end

# Lock the registered Jarfiles and generate a Jarfile.lock.
Expand All @@ -137,7 +145,7 @@ def registered_jarfiles
# A block can be passed in, overriding values from the Jarfiles.
#
# @return [Hash] Lock data
def lock_registered_jarfiles( *args, &blk )
def lock_registered_jarfiles(*args, &blk)
jarfiles = registered_jarfiles
return if jarfiles.empty?
instances = jarfiles.map do |jarfile, spec|
Expand All @@ -148,23 +156,23 @@ def lock_registered_jarfiles( *args, &blk )
end
end
combined = instances.reduce do |result, inst|
LockJar::Domain::DslHelper.merge result, inst
LockJar::Domain::DslMerger.new(result, inst).merge
end
args = args.reject { |arg| arg.is_a? String }
lock combined, *args, &blk
lock(combined, *args, &blk)
end
end

private

def self.extract_args(type, args, &blk )
def self.extract_args(type, args, &blk)
lockfile_or_path = nil
opts = {}
groups = ['default']
args.each do |arg|
case arg
when Hash
opts.merge!( arg )
opts.merge!(arg)
when String
lockfile_or_path = arg
when LockJar::Domain::Lockfile
Expand Down
Loading

0 comments on commit 40bf494

Please sign in to comment.