Top Level Namespace

Defined Under Namespace

Modules: Loom

Constant Summary collapse

LOOM_DIR =

create the loom storage directory

File.expand_path "#{ENV['HOME']}/.loom"
TEMPLATES_DIR =

Constants

File.expand_path File.join File.dirname(__FILE__), '..', 'lib', 'templates'
CONFIG_PATH =
".loomconfig"
GLOBAL_CONFIG_PATH =
"#{LOOM_DIR}/.loomconfig"
TOKEN_PATH =
"#{LOOM_DIR}/token"
TEMP_PATH =
"#{LOOM_DIR}/temp"
OSX_APP_PATH =
"LoomDemo.app"

Instance Method Summary collapse

Instance Method Details

#configObject



143
144
145
146
# File 'lib/helper.rb', line 143

def config
  ensure_configured!
  JSON.parse(File.read(CONFIG_PATH))
end

#configure_directory(dir) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/helper.rb', line 116

def configure_directory(dir)
	say "Configuring Project..."

    loom_config = { 
      :sdk_version => "cocos"
    }

    config_path = ""

    if dir.length > 0
      config_path = File.join dir, CONFIG_PATH
    else
      config_path = CONFIG_PATH
    end

    if File.exists?(config_path)
      say_error "This project is already configured!"
      exit 1
    end
    
    say_tab 1, "Writing file #{config_path}"

    File.open(config_path , 'w') { |f| 
      f.write(JSON.pretty_generate(loom_config)) 
    }
end

#download_sdk(version) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/helper.rb', line 169

def download_sdk(version)
  response = fetch("http://#{@host}/api/v1/download/sdk/#{version}?auth_token=#{get_token}")

  file_path = File.expand_path("#{TEMP_PATH}/#{version}.zip")

  puts "Writing temporary file: #{file_path}"

  FileUtils.mkdir_p TEMP_PATH
  File.open(file_path, 'w') {|f| f.write(response.body) }

  file_path
end

#ensure_configured!Object

Ensures the directory is configured as a loom project



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

def ensure_configured!
  if !File.exists? CONFIG_PATH
    should_configure = choose("This directory has not been configured as a loom project. Configure it now?", :yes, :no)

    if(should_configure == :yes)
      configure_directory ""
    else
      say_error "Cannot complete action in an unconfigured loom project."
      exit 1
    end

  end
end

#ensure_sdk_imported!Object

Ensures the SDK that is specified in the config is imported into the project



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
# File 'lib/helper.rb', line 53

def ensure_sdk_imported!

  sdk_version = config["sdk_version"]

  say "Checking for sdk #{sdk_version}"
  if !sdk_installed? sdk_version
    say "Couldn't find the #{sdk_version} sdk, downloading it'"
    install_remote_sdk sdk_version
  end

  # if the sdk artifacts are already here then we don't need to
  # do anything
  return if(File.exists?("LoomDemo.app") && File.directory?("bin"))

  say "Pulling in sdk files..."

  sdk_path = File.join(LOOM_DIR, "sdks", config["sdk_version"])

  # TODO: Make this windows compatible
  sdk_app_path = File.join sdk_path, "LoomDemo.app"
  sdk_libs_path = File.join sdk_path, "libs"
  sdk_assets_path = File.join sdk_path, "assets"
  project_path = Dir.pwd

  if !File.directory?("bin")
    FileUtils.mkdir_p "bin"
  end

  #copy loomgame executable

  #FileUtils.cp_r(sdk_app_path,"loomgame")

  # Change permissions in the executable
  bin_file = "#{sdk_app_path}/Contents/MacOS/LoomDemo"
  # TODO Add check for bin_file if it exists
  say_tab 1, "Adding permissions to LoomDemo at #{bin_file}"
  FileUtils.chmod(0755, bin_file)

  #copy sdk_libs_path
  say_tab 1, "Copying core libraries from #{sdk_libs_path}"

  FileUtils.mkdir_p "libs"
  Dir.chdir(sdk_libs_path) do
    Dir.glob('**.loom') do |lib|
      say_tab 2, "Copy #{lib}"
      FileUtils.cp(lib, File.join(project_path, "libs"))
    end
  end

  say_tab 1, "Copying assets to #{project_path}/assets"
  FileUtils.cp_r("#{sdk_assets_path}/.", File.join(project_path, "assets"))
