Class: Shop::Command

Inherits:
Object
  • Object
show all
Extended by:
Color
Defined in:
lib/shop/command.rb

Constant Summary

Constants included from Color

Shop::Color::CODES

Class Method Summary collapse

Methods included from Color

colorize, included

Class Method Details

.clean(major) ⇒ Object

Clean cache or class index



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/shop/command.rb', line 374

def clean(major)
  if major.nil? || major == 'cache'
    theme
    files = Dir["themes/#{theme}/cache/*.css"]
    files = files + Dir["themes/#{theme}/cache/*.js"]

    files.each do |f|
      File.delete(f)
    end
  elsif major == 'class'
    version = psVersion
    if version[0, 3] == "1.4"
      return
    end

    print "      Cleaning class index... "
    index = "cache/class_index.php"
    if File.exists?(index)
      File.delete(index)
    end
    File.open(index, "w")
    # the file needs to be chmod'ed to 666
    File.chmod(0666, index)
  end

  done
end

.complete(word) ⇒ Object

Method for the autocompletion



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/shop/command.rb', line 496

def complete(word)
  complete = %{
    new
    init
    install
    module
    module template
    override
    override controller
    override class
    controller
    clean
    clean class
    jshint
    makefile
  }
  puts complete
end

.controller(major) ⇒ Object

TODO:

adapt this to work with PS 1.4 (needs a .php file in the root dir)

Creates a new page with controller and files associated



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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/shop/command.rb', line 307

def controller(major)
  if major.nil?
    puts "#{red("Error")}: Please provide a name for the page"
    exit
  end
  theme
  version = psVersion
  version = version[0, 3]

  # convert the name to camelcase to hyphen
  name = major.split(/(?=[A-Z])/)
  name = name.join('-').downcase

  controller_name = "#{major}Controller"
  filename = "#{controller_name}.php"
  if version.to_f >= 1.5
    controller_path = "controllers/front/#{filename}"
  elsif version == "1.4"
    controller_path = "controllers/#{filename}"
  end

  if File.exists?(controller_path)
    puts "#{red("Error")}: Controller already exists"
    exit
  end

  # datas for templates
  datas = {
    'controller_name' => controller_name,
    'name' => name
  }

  # For PS 1.4 creates a php file a the root
  # and use the appropriate template
  if version[0, 3] == "1.4"
      content = template.template("page.php", datas)
      puts "      #{green("create")} #{name}.php"
      File.open("#{name}.php", "w") do |f|
        f.write(content)
      end
      controllerContent = template.template("controller-1.4.php", datas)
  else
    controllerContent = template.template("controller.php", datas)
  end

  puts "      #{green("create")} #{controller_path}"
  File.open(controller_path, "w") do |f|
    f.write(controllerContent)
  end

  files = [
    "themes/#{theme}/#{name}.tpl",
    "themes/#{theme}/css/#{name}.css",
    "themes/#{theme}/js/#{name}.js"
  ]

  files.each do |f|
    puts "      #{green("create")} #{f}"
    File.open(f, "w")
  end

  clean("class")

  done
end

.dispatch(command, major, minor, extra) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shop/command.rb', line 44

def dispatch(command, major, minor, extra)
  return newProject(major)               if command == 'new'
  return init(major)                     if command == 'init'
  return install                         if command == 'install'
  return shopModule(major, minor, extra) if command == 'module'
  return override(major, minor, extra)   if command == 'override'
  return controller(major)               if command == 'controller'
  return clean(major)                    if command == 'clean'
  return jshint(major)                   if command == 'jshint'
  return makefile                        if command == 'makefile'
  return edit                            if command == 'edit'
  return version                         if command == "-v"
  return version                         if command == "--version"
  return help                            if command == 'help'
  return complete(major)                 if command == 'cmplt'

  puts "\n#{red("Error")}: Command not found"
  return help
end

.doneObject



440
441
442
# File 'lib/shop/command.rb', line 440

def done
  puts "\n      #{green("✔ Done")}"
end

.editObject



444
445
446
447
# File 'lib/shop/command.rb', line 444

def edit
  config = ShopConfig.new
  puts config.edit
end

