Skip to content

Commit

Permalink
Converting Open in new Document to Show as HTML
Browse files Browse the repository at this point in the history
TextMate runs Commands with `Create New Document` synchronously, but
`Show as HTML` asynchronously.  (TextMate will hang until the script
returns, so if you call "mate -w" in the script, you've essentially just
entered an infinite loop.)  I've converted all instances of `Create New
Document` to `Show as HTML` to allow changelists to be authored in
TextMate instead of in the clunky CommitWindow.
  • Loading branch information
appsforartists authored and infininight committed Aug 15, 2012
1 parent 6cc8a81 commit 12fed9e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 84 deletions.
14 changes: 4 additions & 10 deletions Commands/Annotate.plist
Expand Up @@ -17,24 +17,18 @@ from perforce_bundle_helper import *
p4_response = run_p4_command_on_selected_textmate_files('annotate')
html_response = stdout_to_html(p4_response, '')
html_response = html_response.replace('\n//depot/', '\n\n//depot')
for line in p4_response:
if line.endswith('\n'):
line = line[:-1]
if line.startswith('//'):
line = '\n' + line
print(line)
</string>
print(html_response)</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>@4</string>
<key>name</key>
<string>Annotate</string>
<key>output</key>
<string>openAsNewDocument</string>
<string>showAsHTML</string>
<key>uuid</key>
<string>D6F5F368-C476-4882-82EA-D11E22A445BF</string>
</dict>
Expand Down
15 changes: 5 additions & 10 deletions Commands/Submit Changelist….plist
Expand Up @@ -16,27 +16,22 @@ add_tm_support_to_path()
from p4python.P4 import P4
from perforce_bundle_helper import *
# Make sure that a new TextMate window is spawned to input the commit description into
# Make sure that a new TextMate window is spawned to input the commit description into with the cursor on the right line
os.environ['EDITOR'] = os.environ['TM_SUPPORT_PATH'] + '/bin/mate -w -l 25'
p4 = P4()
p4_response = run_p4_command('submit')
if not p4.connected():
p4.connect()
# Ensure human-readible output
p4.tagged = False
print(p4.run('submit'))</string>
print(stdout_to_html(p4_response))</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>@4</string>
<key>name</key>
<string>Submit Changelist…</string>
<key>output</key>
<string>openAsNewDocument</string>
<string>showAsHTML</string>
<key>uuid</key>
<string>BE2B6161-2E23-4C08-B438-409BB1E82DA8</string>
</dict>
Expand Down
29 changes: 9 additions & 20 deletions Commands/Sync with Depot.tmCommand
Expand Up @@ -5,38 +5,27 @@
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/bin/bash
<string>#!/usr/bin/python2.7
p4_info_key="Client root: "
def add_tm_support_to_path():
import os, sys
sys.path.append(os.environ['TM_BUNDLE_SUPPORT'])
# Get the key: value pair containing the workspace path from
# p4
workspace_path=`p4 info | grep "$p4_info_key"`
add_tm_support_to_path()
# Remove the key
workspace_path="${workspace_path#$p4_info_key}/"
from perforce_bundle_helper import *
if [[ $TM_SELECTED_FILE =~ $workspace_path ]]
then
relative_path=${TM_SELECTED_FILE#$workspace_path}
if [[ -d $TM_SELECTED_FILE ]]
then
# p4 requires ... to sync a directory
relative_path="${relative_path}/..."
fi
p4_response = run_p4_command_on_selected_textmate_files('sync')
# eval not needed because $TM_SELECTED_FILE is unquoted
cd $workspace_path
p4 sync $relative_path
fi</string>
print(stdout_to_html(p4_response))</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>@4</string>
<key>name</key>
<string>Sync with Depot</string>
<key>output</key>
<string>openAsNewDocument</string>
<string>showAsHTML</string>
<key>uuid</key>
<string>48C3E848-57FC-4633-814E-724CE05AC124</string>
</dict>
Expand Down
110 changes: 66 additions & 44 deletions Support/perforce_bundle_helper.py
Expand Up @@ -56,61 +56,83 @@ def prepare_file_list_for_p4(file_and_directory_list):
return result


def run_p4_command_on_selected_textmate_files(command, fallback_command = None, fallback_silently = True):
def run_p4_command_on_selected_textmate_files(*args, **kwargs):
file_list = prepare_file_list_for_p4(
get_files_in_p4_workspace(
get_textmate_file_list()
)
)

if not file_list:
return ["These files are not in the P4 workspace."]

else:
p4_response = []

if not p4.connected():
p4.connect()
kwargs['file_list'] = file_list
return run_p4_command(*args, **kwargs)

# This makes P4's output human-readible instead of dict-y
p4.tagged = False

def run_p4_command(command, file_list = [], fallback_command = None, fallback_silently = True):
p4_response = []

#if command is 'sync':
# return file_list


if not p4.connected():
p4.connect()

# This makes P4's output human-readible instead of dict-y
p4.tagged = False

def run_command(command, fallback_command = None):
p4_response = []

try:
'''
When you submit or shelve a changeset without a hardcoded
description, p4 will give you the opportunity to create one in
the app specified by $EDITOR.
'mate -w' spawns TextMate and tells it to wait until you close
the window to send its contents to stdin.
'''
if not os.environ.has_key('EDITOR'):
os.environ['EDITOR'] = 'mate -w'

p4_response = p4.run(command, file_list)
p4.disconnect()

return p4_response

except Exception:
'''
- I tried typing this as a P4Exception, but failed miserably
def run_command(command, fallback_command = None):
p4_response = []
- Another method to consider would be setting exception_level to 0
and printing the warnings straight from p4.warnings instead of
catching them as exceptions.
'''

try:
'''
When you submit or shelve a changeset without a hardcoded
description, p4 will give you the opportunity to create one in
the app specified by $EDITOR.
'mate -w' spawns TextMate and tells it to wait until you close
the window to send its contents to stdin.
'''
if not os.environ.has_key('EDITOR'):
os.environ['EDITOR'] = 'mate -w'

p4_response = p4.run(command, file_list)
p4.disconnect()

return p4_response

# I tried typing this as a P4Exception, but failed miserably
except Exception:
if fallback_command:
p4_response = run_command(fallback_command)
if fallback_command:
p4_response = run_command(fallback_command)

if not fallback_silently or (fallback_silently and not fallback_command):
if p4.warnings:
print('Warnings:')
for message in p4.warnings:
print(message)
if not fallback_silently or (fallback_silently and not fallback_command):
if p4.warnings:
print('<h2>Warnings:</h2>')
for message in p4.warnings:
print(message)

if p4.errors:
print('\nErrors:')
for message in p4.errors:
print(message)

return p4_response

return run_command(command, fallback_command)
if p4.errors:
print('<h2>Errors:</h2>')
for message in p4.errors:
print(message)

return p4_response

return run_command(command, fallback_command)


def stdout_to_html(lines, line_delimiter = '\n'):
html = line_delimiter.join(lines)
html = html.replace('&', '&amp;').replace('<', '&lt;')
return '<pre>' + html + '</pre>'

0 comments on commit 12fed9e

Please sign in to comment.