Class: InvocaGems::Gem

Inherits:
Object
  • Object
show all
Includes:
ShellCommand
Defined in:
lib/invoca_gems/gem.rb

Constant Summary collapse

WORKING_DIRECTORY =
"invoca_gems"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ShellCommand

included, #shell_command

Constructor Details

#initialize(gem_name, presenter, defer_bundle: false) ⇒ Gem

Returns a new instance of Gem.



8
9
10
11
12
13
14
15
16
# File 'lib/invoca_gems/gem.rb', line 8

def initialize(gem_name, presenter, defer_bundle: false)
  @presenter = presenter
  bundle_wrapper = InvocaGems::Bundler.new
  try_load_from_git_source(bundle_wrapper, gem_name)
  try_load_from_path_source(bundle_wrapper, gem_name)
  @name or presenter.stop_with_message("Could not find gem #{gem_name}")
  @dependants = bundle_wrapper.dependants(gem_name)
  @defer_bundle = defer_bundle
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def branch
  @branch
end

#dependantsObject (readonly)

Returns the value of attribute dependants.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def dependants
  @dependants
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def path
  @path
end

#presenterObject (readonly)

Returns the value of attribute presenter.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def presenter
  @presenter
end

#shaObject (readonly)

Returns the value of attribute sha.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def sha
  @sha
end

#uriObject (readonly)

Returns the value of attribute uri.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def uri
  @uri
end

#used_byObject (readonly)

Returns the value of attribute used_by.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def used_by
  @used_by
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/invoca_gems/gem.rb', line 6

def version
  @version
end

Instance Method Details

#bundle_install_applicationObject



205
206
207
208
209
210
211
# File 'lib/invoca_gems/gem.rb', line 205

def bundle_install_application
  unless @defer_bundle
    without_ruby_options do
      shell_command(command: "bundle install --quiet", presenter: presenter)
    end
  end
end

#bundle_install_gemObject



213
214
215
216
217
# File 'lib/invoca_gems/gem.rb', line 213

def bundle_install_gem
  without_ruby_options do
    shell_command(command: "bundle install --quiet", path: repo_directory, presenter: presenter)
  end
end

#commit_changes(message) ⇒ Object



97
98
99
100
# File 'lib/invoca_gems/gem.rb', line 97

def commit_changes(message)
  git_repo.commit("#{local_branch_name} #{message}")
  git_repo.push
end

#configure_appilicationObject



186
187
188
189
190
191
# File 'lib/invoca_gems/gem.rb', line 186

def configure_appilication
  unless @application_configured
    create_working_directory
    @application_configured = true
  end
end

#configure_branchObject



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/invoca_gems/gem.rb', line 136

def configure_branch
  git_repo.unsaved_changes? and presenter.stop_with_message "Unsaved changes found in #{repo_directory}"
  if !git_repo.branch_exists?(local_branch_name)
    presenter.set_subtask "Creating branch #{local_branch_name}"
    git_repo.create_branch_at(local_branch_name, sha)
  end
  presenter.set_subtask "Using branch #{local_branch_name}"
  git_repo.checkout_branch(local_branch_name)

  if git_repo.sha != sha
    presenter.stop_with_message "Commited work not found in gem, use -- mumble mumble to fix."
  end
end

#create_working_directoryObject



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/invoca_gems/gem.rb', line 193

def create_working_directory
  if File.read(".gitignore") !~ /#{WORKING_DIRECTORY}/m
    presenter.set_subtask "updating .gitignore"
    File.open(".gitignore","a") { |f| f.write("# Invoca Gem support\n#{WORKING_DIRECTORY}/**\n") }
  end

  if !File.directory?(WORKING_DIRECTORY)
    presenter.set_subtask "creating #{WORKING_DIRECTORY}"
    Dir.mkdir(WORKING_DIRECTORY)
  end
end

#editObject



18
19
20
21
22
# File 'lib/invoca_gems/gem.rb', line 18

def edit
  configure_branch
  update_gemfile_for_source
  bundle_install_application
end

#get_git_statistics_from_path(path) ⇒ Object



179
180
181
182
183
184
# File 'lib/invoca_gems/gem.rb', line 179

def get_git_statistics_from_path(path)
  repo = repo_for_path(path)
  @uri = repo.uri
  @sha = repo.sha
  @branch = repo.branch
end

#git_repoObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/invoca_gems/gem.rb', line 119

def git_repo
  create_working_directory
  unless @git_repo
    unless repo_exists?
      presenter.set_subtask "creating #{repo_directory} from #{uri}"
      InvocaGems::GitRepo.clone(WORKING_DIRECTORY, uri, name, presenter: presenter)
    end
    @git_repo = repo_for_path(repo_directory)
    @git_repo.fetch
  end
  @git_repo
end

#local_branch_nameObject



157
158
159
# File 'lib/invoca_gems/gem.rb', line 157

def local_branch_name
  @local_branch_name ||= repo_for_path('.').branch
end

#repo_directoryObject



111
112
113
# File 'lib/invoca_gems/gem.rb', line 111

def repo_directory
  WORKING_DIRECTORY + '/' + name
