Class: KRL_CMD::Generate

Inherits:
Object
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/generate.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options) ⇒ Generate

Returns a new instance of Generate.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generate.rb', line 15

def initialize(endpoint, options)
  @shell = Basic.new
  @endpoint = endpoint
  @environment = options["environment"]
  @use_defaults = options["defaults"]
  @force = options["force"]
  @app = KRL_COMMON::get_app  
  @user = KRL_CMD::User.new
  endpoints = {
    "firefox" => lambda {gen_extension},
    "chrome" => lambda {gen_extension},
    "ie" => lambda {gen_extension},
    "bookmarklet" => lambda {gen_bookmarklet},
    "infocard" => lambda {gen_infocard}
  }
  if endpoints[@endpoint]
    endpoints[@endpoint].call
  else
    raise "Unknown endpoint specified (#{@endpoint})"
  end

  
end

Class Method Details

.go(endpoint, options) ⇒ Object



11
12
13
# File 'lib/generate.rb', line 11

def self.go(endpoint, options)
  g = self.new(endpoint,options)
end

Instance Method Details

#gen_bookmarkletObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/generate.rb', line 62

def gen_bookmarklet
  puts "Generating Bookmarklet (#{@environment})"
  bm = ""
  opts = {}
  opts[:env] = @environment
  if ! @use_defaults
    opts[:runtime] = @shell.ask("Runtime to use for the bookmarklet (blank for default):")
  end

  endpoint = @app.endpoint(:bookmarklet, opts)
  
  if endpoint["errors"].empty?
    bm = endpoint["data"]
  else
    raise "Invalid bookmark generated. \n#{endpoint.errors.join("\n")}"
  end
  
  bm_file = File.join(get_endpoint_dir, @environment + "_bookmarklet.html")
  File.open(bm_file, 'w') do |f|
    f.print("<textarea rows='5' cols='100'>#{bm}</textarea><br><br>")
    link = @environment == "prod" ? @app.name : @environment + "_" + @app.name
    f.print("<a href=\"#{bm.gsub('"', '&quot;')}\">#{link}</a>")
  end
  puts "BOOKMARKLET:"
  puts bm
  puts
  puts "Saved bookmarklet to #{bm_file}."
  Launchy::Browser.run('file://' + bm_file)
end

#gen_extensionObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generate.rb', line 39

def gen_extension
  puts "Generating Extension (#{@endpoint} - #{@environment})"

  if @use_defaults
    extname, extauthor, extdesc = "", "", ""
  else
    extname = @shell.ask("Name of the extension (#{@app.name}):")
    extauthor = @shell.ask("Name of the author (#{@user.name}):")
    extdesc = @shell.ask("Description:")
  end
 
  opts = {}
  opts[:extname] = extname unless extname.empty? 
  opts[:extauthor] = extauthor unless extauthor.empty? 
  opts[:extdesc] = extdesc unless extdesc.empty?
  opts[:env] = @environment
  opts[:force_build] = @force ? "Y" : "N"
  opts[:format] = 'url'

  ext = @app.endpoint(@endpoint, opts)
  write_file(ext)      
end

#gen_infocardObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/generate.rb', line 92

def gen_infocard
  puts "Generating Infocard (#{@environment})"

  if @use_defaults
    extname = ""
    datasets = ""
  else
    extname = @shell.ask("Name of the infocard (#{@app.name}):")
    datasets = @shell.ask("Datasets:")
  end

  opts = {}
  opts[:extname] = extname unless extname.empty?
  opts[:datasets] = datasets unless datasets.empty?
  opts[:env] = @environment
  opts[:format] = 'url'

  icard = @app.endpoint(:info_card, opts)
  write_file(icard)
end