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



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

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



217
218
219
# File 'lib/itcss_cli.rb', line 217

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

#find_valid_module(arg) ⇒ Object



231
232
233
234
235
236
237
238
239
# File 'lib/itcss_cli.rb', line 231

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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/itcss_cli.rb', line 242

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



306
307
308
309
310
# File 'lib/itcss_cli.rb', line 306

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



312
313
314
315
316
317
# File 'lib/itcss_cli.rb', line 312

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

#inuit_helpObject



297
298
299
300
301
302
303
# File 'lib/itcss_cli.rb', line 297

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



323
324
325
326
# File 'lib/itcss_cli.rb', line 323

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

#inuit_module_fullname(c_module, filename) ⇒ Object



319
320
321
# File 'lib/itcss_cli.rb', line 319

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

#inuit_new_module(c_module, file, module_object) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/itcss_cli.rb', line 264

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)
    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
      first_init = nil

      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



190
191
192
193
# File 'lib/itcss_cli.rb', line 190

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

#itcss_initObject



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

def itcss_init
  unless File.exist?(@ITCSS_CONFIG_FILE)
    File.open @ITCSS_CONFIG_TEMPLATE do |io|
      template = ERB.new io.read
      first_init = true

      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_init_checkerObject

Helper Methods



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/itcss_cli.rb', line 200

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



131
132
133
134
135
136
137
138
139
# File 'lib/itcss_cli.rb', line 131

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/itcss_cli.rb', line 148

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



141
142
143
144
145
146
# File 'lib/itcss_cli.rb', line 141

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/itcss_cli.rb', line 166

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] = inuit_find_modules(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

#itcss_versionObject



195
196
197
# File 'lib/itcss_cli.rb', line 195

def itcss_version
  puts VERSION
end

#not_a_valid_commandObject



221
222
223
224
225
226
227
228
229
# File 'lib/itcss_cli.rb', line 221

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



213
214
215
# File 'lib/itcss_cli.rb', line 213

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