Class: Doubleshot::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/doubleshot/configuration.rb,
lib/doubleshot/configuration/source_locations.rb

Defined Under Namespace

Classes: SourceLocations

Constant Summary collapse

DEFAULT_PATHS =
[ "Doubleshot", "*LICENSE*", "README*" ]
DEFAULT_WHITELIST =
%w[
  .rb .java .js
  .yaml .yml .json .xml .properties
  .css .less .sass .scss
  .erb .haml .rxml .builder .html
]
PROJECT_MESSAGE =
<<-EOS.margin
# The project name will be used as your JAR's
# artifactId and your Gem's name.
EOS
GROUP_MESSAGE =
<<-EOS.margin
# The groupId of your JAR. This will default to
# the project name if not supplied.
#
# NOTE: You are strongly encouraged to set this
# to something meaningful if you are planning to
# package your project as a JAR.
EOS
VERSION_MESSAGE =
<<-EOS.margin
# Version number that obeys the constraints of
# Gem::Version:
#
#   If any part contains letters, then the version
#     is considered pre-release.
EOS
GEM_REPOSITORY_MESSAGE =
<<-EOS.margin
# Add your custom ruby gem repositories here.
#
# Default is http://rubygems.org
# (defined in Resolver::GemResolver::DEFAULT_REPOSITORY)
EOS
MVN_REPOSITORY_MESSAGE =
<<-EOS.margin
# Add your custom Maven repositories here.
#
# Default is http://repo1.maven.org/maven2
# (defined in Resolver::JarResolver::DEFAULT_REPOSITORY)
EOS
SOURCE_RUBY_MESSAGE =
<<-EOS.margin
# Relative path to the folder containing your
# Ruby source code (.rb files).
# The default is "lib".
EOS
SOURCE_JAVA_MESSAGE =
<<-EOS.margin
# Relative path to the folder containing your
# Java source code (.java files).
# The default is "ext/java".
EOS
SOURCE_TESTS_MESSAGE =
<<-EOS.margin
# Relative path to the folder containing your
# tests. This feature is used when running
#   doubleshot test
# Tests are assumed to be written in Ruby
# exclusively. Java test frameworks are not
# supported.
# This value is optional. The default is "test".
EOS
TARGET_MESSAGE =
<<-EOS.margin
# Relative path to the folder containing the
# compiled Java files (.class files).
# The default is "target".
EOS
JAVA_MAIN_MESSAGE =
<<-EOS.margin
# Class to use for Ant's Main-Class attribute for
# a manifest. The default is "org.jruby.Main".
EOS
WHITELIST_MESSAGE =
<<-EOS.margin
# List of file extensions within source folders
# that will be included during packaging.
# Does NOT override defaults, but adds to them.
# The default list can be found in
# Doubleshot::Configuration::DEFAULT_WHITELIST.
EOS
GEMSPEC_MESSAGE =
<<-EOS.margin
# A subset of Gem::Specification compatible
# with Doubleshot. Since Doubleshot manages
# your Rubygems dependencies, and what files
# are included in your build, those options
# are not applicable (and will be discarded if
# provided).
EOS
GEM_DEPENDENCY_MESSAGE =
<<-EOS.margin
# Add your Gem dependencies here using similar
# syntax to a gemspec, except your method is
# "gem" instead of "add_runtime_dependency".
EOS
JAR_DEPENDENCY_MESSAGE =
<<-EOS.margin
# Add your JAR dependencies using Buildr
# dependency formatting (available as a
# copy-and-paste option on most Maven search
# repositories).
EOS
DEVELOPMENT_MESSAGE =
<<-EOS.margin
# Add your Gem and JAR development dependencies
# similar to above. By default "doubleshot" is
# added as a development dependency to your gemspec.
# That's the equivalent of:
#
#   config.development do
#     config.gem "doubleshot"
#   end
#
# NOTE: The above won't appear in your Doubleshot
# file as it's added during the build process for you.
EOS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/doubleshot/configuration.rb', line 131

