Class: Xezat::Command::Init

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

Constant Summary

Constants included from Xezat

DATA_DIR, INI_FILE, REPOSITORY_DIR, ROOT_DIR, TEMPLATE_DIR, VERSION

Instance Method Summary collapse

Methods included from Xezat

#config, #packages, #variables

Constructor Details

#initialize(options, cygport) ⇒ Init

Returns a new instance of Init.



25
26
27
28
# File 'lib/xezat/command/init.rb', line 25

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

Instance Method Details

#executeObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/xezat/command/init.rb', line 30

def execute
  cygclass_dir = config(@options[:config])['cygwin']['cygclassdir']
  repository_variables = get_repository_variables(@options['repository'])
  raise UnoverwritableCygportError, "#{@cygport} already exists" if FileTest.exist?(@cygport) && !@options['overwrite']

  cygclasses = (@options['inherit'] || '').split(',')
  template_variables = get_template_variables(repository_variables, CygclassManager.new(cygclass_dir), cygclasses)
  File.atomic_write(@cygport) do |f|
    f.write(get_cygport(template_variables, @options['category'], @options['summary'], @options['description'], cygclasses, @cygport))
  end
end

#get_cygport(template_variables, category, summary, description, cygclasses, cygport) ⇒ Object



75
76
77
78
# File 'lib/xezat/command/init.rb', line 75

def get_cygport(template_variables, category, summary, description, cygclasses, cygport)
  erb = File.expand_path(File.join(TEMPLATE_DIR, 'cygport.erb'))
  ERB.new(File.readlines(erb).join(nil), nil, '%-').result(binding)
end

#get_repository_variables(repository) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/xezat/command/init.rb', line 42

def get_repository_variables(repository)
  if repository
    repository_file = File.expand_path(File.join(REPOSITORY_DIR, "#{repository}.json"))
    raise NoSuchRepositoryError, "No such repository: #{template}" unless FileTest.exist?(repository_file) || FileTest.readable?(repository_file)

    JSON.parse(File.read(repository_file), symbolize_names: true)
  else
    {
      HOMEPAGE: '',
      SRC_URI: ''
    }
  end
end

#get_template_variables(original_template_variables, cygclass_manager, cygclasses) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/xezat/command/init.rb', line 56

def get_template_variables(original_template_variables, cygclass_manager, cygclasses)
  vcs_class = nil
  vcs_prefix = 'SRC'
  cygclasses.each do |cygclass|
    raise NoSuchCygclassError, "No such cygclass: #{cygclass}" unless cygclass_manager.include?(cygclass.intern)

    next unless cygclass_manager.vcs?(cygclass.intern)
    raise CygclassConflictError, "#{cygclass} conflict with #{vcs_class}" if vcs_class

    vcs_class = cygclass
  end
  vcs_prefix = vcs_class.to_s.upcase if vcs_class
  vcs_uri = "#{vcs_prefix}_URI".intern
  {
    :HOMEPAGE => original_template_variables[:HOMEPAGE],
    vcs_uri => original_template_variables[vcs_uri]
  }
end