Class: ItcssCli::Init

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

Constant Summary collapse

ITCSS_CONFIG_FILE =
'itcss.yml'
ITCSS_CONFIG_TEMPLATE =
File.expand_path(File.join(File.dirname(__FILE__), "../templates/itcss_config.erb"))
ITCSS_MODULE_TEMPLATE =
File.expand_path(File.join(File.dirname(__FILE__), "../templates/itcss_module.erb"))
ITCSS_APP_TEMPLATE =
File.expand_path(File.join(File.dirname(__FILE__), "../templates/itcss_application.erb"))
ITCSS_FILES =
["settings", "tools", "generic", "base", "objects", "components", "trumps"]
ITCSS_CONFIG =
nil

Instance Method Summary collapse

Instance Method Details

#command_parserObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/itcss_cli.rb', line 31

def command_parser
  # $ itcss init
  if ARGV[0] == 'init'
    init_itcss_config_file


  # $ itcss install example
  elsif ARGV[0] == 'install' && ARGV[1] == 'example'
    init_checker
    new_itcss_basic_structure


  # $ itcss new components buttons
  elsif ARGV[0] == 'new' && ARGV[1] && ARGV[2]
    init_checker

    occur = ITCSS_FILES.each_index.select{|i| ITCSS_FILES[i].include? ARGV[1]}
    if occur.size == 1
      new_itcss_module(ITCSS_FILES[occur[0]], ARGV[2])
    else
      puts "'#{ARGV[1]}' is not an ITCSS module. Try settings, tools, generic, base, objects, components or trumps.".red
      abort
    end
  end

  # $ itcss update
  if ARGV[0] == 'install' || ARGV[0] == 'new' || ARGV[0] == 'update'
    generate_base_file
  end
end

#generate_base_fileObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/itcss_cli.rb', line 114

def generate_base_file
  itcss_files = Dir[ File.join(ITCSS_DIR, '**', '*') ].reject { |p| File.directory? p }

  file_path = "#{ITCSS_DIR}/#{ITCSS_BASE_FILE}.sass"
  contents = "#{ITCSS_BASE_FILE}.sass"
  File.open ITCSS_APP_TEMPLATE do |io|
    template = ERB.new io.read

    File.open file_path, "w+" do |out|
      out.puts template.result binding
    end
  end

  puts "update #{file_path}".blue
end

#init_checkerObject



24
25
26
27
28
29
# File 'lib/itcss_cli.rb', line 24

def init_checker
  if ITCSS_CONFIG.nil?
    puts "There's no #{ITCSS_CONFIG_FILE} created yet. Run `itcss init` to create it.".red
    abort
  end
end

#init_itcss_config_fileObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/itcss_cli.rb', line 62

def init_itcss_config_file
  unless File.exist?(ITCSS_CONFIG_FILE)
    File.open ITCSS_CONFIG_TEMPLATE do |io|
      template = ERB.new io.read

      File.open ITCSS_CONFIG_FILE, "w+" do |out|
        out.puts template.result binding
      end
    end
    puts "create #{ITCSS_CONFIG_FILE}".green
  else
    puts "#{ITCSS_CONFIG_FILE} already exists.".red
    abort
  end
end

#new_itcss_basic_structureObject



78
79
80
81
82
83
84
85
86
# File 'lib/itcss_cli.rb', line 78

def new_itcss_basic_structure
  File.open ITCSS_MODULE_TEMPLATE do |io|
    template = ERB.new io.read

    ITCSS_FILES.each do |file|
      new_itcss_file(file, 'example', template)
    end
  end
end

#new_itcss_file(type, file, template) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/itcss_cli.rb', line 97

def new_itcss_file(type, file, template)
  FileUtils.mkdir_p ITCSS_DIR
  FileUtils.mkdir_p "#{ITCSS_DIR}/#{type}"

  file_path = "#{ITCSS_DIR}/#{type}/_#{type}.#{file}.sass"
  unless File.exist?(file_path)
    contents = "##{type}.#{file}"
    File.open file_path, "w+" do |out|
      out.puts template.result binding
    end
    puts "create #{file_path}".green
  else
    puts "#{file_path} is already created. Please delete it if you want it to be rewritten.".red
    abort
  end
end

#new_itcss_module(type, file) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/itcss_cli.rb', line 88

def new_itcss_module(type, file)
  File.chmod(0644, ITCSS_DIR)

  File.open ITCSS_MODULE_TEMPLATE do |io|
    template = ERB.new io.read
    new_itcss_file(type, file, template)
  end
end