Class: NetLinx::SourceFile

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

Overview

A NetLinx source code file. Typically .axs or .axi.

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ SourceFile

NOTE: SourceFile searches the body of the source file to automatically determine include and module paths.

Parameters:

  • kwargs (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • :file (String)

    Name or path of the file to compile.

  • :compiler_include_paths (Array<String>)

    Additional paths for the compiler to find include files.

  • :compiler_module_paths (Array<String>)

    Additional paths for the compiler to find module files.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/netlinx/source_file.rb', line 12

def initialize(**kwargs)
  @compiler_target_files  = [ kwargs.fetch(:file, nil) ]
  @compiler_include_paths = kwargs.fetch :compiler_include_paths, []
  @compiler_module_paths  = kwargs.fetch :compiler_module_paths,  []
  
  return unless @compiler_target_files.first
  
  source_code = File.open(@compiler_target_files.first).read
  
  # Scan file for additional include paths.
  includes = source_code.scan(/(?i)^\s*(?:\#include)\s+'([\w\-]+)'/)
  
  includes.each do |inc|
    inc = inc.first
    
    path = Dir["./**/#{inc}.*"].first
    next unless path
    
    path = File.expand_path path
    @compiler_include_paths << File.dirname(path)
  end
  
  @compiler_include_paths.uniq!
  
  # Scan file for additional module paths.
  modules = source_code.scan(/(?i)^\s*(?:define_module)\s+'([\w\-]+)'/)
  
  modules.each do |mod|
    mod = mod.first
    
    path = Dir["./**/#{mod}.*"].first
    next unless path
    
    path = File.expand_path path
    @compiler_module_paths << File.dirname(path)
  end
  
  @compiler_module_paths.uniq!
end

Instance Method Details

#compileObject

Execute the compiler on itself.



73
74
75
76
77
# File 'lib/netlinx/source_file.rb', line 73

def compile
  require 'netlinx/compiler'
  compiler = NetLinx::Compiler.new
  result = compiler.compile self
end

#compiler_include_pathsObject

See Also:

  • lib/test/netlinx/compilable.rb interface.


58
59
60
# File 'lib/netlinx/source_file.rb', line 58

def compiler_include_paths
  @compiler_include_paths
end

#compiler_library_pathsObject

See Also:

  • lib/test/netlinx/compilable.rb interface.


68
69
70
# File 'lib/netlinx/source_file.rb', line 68

def compiler_library_paths
  []
end

#compiler_module_pathsObject

See Also:

  • lib/test/netlinx/compilable.rb interface.


63
64
65
# File 'lib/netlinx/source_file.rb', line 63

def compiler_module_paths
  @compiler_module_paths
end

#compiler_target_filesObject

See Also:

  • lib/test/netlinx/compilable.rb interface.


53
54
55
# File 'lib/netlinx/source_file.rb', line 53

def compiler_target_files
  @compiler_target_files
end