Class: Xezat::Command::Bump

Inherits:
Object
  • Object
show all
Includes:
Xezat
Defined in:
lib/xezat/command/bump.rb

Constant Summary

Constants included from Xezat

DATA_DIR, INI_FILE, LOG, REPOSITORY_DIR, ROOT_DIR, TEMPLATE_DIR, VERSION

Instance Method Summary collapse

Methods included from Xezat

#config, #packages, #variables

Constructor Details

#initialize(options, cygport) ⇒ Bump

Returns a new instance of Bump.



24
25
26
27
# File 'lib/xezat/command/bump.rb', line 24

def initialize(options, cygport)
  @options = options
  @cygport = cygport
end

Instance Method Details

#executeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xezat/command/bump.rb', line 29

def execute
  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(@cygport),
      developments: get_development_packages(vars, pkgs),
      files: get_files(vars),
      changelog: get_changelog(vars, @options, readme_file)
  }

  File.atomic_write(readme_file) do |f|
    f.write(get_embedded_contents(vars, info))
  end
end

#get_changelog(variables, options, readme_file) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/xezat/command/bump.rb', line 145

def get_changelog(variables, options, readme_file)
  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.'
    changelog[current_version] = message unless changelog.key?(current_version)
  else
    changelog = Cygchangelog.new
    changelog[current_version] = 'Initial release by fd0 <https://github.com/fd00/>'
  end
  changelog
end

#get_compilers(languages, variables) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/xezat/command/bump.rb', line 76

def get_compilers(languages, variables)
  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]
    if compiler_candidate['package'] == 'python'
      pkg_names = variables[:PKG_NAMES] || variables[:PN]
      if pkg_names.include?('python3-')
        compilers << :'python3'
      elsif pkg_names.include?('pypi-')
        compilers << :'pypi'
      else
        compilers << compiler_candidate['package'].intern
      end
    else
      compilers << compiler_candidate['package'].intern
    end
    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) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/xezat/command/bump.rb', line 67

def get_development_packages(variables, 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) ⇒ Object



160
161
162
163
# File 'lib/xezat/command/bump.rb', line 160

def get_embedded_contents(variables, info)
  erb = File.expand_path(File.join(TEMPLATE_DIR, 'README.erb'))
  ERB.new(File.readlines(erb).join(nil), nil, '%-').result(binding).chop # remove redundant new line
end

#get_files(variables) ⇒ Object



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

def get_files(variables)
  pkg2files = {}
  variables[:pkg_name].each do |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.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) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/xezat/command/bump.rb', line 103

def get_languages(top_src_dir)
  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
end

#get_runtime_packages(cygport) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/xezat/command/bump.rb', line 58

def get_runtime_packages(cygport)
  command = ['bash', File.expand_path(File.join(DATA_DIR, 'invoke_cygport_dep.sh')), cygport]
  result, error, status = Open3.capture3(command.join(' '))
  raise CygportProcessError, error unless status.success?
  result.gsub!(/^.*\*\*\*.*$/, '')
  result.split($INPUT_RECORD_SEPARATOR).map!(&:lstrip)
end

#get_src_uri(vars, cygclasses = CygclassManager.new) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/xezat/command/bump.rb', line 48

def get_src_uri(vars, cygclasses = CygclassManager.new)
  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) ⇒ Object



120
121
122
# File 'lib/xezat/command/bump.rb', line 120

def get_tools(variables)
  DetectorManager.new().detect(variables)
end