class Xezat::Command::Bump

Public Class Methods

new(options, cygport) click to toggle source
# File lib/xezat/command/bump.rb, line 22
def initialize(options, cygport)
  @options = options
  @cygport = cygport
end

Public Instance Methods

execute() click to toggle source
# File lib/xezat/command/bump.rb, line 27
def execute
  Xezat.logger.debug('Start bumping')
  pkgs = packages
  vars = variables(@cygport)
  readme_file = File.expand_path(File.join(vars[:C], 'README'))

  info = {
    src_uri: get_src_uri(vars),
    runtimes: get_runtime_packages(vars, @cygport),
    developments: get_development_packages(vars, pkgs),
    files: get_files(vars),
    changelog: get_changelog(vars, @options, readme_file)
  }

  Xezat.logger.debug('  Write ChangeLog atomically')
  File.atomic_write(readme_file) do |f|
    f.write(get_embedded_contents(vars, info))
  end

  Xezat.logger.debug('End bumping')
end
get_changelog(variables, options, readme_file) click to toggle source
# File lib/xezat/command/bump/changelog.rb, line 11
def get_changelog(variables, options, readme_file)
  Xezat.logger.debug('  Try to append latest log to changelog...')
  current_version = variables[:PVR].intern
  if FileTest.exist?(readme_file)
    raise FilePermissionError, "Cannot read #{readme_file}" unless FileTest.readable?(readme_file)
    raise FilePermissionError, "Cannot write #{readme_file}" unless FileTest.writable?(readme_file)

    changelog = Cygchangelog.new(File.read(readme_file))
    message = options['message'] || 'Version bump.'
    if changelog.length > 1 || !changelog.key?(current_version)
      changelog[current_version] = message # overwrite unless initial package
      Xezat.logger.debug("    '#{message}' appended")
    else
      Xezat.logger.warn('    Initial release protected')
    end
  else
    changelog = Cygchangelog.new
    changelog[current_version] = 'Initial release by fd0 <https://github.com/fd00/>'
    Xezat.logger.debug('    Initial release by you')
  end
  changelog
end
get_compilers(languages, _variables) click to toggle source
# File lib/xezat/command/bump/compiler.rb, line 8
def get_compilers(languages, _variables)
  Xezat.logger.debug('  Collect compilers')
  compiler_file = File.expand_path(File.join(DATA_DIR, 'compilers.json'))
  compiler_candidates = JSON.parse(File.read(compiler_file))
  compilers = []
  languages.uniq.each do |language|
    next unless compiler_candidates.key?(language)

    compiler_candidate = compiler_candidates[language]
    compilers << compiler_candidate['package'].intern
    next unless compiler_candidate.key?('dependencies')

    compiler_candidate['dependencies'].each do |dependency|
      compilers << dependency.intern
    end
  end
  compilers.uniq
end
get_development_packages(variables, packages) click to toggle source
# File lib/xezat/command/bump/development_package.rb, line 11
def get_development_packages(variables, packages)
  Xezat.logger.debug('  Collect development packages')
  compilers = get_compilers(get_languages(variables[:S]), variables)
  tools = get_tools(variables)
  development_packages = (compilers + tools + [:cygport]).uniq.sort
  development_packages.map! do |package|
    packages[package] || ''
  end
end
get_embedded_contents(variables, info) click to toggle source
# File lib/xezat/command/bump.rb, line 49
def get_embedded_contents(variables, info)
  erb = File.expand_path(File.join(TEMPLATE_DIR, 'README.erb'))
  ERB.new(File.readlines(erb).join(nil), trim_mode: '%-').result(binding).chop # remove redundant new line
end
get_files(variables) click to toggle source
# File lib/xezat/command/bump/file.rb, line 11
def get_files(variables)
  Xezat.logger.debug('  Collect files')
  pkg2files = {}
  variables[:pkg_name].each do |pkg_name|
    Xezat.logger.debug("    Collect #{pkg_name}")
    lst_file = File.expand_path(File.join(variables[:T], ".#{pkg_name}.lst"))
    raise IllegalStateError, "No such file: #{lst_file}" unless FileTest.readable?(lst_file)

    lines = File.readlines(lst_file)
    lines.delete_if do |path|
      path.strip!
      path[-1] == File::SEPARATOR # ignore directory
    end
    lines.map! do |path|
      File::SEPARATOR + path
    end
    if variables[:PN] == pkg_name
      readme = File::SEPARATOR + File.join('usr', 'share', 'doc', 'Cygwin', "#{pkg_name}.README")
      lines << readme.strip unless lines.include?(readme)
    end
    pkg2files[pkg_name.intern] = lines.sort
  end
  pkg2files
end
get_languages(top_src_dir) click to toggle source
# File lib/xezat/command/bump/language.rb, line 9
def get_languages(top_src_dir)
  Xezat.logger.debug('  Collect languages')
  languages_file = File.expand_path(File.join(DATA_DIR, 'languages.json'))
  languages_candidates = JSON.parse(File.read(languages_file))
  languages = []
  Find.find(top_src_dir) do |path|
    next if FileTest.directory?(path)

    name = languages_candidates[File.extname(path)]
    if name.nil?
      language = Xezat::Linguist::FileBlob.new(path).language
      next if language.nil?

      name = language.name
    end
    languages << name
  end
  languages.uniq
end
get_runtime_packages(vars, cygport) click to toggle source
# File lib/xezat/command/bump/runtime_package.rb, line 10
def get_runtime_packages(vars, cygport)
  Xezat.logger.debug('  Collect runtime packages from cygport dep')
  result = invoke_cygport_dep(vars, cygport)
  result.gsub(/^.*\*\*\*.*$/, '').split($INPUT_RECORD_SEPARATOR).map(&:lstrip)
end
get_src_uri(vars, cygclasses = CygclassManager.new) click to toggle source
# File lib/xezat/command/bump/src_uri.rb, line 9
def get_src_uri(vars, cygclasses = CygclassManager.new)
  Xezat.logger.debug('  Collect SRC_URI')
  cygclasses.vcs.each do |vcs|
    next unless vars.key?("_#{vcs}_CYGCLASS_".intern)

    src_uri_key = "#{vcs.to_s.upcase}_URI".intern
    return vars[src_uri_key].split if vars.key?(src_uri_key)
  end
  vars[:SRC_URI].split
end
get_tools(variables) click to toggle source
# File lib/xezat/command/bump/tool.rb, line 8
def get_tools(variables)
  DetectorManager.new.detect(variables)
end
invoke_cygport_dep(vars, cygport) click to toggle source
# File lib/xezat/command/bump/cygport_dep.rb, line 10
def invoke_cygport_dep(vars, cygport)
  candidate_files = Find.find(vars[:D]).select do |file|
    file.end_with?('.exe', '.dll', '.so')
  end
  additional_path = candidate_files.map do |file|
    File.dirname(file)
  end.sort.uniq.join(':')
  command = ['bash', File.expand_path(File.join(DATA_DIR, 'invoke_cygport_dep.sh')), cygport]
  result, error, status = Open3.capture3({ 'PATH' => "#{ENV['PATH']}:#{additional_path}" }, command.join(' '))
  raise CygportProcessError, error unless status.success?

  result
end