shop 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YzFkYzk1N2M1YzI3Nzg5YjU4NmQxOTQ2M2RlMWM5N2Y3MmQ2ZGExNQ==
4
+ NWI2MTY0N2JhNDllMDVmNGRhMWRjNzRhM2UxNmY0MjFmNDY0MDI0ZQ==
5
5
  data.tar.gz: !binary |-
6
- YTY3ZTcwNWM2ZmYzNmM2YzA5ZWRjOTQ0ZWQ1ZjI4MjNiOTU0MWE5OA==
6
+ ZThlZmUzYTQzYTM2NTA3ZGQ3YzYxYzZkNWNjZDRhNzdhOTJiMTc2Zg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MjdlMjRlYjM4MjFhN2QxMGI2NjRkZTliMzdmYjRmMTQwZDk0NzUxNWQ2Yzky
10
- N2QzOWQ3NGU3ZDE1OTllM2ZlMTU1MDY0OWZiZjIzOWZhMjgyOGIzYTRmNzQ1
11
- NTJjZjEzYTIxZjdmNGU3NTczYzgxZTMzMjI5MzYyYTJjMmQ2Nzc=
9
+ YjBkNWNkYTY3ZWNkNzM1ZWUyYjM2ZTA5YmY1YjE3MmRjYzUxNzJkYWNiYTYw
10
+ OTBiMzhmNGI2ZjFhNGIzYTcwNmIxYmNlNTdlMzJiMzE0NzVkMGMwNWExMDNk
11
+ YjEzOGE3MGM4MDNlZTEyZmRkMTRiNjZhNjcyYmM0OWJhNGVhMzI=
12
12
  data.tar.gz: !binary |-
13
- Y2YxYjZmNmUxZjgzZTYxMjlhNTgzNTdkZGY3MDYxMmJhNDUwZTFjYjQ4Y2Zh
14
- MDI1MzE5ZDMwNjQyYjFjYTc4MzUyMjJlNjA1ZDUxZTE4NWY0N2M1NWQ3MWUz
15
- N2Y2YWY4ZjQ0MzAzNGM1ODM1Yjc1Nzk0MmMxMzcxZmFlOWMwNmY=
13
+ NjQwYjU4YzkyZWQxNDE5NDkyZDU2N2JhMGQzM2Y4NjFjZDM3OWNhOTZiYjM0
14
+ YWY0MWIwYjJmNTcxNjQ4NzI5ZjliMjQ1YTljMTBjY2I0MTU0NGE0MjI5Mjll
15
+ YTU0YWYzMmQyMWRkZTA0MzEyZDQ4M2QyYzA0MjY2NzE3ZDUwOGQ=
data/README.md CHANGED
@@ -1,62 +1,146 @@
1
1
  # Shop
2
2
 
