class Object

Constants

AUTHORS
BASE_SETTINGS_DIR

The base periodic settings path

DATA_DIR

The folder containing zips and b64 files

PROJECT_ROOT

The root of this project

SETTINGS_DIR

Settings pertaining to radon

SETTINGS_FILE

The settings json file

VERSION

The version

Public Instance Methods

ask(*args) click to toggle source

Prompts the user for input

# File lib/core/text.rb, line 66
def ask(*args)
  print(*args)
  $stdin.gets.chomp
end
classify(name) click to toggle source

Gets the project name in dash-seperated format. Eg: my-project-name

# File lib/core/text.rb, line 72
def classify(name)
  name = name.gsub(/-[a-z]/) {|s| s.upcase }
  name.delete!('-')
  return name.slice(0,1).capitalize + name.slice(1..-1)
end
create(fname) click to toggle source
# File lib/core/text.rb, line 43
def create(fname)
  puts Paint['  create', '#2ecc71'] + " #{fname}" unless $quiet
end
error(*args) click to toggle source
# File lib/core/text.rb, line 7
def error(*args)
  puts Paint['ERROR', '#e74c3c'] + " - #{args.join(' ')}"
end
extract_c(target) click to toggle source
# File lib/core/extract.rb, line 26
def extract_c(target)
  extract_zip(Radon::Environments.get_target_of('c'), target)
end
extract_crystal(target) click to toggle source
# File lib/core/extract.rb, line 30
def extract_crystal(target)
  extract_zip(Radon::Environments.get_target_of('crystal'), target)
  replace_all_tokens(target)
end
extract_go_s(target) click to toggle source
# File lib/core/extract.rb, line 35
def extract_go_s(target)
  extract_zip(Radon::Environments.get_target_of('go_s'), target)
end
extract_gradle(target) click to toggle source
# File lib/core/extract.rb, line 5
def extract_gradle(target)
  extract_zip(Radon::Environments.get_target_of('gradle'), target)
end
extract_java(target) click to toggle source
# File lib/core/extract.rb, line 9
def extract_java(target)
  extract_zip(Radon::Environments.get_target_of('java'), target)
end
extract_maven(target) click to toggle source
# File lib/core/extract.rb, line 13
def extract_maven(target)
  extract_zip(Radon::Environments.get_target_of('maven'), target)
end
extract_python(target) click to toggle source
# File lib/core/extract.rb, line 17
def extract_python(target)
  extract_zip(Radon::Environments.get_target_of('python'), target)
end
extract_ruby(target) click to toggle source
# File lib/core/extract.rb, line 21
def extract_ruby(target)
  extract_zip(Radon::Environments.get_target_of('ruby'), target)
  replace_all_tokens(target)
end
extract_website(target) click to toggle source
# File lib/core/extract.rb, line 39
def extract_website(target)
  extract_zip(Radon::Environments.get_target_of('website'), target)
end
extract_zip(file, destination) click to toggle source

Extracts some zip data to the passed destination

# File lib/core/extract.rb, line 44
def extract_zip(file, destination)
  puts "Creating project under #{File.expand_path(destination)}" unless $quiet
  create(destination)
  FileUtils.mkdir_p(destination)

  Zip::File.open(file) do |zip_file|
    zip_file.each do |f|
      fname = f.name.gsub('{{NAME}}', projectify(destination))
      fpath = File.join(destination, fname)
      if File.exists?(fpath)
        skip(fpath)
      else
        create(fpath)
      end
      zip_file.extract(f, fpath) unless File.exist?(fpath)
    end
  end
end
fail_with(reason) click to toggle source
# File lib/core/util.rb, line 68
def fail_with(reason)
  error(reason)
  exit 1
end
find_and_replace_all(target_dir, find, repl) click to toggle source

Replaces 'find' with 'repl' in every file and directory

# File lib/core/text.rb, line 79
def find_and_replace_all(target_dir, find, repl)
  files = Dir[File.join(target_dir,'**','*')]
  files.each do |file_name|
    next if File.directory?(file_name)
    begin
      text = File.read(file_name)
      # Do the find and replace
      new_contents = text.gsub(find, repl)
      File.open(file_name, 'w') {|f| f.puts new_contents}
    rescue Errno::ENOENT => e
      # Something weird happened (this shouldn't come up)
      error "ERROR GSUBING FILE #{file_name}"
      report_error_to_github(e.backtrace)
      next
    end
  end
end
projectify(name) click to toggle source

Gets the project name in capitalized format. Eg: MyProjectName

# File lib/core/text.rb, line 52
def projectify(name)
  # Name becomes frozen for some reason
  name = name.gsub(/(.)([A-Z])/, '\1-\2')
  name.downcase!
  name.gsub!('..', '')
  name.gsub!('/', '')
  name.gsub!('\\', '')
  name[0] = '' if name[0] == '-'
  name.gsub!('--', '-')
  name.gsub!('_-', '-')
  return name
end
replace_all_tokens(target) click to toggle source

Replaces all tokens in all files in the target directory

# File lib/core/text.rb, line 98
def replace_all_tokens(target)
  find_and_replace_all(target, '{{NAME}}', projectify(target))
  find_and_replace_all(target, '{{CAPSNAME}}', classify(target))
  find_and_replace_all(target, '{{EMAIL}}', Radon::Util.get_email)
  find_and_replace_all(target, '{{GHNAME}}', Radon::Util.get_github_username)
end
report_error_to_github(trace) click to toggle source
# File lib/core/text.rb, line 11
def report_error_to_github(trace)
  commit_string = File.directory?(File.join(PROJECT_ROOT, '.git')) ? "Commit `#{File.read(File.join(PROJECT_ROOT, '.git', 'refs', 'heads', 'master')).strip}`" : nil 
  puts %(
    :::::::::::::::::: COPY BELOW ::::::::::::::::::
    ### Ruby version

    `#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}`

    ### OS

    #{Gem::Platform.local.os}

    ### Radon Version

    `VERSION`

    ### Date Found

    #{Time.now.strftime("%d/%m/%Y %H:%M")}

    ### Backtrace
    ```
    #{trace}
    ```

    #{commit_string}
    :::::::::::::::::: COPY ABOVE ::::::::::::::::::
    #{Paint["Whoops! Looks like you've found a bug in radon. Please copy the text above and open a new issue at ", '#e74c3c'] + Paint['https://github.com/cbrnrd/radon/issues', :bold, :bright]}
  )
  
end
skip(fname) click to toggle source
# File lib/core/text.rb, line 47
def skip(fname)
  puts Paint['  skip', '#f1c40f'] + " #{fname}" unless $quiet
end
vprint(*args) click to toggle source
# File lib/core/text.rb, line 3
def vprint(*args)
  puts args if $verbose && !$quiet
end