droiuby 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/drby +94 -39
  2. data/lib/droiuby/project.rb +71 -24
  3. metadata +2 -2
data/bin/drby CHANGED
@@ -1,23 +1,102 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+
3
4
  require "droiuby"
5
+ require 'optparse'
6
+ require 'ripper'
7
+ require "readline"
8
+
9
+ $droiuby_host = ENV['DROIUBY_HOST'] || '10.0.2.2'
10
+ $device_ip = ENV['DROIUBY_DEVICE'] || nil
11
+
12
+ options = OptionParser.new do |o|
13
+ o.banner =
14
+ "Usage: drby autostart true|false INSTANCE_NAME [options] # Set specified instance to start on droiuby launch
15
+ drby console [options] # Launch an interactive console to the target Android Device running Droiuby
16
+ drby framework update [FOLDER] [options] # updates the droiuby framework from folder (default src_framework)
17
+ drby gopack [PROJECT_NAME] [options] # packages an app and uploads to an Android Device running Droiuby
18
+ drby list [options] # Lists the app instances running on the phone
19
+ drby live [PROJECT_NAME] [options] # runs a web instance of the app and tells Droiuby to load it.
20
+ drby new PROJECT_NAME [options] # Create a new project
21
+ drby pack [PROJECT_NAME] [options] # zips and packages an app
22
+ drby switch INSTANCE_NAME [options] # Switch to the specified instance\n"
23
+
24
+ o.separator ""
25
+ o.separator "options:"
26
+ o.on('-h','--host HOST_IP','The IP Address of the host computer (for droiuby live mode)') { |b| $droiuby_host = b }
27
+ o.on('-d','--device DEVICE_IP','The IP Address of the Android Device') { |b| $device_ip = b }
28
+ o.parse!
29
+ end
4
30
 
5
31
  command = ARGV[0]
6
32
 
33
+ def valid_statement?(stmt)
34
+ begin
35
+ catch(:x) { eval("throw :x; #{stmt}") }
36
+ rescue SyntaxError=>e
37
+ return false
38
+ end
39
+ return true
40
+ end
41
+
7
42
  case command
8
43
  when 'new'
9
- if ARGV.size < 2
10
- puts "Create a new droiuby app"
11
- puts "usage: "
12
- puts "drby new [project name]"
13
- exit(1)
14
- end
15
44
  project_name = ARGV[1]
45
+
46
+ if project_name.blank?
47
+ puts "PROJECT_NAME is required"
48
+ puts "Usage: drby new PROJECT_NAME [options]"
49
+ exit(1)
50
+ end
51
+
16
52
  project = Project.new
17
53
  project.create(project_name,'')
54
+ when 'console'
55
+ puts "droiuby console"
56
+ project = Project.new
57
+ while buf = Readline.readline("> ", true)
58
+
59
+ exit(1) if buf=='exit' || buf=='quit'
60
+
61
+ begin
62
+ while !Ripper.sexp(buf) || !valid_statement?(buf)
63
+ buf = buf + "\n" + Readline.readline("?> ", true) + "\n"
64
+ end
65
+ rescue Interrupt
66
+ puts "\n"
67
+ next
68
+ end
69
+ res = JSON.parse(project.command(buf, $device_ip))
70
+ puts res['result']
71
+ end
72
+ when 'list'
73
+ project = Project.new
74
+ project.list($device_ip)
75
+ when 'switch'
76
+ instance_name = nil
77
+ unless ARGV[1].blank?
78
+ instance_name = ARGV[1]
79
+ else
80
+ puts "instance name required."
81
+ puts 'To get a list of instances you may:'
82
+ puts "drby list"
83
+ puts " "
84
+ puts "Usage: drby switch INSTANCE_NAME"
85
+ exit(1)
86
+ end
87
+ project = Project.new
88
+ project.switch(instance_name, $device_ip)
89
+ when 'autostart'
90
+ switch = ARGV[1]
91
+ if switch.blank?
92
+ puts "Usage: drby autostart true|false [NAMESPACE] [options]"
93
+ exit(1)
94
+ end
95
+ project = Project.new
96
+ project.autostart(switch, ARGV[2], $device_ip)
18
97
  when 'pack'
19
98
  project_name = nil
20
- if ARGV.size == 2
99
+ unless ARGV[1].blank?
21
100
  project_name = ARGV[1]
22
101
  else
23
102
  if !File.exists?('config.droiuby')
@@ -35,46 +114,22 @@ case command
35
114
  end
36
115
  when 'gopack'
37
116
  project_name = nil
38
- if ARGV.size == 3
117
+
118
+ unless ARGV[1].blank?
39
119
  project_name = ARGV[1]
40
- device_ip = ARGV[2]
41
- elsif ARGV.size == 2
42
- device_ip = ARGV[1]
43
- elsif ARGV.size == 1
44
- device_ip = nil
45
- else
46
- puts "zips and uploads app to device"
47
- puts "usage: "
48
- puts "drby gopack [device IP] "
49
- puts "drby gopack [folder] [device IP]"
50
- exit(1)
51
120
  end
52
121
 
53
-
54
122
  project = Project.new
55
- droiuby_host = ENV['DROIUBY_HOST'] || '10.0.2.2'
56
- project.execute(project_name, device_ip, droiuby_host)
123
+ project.execute(project_name, $device_ip, $droiuby_host)
57
124
  when 'live'
58
125
  project_name = nil
59
- if ARGV.size == 3
126
+
127
+ if ARGV[1]
60
128
  project_name = ARGV[1]
