smallvictories 0.0.1 → 0.0.2

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a5e274949e580e1d2979fbfafc6dc0a32005e6d
4
- data.tar.gz: b168cfeecebf9dc19c352210c6d207875b7cabbd
3
+ metadata.gz: afe68ea3a35b1adb6a885d2e98991685fa18f7ec
4
+ data.tar.gz: 847e787c01942e2018a4a5e72540e866eba5bb17
5
5
  SHA512:
6
- metadata.gz: 230c49befd0eb666980238cae3cbdd6ae88ac8712237fdc013c37c06a03a8e8ee25fce536717ce4b77af730bd33efa2e96c62f0c614c61f112c2a38cdd86def3
7
- data.tar.gz: 546cc79b6d356e4fd0bf0fa205be78b9b22e2dc64466da4f263b2c751257e7c7d0e1c1f36dffa7bdc5af7255476dae528cf480d6c89f2b35190c9ce6ba459256
6
+ metadata.gz: 3a3fedb31949829a83834b14ca8fc026314c3a549d31b9492e731f56221df0732c46e0157bdfcbd059b39e49799a279387808033724e6c79ce9bd64d61846193
7
+ data.tar.gz: 222a8cea5277277d3410c6f3198c50cbfc8891fd2e1ae7c4eb352f4ebd099e925a922a569641092c20365415ef41a8b4da857d962581c58dbc1a4e78cc82257e
data/.gitignore CHANGED
@@ -13,3 +13,4 @@
13
13
  *.a
14
14
  mkmf.log
15
15
  /assets
16
+ *.gem
data/bin/sv CHANGED
@@ -1,11 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require './lib/smallvictories'
3
+ require 'smallvictories'
4
4
 
5
5
  def help
