Module: Fluby

Defined in:
lib/fluby.rb

Constant Summary collapse

NAME =
'fluby'
COLORS =
{
  :black => "\033[0;30m",
  :red => "\033[0;31m",
  :green => "\033[0;32m",
  :yellow => "\033[0;33m",
  :blue => "\033[0;34m",
  :purple => "\033[0;35m",
  :cyan => "\033[0;36m",
  :white => "\033[0;37m",
  :whitebold => "\033[1;37m"
}

Class Method Summary collapse

Class Method Details

.alert(txt) ⇒ Object

Terminal



124
125
126
# File 'lib/fluby.rb', line 124

def self.alert txt
  puts "\n\t#{COLORS[:red]}#{txt}#{COLORS[:white]}"
end

.assetsObject



165
166
167
168
169
# File 'lib/fluby.rb', line 165

def self.assets
  Dir.glob(['assets/*.js','assets/*.xml']).each do |file|
    FileUtils.cp file, "deploy/#{File.basename(file)}"
  end
end

.available_templatesObject



119
120
121
# File 'lib/fluby.rb', line 119

def self.available_templates
  return Dir["#{template_path}/generators/**"]
end

.build(*args) ⇒ Object

Compilation



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fluby.rb', line 146

def self.build *args
  if is_a_project?
    say "Building #{project_fullname}"
    assets
    preprocess
    compile
    notify if has_growl?
  else
    alert "This is not a fluby project!"
  end
end

.compileObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/fluby.rb', line 170

def self.compile
  if @nodebug
    txt "Trace disabled"
    trace = " -trace no"
  else
    txt "Trace enabled"
    trace = ""
  end

  txt

  @start = Time.now

  render_template "index.rhtml", "index.html"
  render_template "#{project_name}.rxml", "#{project_name}.xml"

  txt

  system("swfmill simple #{project_name}.xml #{project_name}.swf")
  txt "√ swfmill"
  FileUtils.rm "#{project_name}.xml", {:verbose => false}

  system("mtasc -swf #{project_name}.swf -main -mx -version #{options["player"]} #{trace} #{project_name}.as")
  txt "√ mtasc"
  @end = Time.now

  ["*.html","*.swf"].each do |list|
    Dir[list].each do |file|
      FileUtils.mv file, "deploy/#{file}", {:verbose => false}
    end
  end
  say "Project compiled in #{@end - @start} seconds"
end

.copy_template(source, destination) ⇒ Object



65
66
67
68
# File 'lib/fluby.rb', line 65

def self.copy_template source, destination
  FileUtils.cp source, destination, :verbose => false
  log "create", destination
end

.create(*args) ⇒ Object



101
102
103
# File 'lib/fluby.rb', line 101

def self.create *args
  create_project(*args[0])
end

.create_project(name) ⇒ Object

Project creation



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fluby.rb', line 36

def self.create_project(name)
  # TODO: Support project creation in subfolders (i.e: fluby test/project)
  @project_name = name
  @project_folder = FileUtils.pwd + "/" + name

  puts "\n#{COLORS[:green]}Creating project #{name}"

  # Make folders
  if File.exist? @project_folder
    puts "#{COLORS[:red]}Folder #{name} already exists,"
    puts "#{COLORS[:red]}please choose a different name for your project"
    raise RuntimeError
  end
  FileUtils.mkdir [@project_folder,"#{@project_folder}/deploy","#{@project_folder}/assets","#{@project_folder}/script"], :verbose => false

  # Make files
  ["Rakefile","README","config.yml"].each do |file|
    render_template "#{template_path}/#{file}", "#{@project_folder}/#{file}"
  end

  # Static Templates
  copy_template "#{template_path}/index.rhtml", "#{@project_folder}/index.rhtml"
  copy_template "#{template_path}/project.rxml", "#{@project_folder}/#{name}.rxml"
  copy_template "#{template_path}/swfobject.js", "#{@project_folder}/assets/swfobject.js"

  # Main Class
  render_template "#{template_path}/ASClass.as", "#{@project_folder}/#{name}.as"

end

.current_pathObject



28
29
30
# File 'lib/fluby.rb', line 28

def self.current_path
  Dir.pwd
end

.gem_pathObject



22
23
24
# File 'lib/fluby.rb', line 22

def self.gem_path
  File.dirname(__FILE__) + "/../"
