Skip to content

Commit 0590c79

Browse files
committed
* example to automate rescue feedback
1 parent e39bbc8 commit 0590c79

9 files changed

+143
-22
lines changed

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
system

README.md

+38-10
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ A template for Ruby-based Alfred 2 workflow development.
44

55
## Example Projects
66

7-
* [alfred2_top_workflow]( https://github.com/zhaocai/alfred2-top-workflow )
7+
* [alfred2-top-workflow]( https://github.com/zhaocai/alfred2-top-workflow )
88

99

1010
## Main features:
1111

1212
* Use standard [bundler][gembundler] to easily package, manage, and update ruby gems in the workflow.
1313
* Friendly exception and debug output to the Mac OS X Console
14+
* Automate rescue feedback items to alfred when something goes wrong.
1415

15-
Alfred workflow and feedback related functions are located in a separate [alfred-workflow gem]( https://github.com/zhaocai/alfred-workflow ) which can be easily installed by adding `gem "alfred-workflow", "~>1.0.4"` in the Gemfile.
16+
> Alfred workflow and feedback related functions are located in a separate [alfred-workflow gem]( https://github.com/zhaocai/alfred-workflow ) which can be easily installed by adding `gem "alfred-workflow"` in the Gemfile.
1617
18+
* Functions for smart case query filter of feedback results.
1719
* Functions for finding the bundle ID, cache and storage paths, and query arguments.
1820
* Functions for reading and writing plist files.
1921
* Functions to simplify generating feedback XML for Alfred.
@@ -23,7 +25,7 @@ Alfred workflow and feedback related functions are located in a separate [alfred
2325
## Quick Example
2426

2527
```ruby
26-
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8
28+
require 'rubygems' unless defined? Gem
2729
require "bundle/bundler/setup"
2830
require "alfred"
2931

@@ -32,19 +34,40 @@ Alfred.with_friendly_error do |alfred|
3234

3335
fb.add_file_item(File.expand_path "~/Applications/")
3436

35-
puts fb.to_xml
37+
puts fb.to_alfred(ARGV)
3638
end
3739
```
3840

39-
Code are wrapped in `Alfred.with_friendly_error` block. Exceptions and debug messages are logged to Console log file **~/Library/Logs/Alfred-Workflow.log**.
41+
Main code are wrapped in `Alfred.with_friendly_error` block. Exceptions and debug messages are logged to Console log file **~/Library/Logs/Alfred-Workflow.log**.
4042

43+
One more example with rescue feedback automatically generated!
4144

45+
```ruby
46+
require 'rubygems' unless defined? Gem
47+
require "bundle/bundler/setup"
48+
require "alfred"
4249

50+
def my_code_with_something_goes_wrong
51+
true
52+
end
53+
54+
Alfred.with_friendly_error do |alfred|
55+
alfred.with_rescue_feedback = true
56+
57+
fb = alfred.feedback
58+
59+
if my_code_with_something_goes_wrong
60+
raise Alfred::NoBundleIDError, "Wrong Bundle ID Test!"
61+
end
62+
end
63+
```
4364

4465

4566

4667
## Quick Start Guide
4768

69+
You may directly download the [alfred2-ruby-template]( https://github.com/zhaocai/alfred2-ruby-template/raw/master/alfred2-ruby-template.alfredworkflow ) here, install, and play with the keywords: `test feedback` and `test rescue feedback`.
70+
4871
### Step 1: Clone
4972

5073
Clone or fork this repo to your local directory: https://github.com/zhaocai/alfred2-ruby-template.git
@@ -53,9 +76,10 @@ Clone or fork this repo to your local directory: https://github.com/zhaocai/alfr
5376
Update **domain** and **id** in the `config.yml` file.
5477

5578
```yaml
79+
# bundle_id = "domain.id"
80+
# path is the relative path to the workflow in the project root
5681
---
5782
path: workflow
58-
# bundle_id = "domain.id"
5983
domain: your.domain
6084
id: alfred2-ruby-template
6185
```
@@ -74,19 +98,23 @@ source "https://rubygems.org"
7498
7599
gem "plist"
76100
gem "logging"
77-
gem "alfred-workflow", "~>1.0.4"
101+
gem "alfred-workflow"
78102
```
79103

80-
Run `rake bundle_install` to pull the gems into **workflow/bundle/** folder.
104+
Run `rake bundle:install` to pull the gems into **workflow/bundle/** folder.
81105

82106
### Step 5: Your Code
83107

84108
Now you are good to add your own code based on the previous example.
85109

86110

87-
## Todo
111+
## Troubleshooting
112+
113+
1. System bundler version is too old.
114+
115+
> `sudo gem install bundler`
116+
88117

89-
1. Start with rescue feedback if something goes wrong
90118

91119
## Reference
92120

Rakefile

+13-6
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ task :chdir => [:config] do
2424
end
2525

2626
desc "Install Gems"
27-
task :bundle_install => [:chdir] do
28-
sh %Q{bundle install --standalone} do |ok, res|
27+
task "bundle:install" => [:chdir] do
28+
sh %Q{bundle install --standalone --clean} do |ok, res|
2929
if ! ok
3030
puts "fail to install gems (status = #{res.exitstatus})"
3131
end
3232
end
3333
end
3434

3535
desc "Update Gems"
36-
task :bundle_update => [:chdir] do
37-
sh %Q{bundle update} do |ok, res|
36+
task "bundle:update" => [:chdir] do
37+
sh %Q{bundle update && bundle install --standalone --clean} do |ok, res|
3838
if ! ok
3939
puts "fail to update gems (status = #{res.exitstatus})"
4040
end
@@ -43,16 +43,23 @@ end
4343

4444
desc "Install to Alfred"
4545
task :install => [:config] do
46-
ln_sf File.realpath($config["path"]), File.join(workflow_home, $config["bundleid"])
46+
ln_sf File.expand_path($config["path"]), File.join(workflow_home, $config["bundleid"])
4747
end
4848

4949
desc "Unlink from Alfred"
5050
task :uninstall => [:config] do
5151
rm File.join(workflow_home, $config["bundleid"])
5252
end
5353

54+
55+
56+
57+
desc "Clean up all the extras"
58+
task :clean => [:config] do
59+
end
60+
5461
desc "Remove any generated file"
55-
task :clobber => [:config] do
62+
task :clobber => [:clean] do
5663
rmtree File.join($config["path"], ".bundle")
5764
rmtree File.join($config["path"], "bundle")
5865
end

alfred2-ruby-template.alfredworkflow

15.5 MB
Binary file not shown.

config.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# bundle_id = "domain.id"
2+
# path is the relative path to the workflow in the project root
13
---
24
path: workflow
3-
# bundle id = domain.id
45
domain: me.zhaowu
56
id: alfred2-ruby-template
67

workflow/Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
alfred-workflow (1.1.1)
4+
alfred-workflow (1.2.6)
55
logging (~> 1.8.0)
66
plist (~> 3.1.0)
77
little-plugger (1.1.3)
@@ -15,6 +15,6 @@ PLATFORMS
1515
ruby
1616

1717
DEPENDENCIES
18-
alfred-workflow (>= 1.0.1)
18+
alfred-workflow (>= 1.1.1)
1919
logging
2020
plist

workflow/info.plist

+46-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
<string></string>
1818
</dict>
1919
</array>
20+
<key>BA17BE41-176A-4E9A-B958-A3F0BEA36829</key>
21+
<array>
22+
<dict>
23+
<key>destinationuid</key>
24+
<string>42D05639-3F0F-4760-8214-317167E4173D</string>
25+
<key>modifiers</key>
26+
<integer>0</integer>
27+
<key>modifiersubtext</key>
28+
<string></string>
29+
</dict>
30+
</array>
2031
</dict>
2132
<key>createdby</key>
2233
<string>Zhao Cai</string>
@@ -36,15 +47,15 @@
3647
<key>escaping</key>
3748
<integer>62</integer>
3849
<key>keyword</key>
39-
<string>atest</string>
50+
<string>test feedback</string>
4051
<key>runningsubtext</key>
4152
<string>working...</string>
4253
<key>script</key>
4354
<string>/usr/bin/ruby ./main.rb {query}</string>
4455
<key>subtext</key>
45-
<string>Test Feedback</string>
46-
<key>title</key>
4756
<string>Ruby Based Workflow Template</string>
57+
<key>title</key>
58+
<string>Test Alfred Feedback</string>
4859
<key>type</key>
4960
<integer>0</integer>
5061
<key>withspace</key>
@@ -78,6 +89,33 @@
7889
<key>uid</key>
7990
<string>42D05639-3F0F-4760-8214-317167E4173D</string>
8091
</dict>
92+
<dict>
93+
<key>config</key>
94+
<dict>
95+
<key>argumenttype</key>
96+
<integer>2</integer>
97+
<key>escaping</key>
98+
<integer>63</integer>
99+
<key>keyword</key>
100+
<string>test rescue feedback</string>
101+
<key>runningsubtext</key>
102+
<string>...</string>
103+
<key>script</key>
104+
<string>/usr/bin/ruby ./main_with_rescue_feedback.rb {query}</string>
105+
<key>subtext</key>
106+
<string>Ruby Based Workflow Template</string>
107+
<key>title</key>
108+
<string>Test Alfred Rescue Feedback</string>
109+
<key>type</key>
110+
<integer>0</integer>
111+
<key>withspace</key>
112+
<false/>
113+
</dict>
114+
<key>type</key>
115+
<string>alfred.workflow.input.scriptfilter</string>
116+
<key>uid</key>
117+
<string>BA17BE41-176A-4E9A-B958-A3F0BEA36829</string>
118+
</dict>
81119
</array>
82120
<key>readme</key>
83121
<string></string>
@@ -93,6 +131,11 @@
93131
<key>ypos</key>
94132
<real>140</real>
95133
</dict>
134+
<key>BA17BE41-176A-4E9A-B958-A3F0BEA36829</key>
135+
<dict>
136+
<key>ypos</key>
137+
<real>370</real>
138+
</dict>
96139
</dict>
97140
<key>webaddress</key>
98141
<string>http://github.com/zhaocai/alfred2-ruby-template</string>

workflow/main.rb

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
require "bundle/bundler/setup"
66
require "alfred"
77

8+
9+
10+
811
Alfred.with_friendly_error do |alfred|
912
fb = alfred.feedback
1013

@@ -19,7 +22,21 @@
1922
:arg => "A test feedback Item" ,
2023
:valid => "yes" ,
2124
})
25+
26+
# add an feedback to test rescue feedback
27+
fb.add_item({
28+
:uid => "" ,
29+
:title => "Rescue Feedback Test" ,
30+
:subtitle => "rescue feedback item" ,
31+
:arg => "" ,
32+
:autocomplete => "failed" ,
33+
:valid => "no" ,
34+
})
2235

36+
if ARGV[0].eql? "failed"
37+
alfred.with_rescue_feedback = true
38+
raise Alfred::NoBundleIDError, "Wrong Bundle ID Test!"
39+
end
2340

2441
puts fb.to_xml(ARGV)
2542
end

workflow/main_with_rescue_feedback.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env ruby
2+
# encoding: utf-8
3+
4+
require 'rubygems' unless defined? Gem
5+
require "bundle/bundler/setup"
6+
require "alfred"
7+
8+
9+
def something_goes_wrong
10+
true
11+
end
12+
13+
Alfred.with_friendly_error do |alfred|
14+
alfred.with_rescue_feedback = true
15+
16+
fb = alfred.feedback
17+
18+
if something_goes_wrong
19+
raise Alfred::NoBundleIDError, "Wrong Bundle ID Test!"
20+
end
21+
end
22+
23+
24+

0 commit comments

Comments
 (0)