more 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -67,27 +67,25 @@ If you prefix a file with an underscore, it is considered to be a partial, and w
67
67
 
68
68
  The example above will result in a single CSS file in `public/stylesheets/clients/screen.css`.
69
69
 
70
+ Any `.css` file placed in `app/stylesheets` will be copied into `public/stylesheets` without being parsed through LESS.
71
+
70
72
 
71
73
  Configuration
72
74
  =============
73
75
 
74
- To set the source path (the location of your LESS files):
76
+ Source path: the location of your LESS files (default: app/stylesheets)
75
77
 
76
- Less::More.source_path = "/path/to/less/files"
78
+ Less::More.source_path = "public/stylesheets/less"
77
79
 
78
- You can also set the destination path. Be careful with the formatting here, since this is in fact a route, and not a regular path.
80
+ Destination Path: where the css goes (public/destination_path) (default: stylesheets)
79
81
 
80
82
  Less::More.destination_path = "css"
81
83
 
82
- More can compress your files by removing extra line breaks. This is enabled by default in the `production` environment. To change this setting, set:
83
-
84
- Less::More.compression = true
84
+ More can compress your files by removing extra line breaks (default: true)
85
85
 
86
- If you're on a read-only file system, you can turn page caching off and use Cache-Control headers. This option is on by default on Heroku (see separate section).
86
+ Less::More.compression = false
87
87
 
88
- Less::More.page_cache = false
89
-
90
- More inserts headers in the generated CSS files, letting people know that the file is in fact generated and shouldn't be edited directly. This is by default only enabled in development mode. You can disable this behavior if you want to.
88
+ More inserts headers in the generated CSS files, letting people know that the file is in fact generated and shouldn't be edited directly. (default: true)
91
89
 
92
90
  Less::More.header = false
93
91
 
@@ -95,12 +93,6 @@ To configure More for a specific environment, add configuration options into the
95
93
 
96
94
  If you wish to apply the configuration to all environments, place them in `config/environment.rb`.
97
95
 
98
- Heroku
99
- ======
100
-
101
- The plugin works out-of-the-box on Heroku.
102
-
103
- Heroku has a read-only file system, which means caching the generated CSS with page caching is not an option. Heroku supports caching with Varnish, though, which the plugin will leverage by setting Cache-Control headers so that generated CSS is cached for one month.
104
96
 
105
97
  Tasks
106
98
  =====
@@ -109,7 +101,7 @@ More provides a set of Rake tasks to help manage your CSS files.
109
101
 
110
102
  To parse all LESS files and save the resulting CSS files to the destination path, run:
111
103
 
112
- $ rake more:parse
104
+ $ rake more:generate
113
105
 
114
106
  To delete all generated CSS files, run:
115
107
 
@@ -118,13 +110,10 @@ To delete all generated CSS files, run:
118
110
  This task will not delete any CSS files from the destination path, that does not have a corresponding LESS file in the source path.
119
111
 
120
112
 
121
- Git
122
- ===
123
-
124
- If you are using git to version control your code and LESS for all your stylesheets, you can add this entry to your `.gitignore` file:
125
-
126
- public/stylesheets
113
+ Git / SVN
114
+ =========
127
115
 
116
+ Check in all the generated css(destination path), they are only generated in development
128
117
 
129
118
  Documentation
130
119
  =============
@@ -136,5 +125,6 @@ Contributors
136
125
  ============
