Class: Reflex::Packager::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/reflex/packager/cli.rb

Overview

Command line interface for a packager executable. Instantiated with the runtime profile to package for (Reflex by default).

Instance Method Summary collapse

Constructor Details

#initialize(profile) ⇒ CLI

Returns a new instance of CLI.



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

def initialize(profile)
  @profile = profile
end

Instance Method Details

#create(argv) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/reflex/packager/cli.rb', line 42

def create(argv)
  argv, = parse argv, "Usage: #{@profile.pod_key} new NAME"

  name = argv.shift
  raise Error, 'project name required' unless name
  raise Error, "'#{name}' already exists" if File.exist? name

  FileUtils.mkdir_p name
  File.write File.join(name, 'main.rb'),
    @profile.template.gsub('{{name}}') {name}
  File.write File.join(name, @profile.config_files.first), <<~END
    name: #{name}
    #bundle_id: com.example.#{name.downcase.gsub(/[^a-z0-9]+/, '')}
    #version: 1.0.0
    #icon: icon.png
  END
  puts "Created #{name}/"
  puts "  cd #{name} && ruby main.rb                 # run the application"
  puts "  cd #{name} && #{@profile.pod_key} package .   # package as an application"
end

#package(argv) ⇒ Object

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/reflex/packager/cli.rb', line 63

def package(argv)
  profile = @profile # capture for the instance_eval'd parse block below
  argv, params = parse argv, "Usage: #{profile.pod_key} package [options] [DIR]" do
    on '--platform PLATFORM', 'target platform (default: macos)'
    on '--config PATH',
      "config file path (default: DIR/#{profile.config_files.first})"
    on '--generate-only', 'generate project files but do not build'
    on '--verbose',       'verbose output'
  end

  dir      = argv.shift || '.'
  platform = (params[:platform] || 'macos').to_sym
  klass    = PLATFORMS[platform]
  raise Error, "unknown platform: '#{platform}'" unless klass

  config = Config.load @profile, dir, params[:config]
  klass.new(config, verbose: params[:verbose])
    .package generate_only: params[:'generate-only']
end

#run(argv) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reflex/packager/cli.rb', line 21

def run(argv)
  argv   = argv.dup
  parser = OptionParser.new do |o|
    o.on('--version')    {puts @profile.version; return}
    o.on('-h', '--help') {puts usage;            return}
  end
  parser.order! argv

  case command = argv.shift
  when 'new'     then create  argv
  when 'package' then package argv
  when nil       then puts usage
  else
    $stderr.puts "unknown command: '#{command}'", '', usage
    exit 1
  end
rescue OptionParser::ParseError, Error => e
  $stderr.puts "Error: #{e.message}"
  exit 1
end