Class: Winever::TaskManager

Inherits:
Object
  • Object
show all
Defined in:
lib/winever/task_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TaskManager

Returns a new instance of TaskManager.



26
27
28
29
30
31
32
# File 'lib/winever/task_manager.rb', line 26

def initialize options={}
  if !has_task_scheduler?
    raise "Cannot use win32/taskscheduler on this system. Are you on windows?"
  end

  @options = options
end

Class Method Details

.clear_tasks(options = {}) ⇒ Object



18
19
20
# File 'lib/winever/task_manager.rb', line 18

def self.clear_tasks options={}
  new(options).clear_tasks_except
end

.has_task_scheduler?Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
# File 'lib/winever/task_manager.rb', line 3

def self.has_task_scheduler?
  return @has_task_scheduler unless @has_task_scheduler.nil?
  begin
    require 'win32/taskscheduler'
    @has_task_scheduler = true
  rescue LoadError => e
    @has_task_scheduler = false
  end
  @has_task_scheduler
end

.update_tasks(options = {}) ⇒ Object



22
23
24
# File 'lib/winever/task_manager.rb', line 22

def self.update_tasks options={}
  new(options).update_tasks
end

Instance Method Details

#clear_tasks_except(keep_tasks = []) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/winever/task_manager.rb', line 85

def clear_tasks_except keep_tasks=[]
  ts = Win32::TaskScheduler.new
  task_names = ts.tasks.select{|tn| tn.end_with?('.' + identifier)}
  task_names.concat(Winever::WheneverInterface::existing_tasks_to_remove)

  task_names = task_names.reject{|tn| keep_tasks.include?(tn)}

  task_names.each{|tn| ts.delete(tn) if ts.exists?(tn)}
end

#create_task(cron_entry) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/winever/task_manager.rb', line 95

def create_task cron_entry
  return if self.password.nil?

  task_name = generate_task_name(cron_entry.task_name)

  # Replacing the /dev/null by NUL
  parameters = cron_entry.parameters.gsub(/([\s'"])\/dev\/null([\s'"])/, '\1NUL\2')

  pw = password
  trigger = cron_entry.triggers.first
  work_directory = cron_entry.working_directory

  ts = Win32::TaskScheduler.new(nil, nil, cron_entry.task_folder, true)
  begin
    ts.password = pw
    ts.new_work_item(task_name, trigger)
    ts.application_name = 'cmd'
    ts.parameters = '/C ' + parameters
    ts.working_directory = work_directory
    ts.activate(task_name)
  rescue
    raise 'Failed at setting the task up. It might have been partially created/updated. This most likely means a bad password was entered.'
  end

  task_name
end

#create_tasksObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/winever/task_manager.rb', line 64

def create_tasks
  return if self.password.nil?

  cron_entries = Winever::WheneverInterface.valid_cron_entries(@options)

  created_task_names = []
  cron_entries.each do |cron_entry|
    created_task_names << create_task(cron_entry)
  end
  created_task_names
end

#generate_task_name(task_name) ⇒ Object



122
123
124
# File 'lib/winever/task_manager.rb', line 122

def generate_task_name task_name
  "#{task_name}.#{identifier}"
end

#has_task_scheduler?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/winever/task_manager.rb', line 14

def has_task_scheduler?
  self.class.has_task_scheduler?
end

#identifierObject



126
127
128
129
130
131
# File 'lib/winever/task_manager.rb', line 126

def identifier
  # Removing the characters blocked by the windows file system. The single quote is just for simplicity.
  iden = @options[:identifier].gsub(/[:\/\\<>:"|?*']/, '_')
  raise 'Identifier must contain at least one letter or number.' unless iden =~ /\w/
  iden
end

#passwordObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/winever/task_manager.rb', line 34

def password
  return @options[:password] if @options.has_key?(:password)
  return ENV['WINEVER_PASSWORD'] if ENV['WINEVER_PASSWORD'] && !ENV['WINEVER_PASSWORD'].empty?

  return @password.empty? ? nil : @password if @password
  require 'highline/import'
  prompt = "    To setup tasks, the password of the current user account is needed.\n    Enter the password of the current windows user (or leave blank to avoid doing the changes):\n  PRMP\n\n  pw = ask(prompt){|q| q.echo = false}\n  while pw && !pw.empty? && !validate_password(pw)\n    prompt = <<-PRMP.gsub(/^ +/, '')\n      Invalid password entered.\n      Enter the password of the current windows user (or leave blank to avoid doing the changes):\n    PRMP\n    pw = ask(prompt){|q| q.echo = false}\n  end\n\n\n  #TODO get the password somehow.\n  # require File.expand_path('../../extensions/password', __FILE__)\n  # password = Password.ask(\"Enter password for current user account of the machine to setup tasks: \")\n  # validate_password(password)\n\n  @password = pw\n  pw.empty? ? nil : pw\nend\n".gsub(/^ +/, '')

#update_tasksObject



76
77
78
79
80
81
82
83
# File 'lib/winever/task_manager.rb', line 76

def update_tasks
  return false if self.password.nil?

  task_names = create_tasks
  clear_tasks_except(task_names)

  true
end

#validate_password(password) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/winever/task_manager.rb', line 133

def validate_password password
  # Validate a password by trying to create a task with it. If it fails, then the password is wrong.
  # Will delete the created task after.
  ts = Win32::TaskScheduler.new
  base_test_task_name = test_task_name = "Winever_test_task"
  i = 0
  while ts.exists?(test_task_name)
    i += 1
    test_task_name = "#{base_test_task_name}_#{i}"
  end

  trigger = { :start_year   => 2000,
              :start_month  => 6,
              :start_day    => 12,
              :start_hour   => 13,
              :start_minute => 17,
              :trigger_type => Win32::TaskScheduler::TASK_TIME_TRIGGER_ONCE}

  ts.new_work_item(test_task_name, trigger)
  valid = false
  begin
    ts.password = password
    ts.application_name = "cmd"
    valid = true
  rescue
    ts.password = nil
    valid = false
  end
  ts.delete(test_task_name)

  return valid
end