-
Notifications
You must be signed in to change notification settings - Fork 7
/
xc.rb
123 lines (98 loc) · 2.79 KB
/
xc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#this is going to be the main commandline tool for xc
# it will have functions for creating new projects,
# running them, and other cool stuff. Watch it go!
require 'ftools'
require 'webrick'
def run()
if ARGV[0] == 'server'
make_HTML('.')
server('.')
else
project = ARGV[0]
command = ARGV[1]
if command == 'create'
if !File::directory?(project)
if ARGV.length > 2
dimensions = ARGV[2].split('x')
else
dimensions = ['320','480']
end
new_project(project, dimensions[0], dimensions[1])
else
puts 'project directory already exists'
end
elsif command == 'server'
make_HTML(project)
server(project)
else
puts 'unrecognized command: ' + command
end
end
end
def new_project(name, width, height)
puts "Creating project " + name
Dir.mkdir(name)
Dir.mkdir(name + '/lib')
Dir.mkdir(name +'/resources')
File.copy('./lib/htmltemplate', name + '/lib')
File.copy('./lib/xc.js' , name + '/lib')
File.copy('./lib/jquery-1.4.2.min.js' , name + '/lib')
File.copy('./lib/xc_canvas.js' , name + '/lib')
File.copy('./lib/xc_ios.js' , name + '/lib')
File.copy('./lib/resources/man.png', name + '/resources')
File.copy('./lib/main.js' , name)
File.copy('./xc.rb', name)
write_config(name, width, height)
make_HTML(name)
end
def write_config(directory, width, height)
contents = 'width=' + width +"\n" + 'height=' + height
config = File.new(directory + '/config.xc', 'w')
if config
config.syswrite(contents)
else
puts 'unable to open config file.'
end
end
def make_HTML(directory)
config = IO.readlines(directory + '/config.xc')
width = config[0].split('=')[1].strip
height = config[1].split('=')[1].strip
title = "XC Test"
script = 'main.js'
# lets get the images from the resources directory
images = Dir[directory + '/resources/*'].find_all{|item| item =~ /.*\.png/}
item_count = images.length.to_s()
file_names = ''
images.each {|image|image[directory]='';file_names += '<img src="' + image + '" onLoad="itemLoaded(this);"/>'}
#now read in the html tempate
template = IO.read('lib/htmltemplate')
template['@TITLE'] = title
template['@ITEMCOUNT'] = item_count
template['@WIDTH'] = width
template['@HEIGHT'] = height
template['@SCRIPT'] = script
template['@IMAGES'] = file_names
template['@CANVASWIDTH'] = width
template['@CANVASHEIGHT'] = height
index = File.new(directory + '/index.html', 'w')
if index
index.syswrite(template)
else
puts 'unable to open index.html'
end
end
def start_webrick(config = {})
# always listen on port 8080
config.update(:Port => 8000)
server = WEBrick::HTTPServer.new(config)
yield server if block_given?
['INT', 'TERM'].each {|signal|
trap(signal) {server.shutdown}
}
server.start
end
def server(directory)
start_webrick(:DocumentRoot => directory)
end
run()