6
6
  %Q(
7
7
  Usage: sv [COMMAND] [OPTIONS]
8
8
  Commands:
9
+ watch Watch for changes and compile files
9
10
  hello Puts Hello World!
10
11
  help Prints this help document
11
12
  version Prints the small victories gem version
@@ -20,6 +21,11 @@ def hello
20
21
  puts SmallVictories.hello
21
22
  end
22
23
 
24
+ def watch
25
+ SmallVictories.setup
26
+ SmallVictories.watch
27
+ end
28
+
23
29
  case ARGV[0]
24
30
  when '-v', '--version', 'version'
25
31
  puts SmallVictories::VERSION
@@ -27,6 +33,8 @@ when '-h', '--help', 'help'
27
33
  puts help
28
34
  when 'hello'
29
35
  hello
36
+ when 'watch'
37
+ watch
30
38
  else
31
39
  puts "`#{ARGV[0]}` command not found.\n"
32
40
  puts help
@@ -1,3 +1,3 @@
1
1
  module SmallVictories
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,7 +1,189 @@
1
- require "./lib/smallvictories/version"
1
+ require "smallvictories/version"
2
+ require 'yaml'
3
+ require 'sassc'
4
+ require 'sprockets'
5
+ require 'listen'
2
6
 
3
7
  module SmallVictories
4
- def self.hello
8
+ extend self
9
+ DEFAULT_SASS_DIR = '_sass'
10
+ DEFAULT_STYLESHEET = '_sv_custom.css'
11
+ DEFAULT_CSS_DIR = '_css'
12
+ DEFAULT_SOURCE = ''
13
+ DEFAULT_DESTINATION = '_site'
14
+ ROOT = Dir.pwd
15
+
16
+ def hello
5
17
  "Hello World!"
6
18
  end
19
+
20
+ def config
21
+ if File.exists?('_config.yml')
22
+ YAML.load(File.read('_config.yml')) || {}
23
+ else
24
+ {}
25
+ end
26
+ end
27
+
28
+ def config_folder key
29
+ config[key.to_s].chomp("/").reverse.chomp("/").reverse if config.has_key?(key.to_s)
30
+ end
31
+
32
+ def source
33
+ config_folder(:source) || DEFAULT_SOURCE
34
+ end
35
+
36
+ def full_source_path
37
+ "#{ROOT}/#{source}"
38
+ end
39
+
40
+ def destination
41
+ config_folder(:destination) || DEFAULT_DESTINATION
42
+ end
43
+
44
+ def full_destination_path
45
+ "#{ROOT}/#{destination}/"
46
+ end
47
+
48
+ def css_dir
49
+ config_folder(:css_dir) || DEFAULT_CSS_DIR
50
+ end
51
+
52
+ def full_css_path
53
+ "#{full_source_path}/#{css_dir}/"
54
+ end
55
+
56
+ def stylesheet
57
+ config_folder(:stylesheet) || DEFAULT_STYLESHEET
58
+ end
59
+
60
+ def full_stylesheet_path
61
+ "#{full_css_path}/#{stylesheet}"
62
+ end
63
+
64
+ def sass_dir
65
+ config_folder(:sass_dir) || DEFAULT_SASS_DIR
66
+ end
67
+
68
+ def full_sass_path
69
+ "#{full_source_path}/#{sass_dir}/"
70
+ end
71
+
72
+ def full_sass_path
73
+ "#{full_source_path}/#{sass_dir}/"
74
+ end
75
+
76
+ def build_listener
77
+ Listen.to(
78
+ full_source_path,
79
+ force_polling: true,
80
+ &(listen_handler)
81
+ )
82
+ end
83
+
84
+ def listen_handler
85
+ proc do |modified, added, removed|
86
+ paths = modified + added + removed
87
+ extensions = paths.map{ |path| File.extname(path) }
88
+ extensions.each do |ext|
89
+ case ext
90
+ when '.scss'
91
+ puts sass
92
+ puts compile
93
+ when '.sass'
94
+ puts sass
95
+ puts compile
96
+ else
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def setup
103
+ setup_stylesheet
104
+ end
105
+
106
+ def watch
107
+ listener = build_listener
108
+ listener.start
109
+ puts "👀"
110
+
111
+ trap("INT") do
112
+ listener.stop
113
+ puts "🔥 🔥 🔥 Halting auto-regeneration."
114
+ exit 0
115
+ end
116
+
117
+ sleep_forever
118
+ rescue ThreadError
119
+ # Ctrl-C
120
+ end
121
+
122
+ def sass
123
+ errors, rendered = [], []
124
+ Dir.glob([full_sass_path.concat('*.scss'), full_sass_path.concat('*.sass')]) do |path|
125
+ begin
126
+ file_name = Pathname.new(path).basename.to_s.split('.').first
127
+ next if file_name =~ /^_/ # do not render partials
128
+ file = File.open(path).read
129
+ engine = SassC::Engine.new(file, { style: :compressed, load_paths: [full_sass_path] })
130
+ css = engine.render
131
+ output_file_name = file_name.concat('.css')
132
+ output_path = full_css_path.concat(output_file_name)
133
+ File.open(output_path, 'w') { |file| file.write(css) }
134
+ rendered << "👍 rendered #{destination.concat('/').concat(css_dir).concat('/').concat(output_file_name)}\n"
135
+ rescue => e
136
+ errors << "🔥 Sass 🔥 #{e}\n"
137
+ end
138
+ end
139
+ rendered.join(', ') + errors.join(', ')
140
+ end
141
+
142
+ def compile
143
+ sprockets = Sprockets::Environment.new(ROOT) do |environment|
144
+ environment.js_compressor = :uglify
145
+ environment.css_compressor = :scss
146
+ end
147
+
148
+ sprockets.append_path(full_css_path)
149
+
150
+ errors, compiled = [], []
151
+ begin
152
+ [stylesheet].each do |bundle|
153
+ assets = sprockets.find_asset(bundle)
154
+ prefix, basename = assets.pathname.to_s.split('/')[-2..-1]
155
+ FileUtils.mkpath full_destination_path.concat(prefix)
156
+
157
+ assets.write_to(full_destination_path.concat(basename))
158
+ compiled << "👍 compiled #{destination.concat('/').concat(basename)}\n"
159
+ end
160
+ rescue => e
161
+ puts e.class
162
+ errors << "🔥 Compile 🔥 #{e}\n"
163
+ end
164
+ compiled.join(', ') + errors.join(', ')
165
+ end
166
+
167
+ def sleep_forever
168
+ loop { sleep 1000 }
169
+ end
170
+
171
+ private
172
+
173
+ def create_src_file source, destination
174
+ spec = Gem::Specification.find_by_name("smallvictories")
175
+ contents = File.open(spec.gem_dir.concat('/src/').concat(source)).read
176
+ File.open(destination, 'w') { |file| file.write(contents) }
177
+ end
178
+
179
+ def setup_stylesheet
180
+ setup_directory(full_css_path)
181
+ unless File.exists?(full_stylesheet_path)
182
+ create_src_file('stylesheet.css', full_stylesheet_path)
183
+ end
184
+ end
185
+
186
+ def setup_directory path
187
+ Dir.mkdir(path) unless File.exists?(path)
188
+ end
7
189
  end
@@ -18,6 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_runtime_dependency 'sassc', "~> 1.6"
22
+ spec.add_runtime_dependency 'sprockets', '~> 3.4', '>= 3.4.0'
23
+ spec.add_runtime_dependency 'listen', '~> 3.0', '>= 3.0.3'
24
+
21
25
  spec.add_development_dependency "bundler", "~> 1.7"
22
26
  spec.add_development_dependency "rake", "~> 10.0"
23
27
  spec.add_development_dependency "rspec", "~> 3.3"
@@ -0,0 +1,4 @@
1
+ source: '/my-source-folder/'
2
+ destination: 'my-site-folder/'
3
+ sass_dir: 'my-sass-folder/'
4
+ stylesheet: 'my-stylesheet.css'
File without changes
@@ -0,0 +1,3 @@
1
+ /* Sprockets
2
+ *= require_tree .
3
+ */
@@ -0,0 +1 @@
1
+ .bootstrap{color: black}
@@ -0,0 +1,3 @@
1
+ html {
2
+ background: white;
3
+ }
@@ -0,0 +1,7 @@
1
+ @import 'extra';
2
+
3
+ body {
4
+ div {
5
+ background: red;
6
+ }
7
+ }
@@ -0,0 +1,6 @@
1
+ @import "nothing!"
2
+
3
+ html {
4
+ {
5
+ }
6
+ }
@@ -1,6 +1,100 @@
1
1
  require 'spec_helper'
2
2
  describe SmallVictories do
3
- it 'puts hello' do
4
- expect(SmallVictories.hello).to eq "Hello World!"
3
+ describe '#hello' do
4
+ it 'puts hello' do
5
+ expect(SmallVictories.hello).to eq "Hello World!"
6
+ end
7
+ end
8
+
9
+ describe '#config' do
10
+ context 'with no config file' do
11
+ it 'defaults to source directory' do
12
+ expect(SmallVictories.source).to eq ''
13
+ end
14
+
15
+ it 'defaults to destination directory' do
16
+ expect(SmallVictories.destination).to eq '_site'
17
+ end
18
+
19
+ it 'defaults stylesheet file' do
20
+ expect(SmallVictories.stylesheet).to eq '_sv_custom.css'
21
+ end
22
+
23
+ it 'defaults to _sass directory' do
24
+ expect(SmallVictories.sass_dir).to eq '_sass'
25
+ end
26
+ end
27
+
28
+ context 'with config file' do
29
+ before do
30
+ FileUtils.cp('fixtures/_config.yml', './')
31
+ end
32
+
33
+ after do
34
+ FileUtils.rm('./_config.yml')
35
+ end
36
+
37
+ it 'reads the source folder' do
38
+ expect(SmallVictories.source).to eq 'my-source-folder'
39
+ end
40
+
41
+ it 'reads the destination folder' do
42
+ expect(SmallVictories.destination).to eq 'my-site-folder'
43
+ end
44
+
45
+ it 'reads the _sass directory' do
46
+ expect(SmallVictories.sass_dir).to eq 'my-sass-folder'
47
+ end
48
+
49
+ it 'reads the output css file' do
50
+ expect(SmallVictories.stylesheet).to eq 'my-stylesheet.css'
51
+ end
52
+ end
53
+ end
54
+
55
+ describe '#sass' do
56
+ before do
57
+ allow(SmallVictories).to receive(:source).and_return('./spec/fixtures')
58
+ allow(SmallVictories).to receive(:destination).and_return('./spec/fixtures')
59
+ end
60
+
61
+ context 'with valid sass' do
62
+ it 'renders the css from a scss file' do
63
+ expect(SmallVictories.sass).to include "rendered ./spec/fixtures/_css/stylesheet.css"
64
+ expect(File.open('./fixtures/_css/stylesheet.css').read).to include 'html{background:white}body div{background:red}'
65
+ end
66
+
67
+ after do
68
+ FileUtils.rm('./fixtures/_css/stylesheet.css')
69
+ end
70
+ end
71
+
72
+ context 'with invalid sass' do
73
+ before do
74
+ allow(SmallVictories).to receive(:source).and_return('./spec/fixtures/invalid')
75
+ end
76
+
77
+ it 'returns the error' do
78
+ expect(SmallVictories.sass).to include "Error"
79
+ end
80
+ end
81
+ end
82
+
83
+ describe '#compile' do
84
+ before do
85
+ allow(SmallVictories).to receive(:source).and_return('./spec/fixtures')
86
+ allow(SmallVictories).to receive(:destination).and_return('./spec/fixtures')
87
+ end
88
+
89
+ context 'with valid css' do
90
+ it 'compiles the css file' do
91
+ expect(SmallVictories.compile).to include "compiled ./spec/fixtures/_sv_custom.css"
92
+ expect(File.open('fixtures/_sv_custom.css').read).to include '.bootstrap{color:black}'
93
+ end
94
+
95
+ after do
96
+ FileUtils.rm('fixtures/_sv_custom.css')
97
+ end
98
+ end
5
99
  end
6
100
  end
data/spec/spec_helper.rb CHANGED
@@ -4,4 +4,7 @@ Bundler.setup
4
4
  require 'smallvictories'
5
5
 
6
6
  RSpec.configure do |config|
7
+ config.before do
8
+ FileUtils.cd File.dirname(__FILE__)
9
+ end
7
10
  end
@@ -0,0 +1,7 @@
1
+ /* Small Victories Sprockets File
2
+ *
3
+ * For more details see
4
+ * https://github.com/rails/sprockets#the-directive-processor
5
+ *
6
+ *= require_tree .
7
+ */
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smallvictories
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Dijkstra
@@ -10,6 +10,60 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sassc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sprockets
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 3.4.0
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '3.4'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.4.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: listen
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.3
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '3.0'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.0.3
13
67
  - !ruby/object:Gem::Dependency
14
68
  name: bundler
15
69
  requirement: !ruby/object:Gem::Requirement
@@ -69,8 +123,16 @@ files:
69
123
  - lib/smallvictories.rb
70
124
  - lib/smallvictories/version.rb
71
125
  - smallvictories.gemspec
126
+ - spec/fixtures/_config.yml
127
+ - spec/fixtures/_css/.gitkeep
128
+ - spec/fixtures/_css/_sv_custom.css
129
+ - spec/fixtures/_css/bootstrap.css
130
+ - spec/fixtures/_sass/_extra.scss
131
+ - spec/fixtures/_sass/stylesheet.scss
132
+ - spec/fixtures/invalid/_sass/stylesheet.scss
72
133
  - spec/smallvictories_spec.rb
73
134
  - spec/spec_helper.rb
135
+ - src/stylesheet.css
74
136
  homepage: https://github.com/xxix/smallvictories-gem
75
137
  licenses:
76
138
  - MIT
@@ -96,5 +158,12 @@ signing_key:
96
158
  specification_version: 4
97
159
  summary: A command line utility for building websites.
98
160
  test_files:
161
+ - spec/fixtures/_config.yml
162
+ - spec/fixtures/_css/.gitkeep
163
+ - spec/fixtures/_css/_sv_custom.css
164
+ - spec/fixtures/_css/bootstrap.css
165
+ - spec/fixtures/_sass/_extra.scss
166
+ - spec/fixtures/_sass/stylesheet.scss
167
+ - spec/fixtures/invalid/_sass/stylesheet.scss
99
168
  - spec/smallvictories_spec.rb
100
169
  - spec/spec_helper.rb