Class: Entityjs::Dirc

Inherits:
Object
  • Object
show all
Defined in:
lib/entityjs/dirc.rb

Class Method Summary collapse

Class Method Details

.change_dir(name) ⇒ Object



202
203
204
# File 'lib/entityjs/dirc.rb', line 202

def self.change_dir(name)
  Dir.chdir(Dir.pwd+'/'+name)
end

.copy_config(name) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/entityjs/dirc.rb', line 247

def self.copy_config(name)
  self.copy_file('config.yml')
  
  f = File.open('config.yml', 'r')
  
  contents = f.read
  
  contents = contents.sub(/\$NAME/, name)
  
  File.open('config.yml', "w+") do |f|
    
    f.write(contents)
    
  end
  
end

.copy_file(name, new_name = nil) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/entityjs/dirc.rb', line 231

def self.copy_file(name, new_name=nil)
  
  if new_name.nil?
    new_name = name
  end
  
  template = self.get_root_path+'/lib/blank/'+name
  ::FileUtils.cp template, new_name
  
  path = "/#{name}"
  
  path = self.get_root(path)
  
  puts "Created: #{path}"
end

.create_dir(name, move = false) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/entityjs/dirc.rb', line 216

def self.create_dir(name, move=false)

  if !File.directory? name
    Dir.mkdir(name)
  end
  
  if move
    self.change_dir(name)
  end
end

.create_file(name, contents) ⇒ Object



227
228
229
# File 'lib/entityjs/dirc.rb', line 227

def self.create_file(name, contents)
  File.open(name, "w+") {|f| f.write(contents)}
end

.exists?(file) ⇒ Boolean

checks if file exists in the EntityJS game

Returns:

  • (Boolean)


24
25
26
# File 'lib/entityjs/dirc.rb', line 24

def self.exists?(file)
  return File.file? Dirc.game_root+'/'+file
end

.find_entity_src(ignore = nil) ⇒ Object



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
# File 'lib/entityjs/dirc.rb', line 174

def self.find_entity_src(ignore=nil)
  ignore ||= []

  ents = Dir[Entityjs::source_folder+"/**/*.js"]
  
  #sort by name
  ents.sort!
  
  #push re.js to the top
  
  i = ents.index{|i| i.match(/re\.js$/) }
  
  if i.nil?
    puts 'Error cannot find re.js!'
    return ents
  end
  
  k = ents.delete_at(i)
  
  ents.unshift(k)
  
  if ignore.any?
    ents.delete_if {|i| !i.match(/#{ignore.join('|')}/).nil? }
  end
  
  return ents
end

.find_entity_src_url(ignore = nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/entityjs/dirc.rb', line 97

def self.find_entity_src_url(ignore=nil)
  srcs = self.find_entity_src(ignore)
  
  #remove src directory and replace with entityjs
  srcs = srcs.collect{|k| k[k.rindex('src/')..-1].gsub('src', 'entityjs') }
  
  return srcs
end

.find_eunit_src(ignore = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/entityjs/dirc.rb', line 148

def self.find_eunit_src(ignore=nil)
  ignore ||= []
  ents = Dir[Entityjs::eunit_folder+"/**/*.js"].sort
  
  #make sure qunit is at the top
  
  i = ents.index{|i| i.match(/qunit\.js$/) }
  
  if i.nil?
    puts 'Error cannot find qunit.js!'
    return ents
  end
  
  #remove
  k = ents.delete_at(i)
  
  #push at front
  ents.unshift(k)
  
  if ignore.any?
    ents.delete_if {|i| !i.match(/#{ignore.join('|')}/).nil? }
  end
  
  return ents
end

.find_eunit_src_url(ignore = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/entityjs/dirc.rb', line 138

def self.find_eunit_src_url(ignore=nil)
  srcs = self.find_eunit_src(ignore)
  
  #remove src directory and replace with entityjs
  srcs = srcs.collect{|k| k[k.rindex('qunit/')..-1] }
  
  return srcs
  
end

.find_scripts(ignore = nil, order = nil) ⇒ Object



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/entityjs/dirc.rb', line 106

def self.find_scripts(ignore=nil, order=nil)
  ignore ||= []
  order ||= []
  valids = Compile.valid_scripts.join(",")
  
  scripts = Dir["#{Dirc.game_root}/#{Config.scripts_folder}/**/*.{#{valids}}"].sort
  
  #sort again by extension
  scripts.sort! {|x,y| File.extname(x) <=> File.extname(y) }
  
  #ignore files
  if ignore.any?
    scripts = scripts.delete_if {|i| !i.match(/#{ignore.join('|')}/).nil? }
  end
  
  #order files
  order.reverse.each do |i|
    
    scripts.each do |s|
      
      if s.match /#{i}/
        scripts.delete(s)
        scripts.unshift(s)
      end
      
    end
    
  end
  
  return scripts
end

.find_scripts_short(ignore = nil, order = nil) ⇒ Object

returns all game scripts with short paths



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

def self.find_scripts_short(ignore=nil, order=nil)
  scripts = self.find_scripts_url(ignore, order)
  
  scripts.collect do |i|
    
    i.sub(Config.scripts_folder+'/', '')
    
  end
end

.find_scripts_url(ignore = nil, order = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/entityjs/dirc.rb', line 87

def self.find_scripts_url(ignore=nil, order=nil)
  
  scripts = self.find_scripts(ignore, order)
  
  #change filenames
  scripts = scripts.collect{|k| k[k.rindex(Config.scripts_folder+'/')..-1] }
  
  return scripts
end

.find_styles(ignore = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/entityjs/dirc.rb', line 37

def self.find_styles(ignore=nil)
  ignore ||= []

  styles = Dir["#{Dirc.game_root}/#{Config.styles_folder}/**/*.css"].sort

  if ignore.any?
    styles.delete_if {|i| !i.match(/#{ignore.join('|')}/).nil?}
  end

  return styles
end

.find_styles_url(ignore = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/entityjs/dirc.rb', line 49

def self.find_styles_url(ignore=nil)
  styles = self.find_styles(ignore)
  
  #remove extra folders
  styles = styles.collect do |i|
    i[i.rindex(Config.styles_folder+'/')..-1]
  end

  return styles
end

.find_tests_url(ignore = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/entityjs/dirc.rb', line 60

def self.find_tests_url(ignore=nil)
  valids = Compile.valid_scripts.join(",")
  ignore ||= []
  tests = Dir["#{Dirc.game_root}/#{Config.tests_folder}/**/*.{#{valids}}"].sort
  
  tests = tests.collect do |i|
    i[i.rindex(Config.tests_folder+'/')..-1]
  end
  
  if ignore.any?
    tests.delete_if {|i| !i.match(/#{ignore.join('|')}/).nil? }
  end
  
  return tests
end

.game?Boolean

is the current directory an EntityJS game?

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/entityjs/dirc.rb', line 9

def self.game?
  
  #check if scripts dir exists
  if File.directory? Config.scripts_folder
  
    if @game_root.nil?
      @game_root = Dir.pwd
    end
    return true
  end
  
  return false
end

.game_rootObject



33
34
35
# File 'lib/entityjs/dirc.rb', line 33

def self.game_root
  @game_root
end

.get_root(path) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/entityjs/dirc.rb', line 206

def self.get_root(path)
  cut = Dir.pwd.split(root)
  
  if cut.length == 2
    path = cut.pop+path
  end
  
  return path
end

.to_game_rootObject

path to EntityJS game



29
30
31
# File 'lib/entityjs/dirc.rb', line 29

def self.to_game_root
  Dir.chdir(@game_root)
end