Module: Grably

Includes:
Core
Defined in:
lib/grably.rb,
lib/grably/cli.rb,
lib/grably/job.rb,
lib/grably/server.rb,
lib/grably/version.rb,
lib/grably/core/dsl.rb,
lib/grably/core/task.rb,
lib/grably/job/class.rb,
lib/grably/jobs/sync.rb,
lib/grably/core/colors.rb,
lib/grably/core/digest.rb,
lib/grably/core/module.rb,
lib/grably/core/product.rb,
lib/grably/job/instance.rb,
lib/grably/job/manifest.rb,
lib/grably/jobs/zip/zip.rb,
lib/grably/core/commands.rb,
lib/grably/jobs/text/erb.rb,
lib/grably/core/task/jobs.rb,
lib/grably/jobs/text/json.rb,
lib/grably/jobs/text/text.rb,
lib/grably/jobs/text/yaml.rb,
lib/grably/jobs/zip/unzip.rb,
lib/grably/core/essentials.rb,
lib/grably/core/commands/cp.rb,
lib/grably/core/task/bucket.rb,
lib/grably/core/task/expand.rb,
lib/grably/core/commands/log.rb,
lib/grably/core/commands/run.rb,
lib/grably/core/app/enchancer.rb,
lib/grably/core/configuration.rb,
lib/grably/core/task/enchancer.rb,
lib/grably/core/commands/digest.rb,
lib/grably/utils/pretty_printer.rb,
lib/grably/core/commands/serialize.rb,
lib/grably/core/configuration/pretty_print.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Core, DSL, Digest, FakeColors, Job, Module, ShellColors, Utils Classes: ErbJob, GrablyCLI, JsonJob, Server, SyncJob, TextJob, UnzipJob, YamlJob, ZipJob

Constant Summary collapse

GRABLY_BANNER =
'' \
'__       ______           __    __      __|' \
'\ \     / ____/________ _/ /_  / /_  __/ /|' \
' \ \   / / __/ ___/ __ `/ __ \/ / / / / / |' \
' / /  / /_/ / /  / /_/ / /_/ / / /_/ /_/  |' \
'/_/   \____/_/   \__,_/_.___/_/\__, (_)   |' \
'                              /____/      |'.tr('|', "\n")
VERSION =

Current grably version identifier

[0, 0, 3].freeze
WORKING_DIR =

All working files are stored under .grably directory.

'.grably'.freeze
COLOR_CODES =
{
  normal: 0,
  bright: 1,
  light: 1, # light is synonym for bright for us
  dim: 2,
  underscore: 4,
  blink: 5,
  reverse: 7,
  hidden: 8,

  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
  default: 39,

  bg_black: 40,
  bg_red: 41,
  bg_green: 42,
  bg_yellow: 43,
  bg_blue: 44,
  bg_magenta: 45,
  bg_cyan: 46,
  bg_white: 47,
  bg_default: 49
}.freeze
PLATFORM =

Short OS family name :win - Windows :linux - Linux :mac - OS X

case RUBY_PLATFORM
when /mingw/, /cygwin/
  :windows
when /mac/, /darwin/
  :mac
else
  :linux
end
CORES_NUMER =

Number of CPU cores

case PLATFORM
when :windows
  # this works for windows 2000 or greater
  require 'win32ole'
  wmi = WIN32OLE.connect('winmgmts://')
  query = 'select * from Win32_ComputerSystem'
  wmi.ExecQuery(query).each do |system|
    begin
      processors = system.NumberOfLogicalProcessors
    rescue StandardError => x
      puts 'Warn: ' + x.message
      processors = 1
    end
    return [system.NumberOfProcessors, processors].max
  end
when :mac
  `sysctl -n hw.logicalcpu`.to_i
when :linux
  `cat /proc/cpuinfo | grep processor | wc -l`.to_i
else
  raise "can't determine 'number_of_processors' for '#{RUBY_PLATFORM}'"
end

Constants included from Core

Core::Task

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



27
28
29
# File 'lib/grably.rb', line 27

def config
  @config
end

.export_pathObject



