module GoScript

@author Mike Bland (michael.bland@gsa.gov)

Constants

JEKYLL_BUILD_CMD
JEKYLL_SERVE_CMD
VERSION

Attributes

current_group[R]

Public Instance Methods

args_to_string(args) click to toggle source
# File lib/go_script/go.rb, line 69
def args_to_string(args)
  args ||= ''
  (args.instance_of? Array) ? args.join(' ') : args
end
build_jekyll(extra_args = '') click to toggle source
# File lib/go_script/go.rb, line 87
def build_jekyll(extra_args = '')
  exec_cmd "#{JEKYLL_BUILD_CMD} #{args_to_string extra_args}"
end
check_ruby_version(min_version) click to toggle source
# File lib/go_script/go.rb, line 10
def check_ruby_version(min_version)
  Version.check_ruby_version min_version
end
command_group(group_symbol, description) click to toggle source
# File lib/go_script/go.rb, line 14
def command_group(group_symbol, description)
  location = caller_locations(1, 1).first
  CommandGroup.add_group(group_symbol, description,
    location.path, location.lineno)
  @current_group = group_symbol
end
def_command(id, description, &command_block) click to toggle source
# File lib/go_script/go.rb, line 21
def def_command(id, description, &command_block)
  abort "#{$PROGRAM_NAME}: No command_groups defined" unless current_group
  abort "Command ID must be a symbol: #{id}" unless id.instance_of? Symbol
  self.class.send :define_method, id, ->(*argv) { command_block.call(*argv) }
  path, lineno = command_block.source_location
  CommandGroup.add_command id, @current_group, description, path, lineno
end
exec_cmd(cmd) click to toggle source
# File lib/go_script/go.rb, line 42
def exec_cmd(cmd)
  status = system(cmd, err: :out)
  if $CHILD_STATUS.exitstatus.nil?
    $stderr.puts "could not run command: #{cmd}"
    $stderr.puts '(Check syslog for possible `Out of memory` error?)'
    exit 1
  else
    exit $CHILD_STATUS.exitstatus unless status
  end
end
execute_command(argv) click to toggle source
# File lib/go_script/go.rb, line 29
def execute_command(argv)
  command = argv.shift
  CommandGroup.usage exitstatus: 1 if command.nil?
  CommandGroup.usage if ['-h', '--help', '-help', 'help'].include? command
  send :version if ['-v', '--version', 'version'].include? command
  send CommandGroup.command(command.to_sym), argv
end
file_args_by_extension(file_args, extension) click to toggle source
# File lib/go_script/go.rb, line 74
def file_args_by_extension(file_args, extension)
  if file_args.instance_of? Array
    files_by_extension = file_args.group_by { |f| File.extname f }
    args_to_string files_by_extension[extension]
  else
    args_to_string file_args
  end
end
git_sync_and_deploy(commands, branch: nil) click to toggle source
# File lib/go_script/go.rb, line 91
def git_sync_and_deploy(commands, branch: nil)
  exec_cmd 'git fetch origin #{branch}'
  exec_cmd 'git clean -f'
  exec_cmd "git reset --hard #{branch.nil? ? 'HEAD' : 'origin/' + branch}"
  exec_cmd 'git submodule --sync --recursive'
  exec_cmd 'git submodule update --init --recursive'
  commands.each { |command| exec_cmd command }
end
lint_javascript(basedir, files) click to toggle source
# File lib/go_script/go.rb, line 104
def lint_javascript(basedir, files)
  files = file_args_by_extension files, '.js'
  exec_cmd "#{basedir}/node_modules/.bin/eslint #{files}"
end
lint_ruby(files) click to toggle source
# File lib/go_script/go.rb, line 100
def lint_ruby(files)
  exec_cmd "rubocop #{file_args_by_extension files, '.rb'}"
end
serve_jekyll(extra_args = '') click to toggle source
# File lib/go_script/go.rb, line 83
def serve_jekyll(extra_args = '')
  exec "#{JEKYLL_SERVE_CMD} #{args_to_string extra_args}"
end
update_gems(gems = '') click to toggle source
# File lib/go_script/go.rb, line 53
def update_gems(gems = '')
  exec_cmd "bundle update #{args_to_string gems}"
  exec_cmd 'git add Gemfile.lock'
end
update_node_modules() click to toggle source
# File lib/go_script/go.rb, line 58
def update_node_modules
  abort 'Install npm to update JavaScript components: ' \
    'http://nodejs.org/download/' unless system 'which npm > /dev/null'

  exec_cmd 'npm update'
  exec_cmd 'npm install'
end
version() click to toggle source
# File lib/go_script/go.rb, line 37
def version
  puts "go_script version #{VERSION}"
  exit 0
end