class NetLinx::Workspace
A NetLinx
Studio workspace. Collection of projects. Workspace
-> Project
-> System
Attributes
Public Class Methods
@option kwargs [String] :name ('') Workspace
name. @option kwargs [String] :description ('')
# File lib/netlinx/workspace.rb, line 45 def initialize **kwargs @name = kwargs.fetch :name, '' @description = kwargs.fetch :description, '' @projects = [] @file = kwargs.fetch :file, nil load_workspace @file if @file end
Search backwards through directory tree and return the first workspace found. @return [NetLinx::Workspace]
# File lib/netlinx/workspace.rb, line 20 def self.search dir: nil yaml_config_file_name = 'workspace.config.yaml' dir ||= File.expand_path '.' while dir != File.expand_path('..', dir) workspace_files = Dir["#{dir}/{#{yaml_config_file_name},*.apw}"] dir = File.expand_path('..', dir) next if workspace_files.empty? workspace_files.first.tap do |workspace_file| if workspace_file.end_with? "/#{yaml_config_file_name}" Dir.chdir File.dirname(workspace_file) do return NetLinx::Workspace::YAML.parse_file yaml_config_file_name end else return new file: workspace_files.first end end end nil end
Public Instance Methods
Alias to add a project.
# File lib/netlinx/workspace.rb, line 55 def << project @projects << project project.workspace = self end
Compile
all projects in this workspace.
# File lib/netlinx/workspace.rb, line 83 def compile compiler_results = [] @projects.map do |project| project.systems.select do |system| ENV['NETLINX_ACTIVE_SYSTEM_ONLY'] ? system.active : true end end.flatten.each do |system| system.compile.each { |result| compiler_results << result } end compiler_results end
@return true if the workspace contains the specified file.
# File lib/netlinx/workspace.rb, line 71 def include? file included = false projects.each do |project| included = project.include? file break if included end included end
Generate a {NetLinx::Workspace} from an XML string. @return self
# File lib/netlinx/workspace.rb, line 127 def parse_xml string parse_xml_element REXML::Document.new(string) self end
Directory the workspace resides in.
# File lib/netlinx/workspace.rb, line 66 def path File.dirname @file if @file end
Returns the name of the workspace.
# File lib/netlinx/workspace.rb, line 61 def to_s @name end
@return [String] an XML string representing this workspace.
@todo REXML bug forces :indent to be -1 or else erroneous line feeds are added.
https://bugs.ruby-lang.org/issues/10864
# File lib/netlinx/workspace.rb, line 114 def to_xml indent: -1 str = '<?xml version="1.0" encoding="UTF-8"?>' + "\n" REXML::Document.new.tap do |doc| doc << to_xml_element doc.write output: str, indent: indent end str + "\n" end
@return [REXML::Element] an XML element representing this workspace.
# File lib/netlinx/workspace.rb, line 98 def to_xml_element REXML::Element.new('Workspace').tap do |workspace| workspace.attributes['CurrentVersion'] = '4.0' workspace.add_element('Identifier').tap { |e| e.text = name } workspace.add_element('CreateVersion').tap { |e| e.text = '4.0' } workspace.add_element('Comments').tap { |e| e.text = description } @projects.each { |project| workspace << project.to_xml_element } end end
Private Instance Methods
Load the workspace from a given NetLinx
Studio .apw file.
# File lib/netlinx/workspace.rb, line 135 def load_workspace file raise LoadError, "File does not exist at:\n#{file}" unless File.exists? file doc = nil File.open file, 'r' do |f| doc = REXML::Document.new f end parse_xml_element doc end
# File lib/netlinx/workspace.rb, line 146 def parse_xml_element root # Load workspace params. @name = root.elements['/Workspace/Identifier'].text.strip || '' @description = root.elements['/Workspace/Comments'].text || '' # Load projects. root.each_element '/Workspace/Project' do |e| project = NetLinx::Project.new \ element: e, workspace: self @projects << project end end