38
39
40
# File 'lib/grably.rb', line 38

def export_path
  @export_path || ENV['EXPORT']
end

Class Method Details

.[](path = Dir.pwd, task = :default) ⇒ Object



59
60
61
# File 'lib/grably.rb', line 59

def [](path = Dir.pwd, task = :default)
  Grably::Module.reference(path, task)
end

.cores_numberObject



45
46
47
# File 'lib/grably/core/essentials.rb', line 45

def cores_number
  CORES_NUMBER
end

.export?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/grably.rb', line 50

def export?
  # Ensure boolean value
  !!export_path # rubocop:disable Style/DoubleNegation
end

.export_tasksObject



42
43
44
# File 'lib/grably.rb', line 42

def export_tasks
  @export_tasks ||= (ENV['EXPORT_TASKS'] || '').split(',')
end

.export_tasks=(task_names) ⇒ Object



46
47
48
# File 'lib/grably.rb', line 46

def export_tasks=(task_names)
  @export_tasks = task_names.dup.freeze
end

.exportsObject



55
56
57
# File 'lib/grably.rb', line 55

def exports
  @exports ||= []
end

.initObject



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

def init
  @config = Grably::Core::Configuration.load
end

.method_added(m) ⇒ Object

Make all methods as MODULE methods



12
13
14
# File 'lib/grably/core/commands.rb', line 12

def self.method_added(m)
  module_function m
end