def initialize
  @defaults                    = {}
  @gemspec                     = Gem::Specification.new
  @gemspec.platform            = Gem::Platform.new("java")
  @source                      = SourceLocations.new
  @target                      = default :target, Pathname("target")
  @java_main                   = default :java_main, "org.jruby.Main"

  @runtime_dependencies        = Dependencies.new
  @development_dependencies    = Dependencies.new
  @development_environment     = false

  @paths                       = default :paths, DEFAULT_PATHS.dup
  @whitelist                   = default :whitelist, DEFAULT_WHITELIST.dup

  @project                     = default :project, "hello_world"
  @group                       = default :group, "org.world.hello"
  @version                     = default :version, "0.1"

  @gem_repositories            = default :gem_repositories, Set.new
  @mvn_repositories            = default :mvn_repositories, Set.new
end

Instance Attribute Details

#java_mainObject

Returns the value of attribute java_main.



129
130
131
# File 'lib/doubleshot/configuration.rb', line 129

def java_main
  @java_main
end

Instance Method Details

#__changes__Object



500
501
502
503
504
505
506
# File 'lib/doubleshot/configuration.rb', line 500

def __changes__
  changes = []
  @defaults.each_pair do |key,value|
    changes << key unless instance_variable_get("@#{key}") == value
  end
  changes
end

#add_path(path) ⇒ Object



268
269
270
271
# File 'lib/doubleshot/configuration.rb', line 268

def add_path(path)
  @paths << path
  self
end

#classpathObject



217
218
219
# File 'lib/doubleshot/configuration.rb', line 217

def classpath
  @classpath
end

#classpath=(value) ⇒ Object



221
222
223
# File 'lib/doubleshot/configuration.rb', line 221

def classpath=(value)
  @classpath = value
end

#developmentObject



225
226
227
228
229
230
231
232
# File 'lib/doubleshot/configuration.rb', line 225

def development
  if block_given?
    @development_environment = true
    yield
    @development_environment = false
  end
  @development_dependencies
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


273
274
275
276
277
278
279
280
281
# File 'lib/doubleshot/configuration.rb', line 273

def eql?(other)
  other.is_a?(self.class) &&
    other.target       == target       &&
    other.source       == source       &&
    other.runtime      == runtime      &&
    other.development  == development  &&
    other.paths        == paths        &&
    other.gemspec      == gemspec
end

#gem(name, *requirements) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/doubleshot/configuration.rb', line 234

def gem(name, *requirements)
  dependencies = @development_environment ?
    @development_dependencies :
    @runtime_dependencies

  dependency = dependencies.gems.fetch(name)
  dependencies.gems.add(dependency)

  requirements.each do |requirement|
    dependency.add_requirement(requirement)
  end

  dependency
end

#gem_repositoriesObject



187
188
189
# File 'lib/doubleshot/configuration.rb', line 187

def gem_repositories
  ReadonlyCollection.new(@gem_repositories.empty? ? [ Resolver::GemResolver::DEFAULT_REPOSITORY ] : @gem_repositories)
end

#gem_repository(uri) ⇒ Object



182
183
184
185
# File 'lib/doubleshot/configuration.rb', line 182

def gem_repository(uri)
  @gem_repositories << uri
  uri
end

#gemspec(&b) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/doubleshot/configuration.rb', line 284

def gemspec(&b)
  if b
    yield @gemspec
  else
    @gemspec.rdoc_options = rdoc_options
    @gemspec.require_paths = [ @source.ruby.to_s, @target.to_s ]

    test_files = []
    if @source.tests.exist?
      @source.tests.find do |path|
        test_files << path.to_s if path.file? && @whitelist.include?(path.extname)
      end
    end
    @gemspec.test_files = test_files

    files = []

    @paths.each do |path|
      Pathname::glob(path).each do |match|
        files << match.to_s if match.file?
      end
    end

    if @source.ruby.exist?
      @source.ruby.find do |path|
        files << path.to_s if path.file? && @whitelist.include?(path.extname)
      end
    end

    if @source.java.exist?
      @source.java.find do |path|
        files << path.to_s if path.file? && @whitelist.include?(path.extname)
      end
    end

    if @target.exist?
      @target.find do |path|
        files << path.to_s if path.file? && path.extname != ".class"
      end
    end

    @gemspec.files.concat(files)

    @runtime_dependencies.gems.each do |dependency|
      @gemspec.add_runtime_dependency dependency.name, *dependency.requirements
    end

    @development_dependencies.gems.each do |dependency|
      @gemspec.add_development_dependency dependency.name, *dependency.requirements
    end

    bin = Pathname("bin").expand_path
    if bin.directory?
      bin.find do |path|
        @gemspec.executables << path.relative_path_from(bin).to_s if path.file? && path.executable?
      end
    end

    @gemspec.add_development_dependency "doubleshot"

    @gemspec
  end
