class Tachikoma::Application

Main logic of Tachikoma

Public Class Methods

run(strategy) click to toggle source
# File lib/tachikoma/application.rb, line 13
def self.run(strategy)
  new.run(strategy)
end

Public Instance Methods

authorized_base_url_with_type(fetch_url, type, github_token, _github_account) click to toggle source
# File lib/tachikoma/application.rb, line 284
def authorized_base_url_with_type(fetch_url, type, github_token, _github_account)
  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) click to toggle source
# File lib/tachikoma/application.rb, line 272
def authorized_compare_url_with_type(fetch_url, type, github_token, github_account)
  uri = URI.parse(fetch_url)
  case type
  when 'fork'
    %(#{uri.scheme}://#{github_token}:x-oauth-basic@#{uri.host}#{path_for_fork(uri.path, github_account)})
  when 'shared'
    "#{uri.scheme}://#{github_token}:x-oauth-basic@#{uri.host}#{uri.path}"
  else
    fail InvalidType, "Invalid type #{type}"
  end
end
bundle() click to toggle source
# File lib/tachikoma/application.rb, line 122
def bundle
  warn '[DEPRECATION] `bundle` is deprecated. Please use `bundler` instead.'
  bundler
end
bundler() click to toggle source
# 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) click to toggle source
# File lib/tachikoma/application.rb, line 326
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) click to toggle source

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

# File lib/tachikoma/application.rb, line 321
def bundler_parallel_option(bundler_version, parallel_number)
  return if !bundler_parallel_available?(bundler_version) || parallel_number <= 1
  "--jobs=#{parallel_number}"
end
carthage() click to toggle source
# File lib/tachikoma/application.rb, line 194
def carthage
  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(carthage bootstrap))
    sh(*%w(carthage update))
    sh(*['git', 'add', 'Cartfile.resolved'])
    sh(*['git', 'commit', '-m', "Carthage update #{@readable_time}"]) do
      # ignore exitstatus
    end
    sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
  end
end
carton() click to toggle source
# 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
clean() click to toggle source
# File lib/tachikoma/application.rb, line 63
def clean
  mkdir_p(Tachikoma.repos_path)
  rm_rf(Dir.glob(File.join(Tachikoma.repos_path, '*')))
end
cocoapods() click to toggle source
# File lib/tachikoma/application.rb, line 209
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
composer() click to toggle source
# 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
david() click to toggle source
# 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
fetch() click to toggle source
# 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) click to toggle source
# File lib/tachikoma/application.rb, line 331
def git_clone_depth_option(depth)
  return [nil] unless depth
  ['--depth', depth.to_s]
end
github_token_key(build_for) click to toggle source

build_for = fenix-knight, github_token_key = TOKEN_FENIX_KNIGHT

# File lib/tachikoma/application.rb, line 268
def github_token_key(build_for)
  "TOKEN_#{build_for}".gsub(/-/, '_').upcase
end
load() click to toggle source
# 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
ncu() click to toggle source
# File lib/tachikoma/application.rb, line 224
def ncu
  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(ncu -u))
    sh(*['git', 'add', 'package.json']) if File.exist?('package.json')
    sh(*['git', 'commit', '-m', "Ncu update #{@readable_time}"]) do
      # ignore exitstatus
    end
    sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
  end
end
none() click to toggle source
# 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) click to toggle source
# File lib/tachikoma/application.rb, line 294
def path_for_fork(path, github_account)
  path.sub(%r{^/[^/]+}) { '/' + github_account }
end
pull_request() click to toggle source
# File lib/tachikoma/application.rb, line 255
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) click to toggle source
# File lib/tachikoma/application.rb, line 309
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) click to toggle source
# File lib/tachikoma/application.rb, line 17
def run(strategy)
  load
  fetch
  send(strategy) if respond_to?(strategy)
  pull_request
end
strip_extension(name) click to toggle source
# File lib/tachikoma/application.rb, line 315
def strip_extension(name)
  /\A(?<identity>.*?)(?:\.git)?\z/ =~ name
  identity
end
target_repository_user(type, fetch_url, github_account) click to toggle source
# File lib/tachikoma/application.rb, line 298
def target_repository_user(type, fetch_url, github_account)
  case type
  when 'fork'
    github_account
  when 'shared'
    URI.parse(fetch_url).path.split('/', 3)[1]
  else
    fail InvalidType, "Invalid type: #{type}"
  end
end
yarn() click to toggle source
# File lib/tachikoma/application.rb, line 238
def yarn
  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(yarn install))
    sh(*%w(yarn outdated))
    sh(*%w(yarn upgrade))
    sh(*['git', 'add', 'package.json'])
    sh(*['git', 'add', 'yarn.lock'])
    sh(*['git', 'commit', '-m', "Yarn update #{@readable_time}"]) do
      # ignore exitstatus
    end
    sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
  end
end