end

#repo_exists?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/invoca_gems/gem.rb', line 115

def repo_exists?
  File.directory?(repo_directory)
end

#repo_for_path(path) ⇒ Object



132
133
134
# File 'lib/invoca_gems/gem.rb', line 132

def repo_for_path(path)
  InvocaGems::GitRepo.new(presenter, path)
end

#save(commit_message = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/invoca_gems/gem.rb', line 24

def save(commit_message=nil)
  if git_repo.unsaved_changes?
    if commit_message
      commit_changes(commit_message)
    else
      presenter.stop_with_message "unsaved changes found in #{repo_directory}"
    end
  else
    git_repo.push
  end

  update_gemfile_for_github(gemfile_path: "Gemfile",
                            gem_name: name,
                            gem_uri: uri,
                            new_version: version,
                            new_sha: git_repo.sha)

  presenter.set_subtask("now at #{version} #{git_repo.sha[0..6]}")
  update_dependencies(name, version, git_repo.sha)
  bundle_install_application
end

#try_load_from_git_source(bundle_wrapper, gem_name) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/invoca_gems/gem.rb', line 161

def try_load_from_git_source(bundle_wrapper, gem_name)
  if result = bundle_wrapper.find_git_source(gem_name)
    @name    = result.name
    @uri     = result.uri
    @version = result.version
    @sha     = result.ref
  end
end

#try_load_from_path_source(bundle_wrapper, gem_name) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/invoca_gems/gem.rb', line 170

def try_load_from_path_source(bundle_wrapper, gem_name)
  if !@name && result = bundle_wrapper.find_path_source(gem_name)
    @name = result.name
    @path = result.path
    @version = result.version
    get_git_statistics_from_path(path)
  end
end

#update_dependencies(gem_to_update, version, sha) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/invoca_gems/gem.rb', line 58

def update_dependencies(gem_to_update, version, sha)
  if dependants
    dependants.uniq.each do |dependant|
      presenter.set_task "Updating #{dependant} for #{gem_to_update}"
      gem = self.class.new(dependant, presenter, defer_bundle: true)
      gem.edit

      # Dependant gem update
      gem.update_gemfile_for_github(
        gemfile_path: gem.repo_directory + '/Gemfile',
        gem_name: name,
        gem_uri: uri,
        new_version: version,
        new_sha: sha)

      # Project gem update
      gem.update_gemfile_for_github(
        gemfile_path: gem.repo_directory + '/Gemfile',
        gem_name: name,
        gem_uri: uri,
        new_version: version,
        new_sha: sha)

      gem.update_gem_gemspec(gem.repo_directory + "/#{gem.name}.gemspec", gem_to_update, version)
      gem.bundle_install_gem
      gem.commit_changes("Update #{gem_to_update} to #{version} (#{sha[0..6]})")
      gem.save # Will nest dependency upudate
    end
  end
end

#update_gem_gemspec(gemspec_path, gem_name, version) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/invoca_gems/gem.rb', line 89

def update_gem_gemspec(gemspec_path, gem_name, version)
  presenter.set_subtask "updating #{gemspec_path} for #{gem_name}"
  gemspec = InvocaGems::Gemspec.new(gemspec_path)
  gemspec_gem = gemspec.find(gem_name)
  gemspec_gem.update_version(version)
  gemspec_gem.save!
end

#update_gemfile_for_github(gemfile_path:, gem_name:, gem_uri:, new_version:, new_sha:) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/invoca_gems/gem.rb', line 102

def update_gemfile_for_github(gemfile_path:, gem_name:, gem_uri:, new_version:, new_sha:)
  presenter.set_subtask "updating #{gemfile_path} for #{gem_name}"
  gemfile = InvocaGems::Gemfile.new(gemfile_path)
  gemfile_gem = gemfile.find(gem_name, allow_missing: true) || gemfile.append(gem_name)
  gemfile_gem.update_as_github(new_version, gem_uri, new_sha)
  gemfile_gem.save!
end

#update_gemfile_for_sourceObject



150
151
152
153
154
155
# File 'lib/invoca_gems/gem.rb', line 150

def update_gemfile_for_source
  gemfile = InvocaGems::Gemfile.new('Gemfile')
  gemfile_gem = gemfile.find(name)
  gemfile_gem.update_as_path(repo_directory)
  gemfile_gem.save!
end

#update_version(new_version, new_sha) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/invoca_gems/gem.rb', line 46

def update_version(new_version, new_sha)
  presenter.set_subtask "updating #{name} to #{new_version} #{new_sha}"
  update_gemfile_for_github(
    gemfile_path: "Gemfile",
    gem_name: name,
    gem_uri: uri,
    new_version: new_version,
    new_sha: new_sha)
  update_dependencies(name, new_version, new_sha)
  bundle_install_application
end

#without_ruby_optionsObject



219
220
221
222
223
224
# File 'lib/invoca_gems/gem.rb', line 219

def without_ruby_options
  old_opt = ENV.delete("RUBYOPT")
  yield
ensure
  ENV["RUBYOPT"] = old_opt
end