Class: ItcssCli::Core

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

Instance Method Summary collapse

Constructor Details

#initializeCore

Returns a new instance of Core.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/itcss_cli.rb', line 10

def initialize
  @ITCSS_CONFIG_FILE = 'itcss.yml'
  @ITCSS_CONFIG_TEMPLATE = relative_file_path "../templates/itcss_config.erb"
  @ITCSS_MODULE_TEMPLATE = relative_file_path "../templates/itcss_module.erb"
  @ITCSS_APP_TEMPLATE = relative_file_path "../templates/itcss_application.erb"
  @ITCSS_MODULES = ["requirements", "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 = ['init', 'install', 'new', 'n', 'inuit', 'update', 'u', 'help', 'h', '-h', 'version', 'v', '-v']

  @ITCSS_COMMANDS_DESCRIPTION = [
    "             COMMAND                  ALIAS                               FUNCTION                                 ",
    "itcss init                          |       | Initiates itcss_cli configuration with a itcss.yml file. [start here]",
    "itcss install [filenames]           |       | Creates an example of ITCSS structure in path specified in itcss.yml.",
    "itcss new [module] [filename]       |   n   | Creates a new ITCSS module and automatically import it into imports file.",
    "itcss inuit new [inuit module]      |inuit n| Add specified inuit module as an itcss dependency.",
    "itcss inuit help                    |inuit h| Add specified inuit module as an itcss dependency.",
    "itcss update                        |   u   | Updates the imports file using the files inside ITCSS structure.",
    "itcss help                          | h, -h | Shows all available itcss commands and it's functions.",
    "itcss version                       | v, -v | Shows itcss_cli gem version installed."
  ]

  if File.exist?(@ITCSS_CONFIG_FILE)
    @ITCSS_CONFIG = YAML.load_file(@ITCSS_CONFIG_FILE)
    @ITCSS_DIR ||= @ITCSS_CONFIG['stylesheets_directory']
    @ITCSS_BASE_FILE ||= @ITCSS_CONFIG['stylesheets_import_file']
  else
    @ITCSS_CONFIG = nil
  end

  if File.exist?(@ITCSS_CONFIG_FILE) && @ITCSS_CONFIG['package_manager']
    @ITCSS_PACKAGE_MANAGER ||= @ITCSS_CONFIG['package_manager']
    @INUIT_MODULES ||= @ITCSS_CONFIG['inuit_modules']
  else
    @ITCSS_PACKAGE_MANAGER = nil
  end

  @INUIT_AVAILABLE_MODULES_FILE = relative_file_path "../data/inuit_modules.yml"
  @INUIT_AVAILABLE_MODULES = YAML.load_file(@INUIT_AVAILABLE_MODULES_FILE)
end

Instance Method Details

#command_parserObject

ITCSS



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/itcss_cli.rb', line 61

def command_parser
  # Not a valid command
  unless @ITCSS_COMMANDS.include? ARGV[0]
    not_a_valid_command
  end

  # $ itcss init
  if 'init' == ARGV[0]
    itcss_init


  # $ itcss install example
  elsif 'install' == ARGV[0]
    itcss_init_checker
    itcss_install(ARGV[1])


  # $ itcss new||n [module] [filename]
  elsif ['new', 'n'].include? ARGV[0]
    if find_valid_module ARGV[1]
      if ARGV[2]
        itcss_init_checker
        itcss_new_module(find_valid_module(ARGV[1]), ARGV[2])
      else
        not_a_valid_command
      end
    else
      not_a_valid_command
    end

  # $ itcss inuit||i [module] [filename]
  elsif 'inuit' == ARGV[0]
    inuit_command_parser


  # $ itcss help
  elsif ['help', '-h', 'h'].include? ARGV[0]
    itcss_help


  # $ itcss version
  elsif ['version', '-v', 'v'].include? ARGV[0]
    itcss_version
  end


  # $ itcss update
  if ['install', 'new', 'n', 'inuit', 'update', 'u'].include? ARGV[0]
    itcss_init_checker
    itcss_update_import_file
  end
end

#current_full_commandObject



255
256
257
# File 'lib/itcss_cli.rb', line 255

def current_full_command
  "`itcss #{ARGV.join(' ')}`"
end

#find_valid_module(arg) ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'lib/itcss_cli.rb', line 269

def find_valid_module(arg)
  occur = @ITCSS_MODULES.each_index.select{|i| @ITCSS_MODULES[i].include? arg}
  if occur.size == 1
    return @ITCSS_MODULES[occur[0]]
  else
    puts "'#{arg}' is not an ITCSS module. Try #{@ITCSS_MODULES.join(', ')}.".red
    abort
  end
end

#inuit_command_parserObject

INUIT



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/itcss_cli.rb', line 280

def inuit_command_parser
  if @ITCSS_PACKAGE_MANAGER.nil?
    puts "You didn't choose a package manager. Please do it in itcss.yml".red
    abort
  end

  # $ itcss inuit new [inuit module]
  if ['new', 'n'].include? ARGV[1]
    if ARGV[2] && inuit_find_valid_module(ARGV[2])
      itcss_init_checker
      inuit_module_name_frags = ARGV[2].split('.')
      inuit_new_module(inuit_module_name_frags[0], inuit_module_name_frags[1], inuit_find_valid_module(ARGV[2]))
    else
      not_a_valid_command
    end

  # $ itcss inuit help
  elsif ['help', 'h', '-h'].include? ARGV[1]
    inuit_help
  end
end

#inuit_find_modules(current_module) ⇒ Object

Inuit Helper Methods



348
349
350
351
352
# File 'lib/itcss_cli.rb', line 348

def inuit_find_modules(current_module)
  current_config = YAML.load_file(@ITCSS_CONFIG_FILE)
  current_inuit_modules = current_config["inuit_modules"].select{ |p| p.include? current_module }
  current_inuit_modules.map{ |p| inuit_imports_path p }
end

#inuit_find_valid_module(c_module) ⇒ Object



354
355
356
357
358
359
# File 'lib/itcss_cli.rb', line 354

def inuit_find_valid_module(c_module)
  valid_module = @INUIT_AVAILABLE_MODULES[c_module]
  unless valid_module.nil?
    valid_module
  end
end

#inuit_helpObject



339
340
341
342
343
344
345
# File 'lib/itcss_cli.rb', line 339

def inuit_help
  puts "itcss inuit available commmands:".yellow
  puts "  COMMAND                                   | #{@ITCSS_PACKAGE_MANAGER.upcase} EQUIVALENT"
  puts @INUIT_AVAILABLE_MODULES.map { |e| "  itcss inuit new #{e[0]}"+" "*(26-e[0].size)+"| "+e[1]['slug']  }
  puts "You can check all of these repositories at https://github.com/inuitcss/[inuit module].".yellow
  abort
end

#inuit_imports_path(filename) ⇒ Object



365
366
367
368
# File 'lib/itcss_cli.rb', line 365

def inuit_imports_path(filename)
  frags = filename.split(".")
  "inuit-#{frags[1]}/#{filename}"
end

#inuit_module_fullname(c_module, filename) ⇒ Object



361
362
363
# File 'lib/itcss_cli.rb', line 361

def inuit_module_fullname(c_module, filename)
  "#{c_module}.#{filename}"
end

#inuit_new_module(c_module, file, module_object) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/itcss_cli.rb', line 302

def inuit_new_module(c_module, file, module_object)
  if file
    current_module_name = inuit_module_fullname(c_module, file)
    config_file = @ITCSS_CONFIG_FILE
    current_config = YAML.load_file(config_file)

    if current_config['inuit_modules'].nil?
      current_config['inuit_modules'] = []
    end

    current_config['inuit_modules'] << current_module_name

    unless current_config['inuit_modules'].uniq.length == current_config['inuit_modules'].length
      puts "#{current_module_name} is already added to #{@ITCSS_CONFIG_FILE}.".yellow
      abort
    end

    current_config['inuit_modules'].uniq!

    File.open @ITCSS_CONFIG_TEMPLATE do |io|
      template = ERB.new io.read
      content = current_config.to_yaml

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

    puts "using #{@ITCSS_PACKAGE_MANAGER} to install inuit '#{current_module_name}' dependency...".green
    sleep(2)
    output = `#{@ITCSS_PACKAGE_MANAGER} install --save #{module_object['slug']}`
    puts output

    puts "update #{@ITCSS_CONFIG_FILE}. [added #{current_module_name}]".blue
  end
end

#itcss_helpObject



228
229
230
231
# File 'lib/itcss_cli.rb', line 228

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

#itcss_initObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/itcss_cli.rb', line 114

def itcss_init
  if File.exist?(@ITCSS_CONFIG_FILE)
    puts "There is already a itcss.yml created.".yellow
    puts "Do you want to override it? [ y / n ]"
    user_override_itcss_yml = STDIN.gets.chomp
    unless user_override_itcss_yml == 'y'
      abort
    end
  end

  init_config = {}

  puts "Well done! Let's configure your itcss.yml:".yellow

  puts "Provide the root folder name where the ITCSS file structure should be built:"
  user_itcss_dir = STDIN.gets.chomp
  init_config['stylesheets_directory'] = user_itcss_dir

  puts "What is the name of your base sass file (all ITCSS modules will be imported into it):"
  user_itcss_base_file = STDIN.gets.chomp
  init_config['stylesheets_import_file'] = user_itcss_base_file

  puts "Are you using a package manager? [ y / n ]"
  user_itcss_package_manager = STDIN.gets.chomp
  if user_itcss_package_manager == 'y'
    user_package_manager = true
  end

  if user_package_manager == true
    puts "Choose your package manager [ bower / npm ]:"
    user_package_manager = STDIN.gets.chomp

    unless ['bower', 'npm'].include? user_package_manager
      puts "#{user_package_manager} is not a valid package manager".red
      abort
    end

    init_config['package_manager'] = user_package_manager
  end

  File.open @ITCSS_CONFIG_TEMPLATE do |io|
    template = ERB.new io.read
    content = init_config.to_yaml

    File.open @ITCSS_CONFIG_FILE, "w+" do |out|
      out.puts template.result binding
    end
  end
  puts "#{@ITCSS_CONFIG_FILE} successfully created!".green
end

#itcss_init_checkerObject

Helper Methods



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/itcss_cli.rb', line 238

def itcss_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 == 'path/to/itcss/root' || @ITCSS_BASE_FILE == 'yourapplication'
    puts "You haven't done the itcss_cli's configuration. You must provide your directories settings in itcss.yml.".yellow
    abort
  end
end

#itcss_install(filename) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/itcss_cli.rb', line 165

def itcss_install(filename)
  File.open @ITCSS_MODULE_TEMPLATE do |io|
    template = ERB.new io.read

    @ITCSS_MODULES.each do |file|
      itcss_new_file(file, filename, template)
    end
  end
end

#itcss_new_file(type, file, template) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/itcss_cli.rb', line 182

def itcss_new_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

#itcss_new_module(type, file) ⇒ Object



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

def itcss_new_module(type, file)
  File.open @ITCSS_MODULE_TEMPLATE do |io|
    template = ERB.new io.read
    itcss_new_file(type, file, template)
  end
end

#itcss_update_import_fileObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/itcss_cli.rb', line 200

def itcss_update_import_file
  FileUtils.mkdir_p @ITCSS_DIR

  itcss_files_to_import = {}
  @ITCSS_MODULES.each do |current_module|
    itcss_files_to_import[current_module] = []

    if @INUIT_MODULES
      itcss_files_to_import[current_module] += inuit_find_modules(current_module)
    end

    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

#itcss_versionObject



233
234
235
# File 'lib/itcss_cli.rb', line 233

def itcss_version
  puts VERSION
end

#not_a_valid_commandObject



259
260
261
262
263
264
265
266
267
# File 'lib/itcss_cli.rb', line 259

def not_a_valid_command
  puts "#{current_full_command} is not a valid command. Check out the available commands:".red
  if 'inuit' == ARGV[0]
    inuit_help
  else
    itcss_help
  end
  abort
end

#relative_file_path(filename) ⇒ Object



251
252
253
# File 'lib/itcss_cli.rb', line 251

def relative_file_path(filename)
  File.expand_path(File.join(File.dirname(__FILE__), filename))
end