uki 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,11 @@
3
3
  Project creation, dev server, testing, building for uki apps
4
4
 
5
5
  == Get started
6
+ 1. Install ruby and rubygems (on Windows go to http://rubyinstaller.org/)
7
+ 2. Install uki
6
8
  [sudo] gem install uki
9
+ 3. Use uki command
10
+ uki --help
7
11
  uki new project myapp +jspec
8
12
  cd myapp
9
- uki runserver
13
+ uki run
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.1.0
data/bin/uki CHANGED
@@ -115,22 +115,30 @@ command :run do |c|
115
115
  end
116
116
 
117
117
  command :build do |c|
118
- c.syntax = 'uki build [target]'
118
+ c.syntax = 'uki build [file1 [, file2]]'
119
119
  c.summary = 'Build current project'
120
- c.description = 'Process all javascripts included in the index.html file.
121
- Merges all include() in them and then runs them through
122
- Google Closure Compiler.
120
+ c.description = 'Merges and compresses javascript files.
121
+ Merges all include() and include_css() in js files. Merged output
122
+ is processed through google clousure compiler. Supports .js and .html
123
+ files as parameters.
123
124
 
124
- You can disable the Compiler with --nocompiler option.
125
+ By default builds index.html file in current directory and puts them into
126
+ build dir.
125
127
 
126
- By default resulting files will be put into build directory. You may
127
- override it with [target] option'
128
- c.option '-C', '--nocompiler', 'Disable Google Closure Compiler'
128
+ Note that output dir will be cleared before merging'
129
+ c.option '-C', '--nocompress', 'Disable Google Closure Compiler/Yahoo Compressor'
130
+ c.option '-o output', '--output output', 'Directory to output to'
129
131
  c.when_called do |args, option|
130
- target = args.shift || 'build'
132
+ files = args.empty? ? ['index.html'] : args
133
+ output = option.output || 'build'
131
134
  project = Uki::Project.new('.')
132
- project.build target, :compile => !option.nocompiler
133
- say "Build complete at '#{target}'"
135
+ project.build files,
136
+ :compress => !option.nocompress,
137
+ :output => output,
138
+ :images => args.empty?,
139
+ :clean_output => args.empty?
140
+
141
+ say "Build complete at '#{output}'"
134
142
  end
135
143
  end
136
144
 
Binary file
Binary file
@@ -0,0 +1,67 @@
1
+ require 'fileutils'
2
+ require 'tempfile'
3
+ require 'uki/include_js'
4
+
5
+ module Uki
6
+
7
+ class Builder
8
+
9
+ attr_accessor :path
10
+ attr_accessor :options
11
+
12
+ def initialize(path, options = {})
13
+ @path = path
14
+ @options = options
15
+ end
16
+
17
+ def code
18
+ options[:compress] ? compressed_code : plain_code
19
+ end
20
+
21
+ protected
22
+ def compressed_code
23
+ unless @compressed_code
24
+ code = Uki.include_js(path) do |path|
25
+ if path.match(/.css$/)
26
+ compiled_css path
27
+ else
28
+ File.read(path)
29
+ end
30
+ end
31
+ Tempfile.open('w') { |file|
32
+ file.write(code)
33
+ file.flush
34
+ @compressed_code = compiled_js(file.path)
35
+ }
36
+ end
37
+ @compressed_code
38
+ end
39
+
40
+ def plain_code
41
+ @plain_code ||= Uki.include_js path
42
+ end
43
+
44
+ def compiled_css path
45
+ system "java -jar #{path_to_yui_compressor} #{path} > #{path}.tmp"
46
+ code = File.read("#{path}.tmp")
47
+ FileUtils.rm "#{path}.tmp"
48
+ code
49
+ end
50
+
51
+ def compiled_js path
52
+ system "java -jar #{path_to_google_compiler} --js #{path} > #{path}.tmp"
53
+ code = File.read("#{path}.tmp")
54
+ FileUtils.rm "#{path}.tmp"
55
+ code
56
+ end
57
+
58
+ def path_to_google_compiler
59
+ File.join(UKI_ROOT, 'java', 'compiler.jar')
60
+ end
61
+
62
+ def path_to_yui_compressor
63
+ File.join(UKI_ROOT, 'java', 'yuicompressor.jar')
64
+ end
65
+ end
66
+
67
+ end
@@ -1,13 +1,21 @@
1
+ require 'json'
2
+
1
3
  module Uki
2
4
 
3
- INCLUDE_REGEXP = %r{((?:^|\n)[^\n]\W|^|\n)include\s*\(\s*['"]([^"']+)["']\s*\)(?:\s*;)?(.*?\r?\n|$)}
5
+ INCLUDE_REGEXP = %r{((?:^|\n)[^\n]\W|^|\n)\s*include\s*\(\s*['"]([^"']+)["']\s*\)(?:\s*;)?(.*?\r?\n|$)}
6
+ INCLUDE_CSS_REGEXP = %r{include_css\s*\(\s*['"]([^"']+)["']\s*\)}
4
7
 
5
8
  #
6
9
  # Preprocesses include() calls in js files
7
- def self.include_js path, included = {}, stack = []
10
+ def self.include_js path, included = {}, stack = [], &block
8
11
  raise Exception.new("File #{path} not found.\nStack: #{stack.join(' -> ')}") unless File.exists?(path)
9
12
  path = File.expand_path path
10
- code, base = File.read(path), File.dirname(path)
13
+ base = File.dirname(path)
14
+ code = if block_given?
15
+ yield path
16
+ else
17
+ File.read(path)
18
+ end
11
19
 
12
20
  included[path] = true
13
21
  code.gsub(INCLUDE_REGEXP) do |match|
@@ -21,7 +29,18 @@ module Uki
21
29
  $1 + $3
22
30
  end
23
31
  end
32
+ end.gsub(INCLUDE_CSS_REGEXP) do |match|
33
+ include_css File.join(base, $1), &block
34
+ end
35
+ end
36
+
37
+ def self.include_css path, &block
38
+ code = if block_given?
39
+ yield path
40
+ else
41
+ File.read(path)
24
42
  end
43
+ JSON.dump code
25
44
  end
26
45
 
27
46
  def self.extract_includes path
@@ -2,11 +2,13 @@ require 'fileutils'
2
2
  require 'commander/import'
3
3
  require 'erb'
4
4
  require 'pathname'
5
- require 'uki/include_js'
5
+ require 'uki/builder'
6
6
  require 'base64'
7
7
  require 'digest/md5'
8
8
 
9
9
  class Uki::Project
10
+ CJS_REGEXP = %r{=\s*["']?(([^"' ]+).cjs)}
11
+
10
12
  attr_accessor :dest
11
13
 
12
14
  def initialize dest
@@ -26,14 +28,34 @@ class Uki::Project
26
28
  init_jspec if options[:jspec]
27
29
  end
28
30
 
29
- def build target, options = {}
30
- target = File.join(dest, target) unless Pathname.new(target).absolute?
31
- init_target target
32
- containers = find_containers
33
- cjs = extract_cjs(containers)
34
- build_js cjs, target, options
35
- build_containers containers, target, options
36
- build_images target, options
31
+ def build files, options = {}
32
+ cjs = []
33
+ containers = []
34
+ files.each do |file|
35
+ path = to_path(file)
36
+ next unless File.exists?(path)
37
+ if file.match(/.html$/)
38
+ cjs += extract_cjs(path)
39
+ containers << path
40
+ else
41
+ cjs << path
42
+ end
43
+ end
44
+ cjs.uniq!
45
+ containers.uniq!
46
+
47
+ output = to_path(options[:output])
48
+ FileUtils.rm_rf output if options[:clean_output]
49
+ FileUtils.mkdir_p output
50
+
51
+ cjs.each do |path|
52
+ File.open(File.join(output, File.basename(path)), 'w') do |f|
53
+ f.write( Uki::Builder.new(path, :compress => options[:compress]).code )
54
+ end
55
+ end
56
+
57
+ build_containers containers, output
58
+ build_images output if options[:images]
37
59
  end
38
60
 
39
61
  def create_class template, fullName
@@ -65,12 +87,16 @@ class Uki::Project
65
87
  end
66
88
 
67
89
  protected
90
+ def to_path file
91
+ Pathname.new(file).absolute? ? file : File.join(dest, file)
92
+ end
93
+
68
94
  def write_class template, path
69
95
  package_name = path[0..-2].join('.')
70
96
  class_name = path[-1]
71
97
  class_name = class_name[0,1].upcase + class_name[1..-1]
72
98
  file_name = class_name[0,1].downcase + class_name[1..-1]
73
- target = File.join *(path[0..-2] + [file_name])
99
+ target = File.join( *(path[0..-2] + [file_name]) )
74
100
  target += '.js'
75
101
  File.open(File.join(dest, target), 'w') do |f|
76
102
  f.write template(template).result(binding)
@@ -81,9 +107,9 @@ class Uki::Project
81
107
  def write_function template, path
82
108
  package_name = path[0..-2].join('.')
83
109
  function_name = path[-1]
84
- function_name = function_name[0,1].downcase + function_name[1..-1]
110
+ function_name = function_name[0,1].downcase + function_name[1..-1]
85
111
  file_name = function_name
86
- target = File.join *(path[0..-2] + [file_name])
112
+ target = File.join( *(path[0..-2] + [file_name]) )
87
113
  target += '.js'
88
114
  File.open(File.join(dest, target), 'w') do |f|
89
115
  f.write template(template).result(binding)
@@ -114,56 +140,26 @@ class Uki::Project
114
140
  end
115
141
  end
116
142
 
117
- def build_js cjs, target, options
118
- cjs.each do |c|
119
- c = c.sub('.cjs', '.js')
120
- code = Uki.include_js File.join(dest, c)
121
- FileUtils.mkdir_p File.dirname(c)
122
- filename = File.join(target, c)
123
- File.open(filename, 'w') do |f|
124
- f.write code
125
- end
126
- compile_js filename if options[:compile]
127
- end
128
- end
129
-
130
- def compile_js file
131
- system "java -jar #{path_to_google_compiler} --js #{file} > #{file}.tmp"
132
- FileUtils.rm file
133
- FileUtils.mv "#{file}.tmp", file
134
- end
135
-
136
- def build_containers containers, target, options
143
+ def build_containers containers, output
137
144
  containers.each do |c|
138
- code = File.read(c).gsub(%r{=\s*["']?(([^"' ]+).cjs)}) do |match|
139
- md5 = Digest::MD5.file(File.join(target, "#{$2}.js")).hexdigest
145
+ code = File.read(c).gsub(CJS_REGEXP) do |match|
146
+ md5 = Digest::MD5.file(File.join(output, "#{$2}.js")).hexdigest
140
147
  match.sub('.cjs', ".js?#{md5}")
141
148
  end
142
- File.open(File.join(target, File.basename(c)), 'w') do |f|
149
+ File.open(File.join(output, File.basename(c)), 'w') do |f|
143
150
  f.write code
144
151
  end
145
152
  end
146
153
  end
147
154
 
148
- def build_images target, options
149
- FileUtils.cp_r File.join(dest, 'i'), File.join(target, 'i')
155
+ def build_images output
156
+ FileUtils.cp_r File.join(dest, 'i'), File.join(output, 'i')
150
157
  end
151
158
 
152
- def extract_cjs containers
153
- containers.map do |c|
154
- File.read(c).scan(%r{=\s*["']?([^"' ]+.cjs)})
155
- end.flatten.uniq
159
+ def extract_cjs container
160
+ File.read(container).scan(CJS_REGEXP).map { |match| match[0].sub('.cjs', '.js') }
156
161
  end
157
162
 
158
- def find_containers
159
- Dir.glob File.join(dest, '*.html')
160
- end
161
-
162
- def init_target target
163
- FileUtils.rm_rf target
164
- FileUtils.mkdir_p target
165
- end
166
-
167
163
  def init_dest
168
164
  FileUtils.mkdir_p File.join(dest, project_name)
169
165
  ['view', 'model', 'layout', 'controller'].each do |name|
@@ -240,8 +236,4 @@ class Uki::Project
240
236
  FileUtils.cp_r images_path, File.join(dest, 'i')
241
237
  end
242
238
 
243
- def path_to_google_compiler
244
- File.join(UKI_ROOT, 'frameworks', 'uki', 'compiler.jar')
245
- end
246
-
247
239
  end
@@ -1,10 +1,12 @@
1
+ require 'uki/builder'
2
+
1
3
  get %r{\.cjs$} do
2
4
  path = request.path.sub(/\.cjs$/, '.js').sub(%r{^/}, './')
3
5
  pass unless File.exists? path
4
6
 
5
7
  response.header['Content-type'] = 'application/x-javascript; charset=UTF-8'
6
8
  begin
7
- Uki.include_js(path)
9
+ Uki::Builder.new(path, :optimize => false).code
8
10
  rescue Exception => e
9
11
  message = e.message.sub(/\n/, '\\n')
10
12
  "alert('#{message}')"
@@ -1,5 +1,4 @@
1
1
  require 'sinatra'
2
- require 'uki/include_js'
3
2
  require 'uki/routes'
4
3
 
5
4
  module Uki
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{uki}
8
- s.version = "1.0.2"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Vladimir Kolesnikov"]
12
- s.date = %q{2010-04-05}
12
+ s.date = %q{2010-05-04}
13
13
  s.default_executable = %q{uki}
14
14
  s.description = %q{Project creation, dev server, testing, building for uki apps}
15
15
  s.email = %q{voloko@gmail.com}
@@ -240,7 +240,10 @@ Gem::Specification.new do |s|
240
240
  "frameworks/uki/thin.yaml",
241
241
  "frameworks/uki/uki.rb",
242
242
  "frameworks/uki/uki.ru",
243
+ "java/compiler.jar",
244
+ "java/yuicompressor.jar",
243
245
  "lib/uki.rb",
246
+ "lib/uki/builder.rb",
244
247
  "lib/uki/include_js.rb",
245
248
  "lib/uki/project.rb",
246
249
  "lib/uki/routes.rb",
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
+ - 1
7
8
  - 0
8
- - 2
9
- version: 1.0.2
9
+ version: 1.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Vladimir Kolesnikov
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-05 00:00:00 +04:00
17
+ date: 2010-05-04 00:00:00 +04:00
18
18
  default_executable: uki
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -288,7 +288,10 @@ files:
288
288
  - frameworks/uki/thin.yaml
289
289
  - frameworks/uki/uki.rb
290
290
  - frameworks/uki/uki.ru
291
+ - java/compiler.jar
292
+ - java/yuicompressor.jar
291
293
  - lib/uki.rb
294
+ - lib/uki/builder.rb
292
295
  - lib/uki/include_js.rb
293
296
  - lib/uki/project.rb
294
297
  - lib/uki/routes.rb