Class: PluginTool::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/plugin.rb

Constant Summary collapse

DEFAULT_PLUGIN_LOAD_PRIORITY =
9999999
@@pending_apply =

[]
@@apply_with_turbo =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, options) ⇒ Plugin

Returns a new instance of Plugin.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/plugin.rb', line 13

def initialize(dir, options)
  @plugin_dir = dir
  @options = options
  @@apply_with_turbo = true if options.turbo
  # Check to see if the plugin is valid
  unless File.file?("#{@plugin_dir}/plugin.json")
    end_on_error "Plugin #{@plugin_dir} does not exist (no plugin.json file)"
  end
  pj = File.open("#{@plugin_dir}/plugin.json") { |f| JSON.parse(f.read) }
  @name = pj["pluginName"]
  end_on_error "#{@plugin_dir}/plugin.json: invalid pluginName" unless @name.kind_of?(String)
  @depend = pj["depend"] || []
  end_on_error "#{@plugin_dir}/plugin.json: invalid pluginName" unless @depend.kind_of?(Array)
end

Instance Attribute Details

#dependObject

Returns the value of attribute depend.



29
30
31
# File 'lib/plugin.rb', line 29

def depend
  @depend
end

#loaded_plugin_idObject

Returns the value of attribute loaded_plugin_id.



30
31
32
# File 'lib/plugin.rb', line 30

def loaded_plugin_id
  @loaded_plugin_id
end

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/plugin.rb', line 27

def name
  @name
end

#plugin_dirObject

Returns the value of attribute plugin_dir.



28
29
30
# File 'lib/plugin.rb', line 28

def plugin_dir
  @plugin_dir
end

Class Method Details

.do_applyObject




206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/plugin.rb', line 206

def self.do_apply
  return if @@pending_apply.empty?
  puts "Applying changes on server: #{@@pending_apply.map { |p| p.name } .join(', ')}"
  params = {
    :plugins => @@pending_apply.map { |p| p.loaded_plugin_id }.join(' ')
  }
  params[:turbo] = '1' if @@apply_with_turbo
  r = PluginTool.post_with_json_response("/api/development-plugin-loader/apply", params)
  if r["result"] == 'success'
    @@pending_apply = []
  else
    puts "\n\nDidn't apply changes on server\n\n"
    PluginTool.beep
  end
end

Instance Method Details

#command(cmd, errors) ⇒ Object




101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/plugin.rb', line 101

def command(cmd, errors)
  case cmd
  when 'license-key'
    application_id = @options.args.first
    if application_id == nil || application_id !~ /\A\d+\z/
      end_on_error "Numeric application ID must be specified"
    end
    generate_license_key(application_id)

  when 'pack'
    PluginTool.pack_plugin(self, @options.output, errors)

  when 'reset-db'
    puts "Resetting database on server for #{@name}..."
    reset_result = PluginTool.post_with_json_response("/api/development-plugin-loader/resetdb/#{@loaded_plugin_id}")
    end_on_error "Couldn't remove old database tables" unless reset_result["result"] == 'success'
    apply_result = PluginTool.post_with_json_response("/api/development-plugin-loader/apply", :plugins => @loaded_plugin_id)
    end_on_error "Couldn't apply changes" unless apply_result["result"] == 'success'
    puts "Done."

  when 'uninstall'
    puts "Uninstalling plugin #{@name} from server..."
    reset_result = PluginTool.post_with_json_response("/api/development-plugin-loader/uninstall/#{@loaded_plugin_id}")
    end_on_error "Couldn't uninstall plugin" unless reset_result["result"] == 'success'
    puts "Done."

  when 'test'
    puts "Running tests..."
    params = {}
    params["test"] = @options.args.first unless @options.args.empty?
    test_result = PluginTool.post_with_json_response("/api/development-plugin-loader/run-tests/#{@loaded_plugin_id}", params)
    end_on_error "Couldn't run tests" unless test_result["result"] == 'success'
    puts
    puts test_result["output"] || ''
    puts test_result["summary"] || "(unknown results)"

  when 'develop'
    # do nothing here

  else
    end_on_error "Unknown command '#{cmd}'"

  end
end

#develop_scan_and_upload(first_run) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/plugin.rb', line 151