137
126
  * August Lilleaas ([http://github.com/augustl](http://github.com/augustl))
138
127
  * Logan Raarup ([http://github.com/logandk](http://github.com/logandk))
128
+ * Michael Grosser ([http://github.com/grosser](http://github.com/grosser))
139
129
 
140
130
  LESS is maintained by Alexis Sellier [http://github.com/cloudhead](http://github.com/cloudhead)
@@ -0,0 +1,8 @@
1
+ class ActionController::Base
2
+ def process_with_less(*args)
3
+ Less::More.generate_all
4
+ process_without_less(*args)
5
+ end
6
+
7
+ alias_method_chain :process, :less
8
+ end
data/rails/init.rb CHANGED
@@ -1,13 +1 @@
1
- begin
2
- require 'less'
3
- rescue LoadError => e
4
- e.message << " (You may need to install the less gem)"
5
- raise e
6
- end
7
-
8
- require File.join(File.dirname(__FILE__), '..', 'lib', 'more')
9
-
10
- config.after_initialize {
11
- Less::More.clean
12
- Less::More.parse if Less::More.page_cache?
13
- }
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'controller_extension') if RAILS_ENV == 'development'
@@ -1,19 +1,16 @@
1
- require 'less'
2
- require File.join(File.dirname(__FILE__), '..', 'lib', 'more')
3
-
4
1
  namespace :more do
5
2
  desc "Generate CSS files from LESS files"
6
- task :parse => :environment do
7
- puts "Parsing files from #{Less::More.source_path}."
8
- Less::More.parse
3
+ task :generate => :environment do
4
+ puts "Generating css from less files in #{Less::More.source_path}."
5
+ Less::More.generate_all
9
6
  puts "Done."
10
7
 
11
8
  end
12
9
 
13
10
  desc "Remove generated CSS files"
14
11
  task :clean => :environment do
15
- puts "Deleting files.."
16
- Less::More.clean
12
+ puts "Deleting all generated css files in #{Less::More.destination_path}"
13
+ Less::More.remove_all_generated
17
14
  puts "Done."
18
15
  end
19
16
  end
data/test/more_test.rb CHANGED
@@ -2,81 +2,101 @@ require 'test_helper'
2
2
 
3
3
  class MoreTest < Test::Unit::TestCase
4
4
  def setup
5
- class << Less::More
6
- [:@compression, :@header, :@page_cache].each {|v| remove_instance_variable(v) if instance_variable_defined?(v) }
5
+ [:compression, :header, :destination_path, :source_path].each do |variable|
6
+ Less::More.send("#{variable}=", nil)
7
7
  end
8
8
  end
9
-
10
-
11
- def test_getting_config_from_current_environment_or_defaults_to_production
12
- Less::More::DEFAULTS["development"]["foo"] = 5
13
- Less::More::DEFAULTS["production"]["foo"] = 10
14
-
15
- Rails.expects(:env).returns("development")
16
- assert_equal 5, Less::More.get_cvar("foo")
17
-
18
- Rails.expects(:env).returns("production")
19
- assert_equal 10, Less::More.get_cvar("foo")
20
-
21
- Rails.expects(:env).returns("staging")
22
- assert_equal 10, Less::More.get_cvar("foo")
23
- end
24
-
25
- def test_cvar_gets_predesence_for_user_values
26
- Less::More::DEFAULTS["development"][:page_cache] = false
27
- assert_equal false, Less::More.page_cache?
28
-
29
- Less::More.page_cache = true
30
- assert_equal true, Less::More.page_cache?
31
- end
32
-
33
- def test_page_cache_off_on_heroku
34
- Less::More.page_cache = true
35
- Less::More::DEFAULTS["development"][:page_cache] = true
36
-
37
- # The party pooper
38
- Less::More.expects(:heroku?).returns(true)
39
-
40
- assert_equal false, Less::More.page_cache?
41
- end
42
-
43
- def test_compression
44
- Less::More.compression = true
45
- assert_equal Less::More.compression?, true
46
-
47
- Less::More.compression = false
48
- assert_equal Less::More.compression?, false
49
- end
50
-
51
- def test_source_path
52
- Less::More.source_path = "/path/to/flaf"
53
- assert_equal Pathname.new("/path/to/flaf"), Less::More.source_path
54
- end
55
-
56
- def test_exists
57
- Less::More.source_path = File.join(File.dirname(__FILE__), 'less_files')
58
-
59
- assert Less::More.exists?(["test"])
60
- assert Less::More.exists?(["short"])
61
- assert Less::More.exists?(["sub", "test2"])
62
-
63
- # Partials does not exist
64
- assert !Less::More.exists?(["_global"])
65
- assert !Less::More.exists?(["shared", "_form"])
66
- end
67
-
68
- def test_generate
69
- Less::More.source_path = File.join(File.dirname(__FILE__), 'less_files')
9
+
10
+ def prepare_for_generate
11
+ Less::More.source_path = 'less_files'
12
+ Less::More.destination_path = 'css'
13
+
14
+ `rm -rf #{css_path}`
15
+ `mkdir -p #{css_path}`
16
+ end
17
+
18
+ def css_path
19
+ "#{Rails.root}/public/css"
20
+ end
21
+
22
+ def teardown
23
+ `rm -rf #{css_path}`
24
+ end
25
+
26
+ def test_default_for_header
27
+ assert_equal Less::More.header, true
28
+ end
29
+
30
+ def test_header_can_be_overwritten
31
+ Less::More.header = false
32
+ assert_equal false, Less::More.header
33
+ end
34
+
35
+ def test_default_for_source_path
36
+ assert_equal 'app/stylesheets', Less::More.source_path
37
+ end
38
+
39
+ def test_source_path_can_be_overwritten
40
+ Less::More.source_path = 'xxx'
41
+ assert_equal 'xxx', Less::More.source_path
42
+ end
43
+
44
+ def test_default_for_destination_path
45
+ assert_equal 'stylesheets', Less::More.destination_path
46
+ end
47
+
48
+ def test_destination_path_can_be_overwritten
49
+ Less::More.destination_path = 'xxx'
50
+ assert_equal 'xxx', Less::More.destination_path
51
+ end
52
+
53
+ def test_default_for_compression
54
+ assert_equal nil, Less::More.compression
55
+ end
56
+
57
+ def test_compression_can_be_overwritten
70
58
  Less::More.compression = true
71
-
72
- assert_equal ".allforms { font-size: 110%; }body { color: #222222; }form { font-size: 110%; color: #ffffff;}", Less::More.generate(["test"])
73
- end
74
-
75
- def test_pathname_from_array
76
- Less::More.source_path = File.join(File.dirname(__FILE__), 'less_files')
77
-
78
- assert Less::More.pathname_from_array(["test"]).exist?
79
- assert Less::More.pathname_from_array(["short"]).exist?
80
- assert Less::More.pathname_from_array(["sub", "test2"]).exist?
81
- end
82
- end
59
+ assert_equal true, Less::More.compression
60
+ end
61
+
62
+ def test_generate_with_partials
63
+ prepare_for_generate
64
+ Less::More.generate_all
65
+ css = File.read(File.join(css_path, 'test.css'))
66
+ assert css.include?(".allforms { font-size: 110%; }
67
+ body { color: #222222; }
68
+ form {
69
+ font-size: 110%;
70
+ color: #ffffff;
71
+ }")
72
+ end
73
+
74
+ def test_generate_does_not_parse_css
75
+ prepare_for_generate
76
+ Less::More.generate_all
77
+ original_css = File.read(File.join(css_path, 'plain.css'))
78
+ assert_equal File.read(File.join(Rails.root,'less_files', 'plain.css')), original_css
79
+ end
80
+
81
+ def test_generate_uses_header_when_set
82
+ prepare_for_generate
83
+ Less::More.header = true
84
+ Less::More.generate_all
85
+ css = File.read(File.join(css_path, 'test.css'))
86
+ assert_match /^\/\*/, css # starts with comment -> header
87
+ end
88
+
89
+ def test_generate_uses_no_header_when_not_set
90
+ prepare_for_generate
91
+ Less::More.header = false
92
+ Less::More.generate_all
93
+ css = File.read(File.join(css_path, 'test.css'))
94
+ assert_match /^\.allforms/, css
95
+ end
96
+
97
+ def test_generate_does_not_generate_partials
98
+ prepare_for_generate
99
+ Less::More.generate_all
100
+ assert !File.exist?(File.join(css_path, '_global.css'))
101
+ end
102
+ end
data/test/test_helper.rb CHANGED
@@ -1,42 +1,14 @@
1
1
  require 'test/unit'
2
2
 
3
3
  require 'rubygems'
4
- require 'mocha'
5
4
  require 'active_support'
6
- require 'action_controller'
7
5
 
8
- ActionController::Base.session_store = nil
9
-
10
- module Rails
11
- extend self
12
-
13
- def env
14
- "development"
15
- end
16
-
17
- def root
18
- Pathname.new("/tmp")
6
+ class Rails
7
+ def self.root
8
+ File.expand_path(File.dirname(__FILE__))
19
9
  end
20
-
21
- def backtrace_cleaner
22
- ActiveSupport::BacktraceCleaner.new
23
- end
24
- end
25
-
26
- class ApplicationController < ActionController::Base
27
- end
28
-
29
- begin
30
- require 'less'
31
- rescue LoadError => e
32
- e.message << " (You may need to install the less gem)"
33
- raise e
34
10
  end
35
11
 
36
- require 'more'
12
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
37
13
 
38
- # Ugh.. shouldn't these be required for us?
39
- Dir.chdir("#{File.dirname(__FILE__)}/../") {
40
- require "config/routes"
41
- require 'app/controllers/less_cache_controller'
42
- }
14
+ require 'less/more'
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: more
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Alexis Sellier
7
+ - August Lilleaas
8
+ - Logan Raarup
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2009-10-08 00:00:00 +02:00
13
+ date: 2010-02-13 00:00:00 +01:00
13
14
  default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
@@ -35,12 +36,11 @@ files:
35
36
  - MIT-LICENSE
36
37
  - Rakefile
37
38
  - init.rb
38
- - lib/more.rb
39
+ - lib/controller_extension.rb
39
40
  - rails/init.rb
40
41
  - tasks/more_tasks.rake
41
- - test/test_helper.rb
42
- - test/less_cache_controller_test.rb
43
42
  - test/more_test.rb
43
+ - test/test_helper.rb
44
44
  has_rdoc: true
45
45
  homepage: http://github.com/cloudhead/more
46
46
  licenses: []
data/lib/more.rb DELETED
@@ -1,168 +0,0 @@
1
- # Less::More provides methods for parsing LESS files in a rails application to CSS target files.
2
- #
3
- # When Less::More.parse is called, all files in Less::More.source_path will be parsed using LESS
4
- # and saved as CSS files in Less::More.destination_path. If Less::More.compression is set to true,
5
- # extra line breaks will be removed to compress the CSS files.
6
- #
7
- # By default, Less::More.parse will be called for each request in `development` environment and on
8
- # application initialization in `production` environment.
9
-
10
- begin
11
- require 'less'
12
- rescue LoadError => e
13
- e.message << " (You may need to install the less gem)"
14
- raise e
15
- end
16
-
17
- class Less::More
18
- DEFAULTS = {
19
- "production" => {
20
- :compression => true,
21
- :header => false,
22
- :page_cache => true,
23
- :destination_path => "stylesheets"
24
- },
25
- "development" => {
26
- :compression => false,
27
- :header => true,
28
- :page_cache => false,
29
- :destination_path => "stylesheets"
30
- }
31
- }
32
-
33
- HEADER = %{/*\n\n\n\n\n\tThis file was auto generated by Less (http://lesscss.org). To change the contents of this file, edit %s instead.\n\n\n\n\n*/}
34
-
35
- class << self
36
- attr_writer :compression, :header, :page_cache, :destination_path
37
-
38
- # Returns true if compression is enabled. By default, compression is enabled in the production environment
39
- # and disabled in the development and test environments. This value can be changed using:
40
- #
41
- # Less::More.compression = true
42
- #
43
- # You can put this line into config/environments/development.rb to enable compression for the development environments
44
- def compression?
45
- get_cvar(:compression)
46
- end
47
-
48
- # TODO: Use controllers and page cache to generate the files.
49
- def page_cache?
50
- (not heroku?) && get_cvar(:page_cache)
51
- end
52
-
53
- # Tells the plugin to prepend HEADER to all generated CSS, informing users
54
- # opening raw .css files that the file is auto-generated and that the
55
- # .less file should be edited instead.
56
- #
57
- # Less::More.header = false
58
- def header
59
- result = get_cvar(:header)
60
- get_cvar(:header) ? DEFAULT_HEADER : ""
61
- end
62
-
63
- # The path, or route, where you want your .css files to live.
64
- def destination_path
65
- get_cvar(:destination_path)
66
- end
67
-
68
- # Gets user set values or DEFAULTS. User set values gets precedence.
69
- def get_cvar(cvar)
70
- instance_variable_get("@#{cvar}") || (DEFAULTS[Rails.env] || DEFAULTS["production"])[cvar]
71
- end
72
-
73
- # Returns true if the app is running on Heroku. When +heroku?+ is true,
74
- # +page_cache?+ will always be false.
75
- def heroku?
76
- !!ENV["HEROKU_ENV"]
77
- end
78
-
79
- # Returns the LESS source path, see `source_path=`
80
- def source_path
81
- @source_path || Rails.root.join("app", "stylesheets")
82
- end
83
-
84
- # Sets the source path for LESS files. This directory will be scanned recursively for all *.less files. Files prefixed
85
- # with an underscore is considered to be partials and are not parsed directly. These files can be included using `@import`
86
- # statements. *Example partial filename: _form.less*
87
- #
88
- # Default value is app/stylesheets
89
- #
90
- # Examples:
91
- # Less::More.source_path = "/path/to/less/files"
92
- # Less::More.source_path = Pathname.new("/other/path")
93
- def source_path=(path)
94
- @source_path = Pathname.new(path.to_s)
95
- end
96
-
97
- # Checks if a .less or .lss file exists in Less::More.source_path matching
98
- # the given parameters.
99
- #
100
- # Less::More.exists?(["screen"])
101
- # Less::More.exists?(["subdirectories", "here", "homepage"])
102
- def exists?(path_as_array)
103
- return false if path_as_array[-1].starts_with?("_")
104
-
105
- pathname = pathname_from_array(path_as_array)
106
- pathname && pathname.exist?
107
- end
108
-
109
- # Generates the .css from a .less or .lss file in Less::More.source_path matching
110
- # the given parameters.
111
- #
112
- # Less::More.generate(["screen"])
113
- # Less::More.generate(["subdirectories", "here", "homepage"])
114
- #
115
- # Returns the CSS as a string.
116
- def generate(path_as_array)
117
- source = pathname_from_array(path_as_array)
118
- engine = File.open(source) {|f| Less::Engine.new(f) }
119
- css = engine.to_css
120
- css.delete!("\n") if self.compression?
121
- css
122
- end
123
-
124
- # Generates all the .css files.
125
- def parse
126
- Less::More.all_less_files.each do |path|
127
- # Get path
128
- relative_path = path.relative_path_from(Less::More.source_path)
129
- path_as_array = relative_path.to_s.split(File::SEPARATOR)
130
- path_as_array[-1] = File.basename(path_as_array[-1], File.extname(path_as_array[-1]))
131
-
132
- # Generate CSS
133
- css = Less::More.generate(path_as_array)
134
-
135
- # Store CSS
136
- path_as_array[-1] = path_as_array[-1] + ".css"
137
- destination = Pathname.new(File.join(Rails.root, "public", Less::More.destination_path)).join(*path_as_array)
138
- destination.dirname.mkpath
139
-
140
- File.open(destination, "w") {|f|
141
- f.puts css
142
- }
143
- end
144
- end
145
-
146
- # Removes all generated css files.
147
- def clean
148
- all_less_files.each do |path|
149
- relative_path = path.relative_path_from(Less::More.source_path)
150
- css_path = relative_path.to_s.sub(/le?ss$/, "css")
151
- css_file = File.join(Rails.root, "public", Less::More.destination_path, css_path)
152
- File.delete(css_file) if File.file?(css_file)
153
- end
154
- end
155
-
156
- # Array of Pathname instances for all the less source files.
157
- def all_less_files
158
- Dir[Less::More.source_path.join("**", "*.{less,lss}")].map! {|f| Pathname.new(f) }
159
- end
160
-
161
- # Converts ["foo", "bar"] into a `Pathname` based on Less::More.source_path.
162
- def pathname_from_array(array)
163
- path_spec = array.dup
164
- path_spec[-1] = path_spec[-1] + ".{less,lss}"
165
- Pathname.glob(self.source_path.join(*path_spec))[0]
166
- end
167
- end
168
- end
@@ -1,31 +0,0 @@
1
- require 'test_helper'
2
-
3
- class LessCacheControllerTest < ActionController::IntegrationTest
4
- def setup
5
- Less::More.source_path = File.join(File.dirname(__FILE__), 'less_files')
6
- end
7
-
8
- test "regular stylesheet" do
9
- get "stylesheets/test.css"
10
- assert_response :success
11
- assert @response.body.include?("body { color: #222222; }")
12
- end
13
-
14
- test "sub-folder" do
15
- get "stylesheets/sub/test2.css"
16
- assert_response :success
17
- assert @response.body.include?("div { display: none; }")
18
- end
19
-
20
- test "404" do
21
- Less::More.expects(:generate).never
22
- get "stylesheets/does_not_exist.css"
23
- assert_response 404
24
- end
25
-
26
- test "setting headers with page cache" do
27
- Less::More.expects(:page_cache?).returns(false)
28
- get "stylesheets/test.css"
29
- assert @response.headers["Cache-Control"]
30
- end
31
- end