Method: OpenC3::PluginModel.install_phase1

Defined in:
lib/openc3/models/plugin_model.rb

.install_phase1(gem_file_path, existing_variables: nil, existing_plugin_txt_lines: nil, process_existing: false, scope:, validate_only: false) ⇒ Object

Called by the PluginsController to parse the plugin variables Doesn’t actaully create the plugin during the phase



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
# File 'lib/openc3/models/plugin_model.rb', line 73

def self.install_phase1(gem_file_path, existing_variables: nil, existing_plugin_txt_lines: nil, process_existing: false, scope:, validate_only: false)
  gem_name = File.basename(gem_file_path).split("__")[0]

  temp_dir = Dir.mktmpdir
  tf = nil
  begin
    if File.exist?(gem_file_path)
      # Load gem to internal gem server
      OpenC3::GemModel.put(gem_file_path, gem_install: false, scope: scope) unless validate_only
    else
      gem_file_path = OpenC3::GemModel.get(gem_name)
    end

    # Extract gem and process plugin.txt to determine what VARIABLEs need to be filled in
    pkg = Gem::Package.new(gem_file_path)

    if existing_plugin_txt_lines and process_existing
      # This is only used in openc3cli load when everything is known
      plugin_txt_lines = existing_plugin_txt_lines
      file_data = existing_plugin_txt_lines.join("\n")
      tf = Tempfile.new("plugin.txt")
      tf.write(file_data)
      tf.close
      plugin_txt_path = tf.path
    else
      # Otherwise we always process the new and return both
      pkg.extract_files(temp_dir)
      plugin_txt_path = File.join(temp_dir, 'plugin.txt')
      plugin_text = File.read(plugin_txt_path)
      plugin_txt_lines = []
      plugin_text.each_line do |line|
        plugin_txt_lines << line.chomp
      end
    end

    parser = OpenC3::ConfigParser.new("https://openc3.com")

    # Phase 1 Gather Variables
    variables = {}
    parser.parse_file(plugin_txt_path,
                      false,
                      true,
                      false) do |keyword, params|
      case keyword
      when 'VARIABLE'
        usage = "#{keyword} <Variable Name> <Default Value>"
        parser.verify_num_parameters(2, nil, usage)
        variable_name = params[0]
        if RESERVED_VARIABLE_NAMES.include?(variable_name)
          raise "VARIABLE name '#{variable_name}' is reserved"
        end
        value = params[1..-1].join(" ")
        variables[variable_name] = value
        if existing_variables && existing_variables.key?(variable_name)
          variables[variable_name] = existing_variables[variable_name]
        end
      end
    end

    model = PluginModel.new(name: gem_name, variables: variables, plugin_txt_lines: plugin_txt_lines, scope: scope)
    result = model.as_json(:allow_nan => true)
    result['existing_plugin_txt_lines'] = existing_plugin_txt_lines if existing_plugin_txt_lines and not process_existing and existing_plugin_txt_lines != result['plugin_txt_lines']
    return result
  ensure
    FileUtils.remove_entry(temp_dir) if temp_dir and File.exist?(temp_dir)
    tf.unlink if tf
  end
end