def develop_scan_and_upload(first_run)
  should_apply = first_run
  next_manifest = PluginTool.generate_manifest(@plugin_dir)
  if !(next_manifest.has_key?("plugin.json"))
    # If the plugin.json file is deleted, just uninstall the plugin from the server
    command('uninstall')
    @is_uninstalled = true
    return
  elsif @is_uninstalled
    should_apply = true
  end
  changes = PluginTool.determine_manifest_changes(@current_manifest, next_manifest)
  upload_failed = false
  changes.each do |filename, action|
    filename =~ /\A(.*?\/)?([^\/]+)\z/
    params = {:filename => $2}
    params[:directory] = $1.gsub(/\/\z/,'') if $1
    if action == :delete
      puts "  #{@name}: Deleting #{filename}"
      PluginTool.post_with_json_response("/api/development-plugin-loader/delete-file/#{@loaded_plugin_id}", params)
    else
      puts "  #{@name}: Uploading #{filename}"
      data = File.open("#{@plugin_dir}/#{filename}") { |f| f.read }
      hash = action
      # Minimise file before uploading?
      if @options.minimiser != nil && filename =~ /\A(static|template)\//
        size_before = data.length
        data = @options.minimiser.process(data, filename)
        size_after = data.length
        hash = Digest::SHA256.hexdigest(data)
        puts "        minimisation: #{size_before} -> #{size_after} (#{(size_after * 100) / size_before}%)"
      end
      r = PluginTool.post_with_json_response("/api/development-plugin-loader/put-file/#{@loaded_plugin_id}", params, {:file => [filename, data]})
      if r["result"] == 'success'
        # If the file was uploaded successfully, but the hash didn't match, abort now
        end_on_error "#{@name}: Disagreed with server about uploaded file hash: local=#{hash}, remote=#{r["hash"]}" unless hash == r["hash"]
      else
        # Otherwise mark as a failed upload to stop an apply operation which will fail
        upload_failed = true
      end
      PluginTool.syntax_check(self, filename) if filename =~ SYNTAX_CHECK_REGEXP
    end
  end
  if upload_failed
    puts "\n#{@name}: Not applying changes due to failure\n\n"
  else
    if !(changes.empty?) || should_apply
      @@pending_apply.push(self) unless @@pending_apply.include?(self)
    end
  end
  @current_manifest = next_manifest
end

#develop_setupObject




148
149
# File 'lib/plugin.rb', line 148

def develop_setup
end

#developer_jsonObject




77
78
79
80
81
82
83
84
85
86
# File 'lib/plugin.rb', line 77

def developer_json
  @developer_json ||= begin
    developer_json_pathname = "#{@plugin_dir}/developer.json"
    if File.exist? developer_json_pathname
      JSON.parse(File.read(developer_json_pathname))
    else
      {}
    end
  end
end

#end_on_error(err) ⇒ Object



238
239
240
241
# File 'lib/plugin.rb', line 238

def end_on_error(err)
  puts err
  exit 1
end

#exclude_files_from_syntax_checkObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/plugin.rb', line 88

def exclude_files_from_syntax_check
  @exclude_files_from_syntax_check ||= begin
    # developer.json file might contain some files which should not be syntax checked
    if developer_json['excludeFromSyntaxCheck'].kind_of?(Array)
      developer_json['excludeFromSyntaxCheck']
    else
      []
    end
  end
end

#generate_license_key(application_id) ⇒ Object




224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/plugin.rb', line 224

def generate_license_key(application_id)
  info = File.open("#{@plugin_dir}/plugin.json") { |f| JSON.parse(f.read) }
  if info["installSecret"] == nil
    end_on_error "#{@name}: No installSecret specified in plugin.json"
  end
  license_key = HMAC::SHA1.sign(info["installSecret"], "application:#{application_id}")
  puts "\nPlugin:       \#{@name}\nApplication:  \#{application_id}\nLicense key:  \#{license_key}\n"
end

#plugin_load_priorityObject



44
45
46
47
# File 'lib/plugin.rb', line 44

def plugin_load_priority
  pj = File.open("#{@plugin_dir}/plugin.json") { |f| JSON.parse(f.read) }
  pj['loadPriority'] || DEFAULT_PLUGIN_LOAD_PRIORITY
end

#setup_for_serverObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/plugin.rb', line 49

def setup_for_server
  # Make the first empty manifest (may be replaced from server)
  @current_manifest = {}

  # If minimisation is active, clear the current manifest so all files are uploaded again.
  if @options.minimiser != nil
    @current_manifest = {}
  end

  # See if the plugin has already been registered with the server
  s_found_info = PluginTool.post_with_json_response("/api/development-plugin-loader/find-registration", {:name => @name})
  if s_found_info["found"]
    # Store info returned by the server
    @loaded_plugin_id = s_found_info["plugin_id"]
    @current_manifest = s_found_info["manifest"]
  end

  # If there isn't an existing plugin registered, create a new one
  if @loaded_plugin_id == nil
    s_create_info = PluginTool.post_with_json_response("/api/development-plugin-loader/create")
    end_on_error "Couldn't communicate successfully with server." if s_create_info["protocol_error"]
    end_on_error "Failed to create plugin on server" unless s_create_info["plugin_id"] != nil
    @loaded_plugin_id = s_create_info["plugin_id"]
  end
end

#startObject




39
40
41
42
# File 'lib/plugin.rb', line 39

def start
  # Setup for using the plugin
  @loaded_plugin_id = nil
end