Class: Doubleshot::CLI::Commands::Init

Inherits:
Doubleshot::CLI show all
Defined in:
lib/doubleshot/commands/init.rb

Constant Summary

Constants inherited from Doubleshot::CLI

USAGE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Doubleshot::CLI

commands, detect, inherited, task_name, usage

Constructor Details

#initialize(args) ⇒ Init

Returns a new instance of Init.



47
48
49
50
# File 'lib/doubleshot/commands/init.rb', line 47

def initialize(args)
  @path = Pathname(args.empty? ? "." : args.first)
  @config = Doubleshot::Configuration.new
end

Class Method Details

.optionsObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/doubleshot/commands/init.rb', line 11

def self.options
  Options.new do |options|
    options.banner = "Usage: doubleshot init [PATH]"
    options.separator ""
    options.separator "  [PATH]      The path to your project directory."
    options.separator "              DEFAULT: Current working directory."
    options.separator ""
    options.separator "Summary: #{summary}"
  end
end

.start(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/doubleshot/commands/init.rb', line 22

def self.start(args)
  session = new(args)

  if session.doubleshot_file.exist?
    return puts <<-EOS.margin
      ERROR: A Doubleshot file already exists.
      Rename or delete it to initialize a new one.
    EOS
  end
  session.import_gemspec!
  session.mark_gemspec_for_deletion!

  session.import_pom! if session.pom_file.exist?

  session.generate_doubleshot_file!

  puts <<-EOS.margin
    We have created a Doubleshot file for your project.

    Please review it and make changes as you see fit. It will
    be used for all the things.
  EOS
  return 0
end

.summaryObject



5
6
7
8
9
# File 'lib/doubleshot/commands/init.rb', line 5

def self.summary
  <<-EOS.margin
    Generate a Doubleshot file for your project.
  EOS
end

Instance Method Details

#doubleshot_fileObject



52
53
54
# File 'lib/doubleshot/commands/init.rb', line 52

def doubleshot_file
  Pathname(@path + "Doubleshot")
end

#eval_gemspec(contents) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/doubleshot/commands/init.rb', line 111

def eval_gemspec(contents)
  puts "Importing Gemspec..."
  begin
    ::Gem::Specification.from_yaml(contents)
    # Raises ArgumentError if the file is not valid YAML
  rescue ArgumentError, SyntaxError, ::Gem::EndOfYAMLException, ::Gem::Exception
    begin
      eval(contents, Doubleshot::CLI::binding)
    rescue LoadError, SyntaxError => e
      raise Gem::InvalidSpecificationException, "There was a #{e.class} while evaluating gemspec: \n#{e.message}"
    end
  end
end

#gemspecObject



60
61
62
# File 'lib/doubleshot/commands/init.rb', line 60

def gemspec
  @gemspec ||= Pathname::glob(@path + "*.gemspec").first
end

#generate_doubleshot_file!Object



145
146
147
148
149
# File 'lib/doubleshot/commands/init.rb', line 145

def generate_doubleshot_file!
  doubleshot_file.open("w+") do |file|
    file << @config.to_ruby
  end
end

#import_gemspec!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/doubleshot/commands/init.rb', line 64

def import_gemspec!
  original = gemspec ? eval_gemspec(gemspec.read) : ::Gem::Specification.new

  @config.project = default original.name,    "THE PROJECT NAME"
  @config.version = default original.version, "9000.1"

  @config.gemspec do |spec|
    spec.summary     = default original.summary,     "SUMMARIZE ME"
    spec.description = default original.description, "A VERY DETAILED DESCRIPTION"
    spec.author      = default original.author,      "WHOAMI"
    spec.homepage    = default original.homepage,    "I AM FROM THE INTERNET"
    spec.email       = default original.email,       "[email protected]"
    spec.license     = default original.license,     "MIT-LICENSE"
    spec.executables = original.executables
  end

  original.runtime_dependencies.each do |dependency|
    @config.gem dependency.name, *dependency.requirements_list
  end

  @config.development do
    original.development_dependencies.each do |dependency|
      @config.gem dependency.name, *dependency.requirements_list
    end
  end
end

#import_pom!Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/doubleshot/commands/init.rb', line 91

def import_pom!
  pom_document = REXML::Document.new pom_file.open

  project = pom_document.get_text("project/artifactId")
  @config.project = project if (@config.project.nil? or @config.project == "THE PROJECT NAME") and not project.nil?

  @config.group = pom_document.get_text("project/groupId")

  version = pom_document.get_text("project/version")
  @config.version = version if (@config.version.nil? or @config.version == "9000.1") and not version.nil?

  pom_document.elements.each("project/dependencies/dependency") do |pom_dependency|
    group_id = pom_dependency.get_text("groupId")
    artifact_id = pom_dependency.get_text("artifactId")
    version = pom_dependency.get_text("version")

    @config.jar "#{group_id}:#{artifact_id}:jar:#{version}"
  end
end

#mark_gemspec_for_deletion!Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/doubleshot/commands/init.rb', line 125

def mark_gemspec_for_deletion!
  if gemspec
    delete_me = gemspec.basename.to_s + ".DELETE_ME"
    gemspec.rename(delete_me)
    puts <<-EOS.margin, "", ""

      IMPORTANT: We have renamed your gemspec to:

        #{delete_me}

      Please delete this file after you have reviewed your Doubleshot file.
      It cannot be used to distribute your project. A valid gemspec will be
      generated for you during the build process so that JAR, Gem, and compiled
      sources are included in your distribution.

      DELETE YOUR GEMSPEC!
    EOS
  end
end

#pom_fileObject



56
57
58
# File 'lib/doubleshot/commands/init.rb', line 56

def pom_file
  Pathname(@path + "pom.xml")
end