forked from qt-creator/qt-creator
		
	By writing the needed special commands to a temporary file. What we cannot get rid of is the command for opening a bash that sources that file. Since Terminal usually opens a login shell, but we cannot set a special file for sourcing in that case, the special commands include mimicking the behavior of a login shell by reading the corresponding config files. This is in preparation to setting up the environment for the shell. Since we do not start a new process for the Terminal on macOS, we will need to explicitly export the whole environment after the fact, resulting in potentially dozens of export commands to be executed. Change-Id: Ia24cf1f00e62411734f5d6514d073e11d4cdae6e Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
		
			
				
	
	
		
			79 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
| #!/usr/bin/env python
 | |
| 
 | |
| ############################################################################
 | |
| #
 | |
| # Copyright (C) 2018 The Qt Company Ltd.
 | |
| # Contact: https://www.qt.io/licensing/
 | |
| #
 | |
| # This file is part of Qt Creator.
 | |
| #
 | |
| # Commercial License Usage
 | |
| # Licensees holding valid commercial Qt licenses may use this file in
 | |
| # accordance with the commercial license agreement provided with the
 | |
| # Software or, alternatively, in accordance with the terms contained in
 | |
| # a written agreement between you and The Qt Company. For licensing terms
 | |
| # and conditions see https://www.qt.io/terms-conditions. For further
 | |
| # information use the contact form at https://www.qt.io/contact-us.
 | |
| #
 | |
| # GNU General Public License Usage
 | |
| # Alternatively, this file may be used under the terms of the GNU
 | |
| # General Public License version 3 as published by the Free Software
 | |
| # Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
 | |
| # included in the packaging of this file. Please review the following
 | |
| # information to ensure the GNU General Public License requirements will
 | |
| # be met: https://www.gnu.org/licenses/gpl-3.0.html.
 | |
| #
 | |
| ############################################################################
 | |
| 
 | |
| import os
 | |
| import pipes
 | |
| import subprocess
 | |
| import sys
 | |
| from tempfile import NamedTemporaryFile
 | |
| 
 | |
| def quote_applescript(arg):
 | |
|     return arg.replace('\\', '\\\\').replace('"', '\\"')
 | |
| 
 | |
| def quote_shell(arg):
 | |
|     return pipes.quote(arg)
 | |
| 
 | |
| def apple_script(shell_command):
 | |
|     return '''
 | |
| --Terminal opens a window by default when it is not running, so check
 | |
| on applicationIsRunning(applicationName)
 | |
|         tell application "System Events" to count (every process whose name is applicationName)
 | |
|         return result is greater than 0
 | |
| end applicationIsRunning
 | |
| set terminalWasRunning to applicationIsRunning("Terminal")
 | |
| 
 | |
| set cdScript to "{}"
 | |
| tell application "Terminal"
 | |
|     --do script will open a new window if none given, but terminal already opens one if not running
 | |
|     if terminalWasRunning then
 | |
|         do script cdScript
 | |
|     else
 | |
|         do script cdScript in first window
 | |
|     end if
 | |
|     set currentTab to the result
 | |
|     set currentWindow to first window whose tabs contains currentTab
 | |
|     activate
 | |
| end tell
 | |
| '''.format(shell_command)
 | |
| 
 | |
| def main():
 | |
|     # create temporary file to be sourced into bash that deletes itself
 | |
|     with NamedTemporaryFile(delete=False) as shell_script:
 | |
|         quoted_shell_script = quote_shell(shell_script.name)
 | |
|         commands = ('cd ' + quote_shell(os.getcwd()) + '\n' +
 | |
|                     ' '.join([quote_shell(arg) for arg in sys.argv[1:]]) + '\n' +
 | |
|                     'rm ' + quoted_shell_script + '\n'
 | |
|                     )
 | |
|         shell_script.write(commands)
 | |
|         shell_script.flush()
 | |
|         shell_command = quote_applescript('source ' + quoted_shell_script)
 | |
|         osascript_process = subprocess.Popen(['/usr/bin/osascript'], stdin=subprocess.PIPE)
 | |
|         osascript_process.communicate(apple_script(shell_command))
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     main()
 |