Class: Itcsscli::Core

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

Instance Method Summary collapse

Constructor Details

#initializeCore

Returns a new instance of Core.



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
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/itcsscli.rb', line 18

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_DOC_TEMPLATE = relative_file_path "../templates/itcss_doc.html.erb"
  @ITCSS_DOC_DIR = 'itcssdoc'
  @ITCSS_DOC_FILE = "#{@ITCSS_DOC_DIR}/index.html"
  @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', 'doc', 'd', '-d', 'help', 'h', '-h', 'version', 'v', '-v']

  @ITCSS_COMMANDS_DESCRIPTION = [
    "             COMMAND                  ALIAS                               FUNCTION                                 ",
    "itcss init                          |       | Initiates itcsscli configuration with a #{@ITCSS_CONFIG_FILE} file. [start here]",
    "itcss install [filenames]           |       | Creates an example of ITCSS structure in path specified in #{@ITCSS_CONFIG_FILE}.",
    "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| Shows all available itcss inuit commands and it's functions.",
    "itcss doc                           | d, -d | Generate and open itcssdoc.",
    "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 itcsscli 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']
    @INUIT_PREFIX ||= @ITCSS_CONFIG['inuit_prefix']
  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



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/itcsscli.rb', line 74

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

  # $ itcss doc
  elsif ['doc', '-d', 'd'].include? ARGV[0]
    itcss_init_checker
    initialize_doc
  end

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

#current_full_commandObject



267
268
269
# File 'lib/itcsscli.rb', line 267

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

#find_valid_module(arg) ⇒ Object



281
282
283
284
285
286
287
288
289
# File 'lib/itcsscli.rb', line 281

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



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/itcsscli.rb', line 292

def inuit_command_parser
  if @ITCSS_PACKAGE_MANAGER.nil?
    puts "You didn't choose a package manager. Please do it in #{@ITCSS_CONFIG_FILE}".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

  # $ itcss update
  if ['new', 'n'].include? ARGV[1]
    itcss_update_import_file
  end
end

#inuit_find_modules(current_module) ⇒ Object

Inuit Helper Methods



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

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



371
372
373
374
375
376
# File 'lib/itcsscli.rb', line 371

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

#inuit_helpObject



356
357
358
359
360
361
362
# File 'lib/itcsscli.rb', line 356

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



382
383
384
# File 'lib/itcsscli.rb', line 382

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

#inuit_module_fullname(c_module, filename) ⇒ Object



378
379
380
# File 'lib/itcsscli.rb', line 378

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

#inuit_new_module(c_module, file, module_object) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/itcsscli.rb', line 319

def inuit_new_module(c_module, file, module_object)
  if file
    current_module_name = inuit_module_fullname(c_module, file)
    current_config = YAML.load_file(@ITCSS_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

    @INUIT_MODULES = current_config['inuit_modules']

    puts "using #{@ITCSS_PACKAGE_MANAGER} to install inuit '#{current_module_name}' dependency...".green
    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



243
244
245
246
# File 'lib/itcsscli.rb', line 243

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

#itcss_initObject



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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/itcsscli.rb', line 129

def itcss_init
  if File.exist?(@ITCSS_CONFIG_FILE)
    puts "There is already a #{@ITCSS_CONFIG_FILE} created.".yellow
    puts "Do you want to override it?"
    user_override_itcss_yml = Readline.readline '[ y / n ] > '
    unless user_override_itcss_yml == 'y'
      abort
    end
  end

  init_config = {}

  puts "Well done! Let's configure your #{@ITCSS_CONFIG_FILE}:".yellow

  puts "Provide the root folder name where the ITCSS file structure should be built:"
  user_itcss_dir = Readline.readline '> '
  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 = Readline.readline '> '
  init_config['stylesheets_import_file'] = user_itcss_base_file

  puts "Are you using a package manager?"
  user_itcss_package_manager = Readline.readline '[ y / n ] > '
  if user_itcss_package_manager == 'y'
    user_package_manager = true
  end

  if user_package_manager == true
    puts "Choose your package manager:"
    user_package_manager = Readline.readline '[ bower / npm ] > '

    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
    init_config['inuit_prefix'] = ''
  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



253
254
255
256
257
258
259
260
261
# File 'lib/itcsscli.rb', line 253

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_CONFIG_FILE} file. Please run `itcss init` again to override it.".red
    abort
  end
end

#itcss_install(filename) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/itcsscli.rb', line 181

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/itcsscli.rb', line 198

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)
    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



191
192
193
194
195
196
# File 'lib/itcsscli.rb', line 191

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



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/itcsscli.rb', line 215

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}/", '').gsub(".sass", '').gsub("_", '')}
  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



248
249
250
# File 'lib/itcsscli.rb', line 248

def itcss_version
  puts VERSION
end

#not_a_valid_commandObject



271
272
273
274
275
276
277
278
279
# File 'lib/itcsscli.rb', line 271

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



263
264
265
# File 'lib/itcsscli.rb', line 263

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