Navigation Menu

Skip to content

Commit

Permalink
Added a command to update includes across the project or selected files.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradchoate committed Sep 18, 2011
1 parent d1c1070 commit 20fb9ad
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 58 deletions.
25 changes: 25 additions & 0 deletions Commands/Update Project.tmCommand
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/env ruby
require "#{ENV['TM_BUNDLE_SUPPORT']}/tminclude.rb"
TextMate::Includes.instance.process_persistent_includes_for_project
</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>^@u</string>
<key>name</key>
<string>Update Project / Selected Files</string>
<key>output</key>
<string>showAsTooltip</string>
<key>scope</key>
<string>text.html</string>
<key>uuid</key>
<string>CA24BD98-E4B6-48F8-B15A-84CC533BE1BD</string>
</dict>
</plist>
162 changes: 104 additions & 58 deletions Support/tminclude.rb
Expand Up @@ -9,17 +9,30 @@ module TextMate
class Includes
include Singleton

def initialize
@time = Time.now
end

protected

def reset
@argument_regexp = Regexp.new(/\s*#([^#]+?)#\s*=\s*(?:(["'])([^\2]*?)\2|(\S+))\s*/m).freeze
@depth = 1
@global_vars = {}
@time = Time.now
@ctime = nil
@mtime = nil

init_comment_delimiters()
init_global_vars()
end

def init_comment_delimiters
if ENV['TM_COMMENT_START']
@escape_open = (ENV['TM_COMMENT_START'].dup.strip).freeze
@escape_close = (ENV['TM_COMMENT_END'] ? ENV['TM_COMMENT_END'].dup.strip : '').freeze
else
@escape_open = '<!--'.freeze
@escape_close = '-->'.freeze
end

# non-capturing for .gsub
# <!-- #tminclude "/path/to/file" -->
Expand All @@ -36,18 +49,6 @@ def reset
# version that captures for .scan
@tminclude_regexp_detail = %r{(#{escaped_open} *#(?:tm|bb)include +['"]([^'"]+)['"](?: +([^\n]*?))?)(\n.*?)(#{escaped_open} +end (?:tm|bb)include *(?:\n|$))}im
end

init_global_vars()
end

def init_comment_delimiters
if ENV['TM_COMMENT_START']
@escape_open = (ENV['TM_COMMENT_START'].dup.strip).freeze
@escape_close = (ENV['TM_COMMENT_END'] ? ENV['TM_COMMENT_END'].dup.strip : '').freeze
else
@escape_open = '<!--'.freeze
@escape_close = '-->'.freeze
end
end

def init_global_vars
Expand All @@ -61,7 +62,6 @@ def init_global_vars
# Here are some of BBEdit's global variables. We may choose
# to cherry pick these for support...
@global_vars['dont_update'] = ''
@global_vars['localpath'] = ENV['TM_FILEPATH']
@global_vars['shortusername'] = ENV['USER']
# lazily invoke this one, since it has cost associated with it...
@global_vars['username'] = method("var_username")
Expand All @@ -77,26 +77,7 @@ def init_global_vars
@global_vars['yearnum'] = @time.year
@global_vars['generator'] = "TextMate"

if filename = ENV['TM_FILENAME']
@global_vars['filename'] = filename
@global_vars['basename'] = filename.sub(/\.\w+$/, '')
if filename =~ /(\.\w+$)/
@global_vars['file_extension'] = $1
else
@global_vars['file_extension'] = ''
end

if ctime = File.ctime(ENV['TM_FILEPATH'])
@ctime = ctime
@global_vars['creationdate'] = method('var_creationdate')
@global_vars['creationtime'] = method('var_creationtime')
end
if mtime = File.mtime(ENV['TM_FILEPATH'])
@mtime = mtime
@global_vars['modifieddate'] = method('var_modifieddate')
@global_vars['modifiedtime'] = method('var_modifiedtime')
end
end
init_file_vars(ENV['TM_FILEPATH'])

if ENV['TM_PROJECT_FILEPATH']
dir = ENV['TM_PROJECT_FILEPATH'].dup
Expand All @@ -120,11 +101,30 @@ def init_global_vars
# Local Preview URL

# Unsupported...
# base, base_url, bodytext, charset, dirpath, doctitle, language,
# base, base_url, charset, dirpath, doctitle, language,
# link, machine, meta, path, prefix, real_url, root,
# rootpath, server, title
end

def init_file_vars(file)
@global_vars['localpath'] = file
basename = File.basename(file)
@global_vars['filename'] = basename
@global_vars['basename'] = basename.sub(/\.\w+$/, '')
@global_vars['file_extension'] = File.extname(file)

if ctime = File.ctime(file)
@ctime = ctime
@global_vars['creationdate'] = method('var_creationdate')
@global_vars['creationtime'] = method('var_creationtime')
end
if mtime = File.mtime(file)
@mtime = mtime
@global_vars['modifieddate'] = method('var_modifieddate')
@global_vars['modifiedtime'] = method('var_modifiedtime')
end
end

def parse_arguments(arg_str, vars)
arg_str.scan(@argument_regexp) do | var, quote, val, val2 |
vars[var.downcase] = val.nil? ? val2 : val
Expand Down Expand Up @@ -180,21 +180,19 @@ def process_include(file, args, vars)
# File resolution; expand ~/... paths;
# look for relative files, relative to current file, current project, replace variables
file = replace_variables(file, local_vars)
file = File.expand_path(file)
if File.exist?(filepath = file)
file_dir = File.dirname(ENV['TM_FILEPATH'])
if File.exist?(filepath = File.expand_path(file))
elsif file.match(/^\//) # non-relative path...
print "Could not open file: #{file}"
exit 206
elsif File.exist?(filepath = "#{ENV['TM_FILEPATH']}/#{file}")
elsif File.exist?(filepath = "#{ENV['TM_PROJECT_DIRECTORY']}/#{file}")
raise Exception, "Could not find file: #{file}"
elsif File.exist?(filepath = "#{file_dir}/#{file}")
else
print "Could not open file: #{file}"
exit 206
raise Exception, "Could not find file: #{file}"
end

file = filepath

if @doc_stack.has_key?(file)
print "Error: recursive include for #{file}"
exit 206
raise Exception, "Error: recursive include for #{file}"
end

@doc_stack[file] = true
Expand Down Expand Up @@ -269,19 +267,9 @@ def process_document(doc, vars)
replace_variables(doc, vars)
end

public

def process_persistent_includes
#initialize
reset

def process_persistent_includes_for_string(doc)
vars = {}
doc = STDIN.readlines.join
if doc =~ /\#dont_update\#/
print "This document cannot be updated because it is protected."
exit 206
end
doc = process_document(doc, vars)
process_document(doc, vars)

# lastly, process '#docsize#'
# TBD: support for reporting document size in kb, mb, etc.
Expand All @@ -299,6 +287,64 @@ def process_persistent_includes
end
doc.gsub!(/#docsize#/i, newlen.to_s)
end
end

public

def process_persistent_includes_for_project
require "#{ENV['TM_SUPPORT_PATH']}/lib/textmate.rb"
count = 0
begin
TextMate.each_text_file do | file |
doc = IO.readlines(file).join
if doc.match(/#(tm|bb)include/)
if not doc.match(/#dont_update#/)
ENV['TM_FILEPATH'] = file
reset
newdoc = doc.dup
process_persistent_includes_for_string(newdoc)
if newdoc != doc
if File.writable?(file)
f = File.new(file, "w")
f.write(newdoc)
f.close
count += 1
end
end
end
end
end
rescue Exception => e
print e
exit 206
end
if count == 1
print "1 file updated"
TextMate.rescan_project
elsif count > 1
print "#{count} files updated"
TextMate.rescan_project
else
print "No files needed an update"
end
exit 206
end

def process_persistent_includes
#initialize
reset

doc = STDIN.readlines.join
if doc =~ /\#dont_update\#/
print "This document cannot be updated because it is protected."
exit 206
end
begin
process_persistent_includes_for_string(doc)
rescue Exception => e
print e
exit 206
end
print doc
end

Expand Down
2 changes: 2 additions & 0 deletions info.plist
Expand Up @@ -58,6 +58,7 @@
<array>
<string>0D814247-7A00-46EE-A2A4-45FBBF4B1181</string>
<string>4400BCE9-20E3-426E-B1D7-2C0BCA53BCF8</string>
<string>CA24BD98-E4B6-48F8-B15A-84CC533BE1BD</string>
<string>9AFDEB2C-D9F0-423E-8211-EBB089F51F0C</string>
</array>
<key>name</key>
Expand Down Expand Up @@ -202,6 +203,7 @@
<string>991E7EBD-F3F5-469A-BA01-DC30E04AD472</string>
<string>0D814247-7A00-46EE-A2A4-45FBBF4B1181</string>
<string>4400BCE9-20E3-426E-B1D7-2C0BCA53BCF8</string>
<string>CA24BD98-E4B6-48F8-B15A-84CC533BE1BD</string>
<string>9AFDEB2C-D9F0-423E-8211-EBB089F51F0C</string>
<string>B23D6E15-6B33-11D9-86C1-000D93589AF6</string>
<string>BD4AEF38-638B-4C47-A762-4A0A6190F9C7</string>
Expand Down

0 comments on commit 20fb9ad

Please sign in to comment.