3
- CLI for easier Prestashop development. Actually it's just for the lazy people.
3
+ CLI for easier [PrestaShop](http://www.prestashop.com/en/) development. Actually it's just for the lazy people.
4
4
 
5
- ## Install
5
+ I always use Rakefiles and Makefiles for lots of tasks. With this gem you can use these tasks without having to create any *akefiles.
6
6
 
7
- $ gem install shop
7
+ ## Installation
8
+
9
+ gem install shop
10
+
11
+ Shop has been developed tested on OSX. I have no idea if this works on other platforms.
8
12
 
9
13
  ## Usage
10
14
 
15
+ ### New project
16
+
17
+ Creates a new Prestashop project with:
18
+
19
+ shop new <directory>
20
+
21
+ # example:
22
+ shop new
23
+ # Download the framework in the current directory
24
+
25
+ shop new new-store
26
+ # Download the framework in a new directory `new-store`
27
+
28
+ Download the latest stable version of PrestaShop in the current directory or in a subdirectory if you provide the name as argument.
29
+
30
+ **Note**: Shop can work with an existing PrestaShop project, all you need is to initialize the Shop project (see [Initialization](https://github.com/romainberger/shop/#initialization) below)
31
+
32
+ ### Installation
33
+
34
+ Since 1.5.4 [PrestaShop provides a CLI](http://doc.prestashop.com/display/PS15/Installing+PrestaShop+using+the+command+line) to install your PrestaShop. You can try to remember every arguments, or just use the Shop task and stay relatively sane (no offense to the PrestaShop developers but... come on).
35
+
36
+ # php install/cli.php --name=Romain --lol=whyamitypingallthis
37
+ # nope
38
+ shop install
39
+
40
+ **Note**: the install task actually runs the PrestaShop PHP CLI. It just makes you happier with a nice prompt.
41
+
11
42
  ### Initialization
12
43
 
13
- For some of the task, Shop will need to know the name of the theme you're using. To do that you need to init the project with:
44
+ For some tasks, Shop will need to know the name of the theme you're using. To do that you need to init the project with:
45
+
46
+ shop init <theme-name>
14
47
 
15
- $ shop init <theme-name>
48
+ # example:
49
+ shop init my-theme
16
50
 
17
51
  ### Overrides
18
52
 
19
53
  Creates overrides files for controllers and classes.
20
54
 
21
- Usage: `$ shop override controller|class Name [admin]
55
+ shop override <controller|class> <name> [admin]
22
56
 
23
- $ shop override controller|class Name
24
- # Generates an override for a controller or a class
25
- # example:
26
- $ shop override controller Product
27
- # or
28
- $ shop override class Product
57
+ # examples:
58
+
59
+ # generate an override for the Product controller
60
+ shop override controller Product
29
61
 
30
62
  # by default the controller will be placed in override/controllers/admin. To create an override in
31
63
  # override/controllers/admin add the `admin` argument:
32
- $ shop override controller AdminProducts
64
+ shop override controller AdminProducts admin
65
+
66
+ # generate an override for the Product class
67
+ shop override class Product
33
68
 
34
69
  ### Modules
35
70
 
36
- Create a module or modules' templates files.
71
+ **The modules related tasks will need the Shop project to be [initialized](https://github.com/romainberger/shop)**
72
+
73
+ Creates a new module (with simple module boilerplate):
74
+
75
+ shop module <name>
76
+
77
+ # example:
78
+ shop module dinozaure
79
+
80
+ Creates a module template:
81
+
82
+ shop module template <module-name> <hook-name>
37
83
 
38
- $ shop module <name>
39
- # Generates a boilerplate for a new module
40
84
  # example:
41
- $ shop module dinozaure
85
+ shop module template blockcategories blockcategories
86
+
87
+ Creates a module css file in your theme:
88
+
89
+ shop module css <module-name>
42
90
 
43
- $ shop module template <module-name> <hook-name>
44
91
  # example:
45
- $ shop module blockcategories blockcategories
92
+ shop module css blockcategories
46
93
 
47
94
  ### Cache
48
95
 
49
- $ shop clean cache
50
- # cleans the cache and compile and stuffs
96
+ Cleans the css and js caches.
97
+
98
+ shop clean cache
99
+ # cleans the css and js caches
100
+
101
+ When you create a new override for a controller or a class, the class index needs to be rewritten. You can call this task
51
102
 
52
- $ shop clean class
103
+ shop clean class
53
104
  # clean the class index - automatically done when generating a new controller or class
54
105
 
106
+ **Note**: The class index is automatically regenerated when you create an override with Shop. You probably won't need to run this command.
107
+
108
+ ### Jshint
109
+
110
+ Run jshint on the theme's files.
111
+
112
+ shop jshint
113
+
114
+ To run jshint on the theme's files and the modules' files, run it with the `modules` argument:
115
+
116
+ shop jshint modules
117
+
118
+ To run jshint on **every** files run it with the `hard` argument (prepare yourself for a crapload of errors):
119
+
120
+ shop jshint hard
121
+
122
+ ### Makefile
123
+
124
+ Okay I kinda lied. I still use a Makefile, but only for production servers, as they rarely run gems and I don't necessarily want to install a tool for a limited use (Shop is primarily built for development purpose). But some of these tasks can be usefull on production servers. Shop can generate a Makefile file that will allow you to run some tasks.
125
+
126
+ **The Makefile task will need the Shop project to be [initialized](https://github.com/romainberger/shop)**
127
+
128
+ If you already use a Makefile, the tasks will be added to it. If you don't, it will create a Makefile.
129
+
130
+ shop makefile
131
+
132
+ You can then use these tasks. Their effect are similar to the Shop tasks.
133
+
134
+ make clean-cache
135
+
136
+ make clean-class
137
+
138
+
55
139
  ### Help
56
140
 
57
141
  You can get a list of the available commands with the help:
58
142
 
59
- $ shop help
143
+ shop help
60
144
 
61
145
  # Who
62
146
 
data/lib/shop/command.rb CHANGED
@@ -4,30 +4,133 @@ module Shop
4
4
  class Command
5
5
  class << self
6
6
 
7
+ # Returns the theme name
8
+ def theme
9
+ if init?
10
+ return File.read('.shop')
11
+ else
12
+ puts "Project not initialized. Please run `shop init <theme-name>`"
13
+ exit
14
+ end
15
+ end
16
+
17
+ # Returns the path to the templates directory
18
+ def template_path
19
+ path = File.expand_path File.dirname(__FILE__)
20
+ "#{path}/../../templates"
21
+ end
22
+
7
23
  def execute(*args)
8
24
  command = args.shift
9
25
  major = args.shift
10
26
  minor = args.shift
11
- extra = args.empty? ? nil : args.join(' ')
27
+ extra = args.shift
12
28
 
13
29
  return help unless command
14
30
  dispatch(command, major, minor, extra)
15
31
  end
16
32
 
17
33
  def dispatch(command, major, minor, extra)
34
+ return newProject(major) if command == 'new'
18
35
  return init(major) if command == 'init'
36
+ return install if command == 'install'
19
37
  return shopModule(major, minor, extra) if command == 'module'
20
38
  return override(major, minor, extra) if command == 'override'
21
39
  return clean(major) if command == 'clean'
40
+ return jshint(major) if command == 'jshint'
41
+ return makefile if command == 'makefile'
22
42
  return version if command == "-v"
23
43
  return version if command == "--version"
24
44
  return help if command == 'help'
45
+
46
+ puts "\nCommand not found"
47
+ return help
48
+ end
49
+
50
+ # Download the framework in the current dir
51
+ # or a creates a dir if an argument is given
52
+ def newProject(path)
53
+ unless path.nil?
54
+ FileUtils.mkpath(path)
55
+ else
56
+ path = "./"
57
+ end
58
+
59
+ puts "Please wait..."
60
+ print "Downloading the framework... "
61
+ url = 'https://github.com/PrestaShop/PrestaShop/archive/master.zip'
62
+ open("master.zip", "wb") do |f|
63
+ f << open(url).read
64
+ end
65
+ done
66
+
67
+ # @todo unzip with a ruby way to avoid platform incompatibilities
68
+ print "Unzip... "
69
+ system "unzip -q master.zip"
70
+ done
71
+
72
+ print "Copying... "
73
+ FileUtils.cp_r(Dir["PrestaShop-master/*"], path)
74
+ done
75
+
76
+ print "Cleaning... "
77
+ # remove useless files
78
+ File.delete("master.zip")
79
+ FileUtils.rm_rf("PrestaShop-master")
80
+
81
+ done
82
+ end
83
+
84
+ # Runs the Prestashop install CLI but with a nice prompt
85
+ #
86
+ # See http://doc.prestashop.com/display/PS15/Installing+PrestaShop+using+the+command+line
87
+ def install
88
+ # check if the framework is already installed
89
+ if !File.exists?('config/settings.inc.php')
90
+ puts "PrestaShop appears to be already installed"
91
+ exit
92
+ end
93
+
94
+ puts "Please answer the following: "
95
+
96
+ entry = Hash.new
97
+
98
+ entry[:domain] = ask('Domain: ')
99
+ entry[:db_name] = ask('Database name: ')
100
+ entry[:db_server] = ask('Database server: ') { |q| q.default = 'localhost' }
101
+ entry[:db_user] = ask('Database user: ') { |q| q.default = 'root' }
102
+ entry[:db_password] = ask('Database password: ') { |q| q.default = '' }
103
+ entry[:country] = ask('Country: ') { |q| q.default = 'fr' }
104
+ entry[:firstname] = ask('Firstname: ')
105
+ entry[:lastname] = ask('Lastname: ')
106
+ entry[:password] = ask('Password: ') { |q| q.default = '0123456789' }
107
+ entry[:email] = ask('Email: ')
108
+ entry[:newsletter] = 0 # the PS default is to 1, but nobody wants spam
109
+
110
+ command = "php install-dev/index_cli.php "
111
+ command << "--domain=#{entry[:domain]} "
112
+ command << "--db_name=#{entry[:db_name]} "
113
+ command << "--db_server=#{entry[:db_server]} "
114
+ command << "--db_user=#{entry[:db_user]} "
115
+ command << "--db_password=#{entry[:db_password]} "
116
+ command << "--country=#{entry[:country]} "
117
+ command << "--firstname=#{entry[:firstname]} "
118
+ command << "--lastname=#{entry[:lastname]} "
119
+ command << "--password=#{entry[:password]} "
120
+ command << "--email=#{entry[:email]} "
121
+ command << "--newsletter=#{entry[:newsletter]} "
122
+
123
+ # run the php script
124
+ puts "Installing Prestashop please wait... "
125
+ system command
126
+ done
25
127
  end
26
128
 
27
129
  # Init the project
28
130
  def init(name)
29
131
  if name.nil?
30
- return puts "Error: Please specify the name of the theme"
132
+ puts "Error: Please specify the name of the theme"
133
+ return puts " $ shop init <theme-name>"
31
134
  end
32
135
 
33
136
  File.open('.shop', 'w') do |f|
@@ -37,12 +140,46 @@ module Shop
37
140
  done
38
141
  end
39
142
 
143
+ # Check if the Shop project has been initialized
144
+ #
145
+ # Returns a boolean
146
+ def init?
147
+ File.exists?('.shop')
148
+ end
149
+
40
150
  # Creates a module or a module template
41
151
  # prefixed with shop for obvious reasons
152
+ #
153
+ # The project needs to be initialized
42
154
  def shopModule(major, minor, extra)
155
+ theme
43
156
  if major == 'template'
44
- # template
45
- puts 'create module template'
157
+ path = "themes/#{theme}/modules"
158
+ FileUtils.mkpath(path) unless File.directory?(path)
159
+
160
+ path = "#{path}/#{minor}"
161
+ FileUtils.mkpath(path) unless File.directory?(path)
162
+
163
+ filepath = "#{path}/#{extra}.tpl"
164
+ if File.exists?(filepath)
165
+ puts "File already exists"
166
+ exit
167
+ else
168
+ File.open(filepath, "w") do; end
169
+ end
170
+ elsif major == 'css'
171
+ # css
172
+ path = "themes/#{theme}/css/modules/#{minor}"
173
+ FileUtils.mkpath(path) unless File.directory?(path)
174
+
175
+ filepath = "#{path}/#{minor}.css"
176
+
177
+ if File.exists?(filepath)
178
+ puts "File already exists"
179
+ exit
180
+ elsif
181
+ File.open(filepath, "w") do; end
182
+ end
46
183
  else
47
184
  # create a module
48
185
  if File.directory?("modules/#{major}")
@@ -51,7 +188,9 @@ module Shop
51
188
  FileUtils.mkpath("modules/#{major}")
52
189
  File.open("modules/#{major}/#{major}.php", 'w') do |f|
53
190
  name = major.capitalize
54
- content = "<?php\n\nclass #{name} extends Module {\n\n}\n"
191
+ content = File.read("#{template_path}/module.php")
192
+ content = content.gsub("{{name_capitalize}}", "#{name}")
193
+ content = content.gsub("{{name}}", "#{major}")
55
194
  f.write(content)
56
195
  end
57
196
  end
@@ -98,24 +237,106 @@ module Shop
98
237
  # Clean cache or class index
99
238
  def clean(major)
100
239
  if major == 'cache'
101
- # cache
102
- puts 'clean cache'
103
- elsif major == 'index'
104
- # index
105
- puts 'clean index'
240
+ theme
241
+ files = Dir["themes/#{theme}/cache/*.css"]
242
+ files = files + Dir["themes/#{theme}/cache/*.js"]
243
+
244
+ files.each do |f|
245
+ File.delete(f)
246
+ end
247
+ elsif major == 'class'
248
+ print "Cleaning class index... "
249
+ index = "cache/class_index.php"
250
+ if File.exists?(index)
251
+ File.delete(index)
252
+ end
253
+ File.open(index, "w") do; end
254
+ # the file needs to be chmod'ed to 666
255
+ File.chmod(0666, index)
256
+ end
257
+
258
+ done
259
+ end
260
+
261
+
262
+ # Run jshint
263
+ def jshint(major)
264
+ theme
265
+ files = Dir["themes/#{theme}/js/**/*.js"]
266
+
267
+ if major == 'modules' || major == 'hard'
268
+ modules = Dir["modules/**/*.js"]
269
+ files = files + modules
270
+ end
271
+ if major == 'hard'
272
+ prestashop = Dir["js/**/*.js"]
273
+ files = files + prestashop
274
+ end
275
+
276
+ files.each do |f|
277
+ system "jshint #{f}"
106
278
  end
107
279
  end
108
280
 
281
+ # Create a Makefile or add some tasks to an existing one
282
+ def makefile
283
+ theme
284
+ content = File.read("#{template_path}/Makefile")
285
+ content = content.gsub("{{theme}}", "#{theme}")
286
+ if File.exists?("Makefile")
287
+ File.open("Makefile", "a") do |f|
288
+ f.write(content)
289
+ end
290
+ else
291
+ File.open("Makefile", "w") do |f|
292
+ f.write(content)
293
+ end
294
+ end
295
+
296
+ done
297
+ end
298
+
109
299
  def done
110
300
  puts "✔ Done"
111
301
  end
112
302
 
303
+ # Returns the version of Shop
113
304
  def version
114
305
  puts "Shop #{Shop::VERSION}"
115
306
  end
116
307
 
308
+ # Prints all the command available
117
309
  def help
118
- puts "Output help"
310
+ text = %{
311
+ Shop v.#{Shop::VERSION} -------------------------------
312
+
313
+ shop new <directory> Download the framework
314
+ shop install Install Prestashop
315
+
316
+ shop init Creates a Shop config file
317
+
318
+ shop module <name> Creates a new module
319
+ shop module template <name> <hook> Creates a template for a given module / hook
320
+ shop module css <name> Creates an override for a module css
321
+
322
+ shop override controller <name> Creates an override for a controller
323
+ shop override controller <name> admin Creates an override for an admin controller
324
+ shop override class <name> Creates an override for a class
325
+
326
+ shop clean cache Cleans the css and js caches
327
+ shop clean class Cleans the class index
328
+
329
+ shop jshint Run jshint on the theme files
330
+ shop jshint modules Run jshint on the theme and modules files
331
+ shop jshint hard Run jshint on every files
332
+
333
+ shop makefile Creates a Makefile or add tasks to an existing one
334
+
335
+ See the complete documentation at:
336
+ https://github.com/romainberger/shop
337
+
338
+ }.gsub(/^ {10}/, '')
339
+ puts text
119
340
  end
120
341
  end
121
342
  end
data/lib/shop.rb CHANGED
@@ -3,10 +3,10 @@
3
3
  $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
4
4
 
5
5
  require 'fileutils'
6
+ require 'open-uri'
6
7
 
7
8
  require 'shop/command'
8
- require 'shop/color'
9
9
 
10
10
  module Shop
11
- VERSION = '0.1.0'
11
+ VERSION = '0.1.1'
12
12
  end
data/shop.gemspec CHANGED
@@ -13,13 +13,13 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'shop'
16
- s.version = '0.1.0'
16
+ s.version = '0.1.1'
17
17
  s.date = '2013-03-12'
18
18
  s.rubyforge_project = 'shop'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
21
21
  ## as you like.
22
- s.summary = "Command line interface for easy Prestashop development"
22
+ s.summary = "CLI for easy Prestashop development"
23
23
  s.description = "Because I'm too lazy to create files and type code"
24
24
 
25
25
  ## List the primary authors. If there are a bunch of authors, it's probably
@@ -49,6 +49,7 @@ Gem::Specification.new do |s|
49
49
 
50
50
  ## List your runtime dependencies here. Runtime dependencies are those
51
51
  ## that are needed for an end user to actually USE your code.
52
+ s.add_dependency('highline', "~> 1.6.19")
52
53
 
53
54
  ## List your development dependencies here. Development dependencies are
54
55
  ## those that are only needed during development
@@ -62,7 +63,8 @@ Gem::Specification.new do |s|
62
63
  shop.gemspec
63
64
  lib/shop.rb
64
65
  lib/shop/command.rb
65
- lib/shop/color.rb
66
+ templates/Makefile
67
+ templates/module.php
66
68
  ]
67
69
  # = MANIFEST =
68
70
 
@@ -0,0 +1,14 @@
1
+ CHECK=✔ Done
2
+ HR=--------------------------------------------------
3
+
4
+ clean-class:
5
+ @sudo printf "Cleaning class index..."
6
+ @rm cache/class_index.php
7
+ @touch cache/class_index.php
8
+ @chmod 666 cache/class_index.php
9
+ @echo " ${CHECK}"
10
+
11
+ clean-cache:
12
+ @printf "Cleaning cache..."
13
+ -@rm themes/{{theme}}/cache/*.css themes/{{theme}}/cache/*.js 2>/dev/null || true
14
+ @echo " ${CHECK}"
@@ -0,0 +1,22 @@
1
+ <?php
2
+
3
+ class {{name_capitalize}} extends Module {
4
+
5
+ public function __construct() {
6
+ $this->name = '{{name}}';
7
+ $this->tab = '';
8
+ $this->version = 0.1;
9
+ $this->author = '';
10
+
11
+ parent::__construct();
12
+
13
+ $this->displayName = $this->l('');
14
+ $this->description = $this->l('');
15
+
16
+ }
17
+
18
+ public function install() {
19
+ parent::install();
20
+ }
21
+
22
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain Berger
@@ -9,7 +9,21 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2013-03-12 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: highline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.19
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.19
13
27
  description: Because I'm too lazy to create files and type code
14
28
  email: romain@romainberger.com
15
29
  executables:
@@ -23,7 +37,8 @@ files:
23
37
  - shop.gemspec
24
38
  - lib/shop.rb
25
39
  - lib/shop/command.rb
26
- - lib/shop/color.rb
40
+ - templates/Makefile
41
+ - templates/module.php
27
42
  - README.md
28
43
  - LICENSE.md
29
44
  homepage: https://github.com/romainberger/shop
@@ -50,5 +65,5 @@ rubyforge_project: shop
50
65
  rubygems_version: 2.0.3
51
66
  signing_key:
52
67
  specification_version: 2
53
- summary: Command line interface for easy Prestashop development
68
+ summary: CLI for easy Prestashop development
54
69
  test_files: []
data/lib/shop/color.rb DELETED
@@ -1,54 +0,0 @@
1
- # Shamelessly stolen from https://github.com/holman/boom
2
-
3
- module Shop
4
- # Color collects some methods for colorizing terminal output.
5
- module Color
6
- extend self
7
-
8
- CODES = {
9
- :reset => "\e[0m",
10
-
11
- :cyan => "\e[36m",
12
- :magenta => "\e[35m",
13
- :red => "\e[31m",
14
- :yellow => "\e[33m"
15
- }
16
-
17
- # Tries to enable Windows support if on that platform.
18
- #
19
- # Returns nothing.
20
- def self.included(other)
21
- if RUBY_PLATFORM =~ /win32/ || RUBY_PLATFORM =~ /mingw32/
22
- require 'Win32/Console/ANSI'
23
- end
24
- rescue LoadError
25
- # Oh well, we tried.
26
- end
27
-
28
- # Wraps the given string in ANSI color codes
29
- #
30
- # string - The String to wrap.
31
- # color_code - The String representing he ANSI color code
32
- #
33
- # Examples
34
- #
35
- # colorize("Boom!", :magenta)
36
- # # => "\e[35mBoom!\e[0m"
37
- #
38
- # Returns the wrapped String unless the the platform is windows and
39
- # does not have Win32::Console, in which case, returns the String.
40
- def colorize(string, color_code)
41
- if !defined?(Win32::Console) && !!(RUBY_PLATFORM =~ /win32/ || RUBY_PLATFORM =~ /mingw32/)
42
- # looks like this person doesn't have Win32::Console and is on windows
43
- # just return the uncolorized string
44
- return string
45
- end
46
- "#{CODES[color_code] || color_code}#{string}#{CODES[:reset]}"
47
- end
48
-
49
- # Set up shortcut methods to all the codes define in CODES.
50
- self.class_eval(CODES.keys.reject {|color| color == :reset }.map do |color|
51
- "def #{color}(string); colorize(string, :#{color}); end"
52
- end.join("\n"))
53
- end
54
- end