.execute(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/shop/command.rb', line 34

def execute(*args)
  command = args.shift
  major   = args.shift
  minor   = args.shift
  extra   = args.shift

  return help unless command
  dispatch(command, major, minor, extra)
end

.helpObject

Prints all the command available



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/shop/command.rb', line 455

def help
  text = %{
    Shop v.#{Shop::VERSION} -------------------------------

      shop new <directory>                   Download the framework
      shop install                           Install Prestashop

      shop init                              Creates a Shop config file

      shop module <name>                     Creates a new module
      shop module template <name> <hook>     Creates a template for a given module / hook
      shop module css <name>                 Creates an override for a module css

      shop controller <controller-name>      Creates a new page with a controller and
                                             assets needed

      shop override controller <name>        Creates an override for a controller
      shop override controller <name> admin  Creates an override for an admin controller
      shop override class <name>             Creates an override for a class

      shop clean [cache]                     Cleans the css and js caches
      shop clean class                       Cleans the class index

      shop jshint                            Run jshint on the theme files
      shop jshint modules                    Run jshint on the theme and modules files
      shop jshint hard                       Run jshint on every files

      shop makefile                          Creates a Makefile or add tasks to an existing one

      shop edit                              Open the configuration file in your $EDITOR
      shop help                              Output this help text
      shop version                           Output the version of Shop you are using

    See the complete documentation at:
    https://github.com/romainberger/shop

  }.gsub(/^ {10}/, '')
  puts text
end

.init(name) ⇒ Object

Init the project



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/shop/command.rb', line 169

def init(name)
  if name.nil?
    puts "#{red("Error:")} Please specify the name of the theme"
    return puts "    $ shop init <theme-name>"
  end

  puts "      #{green("create")} .shop"
  File.open('.shop', 'w') do |f|
    f.write(name)
  end

  done
end

.init?Boolean

Check if the Shop project has been initialized

Returns a boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/shop/command.rb', line 186

def init?
  File.exists?('.shop')
end

.installObject

Runs the Prestashop install CLI but with a nice prompt

See doc.prestashop.com/display/PS15/Installing+PrestaShop+using+the+command+line



107
108
109
110
111
112
113
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
164
165
166
# File 'lib/shop/command.rb', line 107

def install
  if installed?
    puts "PrestaShop appears to be already installed"
    exit
  end

  puts "Please answer the following: "

  # Default values
  config = ShopConfig.new
  db_server = config.get('install', 'db_server', 'localhost')
  db_user = config.get('install', 'db_user', 'root')
  db_password = config.get('install', 'db_password')
  country = config.get('install', 'country', 'fr')
  firstname = config.get('install', 'firstname', false)
  lastname = config.get('install', 'lastname', false)
  password = config.get('install', 'password', '0123456789')
  email = config.get('install', 'email', false)

  entry = Hash.new

  entry[:domain]      = ask('Domain: ')
  entry[:db_name]     = ask('Database name: ')
  entry[:db_server]   = ask('Database server: ') { |q| q.default = db_server }
  entry[:db_user]     = ask('Database user: ') { |q| q.default = db_user }
  entry[:db_password] = ask('Database password: ') { |q| q.default = db_password }
  entry[:country]     = ask('Country: ') { |q| q.default = country }
  entry[:firstname]   = ask('Firstname: ') { |q| q.default = firstname if firstname }
  entry[:lastname]    = ask('Lastname: ') { |q| q.default = lastname if lastname }
  entry[:password]    = ask('Password: ') { |q| q.default = password }
  entry[:email]       = ask('Email: ') { |q| q.default = email if email }

  command = "php install-dev/index_cli.php "
  command << "--domain=#{entry[:domain]} "
  command << "--db_name=#{entry[:db_name]} "
  command << "--db_server=#{entry[:db_server]} "
  command << "--db_user=#{entry[:db_user]} "
  command << "--db_password=#{entry[:db_password]} "
  command << "--country=#{entry[:country]} "
  command << "--firstname=#{entry[:firstname]} "
  command << "--lastname=#{entry[:lastname]} "
  command << "--password=#{entry[:password]} "
  command << "--email=#{entry[:email]} "
  command << "--newsletter=0"

  # Creates the database if it doesn't exist
  client = Mysql2::Client.new(
    :host     => entry[:db_server],
    :username => entry[:db_user],
    :password => entry[:db_password]
  )
  puts "Creating the database..."
  client.query("CREATE DATABASE IF NOT EXISTS `#{entry[:db_name]}`")
  client.close()

  # run the php script
  puts "Installing Prestashop please wait..."
  system command
  done
end

.installed?Boolean

Returns if the framework is already installed

Returns:

  • (Boolean)


100
101
102
# File 'lib/shop/command.rb', line 100

def installed?
  File.exists?('config/settings.inc.php')
end

.jshint(major) ⇒ Object

Run jshint



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/shop/command.rb', line 403

def jshint(major)
  theme
  files = Dir["themes/#{theme}/js/**/*.js"]

  if major == 'modules' || major == 'hard'
    modules = Dir["modules/**/*.js"]
    files = files + modules
  end
  if major == 'hard'
    prestashop = Dir["js/**/*.js"]
    files = files + prestashop
  end

  files.each do |f|
    system "jshint #{f}"
  end
end

.makefileObject

Create a Makefile or add some tasks to an existing one



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/shop/command.rb', line 422

def makefile
  theme
  datas = { 'theme' => theme}
  content = template.template('Makefile', datas)
  if File.exists?("Makefile")
    File.open("Makefile", "a") do |f|
      f.write(content)
    end
  else
    File.open("Makefile", "w") do |f|
      puts "      #{green("create")} Makefile"
      f.write(content)
    end
  end

  done
end

.newProject(path) ⇒ Object

Download the framework in the current dir or a creates a dir if an argument is given



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
# File 'lib/shop/command.rb', line 66

def newProject(path)
  unless path.nil?
    FileUtils.mkpath(path)
    puts "Creating new PrestaShop project in ./#{path}"
  else
    path = "./"
    puts "Creating new PrestaShop project"
  end

  print "Downloading the framework... "
  url = 'https://github.com/PrestaShop/PrestaShop/archive/master.zip'
  open("master.zip", "wb") do |f|
    f << open(url).read
  end
  done

  # @todo unzip with a ruby way to avoid platform incompatibilities
  print "Unzip... "
  system "unzip -q master.zip"
  done

  print "Copying... "
  FileUtils.cp_r(Dir["PrestaShop-master/*"], path)
  done

  print "Cleaning... "
  # remove useless files
  File.delete("master.zip")
  FileUtils.rm_rf("PrestaShop-master")

  done
end

.override(major, minor, extra) ⇒ Object

Creates an override for controllers and classes



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
296
297
298
299
300
301
302
# File 'lib/shop/command.rb', line 267

def override(major, minor, extra)
  name = minor.capitalize

  if major == 'controller'
    # controller
    if !extra.nil? && extra == 'admin'
      side = 'admin'
    else
      side = 'front'
    end

    name = "#{name}Controller"
    filename = "#{name}.php"
    path = "override/controllers/#{side}/#{filename}"

  elsif major == 'class'
    # class
    filename = "#{name}.php"
    path = "override/classes/#{filename}"
  end

  content = "<?php\n\nclass #{name} extends #{name}Core {\n\n}\n"

  if !File.directory?('override')
    return puts "#{red("Error:")} You need to be at the root of your Prestashop site"
  end

  puts "      #{green("create")} #{path}"
  File.open(path, 'w') do |f|
    f.write(content)
  end

  clean("class")

  done
end

.psVersionObject

Returns the version of Prestashop installed



24
25
26
27
28
29
30
31
32
# File 'lib/shop/command.rb', line 24

def psVersion
  if !installed?
    puts "PrestaShop does not seem to be installed"
    exit
  end

  line = File.read("config/settings.inc.php")[/^\s*define\('_PS_VERSION_',\s*.*\)/]
  line.match(/.*define\('_PS_VERSION_',\s*\s*['"](.*)['"]\)/)[1]
end

.shopModule(major, minor, extra) ⇒ Object

Creates a module or a module template prefixed with shop for obvious reasons

The project needs to be initialized



194
195
196
197
198
199
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/shop/command.rb', line 194

def shopModule(major, minor, extra)
  if major == 'template'
    theme
    path = "themes/#{theme}/modules"
    FileUtils.mkpath(path) unless File.directory?(path)

    path = "#{path}/#{minor}"
    FileUtils.mkpath(path) unless File.directory?(path)

    filepath = "#{path}/#{extra}"
    filepath = "#{filepath}.tpl" unless filepath.match(/\.tpl$/)
    if File.exists?(filepath)
      puts "#{red("Error:")} File already exists"
      exit
    else
      puts "      #{green("create")} #{filepath}"
      File.open(filepath, "w")
    end
  elsif major == 'css'
    theme
    # css
    path = "themes/#{theme}/css/modules/#{minor}"
    FileUtils.mkpath(path) unless File.directory?(path)

    filepath = "#{path}/#{minor}"
    filepath = "#{filepath}.css" unless filepath.match(/\.css$/)

    if File.exists?(filepath)
      puts "#{red("Error:")} File already exists"
      exit
    elsif
      puts "      #{green("create")} #{filepath}"
      File.open(filepath, "w")
    end
  else
    # create a module
    if File.directory?("modules/#{major}")
      return puts "Module #{major} already exists"
    else
      puts "      #{green("create")} modules/#{major}"
      FileUtils.mkpath("modules/#{major}")
      config = ShopConfig.new
      values = {
        "name_capitalize" => major.capitalize,
        "name" => major,
        "author" => config.get('module', 'author'),
        "tab" => config.get('module', 'tab')
      }
      content = template.template("module.php", values)
      puts "      #{green("create")} modules/#{major}/#{major}.php"
      File.open("modules/#{major}/#{major}.php", 'w') do |f|
        f.write(content)
      end

      # copy the logos
       = config.get('module', 'logo')
      if .length != 0
        if File.exists?("#{}.png")
          puts "      #{green("create")} modules/#{major}/logo.png"
          FileUtils.cp("#{}.png", "modules/#{major}/logo.png")
        end
        if File.exists?("#{}.gif")
          puts "      #{green("create")} modules/#{major}/logo.gif"
          FileUtils.cp("#{}.gif", "modules/#{major}/logo.gif")
        end
      end
    end
  end

  done
end

.templateObject



8
9
10
# File 'lib/shop/command.rb', line 8

def template
  Template.new
end

.themeObject

Returns the theme name



13
14
15
16
17
18
19
20
21
# File 'lib/shop/command.rb', line 13

def theme
  if init?
    return File.read('.shop')
  else
    puts "#{red("Error")}: Project not initialized."
    puts "Please run #{cyan("shop init <theme-name>")}"
    exit
  end
end

.versionObject

Returns the version of Shop



450
451
452
# File 'lib/shop/command.rb', line 450

def version
  puts "Shop #{Shop::VERSION}"
end