maid 0.9.0.alpha.2 → 0.10.0.pre.alpha.2
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.
- checksums.yaml +4 -4
- data/.act-env +1 -0
- data/.act-secrets.example +2 -0
- data/.github/workflows/coverage.yml +29 -0
- data/.github/workflows/lint.yml +24 -0
- data/.github/workflows/release.yml +62 -0
- data/.github/workflows/test.yml +2 -2
- data/.gitignore +2 -0
- data/.release-please-manifest.json +3 -0
- data/.rubocop.yml +35 -0
- data/.rubocop_todo.yml +372 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +31 -0
- data/Gemfile +1 -1
- data/Guardfile +31 -4
- data/README.md +3 -3
- data/Rakefile +1 -1
- data/Vagrantfile +2 -2
- data/lib/maid/app.rb +48 -51
- data/lib/maid/maid.rb +38 -38
- data/lib/maid/numeric_extensions.rb +26 -25
- data/lib/maid/platform.rb +1 -1
- data/lib/maid/rake/task.rb +1 -1
- data/lib/maid/repeat.rb +8 -8
- data/lib/maid/rule_container.rb +3 -3
- data/lib/maid/rules.sample.rb +17 -17
- data/lib/maid/tools.rb +142 -127
- data/lib/maid/trash_migration.rb +4 -4
- data/lib/maid/user_agent.rb +2 -2
- data/lib/maid/version.rb +5 -2
- data/lib/maid/watch.rb +10 -12
- data/maid.gemspec +36 -18
- data/spec/dependency_spec.rb +9 -8
- data/spec/lib/maid/app_spec.rb +31 -23
- data/spec/lib/maid/maid_spec.rb +74 -52
- data/spec/lib/maid/numeric_extensions_spec.rb +1 -1
- data/spec/lib/maid/platform_spec.rb +1 -1
- data/spec/lib/maid/rake/single_rule_spec.rb +11 -6
- data/spec/lib/maid/rake/task_spec.rb +3 -5
- data/spec/lib/maid/rule_spec.rb +1 -1
- data/spec/lib/maid/tools_spec.rb +94 -92
- data/spec/lib/maid/trash_migration_spec.rb +12 -11
- data/spec/lib/maid/user_agent_spec.rb +5 -4
- data/spec/lib/maid_spec.rb +1 -1
- data/spec/spec_helper.rb +19 -3
- metadata +177 -61
data/Vagrantfile
CHANGED
@@ -3,12 +3,12 @@
|
|
3
3
|
|
4
4
|
Vagrant.configure('2') do |config|
|
5
5
|
# See also: `script/vagrant-test`, `script/vagrant-test-all`
|
6
|
-
config.vm.box
|
6
|
+
config.vm.box = ENV['MAID_TARGET_BOX'] || 'hashicorp/precise64'
|
7
7
|
|
8
8
|
config.vm.provider :virtualbox do |vb|
|
9
9
|
# Maid has very low system requirements
|
10
10
|
vb.customize ['modifyvm', :id, '--cpus', 1, '--memory', 192]
|
11
11
|
end
|
12
12
|
|
13
|
-
config.vm.provision(:shell, :
|
13
|
+
config.vm.provision(:shell, path: 'script/vagrant-provision', args: ENV['MAID_TARGET_RUBY'] || '1.9.3')
|
14
14
|
end
|
data/lib/maid/app.rb
CHANGED
@@ -8,18 +8,18 @@ class Maid::App < Thor
|
|
8
8
|
|
9
9
|
desc 'introduction', 'Become aquainted with maid'
|
10
10
|
def introduction
|
11
|
-
say
|
12
|
-
#{Maid::UserAgent.short}
|
13
|
-
#{'=' * Maid::UserAgent.short.length}
|
11
|
+
say <<~EOF
|
12
|
+
#{Maid::UserAgent.short}
|
13
|
+
#{'=' * Maid::UserAgent.short.length}
|
14
14
|
|
15
|
-
#{Maid::SUMMARY}
|
15
|
+
#{Maid::SUMMARY}
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
* Tutorial: https://github.com/benjaminoakes/maid#tutorial
|
18
|
+
* Community & examples: https://github.com/benjaminoakes/maid/wiki
|
19
|
+
* Documentation: http://www.rubydoc.info/gems/maid/#{Maid::VERSION}/Maid/Tools
|
20
20
|
|
21
|
-
For more information, run "maid help".
|
22
|
-
EOF
|
21
|
+
For more information, run "maid help".
|
22
|
+
EOF
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.sample_rules_path
|
@@ -27,10 +27,10 @@ EOF
|
|
27
27
|
end
|
28
28
|
|
29
29
|
desc 'clean', 'Clean based on rules'
|
30
|
-
method_option :rules, :
|
31
|
-
method_option :noop, :
|
32
|
-
method_option :force, :
|
33
|
-
method_option :silent, :
|
30
|
+
method_option :rules, type: :string, aliases: %w[-r]
|
31
|
+
method_option :noop, type: :boolean, aliases: %w[-n --dry-run]
|
32
|
+
method_option :force, type: :boolean, aliases: %w[-f]
|
33
|
+
method_option :silent, type: :boolean, aliases: %w[-s]
|
34
34
|
def clean
|
35
35
|
maid = Maid::Maid.new(maid_options(options))
|
36
36
|
|
@@ -43,16 +43,14 @@ EOF
|
|
43
43
|
return
|
44
44
|
end
|
45
45
|
|
46
|
-
unless options.silent? || options.noop?
|
47
|
-
say "Logging actions to #{ maid.log_device.inspect }"
|
48
|
-
end
|
46
|
+
say "Logging actions to #{maid.log_device.inspect}" unless options.silent? || options.noop?
|
49
47
|
|
50
48
|
maid.load_rules
|
51
49
|
maid.clean
|
52
50
|
end
|
53
51
|
|
54
52
|
desc 'version', 'Print version information (optionally: system info)'
|
55
|
-
method_option :long, :
|
53
|
+
method_option :long, type: :boolean, aliases: %w[-l]
|
56
54
|
def version
|
57
55
|
if options.long?
|
58
56
|
say Maid::UserAgent.value
|
@@ -64,53 +62,49 @@ EOF
|
|
64
62
|
# Since this happens a lot by mistake
|
65
63
|
map '--version' => :version
|
66
64
|
|
67
|
-
desc 'sample', "Create sample rules at #{
|
65
|
+
desc 'sample', "Create sample rules at #{sample_rules_path}"
|
68
66
|
def sample
|
69
67
|
path = self.class.sample_rules_path
|
70
68
|
|
71
69
|
FileUtils.mkdir_p(File.dirname(path))
|
72
70
|
File.open(path, 'w').puts(File.read(File.join(File.dirname(__FILE__), 'rules.sample.rb')))
|
73
71
|
|
74
|
-
say "Sample rules created at #{
|
72
|
+
say "Sample rules created at #{path.inspect}", :green
|
75
73
|
end
|
76
|
-
|
74
|
+
|
77
75
|
desc 'daemon', 'Runs the watch/repeat rules in a daemon'
|
78
|
-
method_option :rules, :
|
79
|
-
method_option :silent, :
|
76
|
+
method_option :rules, type: :string, aliases: %w[-r]
|
77
|
+
method_option :silent, type: :boolean, aliases: %w[-s]
|
80
78
|
def daemon
|
81
79
|
maid = Maid::Maid.new(maid_options(options))
|
82
|
-
|
80
|
+
|
83
81
|
if Maid::TrashMigration.needed?
|
84
82
|
migrate_trash
|
85
83
|
return
|
86
84
|
end
|
87
85
|
|
88
|
-
unless options.silent?
|
89
|
-
|
90
|
-
end
|
91
|
-
|
86
|
+
say "Logging actions to #{maid.log_device.inspect}" unless options.silent?
|
87
|
+
|
92
88
|
maid.load_rules
|
93
89
|
maid.daemonize
|
94
90
|
end
|
95
91
|
|
96
92
|
desc 'logs', 'Manages logs written by maid'
|
97
|
-
method_option :path, :
|
98
|
-
method_option :tail, :
|
93
|
+
method_option :path, type: :boolean, aliases: %w[-p]
|
94
|
+
method_option :tail, type: :boolean, aliases: %w[-t]
|
99
95
|
def logs
|
100
96
|
maid = Maid::Maid.new(maid_options(options))
|
101
97
|
|
102
98
|
if options.path?
|
103
99
|
say maid.log_device
|
104
|
-
|
105
|
-
|
106
|
-
|
100
|
+
elsif File.readable?(maid.log_device)
|
101
|
+
if options.tail?
|
102
|
+
system("tail -f #{maid.log_device}")
|
107
103
|
else
|
108
|
-
|
109
|
-
system("tail -f #{maid.log_device}")
|
110
|
-
else
|
111
|
-
say `tail #{maid.log_device}`
|
112
|
-
end
|
104
|
+
say `tail #{maid.log_device}`
|
113
105
|
end
|
106
|
+
else
|
107
|
+
error "Log file #{maid.log_device} does not exist."
|
114
108
|
end
|
115
109
|
end
|
116
110
|
|
@@ -120,18 +114,16 @@ EOF
|
|
120
114
|
|
121
115
|
if options['noop']
|
122
116
|
# You're testing, so a simple log goes to STDOUT and no actions are taken
|
123
|
-
h[:file_options] = { :
|
117
|
+
h[:file_options] = { noop: true }
|
124
118
|
|
125
119
|
unless options['silent']
|
126
120
|
h[:logger] = false
|
127
121
|
h[:log_device] = STDOUT
|
128
|
-
h[:log_formatter] =
|
122
|
+
h[:log_formatter] = ->(_, _, _, msg) { "#{msg}\n" }
|
129
123
|
end
|
130
124
|
end
|
131
125
|
|
132
|
-
if options['rules']
|
133
|
-
h[:rules_path] = options['rules']
|
134
|
-
end
|
126
|
+
h[:rules_path] = options['rules'] if options['rules']
|
135
127
|
|
136
128
|
h
|
137
129
|
end
|
@@ -144,15 +136,20 @@ EOF
|
|
144
136
|
migration = Maid::TrashMigration
|
145
137
|
banner('Trash Migration', :yellow)
|
146
138
|
|
147
|
-
say
|
139
|
+
say <<~EOF
|
148
140
|
|
149
|
-
You are using Linux and have a "~/.Trash" directory. If you used Maid 0.1.2 or earlier, that directory may exist because Maid incorrectly moved trash files there.
|
141
|
+
You are using Linux and have a "~/.Trash" directory. If you used Maid 0.1.2 or earlier, that directory may exist because Maid incorrectly moved trash files there.
|
150
142
|
|
151
|
-
But no worries. Maid can migrate those files to the correct place.
|
143
|
+
But no worries. Maid can migrate those files to the correct place.
|
152
144
|
|
153
145
|
EOF
|
154
146
|
|
155
|
-
response = ask(
|
147
|
+
response = ask(
|
148
|
+
"Would you like Maid to move the files in #{migration.incorrect_trash.inspect} " \
|
149
|
+
"to #{migration.correct_trash.inspect}?", limited_to: %w[
|
150
|
+
Y N
|
151
|
+
],
|
152
|
+
)
|
156
153
|
|
157
154
|
case response
|
158
155
|
when 'Y'
|
@@ -163,16 +160,16 @@ But no worries. Maid can migrate those files to the correct place.
|
|
163
160
|
|
164
161
|
say('Migrated. See the Maid log for details.')
|
165
162
|
when 'N'
|
166
|
-
say
|
163
|
+
say <<~EOF
|
167
164
|
|
168
|
-
Running Maid again will continue to give this warning until #{
|
165
|
+
Running Maid again will continue to give this warning until #{migration.incorrect_trash.inspect} no longer exists, or the environment variable MAID_NO_MIGRATE_TRASH has a value.
|
169
166
|
|
170
|
-
Exiting...
|
167
|
+
Exiting...
|
171
168
|
EOF
|
172
169
|
|
173
|
-
exit
|
170
|
+
exit(-1)
|
174
171
|
else
|
175
|
-
raise "Reached 'impossible' case (response: #{
|
172
|
+
raise "Reached 'impossible' case (response: #{response.inspect})"
|
176
173
|
end
|
177
174
|
end
|
178
175
|
|
data/lib/maid/maid.rb
CHANGED
@@ -9,18 +9,20 @@ require 'xdg'
|
|
9
9
|
class Maid::Maid
|
10
10
|
include Maid::RuleContainer
|
11
11
|
DEFAULTS = {
|
12
|
-
:
|
12
|
+
progname: 'Maid',
|
13
13
|
|
14
|
-
:
|
15
|
-
# We don't want the log files to grow without check, but 50 MB doesn't seem
|
16
|
-
|
17
|
-
:
|
14
|
+
log_device: File.expand_path('~/.maid/maid.log'),
|
15
|
+
# We don't want the log files to grow without check, but 50 MB doesn't seem
|
16
|
+
# too bad. (We're going with a larger size just for safety right now.)
|
17
|
+
log_shift_age: 5,
|
18
|
+
log_shift_size: 10 * 1_048_576, # 10 * 1 MB
|
18
19
|
|
19
|
-
:
|
20
|
-
:
|
20
|
+
rules_path: File.expand_path('~/.maid/rules.rb'),
|
21
|
+
file_options: { noop: false }, # for `FileUtils`
|
21
22
|
}.freeze
|
22
23
|
|
23
24
|
attr_reader :file_options, :logger, :log_device, :rules_path, :trash_path, :watches, :repeats
|
25
|
+
|
24
26
|
include ::Maid::Tools
|
25
27
|
|
26
28
|
# Make a new Maid, setting up paths for the log and trash.
|
@@ -30,16 +32,16 @@ class Maid::Maid
|
|
30
32
|
# Maid::Maid.new(:log_device => '/home/username/log/maid.log', :trash_path => '/home/username/my_trash')
|
31
33
|
#
|
32
34
|
def initialize(options = {})
|
33
|
-
options = DEFAULTS.merge(options.reject { |
|
35
|
+
options = DEFAULTS.merge(options.reject { |_k, v| v.nil? })
|
34
36
|
|
35
37
|
# TODO: Refactor and simplify (see also https://github.com/benjaminoakes/maid/pull/48#discussion_r1683942)
|
36
|
-
@logger =
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
@logger = if options[:logger]
|
39
|
+
options[:logger]
|
40
|
+
else
|
41
|
+
@log_device = options[:log_device]
|
42
|
+
FileUtils.mkdir_p(File.dirname(@log_device)) unless @log_device.is_a?(IO)
|
43
|
+
@logger = Logger.new(@log_device, options[:log_shift_age], options[:log_shift_size])
|
44
|
+
end
|
43
45
|
|
44
46
|
@logger.progname = options[:progname]
|
45
47
|
@logger.formatter = options[:log_formatter] if options[:log_formatter]
|
@@ -59,16 +61,16 @@ class Maid::Maid
|
|
59
61
|
|
60
62
|
# Start cleaning, based on the rules defined at rules_path.
|
61
63
|
def clean
|
62
|
-
unless @log_device.
|
63
|
-
@logger.info "v#{
|
64
|
+
unless @log_device.is_a?(IO)
|
65
|
+
@logger.info "v#{Maid::VERSION}"
|
64
66
|
@logger.info 'Started'
|
65
67
|
end
|
66
68
|
|
67
69
|
follow_rules
|
68
70
|
|
69
|
-
|
70
|
-
|
71
|
-
|
71
|
+
return if @log_device.is_a?(IO)
|
72
|
+
|
73
|
+
@logger.info 'Finished'
|
72
74
|
end
|
73
75
|
|
74
76
|
# Add the rules at rules_path.
|
@@ -82,26 +84,26 @@ class Maid::Maid
|
|
82
84
|
Kernel.load(path)
|
83
85
|
end
|
84
86
|
rescue LoadError => e
|
85
|
-
|
87
|
+
warn e.message
|
86
88
|
end
|
87
89
|
|
88
|
-
def watch(path, options = {}, &
|
89
|
-
@watches << ::Maid::Watch.new(self, path, options, &
|
90
|
+
def watch(path, options = {}, &block)
|
91
|
+
@watches << ::Maid::Watch.new(self, path, options, &block)
|
90
92
|
end
|
91
93
|
|
92
|
-
def repeat(timestring, options = {}, &
|
93
|
-
@repeats << ::Maid::Repeat.new(self, timestring, options, &
|
94
|
+
def repeat(timestring, options = {}, &block)
|
95
|
+
@repeats << ::Maid::Repeat.new(self, timestring, options, &block)
|
94
96
|
end
|
95
97
|
|
96
98
|
# Daemonizes the process by starting all watches and repeats and joining
|
97
99
|
# the threads of the schedulers/watchers
|
98
100
|
def daemonize
|
99
101
|
if @watches.empty? && @repeats.empty?
|
100
|
-
|
102
|
+
warn 'Cannot run daemon. Nothing to watch or repeat.'
|
101
103
|
else
|
102
104
|
all = @watches + @repeats
|
103
105
|
all.each(&:run)
|
104
|
-
trap(
|
106
|
+
trap('SIGINT') do
|
105
107
|
# Running in a thread fixes celluloid ThreadError
|
106
108
|
Thread.new do
|
107
109
|
all.each(&:stop)
|
@@ -115,18 +117,16 @@ class Maid::Maid
|
|
115
117
|
# Run a shell command.
|
116
118
|
#--
|
117
119
|
# Delegates to `Kernel.\``. Made primarily for testing other commands and some error handling.
|
118
|
-
def cmd(command)
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
raise NotImplementedError, "Unsupported system command: #{ command.inspect }"
|
123
|
-
end
|
120
|
+
def cmd(command) # :nodoc:
|
121
|
+
raise NotImplementedError, "Unsupported system command: #{command.inspect}" unless supported_command?(command)
|
122
|
+
|
123
|
+
`#{command}`
|
124
124
|
end
|
125
125
|
|
126
126
|
private
|
127
127
|
|
128
128
|
# Does the OS support this command?
|
129
|
-
def supported_command?(command)
|
129
|
+
def supported_command?(command) # :nodoc:
|
130
130
|
@@supported_commands ||= {}
|
131
131
|
|
132
132
|
command_name = command.strip.split(/\s+/)[0]
|
@@ -134,20 +134,20 @@ class Maid::Maid
|
|
134
134
|
# TODO: Instead of using `which`, use an alternative listed at:
|
135
135
|
#
|
136
136
|
# http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
|
137
|
-
@@supported_commands[command_name] = supported
|
137
|
+
@@supported_commands[command_name] = supported || !`which #{command_name}`.empty?
|
138
138
|
end
|
139
139
|
|
140
140
|
def default_trash_path
|
141
141
|
# TODO: Refactor module declaration so this can be `Platform`
|
142
142
|
if Maid::Platform.linux?
|
143
143
|
# See the [FreeDesktop.org Trash specification](http://www.ramendik.ru/docs/trashspec.html)
|
144
|
-
path = "#{
|
144
|
+
path = "#{XDG['DATA_HOME']}/Trash/files"
|
145
145
|
elsif Maid::Platform.osx?
|
146
146
|
path = File.expand_path('~/.Trash')
|
147
147
|
else
|
148
|
-
raise NotImplementedError, "Unknown default trash path (unsupported host OS: #{
|
148
|
+
raise NotImplementedError, "Unknown default trash path (unsupported host OS: #{Maid::Platform.host_os.inspect})"
|
149
149
|
end
|
150
150
|
|
151
|
-
"#{
|
151
|
+
"#{path}/"
|
152
152
|
end
|
153
153
|
end
|
@@ -33,32 +33,32 @@ module Maid::NumericExtensions
|
|
33
33
|
def seconds
|
34
34
|
self
|
35
35
|
end
|
36
|
-
alias
|
36
|
+
alias second seconds
|
37
37
|
|
38
38
|
def minutes
|
39
39
|
self * 60
|
40
40
|
end
|
41
|
-
alias
|
41
|
+
alias minute minutes
|
42
42
|
|
43
43
|
def hours
|
44
44
|
self * 3600
|
45
45
|
end
|
46
|
-
alias
|
46
|
+
alias hour hours
|
47
47
|
|
48
48
|
def days
|
49
49
|
self * 24.hours
|
50
50
|
end
|
51
|
-
alias
|
51
|
+
alias day days
|
52
52
|
|
53
53
|
def weeks
|
54
54
|
self * 7.days
|
55
55
|
end
|
56
|
-
alias
|
56
|
+
alias week weeks
|
57
57
|
|
58
58
|
def fortnights
|
59
59
|
self * 2.weeks
|
60
60
|
end
|
61
|
-
alias
|
61
|
+
alias fortnight fortnights
|
62
62
|
|
63
63
|
# Reads best without arguments: 10.minutes.ago
|
64
64
|
def ago(time = ::Time.now)
|
@@ -66,7 +66,7 @@ module Maid::NumericExtensions
|
|
66
66
|
end
|
67
67
|
|
68
68
|
# Reads best with argument: 10.minutes.until(time)
|
69
|
-
alias
|
69
|
+
alias until ago
|
70
70
|
|
71
71
|
# Reads best with argument: 10.minutes.since(time)
|
72
72
|
def since(time = ::Time.now)
|
@@ -74,21 +74,22 @@ module Maid::NumericExtensions
|
|
74
74
|
end
|
75
75
|
|
76
76
|
# Reads best without arguments: 10.minutes.from_now
|
77
|
-
alias
|
77
|
+
alias from_now since
|
78
78
|
|
79
79
|
######################
|
80
80
|
### Maid additions ###
|
81
81
|
######################
|
82
82
|
|
83
|
-
# TODO find a better place for these to live?
|
83
|
+
# TODO: find a better place for these to live?
|
84
84
|
|
85
85
|
# Reads well in a case like:
|
86
86
|
#
|
87
87
|
# 1.week.since? accessed_at('filename')
|
88
88
|
def since?(other_time)
|
89
|
-
other_time <
|
89
|
+
other_time < ago
|
90
90
|
end
|
91
91
|
end
|
92
|
+
|
92
93
|
module SizeToKb
|
93
94
|
# Enables Computer disk size conversion into kilobytes.
|
94
95
|
#
|
@@ -100,29 +101,29 @@ module Maid::NumericExtensions
|
|
100
101
|
def kb
|
101
102
|
self
|
102
103
|
end
|
103
|
-
alias
|
104
|
-
alias
|
105
|
-
alias
|
104
|
+
alias kilobytes kb
|
105
|
+
alias kilobyte kb
|
106
|
+
alias kB kb
|
106
107
|
|
107
108
|
def mb
|
108
|
-
self * 1024
|
109
|
+
self * (1024**1)
|
109
110
|
end
|
110
|
-
alias
|
111
|
-
alias
|
112
|
-
alias
|
111
|
+
alias megabytes mb
|
112
|
+
alias megabyte mb
|
113
|
+
alias mB mb
|
113
114
|
|
114
115
|
def gb
|
115
|
-
self * 1024
|
116
|
+
self * (1024**2)
|
116
117
|
end
|
117
|
-
alias
|
118
|
-
alias
|
119
|
-
alias
|
118
|
+
alias gigabytes gb
|
119
|
+
alias gigabyte gb
|
120
|
+
alias gB gb
|
120
121
|
|
121
122
|
def tb
|
122
|
-
self * 1024
|
123
|
+
self * (1024**3)
|
123
124
|
end
|
124
|
-
alias
|
125
|
-
alias
|
126
|
-
alias
|
125
|
+
alias terabytes tb
|
126
|
+
alias terabyte tb
|
127
|
+
alias tB tb
|
127
128
|
end
|
128
129
|
end
|
data/lib/maid/platform.rb
CHANGED
data/lib/maid/rake/task.rb
CHANGED
data/lib/maid/repeat.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
require 'rufus-scheduler'
|
2
2
|
class Maid::Repeat
|
3
3
|
include Maid::RuleContainer
|
4
|
-
|
4
|
+
|
5
5
|
attr_reader :timestring, :scheduler, :logger
|
6
|
-
|
7
|
-
def initialize(maid, timestring, options = {}, &
|
6
|
+
|
7
|
+
def initialize(maid, timestring, options = {}, &block)
|
8
8
|
@maid = maid
|
9
9
|
@logger = maid.logger # TODO: Maybe it's better to create seperate loggers?
|
10
10
|
@scheduler = Rufus::Scheduler.singleton
|
11
11
|
@timestring = timestring
|
12
12
|
@options = options
|
13
|
-
initialize_rules(&
|
13
|
+
initialize_rules(&block)
|
14
14
|
end
|
15
15
|
|
16
16
|
def run
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
return if rules.empty?
|
18
|
+
|
19
|
+
@scheduler.repeat(timestring, @options) { follow_rules }
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def stop
|
23
23
|
@scheduler.shutdown(:join) # Join the work threads
|
24
24
|
end
|
data/lib/maid/rule_container.rb
CHANGED
@@ -3,9 +3,9 @@ module Maid::RuleContainer
|
|
3
3
|
attr_reader :rules
|
4
4
|
|
5
5
|
# initialize_rules
|
6
|
-
def initialize_rules(&
|
6
|
+
def initialize_rules(&block)
|
7
7
|
@rules ||= []
|
8
|
-
instance_exec(&
|
8
|
+
instance_exec(&block)
|
9
9
|
end
|
10
10
|
|
11
11
|
# Register a rule with a description and instructions (lambda function).
|
@@ -16,7 +16,7 @@ module Maid::RuleContainer
|
|
16
16
|
# Follow all registered rules.
|
17
17
|
def follow_rules(*args)
|
18
18
|
@rules.each do |rule|
|
19
|
-
@logger.info("Rule: #{
|
19
|
+
@logger.info("Rule: #{rule.description}") unless @logger.nil?
|
20
20
|
rule.follow(*args)
|
21
21
|
end
|
22
22
|
end
|
data/lib/maid/rules.sample.rb
CHANGED
@@ -8,12 +8,14 @@
|
|
8
8
|
# what they do, you might run into unwanted results!
|
9
9
|
#
|
10
10
|
# Don't forget, it's just Ruby! You can define custom methods and use them below:
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# def magic(*)
|
13
13
|
# # ...
|
14
14
|
# end
|
15
|
-
#
|
16
|
-
# If you come up with some cool tools of your own, please send me a pull
|
15
|
+
#
|
16
|
+
# If you come up with some cool tools of your own, please send me a pull
|
17
|
+
# request on GitHub! Also, please consider sharing your rules with others via
|
18
|
+
# [the wiki](https://github.com/benjaminoakes/maid/wiki).
|
17
19
|
#
|
18
20
|
# For more help on Maid:
|
19
21
|
#
|
@@ -39,37 +41,35 @@ Maid.rules do
|
|
39
41
|
end
|
40
42
|
|
41
43
|
rule 'Mac OS X applications in zip files' do
|
42
|
-
found = dir('~/Downloads/*.zip').select
|
43
|
-
zipfile_contents(path).any? { |c| c.match(
|
44
|
-
|
44
|
+
found = dir('~/Downloads/*.zip').select do |path|
|
45
|
+
zipfile_contents(path).any? { |c| c.match(%r{\.app/Contents/}) }
|
46
|
+
end
|
45
47
|
|
46
48
|
trash(found)
|
47
49
|
end
|
48
50
|
|
49
51
|
rule 'Misc Screenshots' do
|
50
52
|
dir('~/Desktop/Screen shot *').each do |path|
|
51
|
-
if 1.week.since?(accessed_at(path))
|
52
|
-
move(path, '~/Documents/Misc Screenshots/')
|
53
|
-
end
|
53
|
+
move(path, '~/Documents/Misc Screenshots/') if 1.week.since?(accessed_at(path))
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
57
|
# NOTE: Currently, only Mac OS X supports `duration_s`.
|
58
58
|
rule 'MP3s likely to be music' do
|
59
59
|
dir('~/Downloads/*.mp3').each do |path|
|
60
|
-
if duration_s(path) > 30.0
|
61
|
-
move(path, '~/Music/iTunes/iTunes Media/Automatically Add to iTunes/')
|
62
|
-
end
|
60
|
+
move(path, '~/Music/iTunes/iTunes Media/Automatically Add to iTunes/') if duration_s(path) > 30.0
|
63
61
|
end
|
64
62
|
end
|
65
|
-
|
63
|
+
|
66
64
|
# NOTE: Currently, only Mac OS X supports `downloaded_from`.
|
67
65
|
rule 'Old files downloaded while developing/testing' do
|
68
66
|
dir('~/Downloads/*').each do |path|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
67
|
+
next unless downloaded_from(path).any? do |u|
|
68
|
+
u.match('http://localhost') || u.match('http://staging\.yourcompany\.com')
|
69
|
+
end &&
|
70
|
+
1.week.since?(accessed_at(path))
|
71
|
+
|
72
|
+
trash(path)
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|