Class: Codestrap::Core
- Inherits:
-
Object
- Object
- Codestrap::Core
- Defined in:
- lib/codestrap.rb
Instance Attribute Summary collapse
-
#cli ⇒ Codestrap::Cli
Command line object.
-
#clients ⇒ Array<Codestrap::Client>
List of client objects.
-
#config ⇒ Codestrap::Config
Configuration object created from Codestrapfile.
-
#project ⇒ String
Boilerplate template path.
-
#template ⇒ String
Codestrap template path.
Class Method Summary collapse
-
.logger ⇒ Codestrap::Log
Logging object.
Instance Method Summary collapse
- #execute! ⇒ Object
-
#initialize(argv = nil) ⇒ Core
constructor
A new instance of Core.
-
#list_cmds ⇒ Array
(also: #ls_cmd)
List all stub..
-
#list_straps ⇒ Array
(also: #ls_strap)
Available projects from local and server.
-
#list_stubs ⇒ Array
(also: #ls_codestrap)
Available codestraps from local and server.
-
#logger ⇒ Codestrap::Log
Logging object.
- #strap(dst = nil, src = nil) ⇒ Object
- #strap_links ⇒ Object
-
#strap_metadata(name = nil, options = nil, paths = @config.local.content) ⇒ Hash
Generate strap metadata.
- #stub(dst = nil, src = nil) ⇒ Object
-
#stub_metadata(name = nil, options = nil, paths = @config.local.content) ⇒ Hash
Generate strap metadata.
Constructor Details
#initialize(argv = nil) ⇒ Core
Returns a new instance of Core.
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/codestrap.rb', line 116 def initialize(argv=nil) # CLI options if argv self.cli = Codestrap::Cli.new argv self.cli. end # Config @config = Codestrap::Config.new case @config.local.urls when Array @config.local.urls.each do |url| @clients ||= [] @clients << Codestrap::Client.new(url) end when String @clients ||= [] @clients << Codestrap::Client.new(@config.local.urls) when NilClass else raise Exception, 'Unknown data structure' end self end |
Instance Attribute Details
#cli ⇒ Codestrap::Cli
Command line object
33 34 35 |
# File 'lib/codestrap.rb', line 33 def cli @cli end |
#clients ⇒ Array<Codestrap::Client>
List of client objects
38 39 40 |
# File 'lib/codestrap.rb', line 38 def clients @clients end |
#config ⇒ Codestrap::Config
Configuration object created from Codestrapfile
43 44 45 |
# File 'lib/codestrap.rb', line 43 def config @config end |
#project ⇒ String
Boilerplate template path
28 29 30 |
# File 'lib/codestrap.rb', line 28 def project @project end |
#template ⇒ String
Codestrap template path
23 24 25 |
# File 'lib/codestrap.rb', line 23 def template @template end |
Class Method Details
.logger ⇒ Codestrap::Log
Logging object
109 110 111 112 113 114 |
# File 'lib/codestrap.rb', line 109 def self.logger @@logger ||= begin logger = Codestrap::Log.new @@logger = logger end end |
Instance Method Details
#execute! ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/codestrap.rb', line 142 def execute! # Run type case when cli.command =~ /stub(.+)$/ self.template = $1 stub when cli.command =~ /strap(.+)$/ self.project = $1 strap when self.cli..generate strap_links else logger.error :INVALIDCMD, cli.command exit 1 end exit 0 end |
#list_cmds ⇒ Array Also known as: ls_cmd
List all stub.. and strap.. commands
396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/codestrap.rb', line 396 def list_cmds links = [] dir = @config.local.links if File.directory? dir Dir.chdir(dir) do Dir.glob('*').each do |f| next unless File.symlink? f next unless f =~ /^(?:stub|strap)[A-Za-z0-9._-]+$/ links.push f end end end links end |
#list_straps ⇒ Array Also known as: ls_strap
Available projects from local and server
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/codestrap.rb', line 373 def list_straps straps = [] if @clients @clients.each do |client| straps ||= [] straps += client.straplist || [] straps.flatten! end end @config.local.content.each do |dir| next unless File.directory? dir if File.directory? dir Dir.chdir(dir) do straps += Dir.glob('*').select { |f| File.directory? f } end end end straps.uniq end |
#list_stubs ⇒ Array Also known as: ls_codestrap
Available codestraps from local and server
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/codestrap.rb', line 352 def list_stubs codestraps = [] if @clients @clients.each do |client| codestraps ||= [] codestraps += client.stublist || [] codestraps.flatten! end end @config.local.content.each do |dir| next unless File.directory? dir Dir.chdir(dir) do codestraps += Dir.glob('*.erb').select { |f| File.file? f } end end codestraps.uniq end |
#logger ⇒ Codestrap::Log
Logging object
103 104 105 |
# File 'lib/codestrap.rb', line 103 def logger self.class.logger end |
#strap(dst = nil, src = nil) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/codestrap.rb', line 182 def strap(dst=nil, src=nil) # Create objects obj = Codestrap::Object::Factory.new obj.dirs = @config.local.objects obj.clients = @clients obj.cli = @cli obj.config = @config argv = self.cli.argv renderer = Codestrap::Strap::Factory.new('Standard').construct(argv) renderer.ignore = @config.local.ignore renderer.objects = obj renderer.src = src || self.project renderer.dst = dst || self.cli.argv[0] renderer.pre renderer.execute renderer.post renderer.to_disk end |
#strap_links ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/codestrap.rb', line 202 def strap_links # FixMe - Move to Codestrap::Log unless cli.command =~ /^strap$/ logger.error(:LINKTARGET, cli.command, 'strap') exit 1 end # stub templates newtemplates = list_stubs.map { |f| File.basename(f, '.erb') } - list_cmds.select { |f| f =~ /^stub/ }.map { |f| f =~ /^stub(.+)$/; $1 } newtemplates.each do |file| link = File.(File.join(@config.local.links, 'stub' + file)) if File.symlink?(link) and File.exist?(File.readlink(link)) next elsif File.symlink?(link) File.unlink link end File.symlink cli.abspath, link end # strap templates newprojects = list_straps - list_cmds.select { |f| f =~ /^strap/ }.map { |f| f =~ /^strap(.+)$/; $1 } newprojects.each do |project| link = File. File.join(@config.local.links, 'strap' + project) if File.symlink?(link) and File.exist?(File.readlink(link)) next elsif File.symlink?(link) File.unlink link end File.symlink cli.abspath, link end end |
#strap_metadata(name = nil, options = nil, paths = @config.local.content) ⇒ Hash
Generate strap metadata
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/codestrap.rb', line 265 def (name=nil, =nil, paths=@config.local.content) # TODO - rename tree = {} ||= {src: false, ftype: true, mode: true} cur_dir = Dir.pwd paths.each do |path| next unless File.directory? path Dir.chdir(path) do Dir.entries('.').each do |mod| next if mod =~ /^\.{1,2}$/ next unless File.directory? mod next if name and not name.eql? mod next if tree.has_key? mod tree[mod] = {files: []} Dir.chdir(mod) do Dir.glob('**/**').each do |file| stat = File.stat file obj = {} obj[:file] = file if [:src] case path when %r{^/} obj[:src] = File.(File.join(path, mod, file)) else obj[:src] = File.(File.join(cur_dir, path, mod, file)) end end obj[:ftype] = stat.ftype if [:ftype] obj[:mode] = sprintf('%o', stat.mode) if [:mode] tree[mod][:files] << obj end end end end end tree end |
#stub(dst = nil, src = nil) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/codestrap.rb', line 161 def stub(dst=nil, src=nil) # Create objects obj = Codestrap::Object::Factory.new obj.dirs = @config.local.objects obj.clients = @clients obj.cli = @cli obj.config = @config argv = self.cli.argv # Render setup renderer = Codestrap::Stub::Factory.new('Standard').construct(argv) renderer.objects = obj renderer.src = src || self.template renderer.dst = dst || self.cli.argv[0] renderer.pre renderer.execute renderer.post renderer.to_disk end |
#stub_metadata(name = nil, options = nil, paths = @config.local.content) ⇒ Hash
Generate strap metadata
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/codestrap.rb', line 325 def (name=nil, =nil, paths=@config.local.content) # TODO - rename codestraps = {} ||= {src: false, ftype: true, mode: true} paths.each do |path| full_path = File. path next unless File.directory? full_path Dir.chdir(full_path) do Dir.entries('.').each do |file| next if file =~ /^\.{1,2}$/ next unless File.file? file next if name and not name.eql? File.basename(file, '.erb') mod = File.basename(file, '.erb') stat = File.stat file codestraps[mod] = {} codestraps[mod][:src] = File.(File.join(full_path, file)) if [:src] codestraps[mod][:ftype] = stat.ftype if [:ftype] codestraps[mod][:mode] = sprintf('%o', stat.mode) if [:mode] end end end codestraps end |