Class: Tachikoma::Application

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/tachikoma/application.rb

Overview

Main logic of Tachikoma

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(strategy) ⇒ Object



13
14
15
# File 'lib/tachikoma/application.rb', line 13

def self.run(strategy)
  new.run(strategy)
end

Instance Method Details

#authorized_base_url_with_type(fetch_url, type, github_token, _github_account) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'lib/tachikoma/application.rb', line 238

def authorized_base_url_with_type(fetch_url, type, github_token, )
  uri = URI.parse(fetch_url)
  case type
  when 'fork', 'shared'
    "#{uri.scheme}://#{github_token}:x-oauth-basic@#{uri.host}#{uri.path}"
  else
    fail InvalidType, "Invalid type #{type}"
  end
end

#authorized_compare_url_with_type(fetch_url, type, github_token, github_account) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/tachikoma/application.rb', line 226

def authorized_compare_url_with_type(fetch_url, type, github_token, )
  uri = URI.parse(fetch_url)
  case type
  when 'fork'
    %(#{uri.scheme}://#{github_token}:x-oauth-basic@#{uri.host}#{path_for_fork(uri.path, )})
  when 'shared'
    "#{uri.scheme}://#{github_token}:x-oauth-basic@#{uri.host}#{uri.path}"
  else
    fail InvalidType, "Invalid type #{type}"
  end
end

#bundleObject



122
123
124
125
# File 'lib/tachikoma/application.rb', line 122

def bundle
  warn '[DEPRECATION] `bundle` is deprecated. Please use `bundler` instead.'
  bundler
end

#bundlerObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/tachikoma/application.rb', line 78

def bundler
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    Bundler.with_clean_env do
      sh(*['ruby', '-i', '-pe', '$_.gsub! /^ruby/, "#ruby"', 'Gemfile'])
      sh(*['git', 'config', 'user.name', @commiter_name])
      sh(*['git', 'config', 'user.email', @commiter_email])
      sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
      if File.exist?('Gemfile')
        @bundler_key_file = 'Gemfile'
        @bundler_lock_file = 'Gemfile.lock'
      elsif File.exist?('gems.rb')
        @bundler_key_file = 'gems.rb'
        @bundler_lock_file = 'gems.locked'
      else
        @bundler_key_file = 'Gemfile'
        @bundler_lock_file = 'Gemfile.lock'
      end
      sh(*([
        'bundle',
        '--gemfile', @bundler_key_file,
        '--no-deployment',
        '--without', 'nothing',
        '--path', 'vendor/bundle',
        @parallel_option
      ].compact))
      sh(*%w(bundle update))

      if @bundler_restore_bundled_with
        # restore_bundled_with
        lock_file_contents = File.read(@bundler_lock_file)
        lock_file = RestoreBundledWith::Lock.restore(
          lock_file_contents, @bundler_lock_file)
        File.write(@bundler_lock_file, lock_file.body)
      end

      sh(*['git', 'add', @bundler_lock_file])
      sh(*['git', 'commit', '-m', "Bundle update #{@readable_time}"]) do
        # ignore exitstatus
      end
      sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
    end
  end
end

#bundler_parallel_available?(bundler_version) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
283
# File 'lib/tachikoma/application.rb', line 280

def bundler_parallel_available?(bundler_version)
  # bundler 1.4.0 gets parallel number option
  Gem::Version.create(bundler_version) >= Gem::Version.create('1.4.0')
end

#bundler_parallel_option(bundler_version, parallel_number) ⇒ Object

TODO: refactor to returning command args array like git clone depth option



275
276
277
278
# File 'lib/tachikoma/application.rb', line 275

def bundler_parallel_option(bundler_version, parallel_number)
  return if !bundler_parallel_available?(bundler_version) || parallel_number <= 1
  "--jobs=#{parallel_number}"
end

#cartonObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tachikoma/application.rb', line 127

def carton
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh(*['git', 'config', 'user.name', @commiter_name])
    sh(*['git', 'config', 'user.email', @commiter_email])
    sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
    sh(*%w(carton install))
    sh(*%w(carton update))
    sh(*['git', 'add', 'carton.lock']) if File.exist?('carton.lock')
    sh(*['git', 'add', 'cpanfile.snapshot']) if File.exist?('cpanfile.snapshot')
    sh(*['git', 'commit', '-m', "Carton update #{@readable_time}"]) do
      # ignore exitstatus
    end
    sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
  end
end

#cleanObject



63
64
65
66
# File 'lib/tachikoma/application.rb', line 63

def clean
  mkdir_p(Tachikoma.repos_path)
  rm_rf(Dir.glob(File.join(Tachikoma.repos_path, '*')))
end

#cocoapodsObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/tachikoma/application.rb', line 194

def cocoapods
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh(*['git', 'config', 'user.name', @commiter_name])
    sh(*['git', 'config', 'user.email', @commiter_email])
    sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
    sh(*%w(pod install))
    sh(*%w(pod update))
    sh(*['git', 'add', 'Podfile.lock'])
    sh(*['git', 'commit', '-m', "Cocoapods update #{@readable_time}"]) do
      # ignore exitstatus
    end
    sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
  end
end

#composerObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/tachikoma/application.rb', line 177

def composer
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh(*['git', 'config', 'user.name', @commiter_name])
    sh(*['git', 'config', 'user.email', @commiter_email])
    sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
    # FIXME: Use Octokit.api_endpoint for GitHub Enterprise
    sh(*['composer', 'config', 'github-oauth.github.com', @github_token])
    sh(*['composer', 'install', '--no-interaction'])
    sh(*['composer', 'update', '--no-interaction'])
    sh(*['git', 'add', 'composer.lock'])
    sh(*['git', 'commit', '-m', "Composer update #{@readable_time}"]) do
      # ignore exitstatus
    end
    sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
  end
end

#davidObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/tachikoma/application.rb', line 155

def david
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh(*['git', 'config', 'user.name', @commiter_name])
    sh(*['git', 'config', 'user.email', @commiter_email])
    sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
    sh(*['david', 'update', '--warn404'])
    if File.exist?('npm-shrinkwrap.json')
      sh(*['rm', '-rf', 'node_modules/', 'npm-shrinkwrap.json'])
      sh(*['npm', 'install'])
      sh(*['npm', 'shrinkwrap'])
      sh(*['git', 'add', 'package.json'])
      sh(*['git', 'add', 'npm-shrinkwrap.json'])
    else
      sh(*['git', 'add', 'package.json'])
    end
    sh(*['git', 'commit', '-m', "David update #{@readable_time}"]) do
      # ignore exitstatus
    end
    sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
  end
end

#fetchObject



68
69
70
71
72
73
74
75
76
# File 'lib/tachikoma/application.rb', line 68

def fetch
  clean
  sh(*([
    'git', 'clone',
    *@depth_option,
    @authorized_base_url,
    "#{Tachikoma.repos_path}/#{@build_for}"
  ].compact))
end

#git_clone_depth_option(depth) ⇒ Object



285
286
287
288
# File 'lib/tachikoma/application.rb', line 285

def git_clone_depth_option(depth)
  return [nil] unless depth
  ['--depth', depth.to_s]
end

#github_token_key(build_for) ⇒ Object

build_for = fenix-knight, github_token_key = TOKEN_FENIX_KNIGHT



222
223
224
# File 'lib/tachikoma/application.rb', line 222

def github_token_key(build_for)
  "TOKEN_#{build_for}".gsub(/-/, '_').upcase
end

#loadObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tachikoma/application.rb', line 24

def load
  @build_for = ENV['BUILD_FOR']
  @github_token = ENV[github_token_key(@build_for)]

  base_config_path = File.join(Tachikoma.original_data_path, 'default.yaml')
  base_config = YAML.safe_load_file(base_config_path) || {}
  user_config_path = File.join(Tachikoma.data_path, '__user_config__.yaml')
  user_config = YAML.safe_load_file(user_config_path) if File.exist?(user_config_path)
  user_config ||= {}
  each_config_path = File.join(Tachikoma.data_path, "#{@build_for}.yaml")
  each_config = YAML.safe_load_file(each_config_path) if File.exist?(each_config_path)
  unless each_config
    fail %(Something wrong, BUILD_FOR: #{@build_for}, your config_path: #{each_config_path})
  end

  @configure = base_config.merge(user_config).merge(each_config)

  @commiter_name = @configure['commiter_name']
  @commiter_email = @configure['commiter_email']
  @github_account = @configure['github_account']
  @url = @configure['url']
  @type = @configure['type']
  @base_remote_branch = @configure['base_remote_branch']
  @authorized_compare_url = authorized_compare_url_with_type(@url, @type, @github_token, @github_account)
  @authorized_base_url = authorized_base_url_with_type(@url, @type, @github_token, @github_account)
  @timestamp_format = @configure['timestamp_format']
  @readable_time = Time.now.utc.strftime(@timestamp_format)
  @parallel_option = bundler_parallel_option(Bundler::VERSION, @configure['bundler_parallel_number'])
  @depth_option = git_clone_depth_option(@configure['git_clone_depth'])
  @bundler_restore_bundled_with = @configure['bundler_restore_bundled_with']

  @target_head = target_repository_user(@type, @url, @github_account)
  @pull_request_url = repository_identity(@url)
  @pull_request_body = @configure['pull_request_body']
  @pull_request_base = @configure['pull_request_base']
  @pull_request_head = "#{@target_head}:tachikoma/update-#{@readable_time}"
  @pull_request_title = "Exec tachikoma update #{@readable_time}"
end

#noneObject



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tachikoma/application.rb', line 143

def none
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh(*['git', 'config', 'user.name', @commiter_name])
    sh(*['git', 'config', 'user.email', @commiter_email])
    sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
    sh(*['git', 'commit', '--allow-empty', '-m', "None update #{@readable_time}"]) do
      # ignore exitstatus
    end
    sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
  end
end

#path_for_fork(path, github_account) ⇒ Object



248
249
250
# File 'lib/tachikoma/application.rb', line 248

def path_for_fork(path, )
  path.sub(%r{^/[^/]+}) { '/' +  }
end

#pull_requestObject



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/tachikoma/application.rb', line 209

def pull_request
  @client = Octokit::Client.new access_token: @github_token
  @client.create_pull_request(
    @pull_request_url,
    @pull_request_base,
    @pull_request_head,
    @pull_request_title,
    @pull_request_body
  )
rescue Octokit::UnprocessableEntity
end

#repository_identity(url) ⇒ Object



263
264
265
266
267
# File 'lib/tachikoma/application.rb', line 263

def repository_identity(url)
  project_name, user_name, _ = url.split('/').reverse
  project_name_identity = strip_extension(project_name)
  user_name + '/' + project_name_identity
end

#run(strategy) ⇒ Object



17
18
19
20
21
22
# File 'lib/tachikoma/application.rb', line 17

def run(strategy)
  load
  fetch
  send(strategy) if respond_to?(strategy)
  pull_request
end

#strip_extension(name) ⇒ Object



269
270
271
272
# File 'lib/tachikoma/application.rb', line 269

def strip_extension(name)
  /\A(?<identity>.*?)(?:\.git)?\z/ =~ name
  identity
end

#target_repository_user(type, fetch_url, github_account) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/tachikoma/application.rb', line 252

def target_repository_user(type, fetch_url, )
  case type
  when 'fork'
    
  when 'shared'
    URI.parse(fetch_url).path.split('/', 3)[1]
  else
    fail InvalidType, "Invalid type: #{type}"
  end
end