end

#fetch(uri_str, limit = 10) ⇒ Object

Raises:

  • (ArgumentError)


182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/helper.rb', line 182

def fetch(uri_str, limit = 10)
  # You should choose better exception.
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  response = Net::HTTP.get_response(URI.parse(uri_str))
  case response
  when Net::HTTPSuccess     then response
  when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    response.error!
  end
end

#get_default_sdkObject



230
231
232
# File 'lib/helper.rb', line 230

def get_default_sdk
  get_global_config("default_sdk")
end

#get_global_config(key) ⇒ Object



273
274
275
276
# File 'lib/helper.rb', line 273

def get_global_config(key)
  loom_config = Loom::Config.load GLOBAL_CONFIG_PATH
  loom_config[key]
end

#get_local_config(key) ⇒ Object



258
259
260
261
262
263
# File 'lib/helper.rb', line 258

def get_local_config(key)
  ensure_configured!

  loom_config = Loom::Config.load CONFIG_PATH
  loom_config[key]
end

#get_tokenObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/helper.rb', line 5

def get_token

	if File.exists?(TOKEN_PATH)
		return File.read(TOKEN_PATH)
	else
		say_error "Not Authenticated! Login using 'loom login'"
		exit 1
	end 

end

#install_remote_sdk(sdk_version) ⇒ Object



220
221
222
223
224
225
226
227
228
# File 'lib/helper.rb', line 220

def install_remote_sdk(sdk_version)
  sdk_path = File.join(LOOM_DIR, "sdks", sdk_version)
  say "Downloading remote loom SDK..."
  temp_file = download_sdk(sdk_version)
  say "Installing remote loom SDK..."
  unzip_file(temp_file, sdk_path)
  File.delete(temp_file)
  say_tab 1, "SDK installed. You can type 'loom use #{sdk_version}' in the future to use this SDK."
end

#install_sdk(args, options) ⇒ Object



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
# File 'lib/loom/sdk.rb', line 70

def install_sdk(args, options)
  sdk_version = args.shift unless args.nil?
  sdk_path = File.join(LOOM_DIR, "sdks", sdk_version)

  if sdk_version.nil?
    say "Invalid number of arguments."
    exit 1
  end

  # if the sdk doesn't already exists
  if !File.directory?(sdk_path)
    if options.local.nil?
      install_remote_sdk sdk_version
    else
      say "Installing local loom SDK..."
      unzip_file(options.local, sdk_path)
      say_tab 1, "SDK installed. You can type 'loom use #{sdk_version}' in the future to use this SDK."
    end
  end

  # if this is the first installed sdk, set it as default
  if get_default_sdk.nil?
    set_default_sdk sdk_version
  end
end

#installed_sdksObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/helper.rb', line 239

def installed_sdks

  sdks = Array.new

  Dir.foreach(File.join(LOOM_DIR, "sdks")) do |sdk|
    is_directory = File.directory?(File.join(LOOM_DIR, "sdks", sdk))

    if is_directory && sdk != "." && sdk != ".."
      sdks << sdk
    end
  end

  return sdks
end

#list_installed_sdksObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/helper.rb', line 195

def list_installed_sdks()
  say "Listing installed loom sdks..."

  if !File.directory?(File.join(LOOM_DIR, "sdks")) || Dir[File.join(LOOM_DIR, "sdks", "*")].empty?
    say_error "No SDKs installed"
    exit 1
  end

  Dir.foreach(File.join(LOOM_DIR, "sdks")) do |sdk|
    is_directory = File.directory?(File.join(LOOM_DIR, "sdks",sdk))
    #is_current = sdk == config["sdk_version"]
    #current_symbol = "*"

    if is_directory && sdk != "." && sdk != ".."
     # if is_current
     #   say_warning " #{current_symbol} #{sdk}"
     # else
        say_tab 1, "#{sdk}"
     # end
    end
  end

  exit(0)