end

#groupObject



154
155
156
# File 'lib/doubleshot/configuration.rb', line 154

def group
  @group || @project
end

#group=(name) ⇒ Object



158
159
160
# File 'lib/doubleshot/configuration.rb', line 158

def group=(name)
  @group = name
end

#jar(coordinate) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/doubleshot/configuration.rb', line 249

def jar(coordinate)
  dependencies = @development_environment ?
    @development_dependencies :
    @runtime_dependencies

  dependency = dependencies.jars.fetch(coordinate)
  dependencies.jars.add(dependency)

  dependency
end

#mvn_repositoriesObject



196
197
198
# File 'lib/doubleshot/configuration.rb', line 196

def mvn_repositories
  ReadonlyCollection.new(@mvn_repositories.empty? ? [ Resolver::JarResolver::DEFAULT_REPOSITORY ] : @mvn_repositories)
end

#mvn_repository(uri) ⇒ Object



191
192
193
194
# File 'lib/doubleshot/configuration.rb', line 191

def mvn_repository(uri)
  @mvn_repositories << uri
  uri
end

#pathsObject



264
265
266
# File 'lib/doubleshot/configuration.rb', line 264

def paths
  ReadonlyCollection.new @paths
end

#projectObject



162
163
164
# File 'lib/doubleshot/configuration.rb', line 162

def project
  @project
end

#project=(name) ⇒ Object



166
167
168
169
170
171
# File 'lib/doubleshot/configuration.rb', line 166

def project=(name)
  @gemspec.name = name
  @project = name
  @group = default :group, @project unless __changes__.include? :group
  @project
end

#runtimeObject



260
261
262
# File 'lib/doubleshot/configuration.rb', line 260

def runtime
  @runtime_dependencies
end

#sourceObject



200
201
202
# File 'lib/doubleshot/configuration.rb', line 200

def source
  @source
end

#targetObject



204
205
206
# File 'lib/doubleshot/configuration.rb', line 204

def target
  @target
end

#target=(path) ⇒ Object



208
209
210
# File 'lib/doubleshot/configuration.rb', line 208

def target=(path)
  @target = Pathname(path.to_s)
end

#to_rubyObject



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/doubleshot/configuration.rb', line 348

def to_ruby
  <<-EOS.margin
    # encoding: utf-8

    Doubleshot.new do |config|
#{
        inner_heredoc = false
        to_ruby_body.split("\n").map do |line|
          output = if inner_heredoc
            "        #{line}"
          else
            "          #{line}"
          end

          if line =~ /\<\<\-DESCRIPTION/
            inner_heredoc = true
          elsif line =~ /^\s*DESCRIPTION/
            inner_heredoc = false
          end
          output
        end.join("\n")}
    end
  EOS
end

#to_ruby_bodyObject



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/doubleshot/configuration.rb', line 373

