codekingpro/portable-devtools
114k
1# Copyright (c) 2012 Google Inc. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Visual Studio user preferences file writer."""
6
7import os
8import re
9import socket # for gethostname
10
11from gyp import easy_xml
12
13# ------------------------------------------------------------------------------
14
15
16def _FindCommandInPath(command):
17 """If there are no slashes in the command given, this function
18 searches the PATH env to find the given command, and converts it
19 to an absolute path. We have to do this because MSVS is looking
20 for an actual file to launch a debugger on, not just a command
21 line. Note that this happens at GYP time, so anything needing to
22 be built needs to have a full path."""
23 if "/" in command or "\\" in command:
24 # If the command already has path elements (either relative or
25 # absolute), then assume it is constructed properly.
26 return command
27 else:
28 # Search through the path list and find an existing file that
29 # we can access.
30 paths = os.environ.get("PATH", "").split(os.pathsep)
31 for path in paths:
32 item = os.path.join(path, command)
33 if os.path.isfile(item) and os.access(item, os.X_OK):
34 return item
35 return command
36
37
38def _QuoteWin32CommandLineArgs(args):
39 new_args = []
40 for arg in args:
41 # Replace all double-quotes with double-double-quotes to escape
42 # them for cmd shell, and then quote the whole thing if there
43 # are any.
44 if arg.find('"') != -1:
45 arg = '""'.join(arg.split('"'))
46 arg = '"%s"' % arg
47
48 # Otherwise, if there are any spaces, quote the whole arg.
49 elif re.search(r"[ \t\n]", arg):
50 arg = '"%s"' % arg
51 new_args.append(arg)
52 return new_args
53
54
55class Writer:
56 """Visual Studio XML user user file writer."""
57
58 def __init__(self, user_file_path, version, name):
59 """Initializes the user file.
60
61 Args:
62 user_file_path: Path to the user file.
63 version: Version info.
64 name: Name of the user file.
65 """
66 self.user_file_path = user_file_path
67 self.version = version
68 self.name = name
69 self.configurations = {}
70
71 def AddConfig(self, name):
72 """Adds a configuration to the project.
73
74 Args:
75 name: Configuration name.
76 """
77 self.configurations[name] = ["Configuration", {"Name": name}]
78
79 def AddDebugSettings(
80 self, config_name, command, environment={}, working_directory=""
81 ):
82 """Adds a DebugSettings node to the user file for a particular config.
83
84 Args:
85 command: command line to run. First element in the list is the
86 executable. All elements of the command will be quoted if
87 necessary.
88 working_directory: other files which may trigger the rule. (optional)
89 """
90 command = _QuoteWin32CommandLineArgs(command)
91
92 abs_command = _FindCommandInPath(command[0])
93
94 if environment and isinstance(environment, dict):
95 env_list = [f'{key}="{val}"' for (key, val) in environment.items()]
96 environment = " ".join(env_list)
97 else:
98 environment = ""
99
100 n_cmd = [
101 "DebugSettings",
102 {
103 "Command": abs_command,
104 "WorkingDirectory": working_directory,
105 "CommandArguments": " ".join(command[1:]),
106 "RemoteMachine": socket.gethostname(),
107 "Environment": environment,
108 "EnvironmentMerge": "true",
109 # Currently these are all "dummy" values that we're just setting
110 # in the default manner that MSVS does it. We could use some of
111 # these to add additional capabilities, I suppose, but they might
112 # not have parity with other platforms then.
113 "Attach": "false",
114 "DebuggerType": "3", # 'auto' debugger
115 "Remote": "1",
116 "RemoteCommand": "",
117 "HttpUrl": "",
118 "PDBPath": "",
119 "SQLDebugging": "",
120 "DebuggerFlavor": "0",
121 "MPIRunCommand": "",
122 "MPIRunArguments": "",
123 "MPIRunWorkingDirectory": "",
124 "ApplicationCommand": "",
125 "ApplicationArguments": "",
126 "ShimCommand": "",
127 "MPIAcceptMode": "",
128 "MPIAcceptFilter": "",
129 },
130 ]
131
132 # Find the config, and add it if it doesn't exist.
133 if config_name not in self.configurations:
134 self.AddConfig(config_name)
135
136 # Add the DebugSettings onto the appropriate config.
137 self.configurations[config_name].append(n_cmd)
138
139 def WriteIfChanged(self):
140 """Writes the user file."""
141 configs = ["Configurations"]
142 for config, spec in sorted(self.configurations.items()):
143 configs.append(spec)
144
145 content = [
146 "VisualStudioUserFile",
147 {"Version": self.version.ProjectVersion(), "Name": self.name},
148 configs,
149 ]
150 easy_xml.WriteXmlIfChanged(
151 content, self.user_file_path, encoding="Windows-1252"
152 )
153 