61
- device_ip = ARGV[2]
62
- elsif ARGV.size == 2
63
- device_ip = ARGV[1]
64
- elsif ARGV.size == 1
65
- device_ip = nil
66
- else
67
- puts "starts a live web server and point droiuby to it"
68
- puts "usage: "
69
- puts "drby live [device IP]"
70
- puts "drby live [folder] [device IP]"
71
- exit(1)
72
129
  end
130
+
73
131
  project = Project.new
74
- droiuby_host = ENV['DROIUBY_HOST'] || '10.0.2.2'
75
- project.live(project_name, device_ip, droiuby_host, '')
132
+ project.live(project_name, $device_ip, $droiuby_host, '')
76
133
  else
77
- puts "invalid command"
78
- puts "Should be:"
79
- puts " drby [new|live|gopack|pack]"
134
+ puts options
80
135
  end
@@ -14,17 +14,24 @@ class Project < Thor
14
14
 
15
15
  source_root File.join(File.dirname(__FILE__), 'templates')
16
16
 
17
+ no_commands {
18
+ def map_device_ip(device_ip)
19
+ if device_ip.nil?
20
+ device_ip = '127.0.0.1'
21
+ begin
22
+ `adb forward tcp:4000 tcp:4000`
23
+ rescue Exception=>e
24
+ puts e.inspect
25
+ end
26
+ end
27
+ device_ip
28
+ end
29
+ }
30
+
17
31
  desc "launch device IP [URL]","Tell droiuby to connect to app hosted at URL"
18
32
  def launch(device_ip, url)
19
33
 
20
- if device_ip.nil?
21
- device_ip = '127.0.0.1'
22
- begin
23
- `adb forward tcp:4000 tcp:4000`
24
- rescue Exception=>e
25
- puts e.inspect
26
- end
27
- end
34
+ device_ip = map_device_ip(device_ip)
28
35
 
29
36
  url_str = "http://#{device_ip}:4000/control?cmd=launch&url=#{CGI::escape(url)}"
30
37
  puts "loading application at url #{url}"
@@ -36,6 +43,60 @@ class Project < Thor
36
43
  Net::HTTP.get_print(uri)
37
44
  end
38
45
 
46
+ desc "list [DEVICE IP]","List running app instances"
47
+ def list(device_ip = nil)
48
+ device_ip = map_device_ip(device_ip)
49
+ url_str = "http://#{device_ip}:4000/control?cmd=list"
50
+ puts url_str
51
+ uri = URI.parse(url_str)
52
+ # Shortcut
53
+ response = Net::HTTP.get_response(uri)
54
+ result = JSON.parse(response.body)
55
+ result['list'].split(',').each do |item|
56
+ puts item
57
+ end
58
+ end
59
+
60
+ desc "cmd [command] [DEVICE_IP]", "Send command to a Droiuby instance"
61
+ def command(command_line, device_ip=nil)
62
+ device_ip = map_device_ip(device_ip)
63
+ url_str = "http://#{device_ip}:4000/console?cmd=#{CGI::escape(command_line)}"
64
+ uri = URI.parse(url_str)
65
+ # Shortcut
66
+ response = Net::HTTP.get_response(uri)
67
+ response.body
68
+ end
69
+
70
+ desc "switch [name] [DEVICE IP]","switch to target app instance identified by name"
71
+ def switch(name, device_ip = nil)
72
+ device_ip = map_device_ip(device_ip)
73
+ url_str = "http://#{device_ip}:4000/control?cmd=switch&name=#{CGI::escape(name)}"
74
+ puts url_str
75
+ uri = URI.parse(url_str)
76
+ # Shortcut
77
+ response = Net::HTTP.get_response(uri)
78
+ # Will print response.body
79
+ response = Net::HTTP.get_print(uri)
80
+ end
81
+
82
+ desc "autostart MODE [NAME] [DEVICE IP]","set current app to load on startup"
83
+ def autostart(mode = 'on', name = nil, device_ip = nil)
84
+ device_ip = map_device_ip(device_ip)
85
+ url_str = if mode == 'on'
86
+ "http://#{device_ip}:4000/control?cmd=autostart#{!name.nil? ? "&name=#{CGI::escape(name)}" : ''}"
87
+ else
88
+ "http://#{device_ip}:4000/control?cmd=clearautostart"
89
+ end
90
+
91
+ puts url_str
92
+ uri = URI.parse(url_str)
93
+ # Shortcut
94
+ response = Net::HTTP.get_response(uri)
95
+ # Will print response.body
96
+ Net::HTTP.get_print(uri)
97
+
98
+ end
99
+
39
100
  desc "create NAME [WORKSPACE_DIR]","create a new droiuby project with NAME"
40
101
 
41
102
  def create(name, output_dir = 'projects')
@@ -86,14 +147,7 @@ class Project < Thor
86
147
  File.join(source_dir_args, name)
87
148
  end
88
149
 
89
- if device_ip.nil?
90
- device_ip = '127.0.0.1'
91
- begin
92
- `adb forward tcp:4000 tcp:4000`
93
- rescue Exception=>e
94
- puts e.inspect
95
- end
96
- end
150
+ device_ip = map_device_ip(device_ip)
97
151
 
98
152
  port = 2000
99
153
 
@@ -125,14 +179,7 @@ class Project < Thor
125
179
  File.join(source_dir, name)
126
180
  end
127
181
 
128
- if device_ip.nil?
129
- device_ip = '127.0.0.1'
130
- begin
131
- `adb forward tcp:4000 tcp:4000`
132
- rescue Exception=>e
133
- puts e.inspect
134
- end
135
- end
182
+ device_ip = map_device_ip(device_ip)
136
183
 
137
184
  src_package = if framework
138
185
  File.join(source_dir,'framework_src','build',"#{name}.zip")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: droiuby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-28 00:00:00.000000000 Z
12
+ date: 2013-12-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake