class MonorepoCLI

Constants

CONFIG_FILE_NAME
DEFAULT_CONFIG
EXIT_STATUS_ZERO
VERSION

Public Class Methods

load_config(filename = '') click to toggle source
# File lib/monorepo.rb, line 21
def self.load_config(filename = '')
  config_filenames =
    if filename == ''
      CONFIG_FILE_NAME
    else
      [filename, *CONFIG_FILE_NAME]
    end

  config_filenames.each do |config_filename|
    if File.exist?(config_filename)
      config = DEFAULT_CONFIG.merge YAML.load_file(config_filename)
      return config
    end
  end
  nil
end

Public Instance Methods

bootstrap(*args) click to toggle source
# File lib/commands/bootstrap.rb, line 5
def bootstrap(*args)
  exec ['bundle', *args].join(' ')
end
exec(*args) click to toggle source
# File lib/commands/exec.rb, line 8
def exec(*args)
  command_str = args.join ' '
  config_filename = options[:config_filename] || ''
  config = MonorepoCLI.load_config(config_filename)

  unless config
    STDERR.print "\e[31m[error]\e[0m no configuration file found.\n"
    STDERR.print 'run `monorepo init`'
    exit! 1
  end

  gems = config['gems']
  subdirs = Dir.glob(gems).select { |o| File.directory?(o) }

  if subdirs.empty?
    STDERR.print "\e[31m[error]\e[0m gem folder `#{gems}` not found.\n"
    STDERR.print 'run `monorepo init`'
    exit! 2
  end

  bundler =
    options[:bundler] ||
    %w[YES Y TRUE].include?(config['bundler'].upcase)

  local_command_str = bundler ? "bundle exec #{command_str}" : command_str

  subdirs.each do |gem|
    puts "executing `#{local_command_str}` at #{gem}..."
    system "cd #{gem} && pwd && #{local_command_str}"
    status = $CHILD_STATUS
    exit status.exitstatus unless status.exitstatus == EXIT_STATUS_ZERO
  end
end
init() click to toggle source
# File lib/commands/init.rb, line 9
def init
  gem_location = options[:gems] || 'gems'
  config_filename = options[:config_filename] || './Monorepofile'
  bundler =
    if %w[TRUE YES Y].include?((options[:bundler] || '').upcase)
      'yes'
    else
      'no'
    end

  specified_config = {
    'monorepo' => VERSION,
    'gems' => "#{gem_location}/*",
    'bundler' => bundler,
  }

  File.open(config_filename, 'w') { |f| f.write(specified_config.to_yaml) }

  # gem directory
  if Dir.exist? "./#{gem_location}"
    puts "gem directory`#{gem_location}` already exists."
  else
    Dir.mkdir "./#{gem_location}"
  end
end
list() click to toggle source
# File lib/commands/ls.rb, line 33
def list
  self.ls
end
ls() click to toggle source
# File lib/commands/ls.rb, line 5
def ls
  config_filename = options[:config_filename] || ''
  config = MonorepoCLI.load_config(config_filename)

  unless config
    STDERR.print "\e[31m[error]\e[0m no configuration file found.\n"
    STDERR.print 'run `monorepo init`'
    exit! 1
  end
  gems = config['gems']
  subdirs =
    Dir
    .glob(gems)
    .select { |o| File.directory? o }
    .map { |dir| File.basename dir }

  if subdirs.empty?
    STDERR.print "\e[31m[error]\e[0m gem folder `#{gems}` not found.\n"
    STDERR.print 'run `monorepo init`'
    exit! 2
  end

  puts subdirs.join("\n")
end
rake(*args) click to toggle source
# File lib/commands/rake.rb, line 6
def rake(*args)
  exec(['rake', *args])
end
version() click to toggle source
# File lib/commands/version.rb, line 3
def version
  STDOUT.write "#{VERSION}\n"
end