Class: Pkgr::CLI

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/pkgr/cli.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CLI

Returns a new instance of CLI.



22
23
24
25
26
27
28
# File 'lib/pkgr/cli.rb', line 22

def initialize(opts = {})
  @errors = []
  @uri, @config_files, @version, @name, @host, @ref = opts.values_at(
    :uri, :config_files, :version, :name, :host, :ref
  )
  @app = nil
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



18
19
20
# File 'lib/pkgr/cli.rb', line 18

def app
  @app
end

#config_filesObject (readonly)

Returns the value of attribute config_files.



15
16
17
# File 'lib/pkgr/cli.rb', line 15

def config_files
  @config_files
end

#dirObject (readonly)

Returns the value of attribute dir.



14
15
16
# File 'lib/pkgr/cli.rb', line 14

def dir
  @dir
end

#errorsObject (readonly)

Returns the value of attribute errors.



12
13
14
# File 'lib/pkgr/cli.rb', line 12

def errors
  @errors
end

#hostObject (readonly)

Returns the value of attribute host.



19
20
21
# File 'lib/pkgr/cli.rb', line 19

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/pkgr/cli.rb', line 17

def name
  @name
end

#refObject (readonly)

Returns the value of attribute ref.



20
21
22
# File 'lib/pkgr/cli.rb', line 20

def ref
  @ref
end

#uriObject (readonly)

Returns the value of attribute uri.



13
14
15
# File 'lib/pkgr/cli.rb', line 13

def uri
  @uri
end

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/pkgr/cli.rb', line 16

def version
  @version
end

Instance Method Details

#buildObject



53
54
55
56
57
58
59
# File 'lib/pkgr/cli.rb', line 53

def build
  if host.nil?
    puts "Can't build the package. You must pass the --host option for this."
  else
    @app.build_debian_package(host)
  end
end

#bumpObject



61
62
63
# File 'lib/pkgr/cli.rb', line 61

def bump
  @app.bump!(:custom, version)
end

#bundleObject



65
66
67
68
69
# File 'lib/pkgr/cli.rb', line 65

def bundle
  sh "bundle install"
  sh "git add -f Gemfile.lock"
  sh "if git status --porcelain | grep Gemfile.lock; then git commit -m '[pkgr] Update Gemfile.lock.'; fi"
end

#checkoutObject



71
72
73
# File 'lib/pkgr/cli.rb', line 71

def checkout
  sh "if git branch | grep '#{pkgr_branch}'; then git checkout #{pkgr_branch}; else git checkout -b #{pkgr_branch} #{ref}; fi"
end

#clone_repositoryObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pkgr/cli.rb', line 75

def clone_repository
  parsed_uri = URI.parse(uri)
  case parsed_uri.scheme
  when nil, "file"
    @dir = parsed_uri.path
  else
    @dir = File.basename(uri, ".git")
    sh "git clone #{uri}"
  end
  @dir = File.expand_path @dir
end

#configure_appObject

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pkgr/cli.rb', line 87

def configure_app
  @app = Pkgr::App.new(dir, "config/pkgr.yml")
  @app.config['git_ref'] = pkgr_branch
  @app.config['config_files'].push(*Dir["config/*.yml"].map{|f| File.basename(f)}).uniq!
  if name.nil?
    @app.config['name'] = File.basename(dir) if @app.name.nil?
  else
    @app.config['name'] = name
  end
  raise Error, "The app is not correctly configured: #{@app.errors.join(", ")}" unless @app.valid?
  @app.write_config
end

#copy_example_config_filesObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/pkgr/cli.rb', line 116

def copy_example_config_files
  [".example", ".dist"].each do |pattern|
    Dir["config/*.yml#{pattern}"].each do |file|
      target = File.basename(file, pattern)
      unless File.exist?("config/#{target}")
        FileUtils.cp(file, "config/#{target}")
      end
    end
  end
end

#copy_remote_config_filesObject

Download the given config files



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pkgr/cli.rb', line 101

def copy_remote_config_files
  (config_files || []).each do |file|
    filename, file_uri = file.split("::")
    if file_uri.nil?
      file_uri = filename
      filename = File.basename(file_uri)
    end

    file_uri = File.expand_path(file_uri) if URI.parse(file_uri).scheme.nil?
    target = "config/#{filename}"
    puts "Copying #{file_uri} into #{target}..."
    File.open(target, "w+") { |f| f << open(file_uri).read }
  end
end

#generateObject



127
128
129
130
131
132
133
# File 'lib/pkgr/cli.rb', line 127

def generate
  @app.generate_required_files
  sh "git add debian/"
  sh "if git status --porcelain | grep debian/; then git commit -m '[pkgr] Add debian files.'; fi"
  sh "git add bin/"
  sh "if git status --porcelain | grep bin/; then git commit -m '[pkgr] Add executable file.'; fi"
end

#pkgr_branchObject



135
136
137
# File 'lib/pkgr/cli.rb', line 135

def pkgr_branch
  "pkgr-#{ref}"
end

#runObject

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pkgr/cli.rb', line 30

def run
  raise Error, "Can't run pkgr: #{errors.join(", ")}" unless valid?
  clone_repository
  Dir.chdir(dir) do
    checkout
    copy_remote_config_files
    copy_example_config_files
    setup
    bundle
    configure_app
    generate
    bump
    build
  end
end

#setupObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pkgr/cli.rb', line 139

def setup
  Pkgr.setup(dir)

  gemfile = File.read("Gemfile")
  unless gemfile =~ /^gem 'pkgr'/
    File.open("Gemfile", "a") do |f|
      f.puts
      f.puts "gem 'pkgr'"
    end
  end

  unless gemfile =~ /^gem 'thin'/
    File.open("Gemfile", "a") do |f|
      f.puts
      f.puts "gem 'thin'"
    end
  end

  sh "git add Gemfile"
  sh" if git status --porcelain | grep Gemfile; then git commit -m '[pkgr] Update Gemfile.'; fi"
  sh "git add -f config/*.yml"
  sh" if git status --porcelain | grep config/*.yml; then git commit -m '[pkgr] Update configuration files.'; fi"
end

#valid?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/pkgr/cli.rb', line 46

def valid?
  @errors.clear
  @errors.push("You must pass a repository URI through --uri") if uri.nil?
  @errors.push("You must pass a version number through --bump") if version.nil?
  @errors.empty?
end