ReinH-track 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 REIN HENRICHS
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM = "track"
8
+ GEM_VERSION = "1.0.0"
9
+ AUTHOR = "Rein Henrichs"
10
+ EMAIL = "reinh@reinh.com"
11
+ HOMEPAGE = "http://github.com/ReinH/track"
12
+ SUMMARY = "A gem that provides an awesome command line interface for time tracking that is awesome."
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE", 'TODO']
20
+ s.summary = SUMMARY
21
+ s.description = s.summary
22
+ s.author = AUTHOR
23
+ s.email = EMAIL
24
+ s.homepage = HOMEPAGE
25
+
26
+ # Uncomment this to add a dependency
27
+ # s.add_dependency "foo"
28
+
29
+ s.autorequire = GEM
30
+ s.files = %w(LICENSE README.rdoc Rakefile TODO) + Dir.glob("{bin,lib,spec}/**/*")
31
+ s.executables << 'track'
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ desc "Run specs"
37
+ Spec::Rake::SpecTask.new do |t|
38
+ t.spec_files = FileList['spec/**/*_spec.rb']
39
+ t.spec_opts = %w(-fs --color)
40
+ end
41
+
42
+
43
+ Rake::GemPackageTask.new(spec) do |pkg|
44
+ pkg.gem_spec = spec
45
+ end
46
+
47
+ desc "install the gem locally"
48
+ task :install => [:package] do
49
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
50
+ end
51
+
52
+ desc "create a gemspec file"
53
+ task :make_spec do
54
+ File.open("#{GEM}.gemspec", "w") do |file|
55
+ file.puts spec.to_ruby
56
+ end
57
+ end
data/TODO ADDED
@@ -0,0 +1 @@
1
+
data/bin/track ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'lib/track'
5
+
6
+ home = ENV['HOME']
7
+ config_file = "#{home}/.track.yml"
8
+
9
+ banner = <<-BANNER
10
+ Usage: track command
11
+
12
+ Available commands:
13
+
14
+ <project> <task> => start a task on a project
15
+ stop => stop a started task
16
+
17
+ BANNER
18
+
19
+ if ARGV.empty?
20
+ puts banner
21
+ exit 0
22
+ elsif not File.exists?(config_file)
23
+ STDERR.puts "Create a #{home}/.track.yml file please."
24
+ exit 1
25
+ else
26
+ options = YAML.load_file(config_file)
27
+ options ||= {}
28
+ Track.new(options).run(ARGV)
29
+ end
data/lib/track.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'date'
2
+
3
+ class Track
4
+ attr_reader :options, :projects
5
+ def initialize(options={})
6
+ @options = options
7
+ @projects = @options['projects'] || {}
8
+ end
9
+
10
+ def run(argv)
11
+ argv.first == "stop" ? stop : start(*argv)
12
+ end
13
+
14
+ private
15
+
16
+ def start(*args)
17
+ stop
18
+ project_name = args.shift
19
+ description = args.join(' ').strip
20
+ project = projects[project_name] || project_name
21
+
22
+ write_line(project, description)
23
+ end
24
+
25
+ def stop
26
+ return unless File.size?(log_filename)
27
+ File.open(log_filename, 'r+') do |file|
28
+ lines = file.readlines
29
+ lines.last.sub!(placeholder, time_string)
30
+ file.rewind
31
+ file.write(lines.join)
32
+ end
33
+ end
34
+
35
+ def write_line(project, description)
36
+ line = "* [#{time_string} - #{placeholder}] "
37
+ line << project if project
38
+ line << ":\t" << description unless description.empty?
39
+ File.open(log_filename, 'a') do |file|
40
+ file.puts(line)
41
+ end
42
+ end
43
+
44
+ def time_string(time = Time.now)
45
+ time.strftime('%H:%M')
46
+ end
47
+
48
+ def log_filename
49
+ str = options['filename'] || 'track'
50
+ str += '-'
51
+ str += Date.today.to_s
52
+ str += '.textile'
53
+ return str
54
+ end
55
+
56
+ def placeholder
57
+ "--:--"
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'bin')
3
+ load 'track'
@@ -0,0 +1,132 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Track do
4
+ TMP_FILENAME = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'time_log.tmp'))
5
+
6
+ before do
7
+ @track = Track.new
8
+ @track.stub!(:log_filename).and_return(TMP_FILENAME)
9
+
10
+ @time = Time.now
11
+ @time.stub!(:now).and_return(@time)
12
+ @time_string = @time.strftime('%H:%M')
13
+ end
14
+
15
+ after do
16
+ File.unlink(TMP_FILENAME) if File.exists?(TMP_FILENAME)
17
+ end
18
+
19
+ def line_count
20
+ `wc -l #{TMP_FILENAME} 2>/dev/null`.to_i
21
+ end
22
+ private :line_count
23
+
24
+ def last_line
25
+ File.readlines(TMP_FILENAME).last.chomp
26
+ end
27
+
28
+ describe "#run" do
29
+ describe "when the first argument is \"stop\"" do
30
+ it "stops" do
31
+ @track.should_receive(:stop)
32
+ @track.run(["stop"])
33
+ end
34
+ end
35
+
36
+ describe "when the first argument is not stop" do
37
+ it "starts" do
38
+ @track.should_receive(:start)
39
+ @track.run(["tm"])
40
+ end
41
+
42
+ it "passes the arguments to start" do
43
+ @track.should_receive(:start).with(:one, :two)
44
+ @track.run([:one, :two])
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "#start" do
50
+ it "stops any started task" do
51
+ @track.should_receive(:stop)
52
+ @track.send(:start)
53
+ end
54
+
55
+ it "appends a line to the log file" do
56
+ lambda do
57
+ @track.send(:start, "Project", "Description")
58
+ end.should change{line_count}
59
+ end
60
+
61
+ end
62
+
63
+ describe "#write_line" do
64
+ before do
65
+ @track.send(:write_line, "Project", "Description")
66
+ end
67
+
68
+ it "includes the start time" do
69
+ last_line.should include(@time_string)
70
+ end
71
+
72
+ it "includes a placeholder for the end time" do
73
+ last_line.should include('--:--')
74
+ end
75
+
76
+ it "wraps the times in []" do
77
+ last_line.should match(/\[.+\]/)
78
+ end
79
+
80
+ it "includes the project name followed by a \":\"" do
81
+ last_line.should include("Project:")
82
+ end
83
+
84
+ it "includes the description" do
85
+ last_line.should include("Project:")
86
+ end
87
+
88
+ describe "with project \"Project\" and description \"Description\"" do
89
+ it "looks like \"* [<time> - --:--] Project: Description\"" do
90
+ last_line.should == "* [#{@time_string} - --:--] Project:\tDescription"
91
+ end
92
+ end
93
+ end
94
+
95
+ describe "#stop" do
96
+ it "replaced the end time placeholder with the end time" do
97
+ @track.send(:start)
98
+ @track.send(:stop)
99
+ last_line.should_not include('--:--')
100
+ last_line.should include("[#@time_string - #@time_string]")
101
+ end
102
+
103
+ it "does not change the last line if there is nothing to stop" do
104
+ @track.send(:start)
105
+ @track.send(:stop)
106
+ lambda {@track.send(:stop)}.should_not change{last_line}
107
+ end
108
+ end
109
+ end
110
+
111
+ describe Track do
112
+ before do
113
+ @track = Track.new
114
+ end
115
+
116
+ describe "#log_filename" do
117
+ it "starts with the filename specified in the options" do
118
+ @track.options['filename'] = 'file'
119
+ @track.send(:log_filename).should match(/^file/)
120
+ end
121
+
122
+ it "includes the current date" do
123
+ require 'date'
124
+ @track.send(:log_filename).should include(Date.today.to_s)
125
+ end
126
+
127
+ it "is a textile file" do
128
+ @track.send(:log_filename).should match(/\.textile$/)
129
+ end
130
+
131
+ end
132
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ReinH-track
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rein Henrichs
8
+ autorequire: track
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-11 00:00:00 -08:00
13
+ default_executable: track
14
+ dependencies: []
15
+
16
+ description: A gem that provides an awesome command line interface for time tracking that is awesome.
17
+ email: reinh@reinh.com
18
+ executables:
19
+ - track
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ - TODO
26
+ files:
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - TODO
31
+ - bin/track
32
+ - lib/track.rb
33
+ - spec/fixtures
34
+ - spec/spec_helper.rb
35
+ - spec/track_spec.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/ReinH/track
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: A gem that provides an awesome command line interface for time tracking that is awesome.
62
+ test_files: []
63
+