class CKick::SubDirectory

Represents a project sub directory (in respect to CMake sub_directory() command)

Attributes

has_cmake[R]

whether or not this directory has targets and a CMakeLists.txt

name[R]

directory name

parent_dir[R]

parent directory

Public Class Methods

new(args={}) click to toggle source
  • args - SubDirectory hash (directly a CKickfile :subdirs Array element parsed with keys as Symbol), must be a Hash

Input hash keys
  • :name - subdirectory name

  • :libraries - libraries built in this subdirectory (in respect to CMake add_library() command), Array of Hash each containing information for CKick::Library::new

  • :executables - executables built in this subdirectory (in respect to CMake add_executable() command), Array of Hash each containing information for CKick::Executable::new

  • :subdirs - recursive subdirectories (in respect to CMake subdirectory() command), must be a Array of Hash passed to CKick::SubDirectory::new

  • :has_cmake - whether or not this subdirectory has a CMakeLists.txt (consequently builds targets or not)

# File lib/ckick/sub_directory.rb, line 34
def initialize args={}
  name = args[:name]
  raise IllegalInitializationError, "no name provided to sub directory" unless name.is_a?(String) && !name.empty?
  libs = args[:libraries]
  raise IllegalInitializationError, ":libraries argument is not an Array" unless libs.is_a?(Array) || libs.nil?
  exes = args[:executables]
  raise IllegalInitializationError, ":executables argument is not an Array" unless exes.is_a?(Array) || exes.nil?
  subdirs = args[:subdirs]
  raise IllegalInitializationError, ":subdirs is not an Array" unless subdirs.is_a?(Array) || subdirs.nil?

  has_cmake = args[:has_cmake].nil? || args[:has_cmake]

  if (!exes.nil? || !libs.nil?) && !has_cmake
    raise BadSubDirectoryError, "A subdirectory not containing a CMakeLists cannot contain targets."
  end

  @name = name
  @has_cmake = has_cmake

  @libraries = []
  libs.each do |lib|
    @libraries << Library.new(lib)
  end
  @executables = []
  exes.each do |exe|
    @executables << Executable.new(exe)
  end

  @subdirs = []
  subdirs.each do |subdir|
    @subdirs << SubDirectory.new(subdir)
  end

  @parent_dir = nil
end

Public Instance Methods

cmake() click to toggle source

subdirectory CMakeLists.txt file content

# File lib/ckick/sub_directory.rb, line 109
def cmake
  res = ''

  res << targets.collect { |t| t.cmake }.join("\n")

  unless @subdirs.empty?
    res << "\n"
    res << @subdirs.collect do |subdir|
      "add_subdirectory(#{subdir.name})"
    end.join("\n")
  end

  res
end
create_structure() click to toggle source

creates subdirectory structure

this method is called recursively on subdirectories

# File lib/ckick/sub_directory.rb, line 92
def create_structure
  PathDelegate.create_directory(path)

  if @has_cmake
    PathDelegate.write_file(path, "CMakeLists.txt", cmake)

    targets.each do |t|
      t.create_structure
    end
  end

  @subdirs.each do |subdir|
    subdir.create_structure
  end
end
path() click to toggle source

subdirectory path

# File lib/ckick/sub_directory.rb, line 84
def path
  raise NoParentDirError, "sub directory #{@name} has no parent set" unless @parent_dir
  File.join(@parent_dir, @name)
end
to_hash() click to toggle source

converts to Hash (to CKickfile :subdirs element)

# File lib/ckick/sub_directory.rb, line 76
def to_hash
  if !@has_cmake
    return to_no_empty_value_hash.without(:parent_dir)
  end
  to_no_empty_value_hash.without(:parent_dir, :has_cmake)
end
to_s() click to toggle source

converts to String, subdirectory name as is

# File lib/ckick/sub_directory.rb, line 71
def to_s
  @name
end

Private Instance Methods

set_parent(parent_dir) click to toggle source

sets parent directory path

this method is called recursively on subdirectories

# File lib/ckick/sub_directory.rb, line 134
def set_parent(parent_dir)
  @parent_dir = parent_dir

  targets.each do |t|
    t.__send__ :set_parent, path
  end

  @subdirs.each do |subdir|
    subdir.__send__ :set_parent, path
  end
end
targets() click to toggle source

Array of all targets ( CKick::Target ) contained in this subdirectory

# File lib/ckick/sub_directory.rb, line 127
def targets
  @executables + @libraries
end