Method: OpenC3::LocalMode.update_local_plugin

Defined in:
lib/openc3/utilities/local_mode.rb

.update_local_plugin(plugin_file_path, plugin_hash, old_plugin_name: nil, scope:) ⇒ Object

If old_plugin_name then this is an online upgrade



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/openc3/utilities/local_mode.rb', line 210

def self.update_local_plugin(plugin_file_path, plugin_hash, old_plugin_name: nil, scope:)
  if ENV['OPENC3_LOCAL_MODE'] and Dir.exist?(OPENC3_LOCAL_MODE_PATH)
    variables = plugin_hash['variables']
    if variables
      PluginModel::RESERVED_VARIABLE_NAMES.each do |name|
        variables.delete(name)
      end
    end
    if plugin_file_path =~ Regexp.new("^#{OPENC3_LOCAL_MODE_PATH}/#{scope}/")
      # From local init - Always just update the exact one
      File.open(File.join(File.dirname(plugin_file_path), 'plugin_instance.json'), 'wb') do |file|
        file.write(JSON.pretty_generate(plugin_hash, :allow_nan => true))
      end
    else
      # From online install / update
      # Or init install of container plugin
      # Try to find an existing local folder for this plugin
      found = false

      gem_name = File.basename(plugin_file_path).split('-')[0..-2].join('-')
      FileUtils.mkdir_p("#{OPENC3_LOCAL_MODE_PATH}/#{scope}")

      Dir.each_child("#{OPENC3_LOCAL_MODE_PATH}/#{scope}") do |plugin_dir|
        full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/#{plugin_dir}"
        next if plugin_dir == "targets_modified" or not File.directory?(full_folder_path)

        gems, plugin_instance = scan_plugin_dir(full_folder_path)
        next if gems.length > 1

        if gems.length == 1
          found_gem_name = File.basename(gems[0]).split('-')[0..-2].join('-')
          if found_gem_name == gem_name
            # Same gem at least - Now see if same instance
            if plugin_instance
              if old_plugin_name
                # And we're updating a plugin
                data = File.read(plugin_instance)
                json = JSON.parse(data, :allow_nan => true, :create_additions => true)
                if json["name"] == old_plugin_name
                  # Found plugin to update
                  found = true
                  update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name)
                end
              else
                # New install of same plugin - Leave it alone
              end
            else
              # No existing instance.json, but we found the same gem
              # This shouldn't happen without users using this wrong
              # We will update
              found = true
              update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name)
            end
          end
        end
      end

      unless found
        # Then we will make a local version
        # Create a folder for this plugin and add gem and plugin_instance.json
        folder_name = gem_name
        count = 1
        while File.exist?("#{OPENC3_LOCAL_MODE_PATH}/#{scope}/#{folder_name}")
          folder_name = gem_name + "-" + count.to_s
          count += 1
        end
        full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/#{folder_name}"
        update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name)
      end
    end
  end
end