Class: CappRuby::AppBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cappruby/app_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ AppBuilder

Returns a new instance of AppBuilder.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cappruby/app_builder.rb', line 33

def initialize(args)
  @app_root = Dir.getwd
  @app_name = File.basename(@app_root)
  @app_title = (@app_name.split('_').collect { |p| p.capitalize }).join ' '
  
  unless File.exist? 'config/build.yml'
    puts "Missing config/build.yml file. Sure this is an application dir?"
    exit
  end
  
  @build_options = YAML.load_file('config/build.yml')
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



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

def app_name
  @app_name
end

#app_rootObject (readonly)

Returns the value of attribute app_root.



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

def app_root
  @app_root
end

#app_titleObject (readonly)

Returns the value of attribute app_title.



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

def app_title
  @app_title
end

#build_optionsObject (readonly)

Returns the value of attribute build_options.



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

def build_options
  @build_options
end

Instance Method Details

#build!Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/cappruby/app_builder.rb', line 50

def build!
  # also rebuild CappRuby framework (for now)
  FrameworkBuilder.new.build!
  FileUtils.mkdir_p build_dir
  write_info_plist_file
  write_main_j_file
  write_index_html_file

  File.symlink(File.join(@app_root, 'Frameworks'), File.join(build_dir, 'Frameworks')) unless File.exist? File.join(build_dir, 'Frameworks')
end

#build_dirObject



46
47
48
# File 'lib/cappruby/app_builder.rb', line 46

def build_dir
  @build_dir ||= File.join(@app_root, 'build')
end

#index_html_fileObject



107
108
109
# File 'lib/cappruby/app_builder.rb', line 107

def index_html_file
  File.join(build_dir, 'index.html')
end

#info_plist_fileObject



61
62
63
# File 'lib/cappruby/app_builder.rb', line 61

def info_plist_file
  File.join(build_dir, 'Info.plist')
end

#main_j_fileObject



82
83
84
# File 'lib/cappruby/app_builder.rb', line 82

def main_j_file
  File.join(build_dir, 'main.j')
end

#write_index_html_fileObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cappruby/app_builder.rb', line 111

def write_index_html_file
  File.open(index_html_file, 'w') do |f|
    f.puts %{<?xml version="1.0" encoding="UTF-8"?>}
    f.puts %{<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">}
    f.puts %{<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en">}
    f.puts %{<head>}
    f.puts %{<meta http-equiv="X-UA-Compatible" content="IE-EmulateIE7" />}
    f.puts %{<title>#{app_title}</title>}
    f.puts %{<script type="text/javascript">}
    f.puts %{OBJJ_MAIN_FILE = "main.j"}
    f.puts %{</script>}
    f.puts %{<script src = "Frameworks/Objective-J/Objective-J.js" type = "text/javascript"></script>}
    f.puts %{<style type = "text/css">}
    f.puts %{body{margin:0; padding:0;}}
    f.puts %{</style>}
    f.puts %{</head>}
    f.puts %{<body>}
    f.puts %{</body>}
    f.puts %{</html>}
  end
end

#write_info_plist_fileObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cappruby/app_builder.rb', line 65

def write_info_plist_file
  File.open(info_plist_file, 'w') do |f|
    f.puts %{<?xml version="1.0" encoding="UTF-8"?>}
    f.puts %{<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">}
    f.puts %{<plist version="1.0">}
    f.puts %{<dict>}
    f.puts %{  <key>CPApplicationDelegateClass</key>}
  	f.puts %{  <string>AppController</string>}
  	f.puts %{  <key>CPBundleName</key>}
  	f.puts %{  <string>#{app_name}</string>}
  	f.puts %{  <key>CPPrincipalClass</key>}
  	f.puts %{  <string>CPApplication</string>}
    f.puts %{</dict>}
    f.puts %{</plist>}
  end
end

#write_main_j_fileObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cappruby/app_builder.rb', line 86

def write_main_j_file
  File.open(main_j_file, 'w') do |f|
    f.puts %{@import <Foundation/Foundation.j>}
    f.puts %{@import <AppKit/AppKit.j>}
    f.puts %{@import <CappRuby/CappRuby.j>}
    
    f.puts %{function main(args, namedArgs) \{}
    f.puts %{cappruby_main("/lib/application.rb", args, namedArgs);}
    f.puts %{\};}
    
    # now we need to write all .rb files (in opal compatible way)
    rb_sources = File.join(app_root, 'lib', '**', '*.rb')
    Dir.glob(rb_sources).each do |rb|
      name = /^#{app_root}(.*)$/.match(rb)[1]
      b = RubyBuilder.new(rb, self, name)
      c = b.build!
      f.puts %{cappruby_file("#{name}", #{c});}
    end
  end
end