Class: Pendaxes::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/pendaxes/command_line.rb

Defined Under Namespace

Classes: Oneshot

Constant Summary collapse

DEFAULT_CONFIG_FILE =
".pendaxes.yml"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CommandLine

Returns a new instance of CommandLine.



13
14
15
16
# File 'lib/pendaxes/command_line.rb', line 13

def initialize(*args)
  @args = args.dup
  @config = nil
end

Instance Method Details

#configObject



18
19
20
21
22
23
# File 'lib/pendaxes/command_line.rb', line 18

def config
  return @config if @config
  return nil if @args.empty? && !File.exist?(DEFAULT_CONFIG_FILE)

  @config = Config.new(YAML.load_file(@args.first || DEFAULT_CONFIG_FILE))
end

#detectObject



147
148
149
150
# File 'lib/pendaxes/command_line.rb', line 147

def detect
  @detector = Detector.find(config.detection.use.to_sym).new(@workspace, {out: $stdout}.merge(config.detection))
  @pendings = @detector.detect
end

#initObject



112
113
114
# File 'lib/pendaxes/command_line.rb', line 112

def init
  write_default_config(to: @args[1], path: @args[2], repository: @args[3])
end

#notifyObject



152
153
154
155
156
157
158
159
# File 'lib/pendaxes/command_line.rb', line 152

def notify
  config.notifications.map{|x| Hashr.new(x) }.each do |notification|
    puts " * #{notification.use}"
    notificator = Notificator.find(notification.use.to_sym).new({out: $stdout}.merge(notification))
    notificator.add @pendings
    notificator.notify
  end
end

#runObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/pendaxes/command_line.rb', line 161

def run
  case @args.first
  when "--help"
    usage
  when "--init"
    init
  else
    unless config
      warn "./.pendaxes.yml not exists. showing usage..."
      usage
      return 1
    end

    if config.workspace
      puts "=> Update repository"
      update
    else
      warn "=> Using this working copy. To use automatic fetching, please fill 'workspace' in your config file."
      workspace # to initialize @workspace
    end

    puts "=> Detect pendings"
    detect

    puts "=> Send notifications"
    notify
  end

  0
end

#updateObject



143
144
145
# File 'lib/pendaxes/command_line.rb', line 143

def update
  workspace.update if config.workspace
end

#usageObject



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

def usage
  puts <<-USAGE
Usage:
  pendaxes [config]
config: (default = ./.pendaxes.yml)
        Path to config file to use.

  pendaxes --init [config] [path] [remote]

config: (default = ./.pendaxes.yml)
        Where to write config file.
path:   (optional)
        Path to working copy.
remote: (optional)
        remote git url. if `path` doesn't exist,
        will be cloned from this onto `path`.

  pendaxes --help

Show this help.
  USAGE
end

#workspaceObject



139
140
141
# File 'lib/pendaxes/command_line.rb', line 139

def workspace
  @workspace ||= Workspace.new(config.workspace || {path: Dir.pwd})
end

#write_default_config(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
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
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
105
106
107
108
109
110
# File 'lib/pendaxes/command_line.rb', line 25

def write_default_config(options={})
  options[:to] ||= DEFAULT_CONFIG_FILE
  workspace = (options[:path] || options[:repository])
  open(options[:to], 'w') do |io|
    io.puts <<-EOC
# Pendaxes default configuration file.
# See https://github.com/cookpad/pendaxes/wiki/Configuration for detail.

##
# for automatic git-pulling. recommend to set if you run periodically.
#
# path:       path to working copy to use.
#             if not exist, pendaxes will clone automatically from `repository`.
#             PENDAXES WILL DO "git reset --hard".
#             MAKE SURE path TO BE NOT SAME WITH working copy you use.
#
# repository: remote url of repository.
#             if `path` doesn't exist, will automatically cloned from specified url.
##

#{workspace            ? '' : '# '}workspace:
#{options[:path]       ? '' : '# '}  path: #{options[:path] || '/tmp/repository'}
#{options[:repository] ? '' : '# '}  repository: #{options[:repository] || "[email protected]:cookpad/pendaxes.git"}

##
# Settings of notification.
# See https://github.com/cookpad/pendaxes/wiki/Configuration for detail.
##

notifications:
  - use: file       # write report to file.
to: report.html # path to write.
reporter:
  use: haml
#  - use: mail      # send mails to each committer of pending.
#    reporter:
#      use: haml
#    from: [email protected]
#    delivery_method: sendmail
#  - use: terminal # show pendings on STDOUT.

##
# Settings for pending detection
##

detector:
  use: rspec # Use rspec detector. Currently only rspec is available.
  pattern:   # pattern of rspec file. this will be passed right after of "git grep --".
  - '*_spec.rb'
#  - 'spec/**/*.rb'
    EOC
  end

  puts "Just written default config file to #{options[:to]} !"
  if workspace
    puts "\nWith automatic git-pulling enabled:"

    if options[:repository] && options[:path]
      puts "  * using #{options[:repository]}"
      puts "  * will cloned at #{options[:path]}"

      if File.exist?(options[:path])
        puts "WARNING: #{options[:path]} already exists! Pendaxes does `git reset --hard`, be careful!"
      end
    elsif options[:path]
      puts "  * using working copy on #{options[:path]}"
      puts "WARNING: #{options[:path]} doesn't exist, you have to clone." unless File.exist?(options[:path])
    elsif options[:repository]
    end
  else
    puts <<-EOM

In default configuration, this will do:

1. Detect pendings
2. Write report file to "./report.html"

For more about configuration, see https://github.com/cookpad/pendaxes/wiki/Configuration !
    EOM

    if File.exist?(".git")
      puts "\nWARNING: this directory doesn't seem git working copy."
      puts "(Pendaxes works with git working copy.)"
    end
  end
end