Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
It works!
  • Loading branch information
garethrees committed May 24, 2012
1 parent 5ace961 commit 4caa1e4
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 23 deletions.
158 changes: 148 additions & 10 deletions app.rb
Expand Up @@ -4,24 +4,162 @@

class App < Sinatra::Base
set :root, File.dirname(__FILE__)

# ROUTES #
APP_ROOT = File.dirname(__FILE__)

get "/" do
erb :index
end

get "/repo" do
repo = Grit::Git.new('/tmp/repo')
@process = repo.clone({:process_info => true, :progress => true, :timeout => false}, "#{params[:repo_url]}", "tmp/test")
@todos = %x{grep -RIn TODO #{File.dirname(__FILE__)}/tmp/test}
FileUtils.rm_rf("#{File.dirname(__FILE__)}/tmp/test")
git = Grit::Git.new('/tmp/repo')

# TODO: sanitize repo strings
@repo = params[:repo].strip
tmp_folder = @repo.sub "/", "-"
@tmp_folder = tmp_folder

# Clone the repo
@process = git.clone({ :process_info => true, :progress => true, :timeout => false }, "git://github.com/#{@repo}.git", "tmp/#{tmp_folder}")

# Get the contents
dir = Dir.chdir("tmp/#{tmp_folder}")
@dir = []
@dir << Dir.pwd


# dir_in_array = ["#{APP_ROOT}/tmp/#{tmp_folder}"]

@todos = []

["TODO"].each do |annotation|
@todos << SourceAnnotationExtractor.enumerate(annotation.downcase.intern, @dir)
end

# Remove the folder once we're done and render the view
FileUtils.rm_rf("#{APP_ROOT}/tmp/#{tmp_folder}")
erb :index
end
end

# Implements the logic behind the rake tasks for annotations like
#
# rake notes
# rake notes:optimize
#
# and friends. See <tt>rake -T notes</tt> and <tt>railties/lib/tasks/annotations.rake</tt>.
#
# Annotation objects are triplets <tt>:line</tt>, <tt>:tag</tt>, <tt>:text</tt> that
# represent the line where the annotation lives, its tag, and its text. Note
# the filename is not stored.
#
# Annotations are looked for in comments and modulus whitespace they have to
# start with the tag optionally followed by a colon. Everything up to the end
# of the line (or closing ERB comment tag) is considered to be their text.
class SourceAnnotationExtractor
class Annotation < Struct.new(:line, :tag, :text)
# Returns a representation of the annotation that looks like this:
#
# [126] [TODO] This algorithm is simple and clearly correct, make it faster.
#
# If +options+ has a flag <tt>:tag</tt> the tag is shown as in the example above.
# Otherwise the string contains just line and text.
def to_s(options={})
s = "[#{line.to_s.rjust(options[:indent])}] "
s << "[#{tag}] " if options[:tag]
s << text
end
end

# Prints all annotations with tag +tag+ under the root directories +app+, +config+, +lib+,
# +script+, and +test+ (recursively). Only filenames with extension
# +.builder+, +.rb+, +.erb+, +.haml+, +.slim+, +.css+, +.scss+, +.js+, and
# +.coffee+ are taken into account. The +options+ hash is passed to each
# annotation's +to_s+.
#
# This class method is the single entry point for the rake tasks.
def self.enumerate(tag, dirs, options={})
puts "ENUMERATE FOR #{tag} IN #{dirs.inspect}"
extractor = new(tag, dirs)
extractor.display(extractor.find(dirs), options)
end

# HELPERS #
attr_reader :tag, :directories

helpers do

def initialize(tag, directories)
@tag = tag
@@directories = directories
end
end

# Returns a hash that maps filenames under +dirs+ (recursively) to arrays
# with their annotations.
def find(dirs = Annotation.directories)
puts "FIND: #{dirs.inspect}"
dirs.inject({}) { |h, dir| h.update(find_in(dir)) }
end

# Returns a hash that maps filenames under +dir+ (recursively) to arrays
# with their annotations. Only files with annotations are included, and only
# those with extension +.builder+, +.rb+, +.erb+, +.haml+, +.slim+, +.css+,
# +.scss+, +.js+, and +.coffee+
# are taken into account.
def find_in(dir)
puts "FIND_IN: #{dir}"
results = {}

Dir.glob("#{dir}/*") do |item|
next if File.basename(item)[0] == ?.

if File.directory?(item)
results.update(find_in(item))
elsif item =~ /\.(builder|rb|coffee)$/
puts "ITEM IS RB #{item}"
# results.update(extract_annotations_from(item, /#\s*(#{tag}):?\s*(.*)$/))
results.update(extract_annotations_from(item, /#\s*(TODO):?\s*(.*)$/))
elsif item =~ /\.(css|scss|js)$/
results.update(extract_annotations_from(item, /\/\/\s*(#{tag}):?\s*(.*)$/))
elsif item =~ /\.erb$/
results.update(extract_annotations_from(item, /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/))
elsif item =~ /\.haml$/
results.update(extract_annotations_from(item, /-\s*#\s*(#{tag}):?\s*(.*)$/))
elsif item =~ /\.slim$/
results.update(extract_annotations_from(item, /\/\s*\s*(#{tag}):?\s*(.*)$/))
end
end

results
end

# If +file+ is the filename of a file that contains annotations this method returns
# a hash with a single entry that maps +file+ to an array of its annotations.
# Otherwise it returns an empty hash.
def extract_annotations_from(file, pattern)
puts "EXTRACTING #{pattern} FROM #{file}"
lineno = 0
result = File.readlines(file).inject([]) do |list, line|
lineno += 1
puts "LINE_NO #{lineno} | LIST: #{list}"
next list unless line =~ pattern
puts "FOUND A MATCH? #{list.inspect}"
list << Annotation.new(lineno, $1, $2)
end
puts "RESULT? #{result.inspect}"
result.empty? ? {} : { file => result }
end

# Prints the mapping from filenames to annotations in +results+ ordered by filename.
# The +options+ hash is passed to each annotation's +to_s+.
def display(results, options={})
# options[:indent] = results.map { |f, a| a.map(&:line) }.flatten.max.to_s.size
# results.keys.sort.each do |file|
# puts "#{file}:"
# results[file].each do |note|
# puts " * #{note.to_s(options)}"
# end
# puts
# end
puts "RESULTS:: #{results.inspect}"
return results
end
end


17 changes: 17 additions & 0 deletions public/stylesheets/application.css
@@ -0,0 +1,17 @@
body {
font-family: Monaco, monospace;
margin: 20px auto;
max-width: 900px;
min-width: 600px;
width: 100%; }

.submit-repo {
margin-bottom: 1em; }
.submit-repo label {
font-size: 16px; }
.submit-repo input[type=text] {
border: 2px solid #4183C4;
color: #444;
font-size: 18px;
padding: 0.4em;
width: 480px; }
25 changes: 25 additions & 0 deletions public/stylesheets/sass/application.scss
@@ -0,0 +1,25 @@
body {
font-family: Monaco, monospace;
margin: 20px auto;
max-width: 900px;
min-width: 600px;
width: 100%;
}

header {

}

.submit-repo {
margin-bottom: 1em;
label {
font-size: 16px;
}
input[type=text] {
border: 2px solid #4183C4;
color: #444;
font-size: 18px;
padding: 0.4em;
width: 480px;
}
}
31 changes: 18 additions & 13 deletions views/index.erb
@@ -1,20 +1,25 @@
<h1>TODO</h1>
<form action="repo" method="get" accept-charset="utf-8" class="submit-repo">

<form action="repo" method="get" accept-charset="utf-8">
<label for="repo">github.com/</label><input type="text" name="repo" value="" id="repo" placeholder="rails/rails"/>
<input type="submit" value="Find TODOs &rarr;">

<label for="repo_url">Repo</label><input type="text" name="repo_url" value="" id="repo_url" />


<p><input type="submit" value="Continue &rarr;"></p>
</form>

<section id="repo">
<%= params[:repo_url] %>
<%# @process.inspect.to_s if @process %>
</section>
<% if @repo %>
<section id="repo">
TODOs for: <a href="http://github.com/<%= @repo %>">http://github.com/<%= @repo %></a>
</section>
<% end %>

<hr />

<section id="todos">
<%= @todos if @todos %>
</section>
<% if @todos %>
<section id="todos">
<% @todos.each do |todo| %>
<% todo.each_pair do |k, v| %>
<% line_url = "http://github.com/#{@repo}/blob/master" + k.gsub("/Users/gareth/Code/hackdo/tmp/#{@tmp_folder}", "") + "#L#{v.first.line}" %>
<p><a href="<%= line_url %>"><%= k.gsub("/Users/gareth/Code/hackdo/tmp/#{@tmp_folder}", "") %> [<%= v.first.line %>]</a> <%= v.first.tag.upcase %>: <%= v.first.text %></p>
<% end %>
<% end %>
</section>
<% end %>
7 changes: 7 additions & 0 deletions views/layout.html.erb → views/layout.erb
Expand Up @@ -45,6 +45,13 @@
<![endif]-->
</head>
<body>
<header>
<h1>Forgotten TODOs</h1>
</header>

<div id="main" role="main">
<%= yield %>
</div>

</body>
</html>

0 comments on commit 4caa1e4

Please sign in to comment.