Class: Rake::CompileTask

Inherits:
TaskLib
  • Object
show all
Defined in:
lib/jquery_plugin_gen/compiletask.rb

Overview

Create a compiling task that will build the project into distributable files (e.g a single and a packed js file with compression).

The CompileTask will create the following targets:

:compile

Create all the requested package files.

:clobber_compile

Delete all the package files. This target is automatically added to the main clobber target.

:recompile

Rebuild the package files from scratch, even if they are not out of date.

Example:

 JqueryPluginGen::CompileTask.new('queryplugin') do |p|
   p.js_files.include("src/**/*.js")
   p.css_files.include("src/css/**/*.css")
   p.image_files.include("src/images/**/*.*")
   p.js_dir = "src"
   p.images_dir = "images"
   p.css_dir = "css"
   p.package_files.include("README.txt", "HISTORY.txt")
   p.package_dir = "build"
 end

Note: Windows users require sed and perl for rake:compile - please install onto PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) {|_self| ... } ⇒ CompileTask

Create a Compile Task

Yields:

  • (_self)

Yield Parameters:



76
77
78
79
80
81
# File 'lib/jquery_plugin_gen/compiletask.rb', line 76

def initialize(name)
  init(name)
  yield self if block_given?
  define unless name.nil?
  puts "Windows users require sed and perl for rake:compile - please install onto PATH" if /mswin|mingw/ =~ RUBY_PLATFORM
end

Instance Attribute Details

#css_dirObject

Directory used to store the package files (default is ‘css’).



52
53
54
# File 'lib/jquery_plugin_gen/compiletask.rb', line 52

def css_dir
  @css_dir
end

#css_filesObject

List of files to be included in the package.



70
71
72
# File 'lib/jquery_plugin_gen/compiletask.rb', line 70

def css_files
  @css_files
end

#image_filesObject

List of files to be included in the package.



73
74
75
# File 'lib/jquery_plugin_gen/compiletask.rb', line 73

def image_files
  @image_files
end

#images_dirObject

Directory used to store the package files (default is ‘images’).



49
50
51
# File 'lib/jquery_plugin_gen/compiletask.rb', line 49

def images_dir
  @images_dir
end

#js_filesObject

List of files to be included in the package.



67
68
69
# File 'lib/jquery_plugin_gen/compiletask.rb', line 67

def js_files
  @js_files
end

#nameObject

Name of the plugin files (don’t add .js, eg ‘jquery.mywidget’).



46
47
48
# File 'lib/jquery_plugin_gen/compiletask.rb', line 46

def name
  @name
end

#need_packerObject

True if the single js file is compressed using PACKER (default is true).



61
62
63
# File 'lib/jquery_plugin_gen/compiletask.rb', line 61

def need_packer
  @need_packer
end

#need_sedObject

True if the single js file is compiled using SED (default is true).



58
59
60
# File 'lib/jquery_plugin_gen/compiletask.rb', line 58

def need_sed
  @need_sed
end

#package_dirObject

Directory used to store the package files (default is ‘build’).



55
56
57
# File 'lib/jquery_plugin_gen/compiletask.rb', line 55

def package_dir
  @package_dir
end

#package_filesObject

List of files to be included in the package.



64
65
66
# File 'lib/jquery_plugin_gen/compiletask.rb', line 64

def package_files
  @package_files
end

Instance Method Details

#defineObject

Create the tasks defined by this task library.



102
103
104
105
106
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
# File 'lib/jquery_plugin_gen/compiletask.rb', line 102

def define
  fail "Name required (or :noversion)" if @name.nil?
  @name = nil if :noname == @name

  desc "Build all the packages"
  task :compile => :pack do
    [images_dir, css_dir].each { |dir| mkdir_p("#{package_dir_path}/#{dir}") }
    FileUtils.cp(image_files, package_images_path)
    FileUtils.cp(css_files, package_css_path)
    FileUtils.cp(package_files, package_dir_path)
 end
  
  desc "Force a rebuild of the package files"
  task :recompile => [:clobber_compile, :compile]
  
  desc "Remove compile products" 
  task :clobber_compile do
    FileUtils.rm_r(package_dir_path) rescue nil
  end
  
  desc "Merge js files into one"
  task :merge do
    FileUtils.mkdir(package_dir_path) rescue nil
    `#{sed_command}`
  end
  
  desc "Compress js files to min"
  task :pack => :merge do
    `#{packer_command}`
  end

  task :clobber => [:clobber_compile]

  self
end

#init(name) ⇒ Object

Initialization that bypasses the “yield self” and “define” step.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jquery_plugin_gen/compiletask.rb', line 84

def init(name)
  @name = name
  @package_files = Rake::FileList["README.txt", "HISTORY.txt"]
  @package_dir = 'build'
  @need_sed = true
  @need_packer = true
  @images_dir = 'images'
  @css_dir = 'css'
  @js_dir = 'src'
  @js_files = Rake::FileList.new
  @js_files.include("#{@js_dir}/**/*.js")
  @image_files = Rake::FileList.new
  @image_files.include("#{@js_dir}/#{@image_dir}/**/*.*")
  @css_files = Rake::FileList.new
  @css_files.include("#{@js_dir}/#{@css_dir}/**/*.css")     
end

#package_css_pathObject



142
143
144
# File 'lib/jquery_plugin_gen/compiletask.rb', line 142

def package_css_path
  "#{package_dir_path}/#{css_dir}"
end

#package_dir_pathObject



138
139
140
# File 'lib/jquery_plugin_gen/compiletask.rb', line 138

def package_dir_path
  "#{package_dir}"
end

#package_images_pathObject



146
147
148
# File 'lib/jquery_plugin_gen/compiletask.rb', line 146

def package_images_path
  "#{package_dir_path}/#{images_dir}"
end

#package_jsObject



150
151
152
# File 'lib/jquery_plugin_gen/compiletask.rb', line 150

def package_js
  "#{package_dir_path}/#{name}.js"
end

#packed_jsObject



154
155
156
# File 'lib/jquery_plugin_gen/compiletask.rb', line 154

def packed_js
  "#{package_dir_path}/#{name}.min.js"
end

#packer_commandObject



162
163
164
# File 'lib/jquery_plugin_gen/compiletask.rb', line 162

def packer_command
  "perl -I #{File.dirname(__FILE__)}/../packer #{File.dirname(__FILE__)}/../packer/jsPacker.pl -i #{package_js} -o #{packed_js} -e62"
end

#sed_commandObject



158
159
160
# File 'lib/jquery_plugin_gen/compiletask.rb', line 158

def sed_command
  "sed -e '1 s/^\xEF\xBB\xBF//' #{js_files} > #{package_js}"
end