contao 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,58 +0,0 @@
1
- require 'coffee_script'
2
- require 'contao/compiler'
3
- require 'fileutils'
4
-
5
- module TechnoGate
6
- module Contao
7
- class CoffeescriptCompiler < Compiler
8
-
9
- def initialize(options = {})
10
- super
11
- end
12
-
13
- def clean
14
- FileUtils.rm_rf output_path.to_s if File.exists?(output_path)
15
-
16
- super
17
- end
18
-
19
- protected
20
-
21
- def input_from_config_path
22
- Application.config.javascripts_path
23
- end
24
-
25
- def output_from_config_path
26
- Contao.expandify("tmp/compiled_javascript")
27
- end
28
-
29
- def compiler_name
30
- :coffeescript
31
- end
32
-
33
- # Compile assets
34
- #
35
- # This method compiled coffeescripts from
36
- # Application.config.javascripts_path into
37
- # Contao.root.join('tmp/compiled_javascript')
38
- def compile_assets
39
- input_path.each do |src_path|
40
- Dir["#{Contao.expandify(src_path)}/**/*.coffee"].sort.each do |file|
41
- dest = compute_destination_filename(src_path, file)
42
- FileUtils.mkdir_p File.dirname(dest)
43
- File.open(dest, 'w') do |f|
44
- f.write ::CoffeeScript.compile(File.read(file))
45
- end
46
- end
47
- end
48
- end
49
-
50
- def compute_destination_filename(src_path, file)
51
- dest = "#{output_path}/#{src_path.gsub('/', '_')}/"
52
- dest << file.gsub(/.*#{Regexp.escape src_path}\//, '').gsub(/\.coffee$/, '')
53
- dest << '.js' unless File.extname(dest) == '.js'
54
- dest
55
- end
56
- end
57
- end
58
- end
@@ -1,128 +0,0 @@
1
- require 'active_support/core_ext/string/inflections'
2
- require 'json'
3
-
4
- module TechnoGate
5
- module Contao
6
- class Compiler
7
- attr_accessor :options
8
-
9
- def initialize(options = {})
10
- @options = options
11
- @manifest_path = assets_public_path.join('manifest.json')
12
- end
13
-
14
- # Compile assets
15
- def compile
16
- prepare_folders
17
- compile_assets
18
- create_hashed_assets if Contao.env == :production
19
- generate_manifest
20
- notify
21
-
22
- self
23
- end
24
-
25
- def self.compile
26
- new.compile
27
- end
28
-
29
- def clean
30
- FileUtils.rm_rf Contao.expandify(Application.config.assets_public_path)
31
- end
32
-
33
- def self.clean
34
- new.clean
35
- end
36
-
37
- protected
38
- def assets_public_path
39
- Contao.expandify(Contao::Application.config.assets_public_path)
40
- end
41
-
42
- def input_from_config_path
43
- raise "Child class must define this"
44
- end
45
-
46
- def input_from_options
47
- @options[:input][compiler_name] rescue nil
48
- end
49
-
50
- def input_path
51
- input_from_options || input_from_config_path
52
- end
53
-
54
- def output_from_config_path
55
- raise "Child class must define this"
56
- end
57
-
58
- def output_from_options
59
- @options[:output][compiler_name] rescue nil
60
- end
61
-
62
- def output_path
63
- output_from_options || output_from_config_path
64
- end
65
-
66
- # Prepare folders
67
- def prepare_folders
68
- FileUtils.mkdir_p assets_public_path
69
- end
70
-
71
- def compile_assets
72
- end
73
-
74
- def create_hashed_assets
75
- end
76
-
77
- def notify
78
- klass_name = self.class.to_s.split('::').last
79
-
80
- Notifier.notify("#{klass_name.underscore.humanize} finished successfully.", title: klass_name)
81
- end
82
-
83
- # Create a diges for a given file
84
- #
85
- # This method creates a digested file for a given file path
86
- #
87
- # @param [Pathname | String] file_path
88
- def create_digest_for_file(file_path)
89
- digest = Digest::MD5.hexdigest File.read(file_path)
90
- digested_file_path = "#{file_path.to_s.chomp(File.extname(file_path))}-#{digest}#{File.extname(file_path)}"
91
- FileUtils.cp file_path, digested_file_path
92
-
93
- digested_file_path
94
- end
95
-
96
- # This method generates a manifest of all generated files
97
- # so it can be parsed and processed by the PHP application
98
- #
99
- # This method is expected to be overridden
100
- def generate_manifest
101
- end
102
-
103
- def generate_manifest_for(key, extension)
104
- manifest = JSON.parse(File.read(@manifest_path)) rescue {}
105
- manifest[key] = []
106
-
107
- all_files = Dir["#{assets_public_path}/**/*.#{extension}"]
108
- digested_files = all_files.select {|f| f =~ /^.+-[0-9a-f]{32}\.#{extension}$/}
109
- non_digested_files = all_files - digested_files
110
-
111
- if Contao.env == :production
112
- files = digested_files
113
- else
114
- files = non_digested_files
115
- end
116
-
117
- files.each do |file|
118
- file.slice! "#{assets_public_path}/"
119
- manifest[key] << file
120
- end
121
-
122
- File.open(@manifest_path, 'w') do |f|
123
- f.write(manifest.to_json)
124
- end
125
- end
126
- end
127
- end
128
- end
@@ -1,77 +0,0 @@
1
- require 'contao/compiler'
2
- require 'fileutils'
3
- require 'uglifier'
4
-
5
- module TechnoGate
6
- module Contao
7
- class JavascriptCompiler < Compiler
8
-
9
- def initialize(options = {})
10
- super
11
- end
12
-
13
- protected
14
-
15
- def compiler_name
16
- :javascript
17
- end
18
-
19
- def input_from_config_path
20
- Application.config.javascripts_path
21
- end
22
-
23
- def output_from_config_path
24
- Contao.expandify(Application.config.assets_public_path)
25
- end
26
-
27
- # Compile assets
28
- #
29
- # This method compiles javascripts from
30
- # Application.config.javascripts_path into
31
- # Application.config.assets_public_path/application.js and it uglifies
32
- # only if the environment is equal to :production
33
- def compile_assets
34
- tmp_app_js = Contao.root.join('tmp/application.js')
35
- FileUtils.mkdir_p File.dirname(tmp_app_js)
36
-
37
- File.open(tmp_app_js, 'w') do |compressed|
38
- javascripts_path.each do |src_path|
39
- Dir["#{Contao.expandify(src_path)}/**/*.js"].sort.each do |f|
40
- if TechnoGate::Contao.env == :production
41
- compressed.write(Uglifier.new.compile(File.read(f)))
42
- else
43
- compressed.write("// #{f}\n")
44
- compressed.write(File.read(f))
45
- compressed.write("\n")
46
- end
47
- end
48
- end
49
- end
50
-
51
- FileUtils.mv tmp_app_js, application_js_path
52
- end
53
-
54
- # Generate source folders give the exact source and the folder
55
- # under tmp/compiled_javascript on which CoffeeScript compiler
56
- # adds javascript files to.
57
- def javascripts_path
58
- input_path.map do |path|
59
- ["tmp/compiled_javascript/#{path.gsub('/', '_')}", path]
60
- end.flatten
61
- end
62
-
63
- # This function creates a hashed version of the assets
64
- def create_hashed_assets
65
- create_digest_for_file(Contao.expandify(Contao::Application.config.assets_public_path).join("application.js"))
66
- end
67
-
68
- def application_js_path
69
- Pathname(output_from_config_path).join("application.js")
70
- end
71
-
72
- def generate_manifest
73
- generate_manifest_for("javascripts", "js")
74
- end
75
- end
76
- end
77
- end
@@ -1,56 +0,0 @@
1
- require 'contao/compiler'
2
- require 'compass'
3
- require 'compass/commands'
4
-
5
- module TechnoGate
6
- module Contao
7
- class StylesheetCompiler < Compiler
8
-
9
- def initialize(options = {})
10
- super
11
- end
12
-
13
- def clean
14
- @cleaner ||= Compass::Commands::CleanProject.new(
15
- Contao.root,
16
- configuration_file: Contao.root.join('config', 'compass.rb')
17
- )
18
-
19
- @cleaner.execute
20
-
21
- super
22
- end
23
-
24
- protected
25
- def compiler_name
26
- :stylesheet
27
- end
28
-
29
- # This class can't be told where to get assets from or where to compile to
30
- # unless I figure out how to configure the UpdateProject without a file
31
- def input_from_config_path
32
- end
33
- def output_from_config_path
34
- end
35
-
36
- def compile_assets
37
- @updater ||= Compass::Commands::UpdateProject.new(
38
- Contao.root,
39
- configuration_file: Contao.root.join('config', 'compass.rb')
40
- )
41
-
42
- @updater.execute
43
- end
44
-
45
- def create_hashed_assets
46
- Dir["#{Contao.expandify(Contao::Application.config.assets_public_path)}/**/*.css"].each do |file|
47
- create_digest_for_file Pathname(file).expand_path
48
- end
49
- end
50
-
51
- def generate_manifest
52
- generate_manifest_for("stylesheets", "css")
53
- end
54
- end
55
- end
56
- end
@@ -1,22 +0,0 @@
1
- namespace :assets do
2
- desc "Compile javascript"
3
- task :javascript do
4
- TechnoGate::Contao::CoffeescriptCompiler.compile
5
- TechnoGate::Contao::JavascriptCompiler.compile
6
- end
7
-
8
- desc "Compile stylesheet"
9
- task :stylesheet do
10
- TechnoGate::Contao::StylesheetCompiler.compile
11
- end
12
-
13
- desc "Clean assets"
14
- task :clean do
15
- TechnoGate::Contao::StylesheetCompiler.clean
16
- TechnoGate::Contao::CoffeescriptCompiler.clean
17
- TechnoGate::Contao::JavascriptCompiler.clean
18
- end
19
-
20
- desc "Precompile assets"
21
- task :precompile => [:clean, :stylesheet, :javascript]
22
- end
@@ -1,8 +0,0 @@
1
- begin
2
- require 'jasmine'
3
- load 'jasmine/tasks/jasmine.rake'
4
- rescue LoadError
5
- task :jasmine do
6
- abort "Jasmine is not available. In order to run jasmine, you must: (sudo) gem install jasmine"
7
- end
8
- end
data/lib/guard/assets.rb DELETED
@@ -1,117 +0,0 @@
1
- require 'guard'
2
- require 'guard/guard'
3
- require 'active_support/core_ext/string/inflections'
4
- require 'active_support/core_ext/object/try'
5
-
6
- module Guard
7
- class Assets < ::Guard::Guard
8
- # Initialize a Guard.
9
- # @param [Array<Guard::Watcher>] watchers the Guard file watchers
10
- # @param [Hash] options the custom Guard options
11
- def initialize(watchers = [], options = {})
12
- super
13
-
14
- @options = options
15
- @compilers = instantiate_compilers
16
- end
17
-
18
- # Call once when Guard starts. Please override initialize method to init stuff.
19
- # @raise [:task_has_failed] when start has failed
20
- def start
21
- run_all
22
- end
23
-
24
- # Called when just `enter` is pressed
25
- # This method should be principally used for long action like running all specs/tests/...
26
- # @raise [:task_has_failed] when run_all has failed
27
- def run_all
28
- @compilers.each do |compiler|
29
- compiler.clean
30
- end
31
-
32
- call_compilers
33
- end
34
-
35
- # Called on file(s) modifications that the Guard watches.
36
- # @param [Array<String>] paths the changes files or paths
37
- # @raise [:task_has_failed] when run_on_changes has failed
38
- def run_on_changes(paths)
39
- compile(paths)
40
- end
41
-
42
- # Called on file(s) deletions that the Guard watches.
43
- # @param [Array<String>] paths the deleted files or paths
44
- # @raise [:task_has_failed] when run_on_changes has failed
45
- def run_on_removals(paths)
46
- compile(paths)
47
- end
48
-
49
- protected
50
- def instantiate_compilers
51
- @options.merge!(compilers: [:stylesheet, :coffeescript, :javascript]) unless @options[:compilers]
52
-
53
- sort_compilers(@options[:compilers]).map(&:to_s).map do |compiler|
54
- self.instance_variable_set(
55
- "@#{compiler}_compiler",
56
- "::TechnoGate::Contao::#{compiler.camelize}Compiler".constantize.new(@options)
57
- )
58
-
59
- self.instance_variable_get "@#{compiler}_compiler"
60
- end
61
- end
62
-
63
- def sort_compilers(unsorted_compilers)
64
- compilers = []
65
- compilers << :stylesheet if unsorted_compilers.include? :stylesheet
66
- compilers << :coffeescript if unsorted_compilers.include? :coffeescript
67
- compilers << :javascript if unsorted_compilers.include? :javascript
68
-
69
- compilers
70
- end
71
-
72
- def compile(paths)
73
- coffeescript = javascript = stylesheet = false
74
-
75
- paths.each do |path|
76
- coffeescript = true if !coffeescript && is_coffeescript?(path)
77
- javascript = true if !javascript && is_javascript?(path)
78
- stylesheet = true if !stylesheet && is_stylesheet?(path)
79
- end
80
-
81
- compilers = @compilers.clone
82
- compilers.delete(:stylesheet) unless stylesheet
83
- compilers.delete(:javascript) unless javascript
84
- compilers.delete(:coffeescript) unless coffeescript
85
-
86
- call_compilers compilers
87
- end
88
-
89
- def call_compilers(compilers = @compilers)
90
- compilers.each do |compiler|
91
- compiler.compile
92
- end
93
- end
94
-
95
- def is_coffeescript?(path)
96
- file_in_path?(path, TechnoGate::Contao::Application.config.javascripts_path) && File.extname(path) == '.coffee'
97
- end
98
-
99
- def is_javascript?(path)
100
- file_in_path?(path, TechnoGate::Contao::Application.config.javascripts_path) && File.extname(path) == '.js'
101
- end
102
-
103
- def is_stylesheet?(path)
104
- file_in_path?(path, TechnoGate::Contao::Application.config.stylesheets_path)
105
- end
106
-
107
- def file_in_path?(file, paths)
108
- paths = [paths] if paths.is_a?(String)
109
-
110
- paths.each do |path|
111
- return true if file.start_with?(path)
112
- end
113
-
114
- false
115
- end
116
- end
117
- end