def to_ruby_body
  spec = gemspec

  config_changes = __changes__
  source_changes = @source.__changes__

  <<-EOS.margin
    #{Doubleshot::Configuration::PROJECT_MESSAGE}
    #{"#   " unless config_changes.include? :project}config.project = #{@project.inspect}

    #{Doubleshot::Configuration::GROUP_MESSAGE}
    #{"#   " unless config_changes.include? :group}config.group = #{@group.inspect}

    #{Doubleshot::Configuration::VERSION_MESSAGE}
    #{"#   " unless config_changes.include? :version}config.version = #{@version.to_s.inspect}


    #{Doubleshot::Configuration::GEM_REPOSITORY_MESSAGE}
    #{
      if config_changes.include? :gem_repositories
        (@gem_repositories).map do |repository|
          "config.gem_repository #{repository.inspect}"
        end.join("\n        ")
      else
        "#   config.gem_repository \"https://rubygems.org\"\n" +
        "#   config.gem_repository \"http://gems.example.com\""
      end
    }

    #{Doubleshot::Configuration::MVN_REPOSITORY_MESSAGE}
    #{
      if config_changes.include? :mvn_repositories
        (@mvn_repositories).map do |repository|
          "config.mvn_repository #{repository.inspect}"
        end.join("\n        ")
      else
        "#   config.mvn_repository \"http://repo1.maven.org/maven2\"\n" +
        "#   config.mvn_repository \"http://repository.jboss.com/maven2\""
      end
    }


    #{Doubleshot::Configuration::SOURCE_RUBY_MESSAGE}
    #{"#   " unless source_changes.include? :ruby}config.source.ruby    = #{@source.ruby.to_s.inspect}

    #{Doubleshot::Configuration::SOURCE_JAVA_MESSAGE}
    #{"#   " unless source_changes.include? :java}config.source.java    = #{@source.java.to_s.inspect}

    #{Doubleshot::Configuration::SOURCE_TESTS_MESSAGE}
    #{"#   " unless source_changes.include? :tests}config.source.tests   = #{@source.tests.to_s.inspect}


    #{Doubleshot::Configuration::TARGET_MESSAGE}
    #{"#   " unless config_changes.include? :target}config.target = #{@target.to_s.inspect}


    #{Doubleshot::Configuration::WHITELIST_MESSAGE}
    #{
      if config_changes.include? :whitelist
        (@whitelist - DEFAULT_WHITELIST).map do |ext|
          "config.whitelist #{ext.inspect}"
        end.join("\n        ")
      else
        "#   config.whitelist \".ext\""
      end
    }


    #{Doubleshot::Configuration::GEM_DEPENDENCY_MESSAGE}
    #{
      if @runtime_dependencies.gems.empty?
        "#   config.gem \"bcrypt-ruby\", \"~> 3.0\""
      else
        @runtime_dependencies.gems.map do |dependency|
          if dependency.requirements.empty?
            "config.gem \"#{dependency}\""
          else
            "config.gem \"#{dependency}\", \"#{dependency.requirements.map(&:to_s).join("\", \"")}\""
          end
        end.join("\n        ")
      end
    }

    #{Doubleshot::Configuration::JAR_DEPENDENCY_MESSAGE}
    #{
      if @runtime_dependencies.jars.empty?
        "#   config.jar \"ch.qos.logback:logback:jar:0.5\""
      else
        @runtime_dependencies.jars.map do |dependency|
          "config.jar \"#{dependency}\""
        end.join("\n        ")
      end
    }

    #{Doubleshot::Configuration::DEVELOPMENT_MESSAGE}
    #{
      unless @development_dependencies.empty?
        "config.development do\n        " +
        (
          @development_dependencies.gems.map do |dependency|
            if dependency.requirements.empty?
              "  config.gem \"#{dependency}\""
            else
              "  config.gem \"#{dependency}\", \"#{dependency.requirements.map(&:to_s).join("\", \"")}\""
            end
          end +
        @development_dependencies.jars.map do |dependency|
            "  config.jar \"#{dependency}\""
        end
        ).join("\n        ") + "\n        end"
      end
    }

    #{Doubleshot::Configuration::GEMSPEC_MESSAGE}
    config.gemspec do |spec|
      spec.summary        = #{spec.summary.inspect}
      spec.description    = <<-DESCRIPTION
    #{spec.description.strip}
    DESCRIPTION
      spec.homepage       = #{spec.homepage.inspect}
      spec.author         = #{spec.author.inspect}
      spec.email          = #{spec.email.inspect}
      spec.license        = #{spec.license.inspect}
    end
  EOS
end

#versionObject



173
174
175
# File 'lib/doubleshot/configuration.rb', line 173

def version
  @version
end

#version=(version) ⇒ Object



177
178
179
180
# File 'lib/doubleshot/configuration.rb', line 177

def version=(version)
  @gemspec.version = version
  @version = version
end

#whitelist(extname) ⇒ Object



212
213
214
215
# File 'lib/doubleshot/configuration.rb', line 212

def whitelist(extname)
  @whitelist << extname.ensure_starts_with(".")
  self
end