Class: Sign::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/sign/generator.rb

Instance Method Summary collapse

Instance Method Details

#create(license, name, year) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sign/generator.rb', line 5

def create(license, name, year)
  raise ArgumentError, "#{license} is not available." unless File.readable?(license)

  license_template = File.read(license)
  name = get_name if name.nil?
  year = get_year if year.nil?
  
  if placeholders_exist?(license_template)
    new_license = license_template.gsub("[AUTHOR]", name)
    new_license = new_license.gsub("[YEAR]", year)
  else
    new_license = license_template
  end
  
  create_new_file(new_license)
end

#create_new_file(new_license) ⇒ Object



28
29
30
31
32
33
# File 'lib/sign/generator.rb', line 28

def create_new_file(new_license)
  new_file = File.new("LICENSE", "w")
  new_file.puts(new_license)
  new_file.close
  puts "License created \033[91m<3\033[0m"
end

#find_nameObject



43
44
45
46
47
48
49
# File 'lib/sign/generator.rb', line 43

def find_name
  File.foreach("#{gitconfig_path}/.gitconfig").detect do |line| 
    if line.match("\tname")
      return line.scan(/= (.*)/).last[0]
    end
  end
end

#get_nameObject



35
36
37
38
39
40
41
# File 'lib/sign/generator.rb', line 35

def get_name
  if gitconfig_exist?
    find_name
  else
    return ""
  end
end

#get_yearObject



51
52
53
# File 'lib/sign/generator.rb', line 51

def get_year
  return Date.today.year.to_s
end

#gitconfig_exist?Boolean

Returns:

  • (Boolean)


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

def gitconfig_exist?
  File.exist?("#{gitconfig_path}/.gitconfig")
end

#gitconfig_pathObject



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

def gitconfig_path
  File.expand_path("~/", __dir__)
end

#placeholders_exist?(license_template) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/sign/generator.rb', line 22

def placeholders_exist?(license_template)
  placeholders = ["[YEAR]", "[AUTHOR]"]
  
  placeholders.any? { |placeholder| license_template.include?(placeholder) }
end