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_MODULES =
["settings", "tools", "generic", "base", "objects", "components", "trumps"]
ITCSS_FILES =
{
  "requirements" => "Vendor libraries",
  "settings" => "Sass vars, etc.",
  "tools" => "Functions and mixins.",
  "generic" => "Generic, high-level styling, like resets, etc.",
  "base" => "Unclasses HTML elements (e.g. `h2`, `ul`).",
  "objects" => "Objects and abstractions.",
  "components" => "Your designed UI elements (inuitcss includes none of these).",
  "trumps" => "Overrides and helper classes."
}
ITCSS_COMMANDS =
[
  "itcss init                       | Initiates itcss_cli configuration with a itcss.yml file. [start here]",
  "itcss install example            | Creates an example of ITCSS structure in path specified in itcss.yml.",
  "itcss new [module] [filename]    | Creates a new ITCSS module and automatically import it into imports file.",
  "itcss update                     | Updates the imports file using the files inside ITCSS structure.",
  "itcss help                       | Shows all available itcss commands and it's functions.",
  "itcss version                    | Shows itcss_cli gem version installed. [short-cut alias: '-v', 'v']"
]
ITCSS_CONFIG =
nil

Instance Method Summary collapse

Instance Method Details

#command_parserObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/itcss_cli.rb', line 56

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_MODULES.each_index.select{|i| ITCSS_MODULES[i].include? ARGV[1]}
    if occur.size == 1
      new_itcss_module(ITCSS_MODULES[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


  # $ itcss help
  elsif ARGV[0] == 'help'
    itcss_help


  # $ itcss version
  elsif ARGV[0] == 'version' || ARGV[0] == '-v' || ARGV[0] == 'v'
    itcss_version
  end

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

#generate_base_fileObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/itcss_cli.rb', line 150

def generate_base_file
  FileUtils.mkdir_p ITCSS_DIR

  itcss_files_to_import = {}
  ITCSS_MODULES.each do |current_module|
    itcss_module_files = Dir[ File.join("#{ITCSS_DIR}/#{current_module}/", '**', '*') ].reject { |p| File.directory? p }
    itcss_files_to_import[current_module] = itcss_module_files.map{|s| s.gsub("#{ITCSS_DIR}/", '')}
  end

  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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/itcss_cli.rb', line 43

def init_checker
  if ITCSS_CONFIG.nil?
    puts "There's no #{ITCSS_CONFIG_FILE} created yet. Run `itcss init` to create it.".red
    abort
  elsif ITCSS_DIR.nil? || ITCSS_BASE_FILE.nil?
    puts "Something is wrong with your itcss.yml file. Please delete it and run `itcss init` again.".red
    abort
  elsif ITCSS_DIR == 'TODO' || ITCSS_BASE_FILE == 'TODO'
    puts "You haven't done the itcss_cli's configuration. You must provide your directories settings in itcss.yml.".yellow
    abort
  end
end

#init_itcss_config_fileObject



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

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
    puts "Well done! Please do your own configurations in itcss.yml.".yellow
  else
    puts "#{ITCSS_CONFIG_FILE} already exists.".red
    abort
  end
end

#itcss_helpObject



172
173
174
175
# File 'lib/itcss_cli.rb', line 172

def itcss_help
  puts "itcss_cli available commmands:".yellow
  puts ITCSS_COMMANDS.map{|s| s.prepend("  ")}
end

#itcss_versionObject



177
178
179
# File 'lib/itcss_cli.rb', line 177

def itcss_version
  puts VERSION
end

#new_itcss_basic_structureObject



115
116
117
118
119
120
121
122
123
# File 'lib/itcss_cli.rb', line 115

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

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

#new_itcss_file(type, file, template) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/itcss_cli.rb', line 132

def new_itcss_file(type, file, template)
  FileUtils.mkdir_p ITCSS_DIR
  FileUtils.mkdir_p "#{ITCSS_DIR}/#{type}"
  FileUtils.chmod "u=wrx,go=rx", ITCSS_DIR

  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



125
126
127
128
129
130
# File 'lib/itcss_cli.rb', line 125

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