Class: CKick::Project

Inherits:
Object
  • Object
show all
Includes:
Hashable
Defined in:
lib/ckick/project.rb

Overview

This class represents a CMake project (as a main CMakeLists)

Constant Summary collapse

NAME_MATCH =

project name match pattern

/^[[A-Z][a-z]_[0-9]]+$/
CMAKE_VERSION_MATCH =

version match

/^[0-9](\.[0-9]){0,2}$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hashable

#to_no_empty_value_hash

Constructor Details

#initialize(args) ⇒ Project

  • args - project hash (directly the CKickfile parsed with keys as Symbol), must be a Hash

Input hash keys
  • :name - project name, must respect NAME_MATCH

  • :cmake_min_version - CMake minimum version (must be String), defaults to ‘3’, must match CMAKE_VERSION_MATCH

  • :root - Project root directory, usually ‘.’

  • :build_dir - Project build directory, usually ‘build’

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

  • :plugins - Array of Hash containing {:name => CKick::Plugin class name, :args => args for respective :initialize method }



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ckick/project.rb', line 44

def initialize args
  name = args[:name] || ""
  raise IllegalInitializationError, "name must be a non-empty string only containing alphanumeric characters" unless name.is_a?(String) && name.match(NAME_MATCH)

  min_v = args[:cmake_min_version] || '3'
  raise IllegalInitializationError, "cmake_min_version is non-String" unless min_v.is_a?(String)
  raise IllegalInitializationError, "cmake_min_version has non working pattern x or x.y or x.y.z" unless min_v.match(CMAKE_VERSION_MATCH)

  root = args[:root] || ""
  raise IllegalInitializationError, "root directory is non-String" unless root.is_a?(String)
  raise IllegalInitializationError, "root directory is empty" if root.empty?

  build_dir = args[:build_dir] || ""
  raise IllegalInitializationError, "build directory is non-String" unless build_dir.is_a?(String)
  raise IllegalInitializationError, "build directory is empty" if build_dir.empty?

  @name = name
  @cmake_min_version = min_v
  @root = root
  @build_dir = build_dir
  @dependencies = Dependencies.new(args[:dependencies] || {})

  @plugins = []
  args[:plugins].each do |plugin|
    @plugins << PluginDelegate.find(plugin)
  end

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

  @subdirs_initiated = false
  init_subdirs
end

Instance Attribute Details

#build_dirObject (readonly)

project build directory



28
29
30
# File 'lib/ckick/project.rb', line 28

def build_dir
  @build_dir
end

#dependenciesObject (readonly)

project CKick::Dependencies



22
23
24
# File 'lib/ckick/project.rb', line 22

def dependencies
  @dependencies
end

#rootObject (readonly)

project root directory



25
26
27
# File 'lib/ckick/project.rb', line 25

def root
  @root
end

#subdirsObject (readonly)

each project CKick::SubDirectory



19
20
21
# File 'lib/ckick/project.rb', line 19

def subdirs
  @subdirs
end

Instance Method Details

#cmakeObject

main project’s CMakeLists.txt content



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ckick/project.rb', line 129

def cmake
  append_plugin_paths

  res = "project(#{@name})\n" +
        "cmake_minimum_required(VERSION #{@cmake_min_version})\n\n"

  res << "set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)\n" \
         "set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)\n" \
         "set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)\n\n"

  res << @dependencies.cmake << "\n\n"

  res << plugins_cmake << "\n\n" unless @plugins.empty?

  @subdirs.each do |dir|
    res << "add_subdirectory(#{dir.name})\n" if dir.has_cmake
  end

  res
end

#create_structureObject

creates the project CMake structure



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ckick/project.rb', line 102

def create_structure
  raise "SubDirectories have not been initiated" unless @subdirs_initiated

  run_plugins

  PathDelegate.create_directory path
  PathDelegate.write_file(path, "CMakeLists.txt", cmake)

  @subdirs.each do |subdir|
    subdir.create_structure
  end

  call_plugins
end

#pathObject

project root directory path



97
98
99
# File 'lib/ckick/project.rb', line 97

def path
  @root
end

#plugin_name(plugin) ⇒ Object

:nodoc:



191
192
193
194
195
196
197
# File 'lib/ckick/project.rb', line 191

def plugin_name(plugin) #:nodoc:
  if plugin.respond_to?(:name)
    return plugin.name
  else
    return "<inline plugin>"
  end
end

#register_plugin(plugin = nil, &block) ⇒ Object

register an additionnal CKick::Plugin or block

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
126
# File 'lib/ckick/project.rb', line 118

def register_plugin(plugin=nil, &block)
  raise ArgumentError, "" unless plugin.is_a?(::CKick::Plugin) || block

  if plugin.is_a?(::CKick::Plugin)
    @plugins << plugin
  elsif plugin.nil? && block
    @plugins << block
  end
end

#set_name(name) ⇒ Object

set the project name, must match NAME_MATCH



81
82
83
84
# File 'lib/ckick/project.rb', line 81

def set_name(name)
  raise BadProjectNameError, "project name must be a non-empty alphanumeric string" unless name.is_a?(String) && name.match(NAME_MATCH)
  @name = name
end

#to_hashObject

convert to Hash (to CKickfile)



92
93
94
# File 'lib/ckick/project.rb', line 92

def to_hash
  to_no_empty_value_hash.without(:subdirs_initiated)
end

#to_sObject

convert to String -> the project name as is



87
88
89
# File 'lib/ckick/project.rb', line 87

def to_s
  @name
end