class Codestrap::Core
Attributes
Command line object
@return [Codestrap::Cli]
List of client objects
@return [Array<Codestrap::Client>]
Configuration object created from Codestrapfile
@return [Codestrap::Config]
Set Environment variables.
Boilerplate template path
@return [String]
Codestrap
template path
@return [String]
Public Class Methods
Logging object @return [Codestrap::Log]
# File lib/codestrap.rb, line 120 def self.logger @@logger ||= begin logger = Codestrap::Log.new @@logger = logger end end
# File lib/codestrap.rb, line 127 def initialize(argv=nil) # CLI options if argv self.cli = Codestrap::Cli.new argv self.cli.options 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
Public Instance Methods
Environment variables. Defaults to system environment variables
@return [Hash]
# File lib/codestrap.rb, line 52 def env @env ||= ENV.to_hash end
# File lib/codestrap.rb, line 153 def execute! # Run type case when cli.command =~ /stub(.+)$/ self.template = $1 stub when cli.command =~ /strap(.+)$/ self.project = $1 strap when self.cli.options.generate strap_rcfile strap_init_home strap_links else logger.error :INVALIDCMD, cli.command exit 1 end exit 0 end
List all stub.. and strap.. commands
@return [Array]
# File lib/codestrap.rb, line 492 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
Available projects from local and server
@return [Array]
# File lib/codestrap.rb, line 469 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
Available codestraps from local and server
@return [Array]
# File lib/codestrap.rb, line 448 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
Logging object @return [Codestrap::Log]
# File lib/codestrap.rb, line 114 def logger self.class.logger end
Set strap project @param [String] project
Boilerplate name
@return [String|nil]
Boilerplate name or nil
# File lib/codestrap.rb, line 90 def project=(project) # Local templates @config.local.content.each do |path| next unless File.directory? path projectpath = File.join(path, project) next unless File.directory?(projectpath) @project = projectpath return @project end # Remote templates @clients.each do |client| projectpath = client.getstrap(project) next unless projectpath and File.exist? projectpath @project = projectpath return @project end logger.error :STRAPMISSING, project exit 1 end
# File lib/codestrap.rb, line 195 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
Initialize home directory
@param [String] codestrap
Optional name of codestrap directory in $HOME. Defaults to '.codestrap'
# File lib/codestrap.rb, line 263 def strap_init_home(codestrap='.codestrap') root_path = File.join(env['HOME'], codestrap) unless File.exist?(root_path) puts "Creating #{root_path}" Dir.mkdir(root_path) end %w(bin content objects).each do |dir| path = File.join(root_path, dir) unless File.exist?(path) puts "Creating #{path}" FileUtils.mkpath path end end codestrapfile = File.join(env['HOME'], codestrap, 'Codestrapfile') unless File.exist?(codestrapfile) tmpfile = Tempfile.new('codestrapfile') output = <<EOF Codestrapfile.config do |conf| # Base directory or list of base directories that contain "object" and "content" sub directories # Defaults to $HOME/.codestrap #conf.local.base = [ "ENV['HOME']/.codestrap", '/usr/local/codestrap' ] #conf.local.base = "ENV['HOME']/.codestrap" # Ignore paths or list of paths to ignore #conf.local.ignore = [ '.git', '.svn' ] #conf.local.ignore = '.git' end EOF tmpfile.write(output) tmpfile.close File.rename(tmpfile, codestrapfile) end end
Generate symlinks
# File lib/codestrap.rb, line 298 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.expand_path(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.expand_path 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
Generate strap metadata
@param [String] name
Optional module name
@param [Hash] options
Flags to turn off/on metadata { :src => [true|false] :ftype => [true|false] :mode => [true|false] }
@param [Array] paths
Optional path list for content search
@return [Hash]
Strap directory structure metadata { 'strap_name' => { :files => [ { :file => 'relative/tree/file/path', :src => '/absolute/file/source', :ftype => 'File type as returned by File::Stat.new(file).ftype, EG "file", "directory"' :mode => 'File mode as returned by File::Stat.new(file).mode' } ] } }
# File lib/codestrap.rb, line 361 def strap_metadata(name=nil, options=nil, paths=@config.local.content) # TODO - rename tree = {} options ||= {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 options[:src] case path when %r{^/} obj[:src] = File.expand_path(File.join(path, mod, file)) else obj[:src] = File.expand_path(File.join(cur_dir, path, mod, file)) end end obj[:ftype] = stat.ftype if options[:ftype] obj[:mode] = sprintf('%o', stat.mode) if options[:mode] tree[mod][:files] << obj end end end end end tree end
Adds an entry to rcfile depending on shell
# File lib/codestrap.rb, line 217 def strap_rcfile line = "\nexport PATH=$PATH:$HOME/.codestrap/bin\n" # Check shell rcfile = begin case env['SHELL'] when %r{/bin/bash$} File.join(env['HOME'], '.bash_profile') when %r{/bin/zsh$} File.join(env['HOME'], '.zshrc') when %r{/bin/sh$} File.join(env['HOME'], '.profile') else nil end end # Check rcfile write = begin case when !rcfile false when(File.exist?(rcfile) and File.foreach(rcfile).grep(%r{export PATH=\$PATH:\$HOME/\.codestrap/bin}).length < 1) line = "\n" + line true when(not File.exist?(rcfile)) true else false end end # Write file if write puts "Adding PATH='$PATH:$HOME/.codestrap/bin' to #{rcfile}" File.open(rcfile,'a') do |fh| fh.write(line) end end end
# File lib/codestrap.rb, line 174 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
Generate strap metadata
@param [String] name
Optional module name
@param [Hash] options
Flags to turn off/on metadata { :src => [true|false] :ftype => [true|false] :mode => [true|false] }
@param [Array] paths
Optional path list for content search
@return [Hash]
Strap directory structure metadata { 'stub_name' => { :src => 'relative/tree/file/path.erb', :ftype => 'File type as returned by File::Stat.new(file).ftype, EG "file", "directory"' :mode => 'File mode as returned by File::Stat.new(file).mode' } }
# File lib/codestrap.rb, line 421 def stub_metadata(name=nil, options=nil, paths=@config.local.content) # TODO - rename codestraps = {} options ||= {src: false, ftype: true, mode: true} paths.each do |path| full_path = File.expand_path 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.expand_path(File.join(full_path, file)) if options[:src] codestraps[mod][:ftype] = stat.ftype if options[:ftype] codestraps[mod][:mode] = sprintf('%o', stat.mode) if options[:mode] end end end codestraps end
Set stub template @param [String] template
Codestrap name
@return [String|nil]
Codestrap name or nil
# File lib/codestrap.rb, line 61 def template=(template) # Local templates @config.local.content.each do |path| next unless File.directory? path ['.erb'].each do |prefix| templatepath = File.join(path, "#{template}#{prefix}") next unless File.exist? templatepath @template = templatepath return @template end end # Remote templates @clients.each do |client| templatepath = client.getstub(template) next unless templatepath and File.exist? templatepath @template = templatepath return @template end logger.error :STUBMISSING, template exit 1 end