end

.has_growl?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/fluby.rb', line 98

def self.has_growl?
  return is_mac? && !`which "growlnotify"`.empty?
end

.in_textmate?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
# File 'lib/fluby.rb', line 86

def self.in_textmate?
  begin
    if TextMate
      return true
    end
  rescue
    return false
  end
end

.is_a_project?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/fluby.rb', line 31

def self.is_a_project?
  File.exist?("#{current_path}/index.rhtml")
end

.is_mac?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/fluby.rb', line 95

def self.is_mac?
  return RUBY_PLATFORM =~ /darwin/
end

.log(type, string) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/fluby.rb', line 78

def self.log type, string
  case type
  when "alert"
    puts "\t#{COLORS[:red]}Alert:\t#{string}#{COLORS[:white]}"
  when "create"
    puts "\t#{COLORS[:white]}Created:\t#{COLORS[:cyan]}#{string}#{COLORS[:white]}"
  end
end

.method_missing(name) ⇒ Object



219
220
221
# File 'lib/fluby.rb', line 219

def self.method_missing name
  options[name.to_s]
end

.notifyObject



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

def self.notify
  %x(growlnotify --name Rake -m 'Finished building #{project_name} in #{@end - @start} seconds')
end

.optionsObject



141
142
143
# File 'lib/fluby.rb', line 141

def self.options
  YAML.load_file('config.yml')
end

.preprocessObject



203
204
205
# File 'lib/fluby.rb', line 203

def self.preprocess
  
end

.project_fullnameObject



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

def self.project_fullname
  "#{project_name} v#{options['version']}"
end

.project_nameObject

Project data



135
136
137
# File 'lib/fluby.rb', line 135

def self.project_name
  options["app"]
end

.release(*args) ⇒ Object



157
158
159
160
161
# File 'lib/fluby.rb', line 157

def self.release *args
  @nodebug = true
  build
  @nodebug = false
end

.render_template(source, destination = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/fluby.rb', line 69

def self.render_template source, destination=nil
  if destination.nil?
    destination = source
  end
  open(destination,"w") do |f|
    f << ERB.new(IO.read(source)).result(binding)
  end
  log "create", destination
end

.say(txt) ⇒ Object



127
128
129
# File 'lib/fluby.rb', line 127

def self.say txt
  puts "\n\t#{COLORS[:green]}#{txt}#{COLORS[:white]}"
end

.setup_debugObject

Utils



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fluby.rb', line 106

def self.setup_debug
  debug_cfg_file = "/Library/Application\ Support/Macromedia/mm.cfg"
  system("touch '#{debug_cfg_file}'")
  File.open(debug_cfg_file,"w") do |f|
    f << <<-EOF
ErrorReportingEnable=1
TraceOutputFileEnable=1
EOF

  end unless File.exist?(debug_cfg_file)
  %x(touch "$HOME/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt")
end

.template_pathObject



25
26
27
# File 'lib/fluby.rb', line 25

def self.template_path
  gem_path + "/lib/templates"
end

.txt(txt = "") ⇒ Object



130
131
132
# File 'lib/fluby.rb', line 130

def self.txt txt=""
  puts "\t#{COLORS[:white]}#{txt}#{COLORS[:white]}"
end

.updateObject

Migration



208
209
210
211
212
213
214
215
216
217
# File 'lib/fluby.rb', line 208

def self.update
  @project_name = current_path.split('/').last
  render_template "#{template_path}/config.yml", "#{current_path}/config.yml" unless File.exist?("#{current_path}/config.yml")
  FileUtils.mv "#{current_path}/Rakefile", "#{current_path}/Rakefile.old"
  render_template "#{template_path}/Rakefile", "#{current_path}/Rakefile"
  FileUtils.mv "#{current_path}/index.rhtml", "#{current_path}/index.rhtml.old"
  copy_template "#{template_path}/index.rhtml", "#{current_path}/index.rhtml"
  FileUtils.mv "#{current_path}/#{@project_name}.rxml", "#{current_path}/#{@project_name}.rxml.old"
  copy_template "#{template_path}/project.rxml", "#{current_path}/#{@project_name}.rxml"
end

.versionObject



19
20
21
# File 'lib/fluby.rb', line 19

def self.version
  %x(cat #{gem_path}/VERSION)
end