end

#say_tab(num_tabs, value) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/helper.rb', line 106

def say_tab(num_tabs,value)

	result = ""
	num_tabs.times do |t|
		result += "    "
	end

	say result + value
end

#sdk_installed?(version) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/helper.rb', line 254

def sdk_installed?(version)
  File.directory?(File.join(LOOM_DIR, "sdks", version))
end

#search_for_remote_sdk(search) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/helper.rb', line 158

def search_for_remote_sdk(search)

  if(search.nil? || search.empty?)
    response = fetch("http://#{@host}/api/v1/sdks?auth_token=#{get_token}")
  else
    response = fetch("http://#{@host}/api/v1/sdk/#{search}?auth_token=#{get_token}")
  end

  response.body
end

#search_for_sdk(args, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/loom/sdk.rb', line 56

def search_for_sdk(args, options)
  sdk_version = args.shift unless args.nil?

  response = search_for_remote_sdk(sdk_version)

  say "== Remote SDKs =="
  say response

  if response.include? "No sdk found"
    exit 1
  end

end

#set_default(args, options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/loom/sdk.rb', line 40

def set_default(args, options)

  sdk_version = args.shift unless args.nil?

  if sdk_version.nil?
    say "Invalid number of arguments."
    exit 1
  elsif !sdk_installed?(sdk_version)
    say "Cannot set default SDK to '#{sdk_version}' it is not installed"
    exit 1
  end

  set_default_sdk(sdk_version)

end

#set_default_sdk(sdk_version) ⇒ Object



234
235
236
237
# File 'lib/helper.rb', line 234

def set_default_sdk(sdk_version)
  set_global_config("default_sdk", sdk_version)
  say "Default SDK set to #{sdk_version}"
end

#set_global_config(key, value) ⇒ Object



278
279
280
281
282
# File 'lib/helper.rb', line 278

def set_global_config(key, value)
  loom_config = Loom::Config.load GLOBAL_CONFIG_PATH
  loom_config[key] = value
  loom_config.save! GLOBAL_CONFIG_PATH
end

#set_local_config(key, value) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/helper.rb', line 265

def set_local_config(key, value)
  ensure_configured!

  loom_config = Loom::Config.load CONFIG_PATH
  loom_config[key] = value
  loom_config.save! CONFIG_PATH
end

#setup_env(command) ⇒ Object



16
17
18
# File 'lib/helper.rb', line 16

def setup_env(command)
  command.option '--env String', String, 'sets the authentication endpoint to a specific environment (for testing only).'
end

#setup_host(options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/helper.rb', line 21

def setup_host(options)
  options.default :env => "prod"

  if options.env == "prod"
    @host = "theengine.co"
  elsif options.env == "staging"
    @host = "theengineco-staging.herokuapp.com"
  elsif options.env == "local"
    @host = "localhost:3000"
  else
    say "Environment #{options.env} is not supported"
    exit 1
  end
end

#uninstall_sdk(args, options) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/loom/sdk.rb', line 96

def uninstall_sdk(args, options)
  sdk_version = args.shift unless args.nil?
  sdk_path = File.join(LOOM_DIR, "sdks", sdk_version)

  if sdk_version.nil?
    say "Invalid number of arguments."
    exit 1
  end

  if File.directory?(sdk_path)
    FileUtils.rm_rf sdk_path
    say "Successfully uninstalled SDK: #{sdk_version}"
  else
    say "Cannot remove SDK: #{sdk_version}, it isn't installed"
  end
end

#unzip_file(file, destination) ⇒ Object



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

def unzip_file (file, destination)
  Zip::ZipFile.open(file) do |zip_file|
    zip_file.each do |f|
      f_path=File.join(destination, f.name)
      FileUtils.mkdir_p(File.dirname(f_path))
      zip_file.extract(f, f_path) unless File.exist?(f_path)
    end
  end
end