Class: MonorepoCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/monorepo.rb,
lib/commands/ls.rb,
lib/commands/exec.rb,
lib/commands/init.rb,
lib/commands/rake.rb,
lib/commands/version.rb,
lib/commands/bootstrap.rb

Constant Summary collapse

VERSION =
MONOREPO_VERSION
CONFIG_FILE_NAME =
[
  'Monorepofile',
  'Monorepofile.yml',
  'Monorepofile.yaml',
  'monorepofile',
  'monorepofile.yml',
  'monorepofile.yaml',
].freeze
DEFAULT_CONFIG =
{
  'gems' => 'gems/*',
  'bundler' => 'no',
}.freeze
EXIT_STATUS_ZERO =
0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_config(filename = '') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

Instance Method Details

#bootstrap(*args) ⇒ Object



5
6
7
# File 'lib/commands/bootstrap.rb', line 5

def bootstrap(*args)
  exec ['bundle', *args].join(' ')
end

#exec(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

#initObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

#listObject



33
34
35
# File 'lib/commands/ls.rb', line 33

def list
  self.ls
end

#lsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



6
7
8
# File 'lib/commands/rake.rb', line 6

def rake(*args)
  exec(['rake', *args])
end

#versionObject



3
4
5
# File 'lib/commands/version.rb', line 3

def version
  STDOUT.write "#{VERSION}\n"
end