.run(cmd, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/grably/core/commands/run.rb', line 27

def run(cmd, &block)
  env = {}

  cmd = [cmd] if cmd.is_a?(Hash)
  cmd = cmd.split(' ') unless cmd.is_a?(Array)
  cmd = cmd.flatten.compact

  cmd.map! do |c|
    if c.is_a?(String) && c.empty?
      c = nil
    elsif c.is_a?(Hash)
      env.merge!(c)
      c = nil
    end
    c = c.to_s unless c.nil?
    c
  end

  cmd = cmd.flatten.compact

  # Not all windows apps "slash tolerant"
  if windows?
    cmd[0] = cmd[0].tr('/', '\\') unless cmd.empty?
  end

  pwd = nil
  if env['__WORKING_DIR']
    pwd = Dir.pwd
    Dir.chdir(env.delete('__WORKING_DIR'))
  end

  r = []
  IO.popen([env, cmd, { err: %i(child out) }].flatten) do |o|
    o.sync = true
    o.each do |l|
      r << l
      yield(l) unless block.nil?
    end
  end

  Dir.chdir(pwd) if pwd

  raise 'error: '.red.bright + cmd.red + "\nfail log: #{r}".green unless $?.exitstatus.zero?

  r.join
end

.run_log(cmd) ⇒ Object



23
24
25
# File 'lib/grably/core/commands/run.rb', line 23

def run_log(cmd)
  run(cmd) { |l| log "  #{l}" }
end

.run_safe(cmd) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/grably/core/commands/run.rb', line 74

def run_safe(cmd)
  begin
    run(cmd)
  rescue
    return false
  end

  true
end

.serverObject



32
33
34
# File 'lib/grably.rb', line 32

def server
  @server ||= Grably::Server.new
end

.versionString

Returns grably version string

Returns:



8
9
10
# File 'lib/grably/version.rb', line 8

def version
  VERSION.join('.')
end

Instance Method Details

#configObject Also known as: c, cfg, conf



17
18
19
# File 'lib/grably.rb', line 17

def config
  Grably.config
end

#cp(products, dst_dir, base_dir: nil, log: false) ⇒ Array<Product>

Copy files or products to destination directory

Parameters:

  • products (Array<Product>)

    list of products to copy

  • dst_dir (String)

    destination directory to copy products

  • base_dir (String) (defaults to: nil)

    if provided all products will be copied relative to this path inside destination

  • log (Boolean) (defaults to: false)

    if provided will log all actions to STDOUT. false by default

Returns:



10
11
12
13
14
15
16
# File 'lib/grably/core/commands/cp.rb', line 10

def cp(products, dst_dir, base_dir: nil, log: false)
  products = Product.expand(products)
  dst_dir = File.expand_path(dst_dir)
  dst_dir = File.join(dst_dir, base_dir) if base_dir

  products.map { |product| copy_product(product, dst_dir, log: log) }
end

#cp_smart(products, dst_dir, log: false) ⇒ Array<Product>

Smart copy operates over product lists in the way like it directory structures. All chaned products will be replaced and missing products will be removed.

Parameters:

  • products (Array<Product>)

    list of products to copy. Any expand expression will work as expected.

  • dst_dir (String)

    target directory path.

  • log (Boolean) (defaults to: false)

    if set to true log all actions to STDOUT. false by default

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/grably/core/commands/cp.rb', line 27

def cp_smart(products, dst_dir, log: false) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  # Ensure dst_dir is created
  FileUtils.mkdir_p(dst_dir) unless File.exist?(dst_dir)
  # Create Hash structures containing product.dst => product mappings
  update = ->(acc, elem) { acc.update(elem.dst => elem) }
  src_products = Product.expand(products).inject({}, &update)
  dst_products = Product.expand(dst_dir).inject({}, &update)

  # Looking for missing files
  remove_files = (dst_products.keys - src_products.keys).each do |dst_key|
    log "Remove #{dst_key} (#{dst_products[dst_key].src}" if log
  end
  FileUtils.rm(remove_files.map { |k| dst_products[k].src })
  rm_empty_dirs(dst_dir)
  # Update rest
  src_products.map do |dst, product|
    dst_product = dst_products[dst]
    update_smart(product, dst_product, dst_dir, log: log)
  end
end

#cp_sys(src, dst, log: false) ⇒ Object

Copy product to dst using FileUtils



49
50
51
52
53
54
# File 'lib/grably/core/commands/cp.rb', line 49

def cp_sys(src, dst, log: false)
  src = src.src if src.is_a?(Product)
  dst = dst.src if dst.is_a?(Product)
  log "Copy #{File.basename(src)} to #{dst}" if log
  FileUtils.cp_r(src, dst)
end

#digest(src, buff_len: 4096) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/grably/core/commands/digest.rb', line 4

def digest(src, buff_len: 4096)
  src = src.src if src.is_a?(Product)
  sha = ::Digest::SHA2.new
  File.open(src, 'rb') do |f|
    sha << f.read(buff_len) until f.eof?
  end
  sha.hexdigest
end

#err(msg) ⇒ Object



8
9
10
# File 'lib/grably/core/commands/log.rb', line 8

def err(msg)
  puts 'error: '.red.bright + msg
end

#load_obj(filename) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/grably/core/commands/serialize.rb', line 2

def load_obj(filename)
  return nil unless File.exist? filename
  File.open(filename) do |f|
    Marshal.load(f) # rubocop:disable Security/MarshalLoad
  end
rescue StandardError => x
  raise(x, "Can't deserialize #{filename}")
end

#log(msg) ⇒ Object



2
3
4
5
6
# File 'lib/grably/core/commands/log.rb', line 2

def log(msg)
  puts msg
  # flush stdout - we need that to have submodules displaying OK
  $stdout.flush
end

#relative_path(base, path) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/grably/core/commands.rb', line 16

def relative_path(base, path)
  # TODO: Reimplement
  Dir.chdir(base) do
    path = File.expand_path(path)
  end
  path
end

#save_obj(filename, obj) ⇒ Object



11
12
13
14
15
# File 'lib/grably/core/commands/serialize.rb', line 11

def save_obj(filename, obj)
  dir = File.dirname(filename)
  FileUtils.mkdir(dir) unless File.exist?(dir)
  File.open(filename, 'w') { |f| Marshal.dump(obj, f) }
end

#trace(msg) ⇒ Object



16
17
18
# File 'lib/grably/core/commands/log.rb', line 16

def trace(msg)
  puts msg if ENV['TRACE']
end

#warn(msg) ⇒ Object



12
13
14
# File 'lib/grably/core/commands/log.rb', line 12

def warn(msg)
  puts 'warning: '.cyan.bright + msg.to_s.cyan
end