2727import consulo .content .bundle .SdkType ;
2828import consulo .execution .RuntimeConfigurationException ;
2929import consulo .execution .action .Location ;
30- import consulo .execution .configuration .AbstractRunConfiguration ;
3130import consulo .execution .configuration .ConfigurationFactory ;
31+ import consulo .execution .configuration .EnvironmentAware ;
32+ import consulo .execution .configuration .LegacyRunConfigurationConverter ;
3233import consulo .execution .configuration .LocatableConfiguration ;
34+ import consulo .execution .configuration .ModuleBasedConfiguration ;
35+ import consulo .execution .configuration .RunConfigurationModule ;
3336import consulo .execution .configuration .RuntimeConfigurationError ;
34- import consulo .execution .configuration .log .ui .LogConfigurationPanel ;
35- import consulo .execution .configuration .ui .SettingsEditor ;
36- import consulo .execution .configuration .ui .SettingsEditorGroup ;
37- import consulo .execution .localize .ExecutionLocalize ;
37+ import consulo .execution .configuration .WorkingDirectoryAware ;
38+ import consulo .execution .configuration .fragment .EnvironmentFragmentState ;
39+ import consulo .execution .configuration .fragment .WorkingDirectoryFragmentState ;
3840import consulo .execution .test .AbstractTestProxy ;
39- import consulo .execution .ui .awt .EnvironmentVariablesComponent ;
4041import consulo .ide .impl .idea .util .PathMappingSettings ;
4142import consulo .language .psi .PsiElement ;
4243import consulo .language .psi .util .PsiTreeUtil ;
5051import consulo .python .impl .localize .PyLocalize ;
5152import consulo .python .module .extension .PyModuleExtension ;
5253import consulo .util .lang .StringUtil ;
53- import consulo .util .xml .serializer .InvalidDataException ;
54- import consulo .util .xml .serializer .JDOMExternalizerUtil ;
55- import consulo .util .xml .serializer .WriteExternalException ;
5654import consulo .virtualFileSystem .VirtualFile ;
57- import org .jdom .Element ;
5855import org .jspecify .annotations .Nullable ;
5956
6057import java .io .File ;
6562/**
6663 * @author Leonid Shalupov
6764 */
68- public abstract class AbstractPythonRunConfiguration <T extends AbstractPythonRunConfiguration > extends AbstractRunConfiguration implements LocatableConfiguration ,
69- AbstractPythonRunConfigurationParams , CommandLinePatcher
65+ public abstract class AbstractPythonRunConfiguration <T extends AbstractPythonRunConfiguration > extends ModuleBasedConfiguration < RunConfigurationModule >
66+ implements LocatableConfiguration , AbstractPythonRunConfigurationParams , CommandLinePatcher , WorkingDirectoryAware , EnvironmentAware
7067{
7168 /**
7269 * When passing path to test to runners, you should join parts with this char.
7370 * I.e.: file.py::PyClassTest::test_method
7471 */
7572 public static final String TEST_NAME_PARTS_SPLITTER = "::" ;
76- private String myInterpreterOptions = "" ;
77- private String myWorkingDirectory = "" ;
78- private String mySdkHome = "" ;
79- private boolean myUseModuleSdk ;
80- private boolean myAddContentRoots = true ;
81- private boolean myAddSourceRoots = true ;
82-
83- protected PathMappingSettings myMappingSettings ;
84- /**
85- * To prevent "double module saving" child may enable this flag
86- * and no module info would be saved
87- */
88- protected boolean mySkipModuleSerialization ;
73+
74+ @ Nullable
75+ private PathMappingSettings myMappingSettings ;
8976
9077 @ RequiredReadAction
9178 public AbstractPythonRunConfiguration (Project project , ConfigurationFactory factory )
9279 {
93- super (project , factory );
80+ super (new RunConfigurationModule ( project ) , factory );
9481 getConfigurationModule ().init ();
9582 }
9683
84+ @ Override
85+ public AbstractPythonRunConfigurationOptions getOptions ()
86+ {
87+ return (AbstractPythonRunConfigurationOptions ) super .getOptions ();
88+ }
89+
90+ /**
91+ * Converts the interpreter, working directory and environment of an old configuration element.
92+ */
93+ protected static void convertLegacyCommonState (LegacyRunConfigurationConverter converter , AbstractPythonRunConfigurationOptions options )
94+ {
95+ AbstractPythonRunConfigurationLegacyState old = converter .readXml (AbstractPythonRunConfigurationLegacyState .class );
96+ options .setInterpreterOptions (old .INTERPRETER_OPTIONS );
97+ options .setSdkHome (old .SDK_HOME );
98+ options .setUseModuleSdk (old .IS_MODULE_SDK );
99+ options .setAddContentRoots (old .ADD_CONTENT_ROOTS );
100+ options .setAddSourceRoots (old .ADD_SOURCE_ROOTS );
101+
102+ converter .addFragment (new WorkingDirectoryFragmentState (old .WORKING_DIRECTORY ));
103+ converter .addFragment (new EnvironmentFragmentState (converter .readEnvs (), old .PARENT_ENVS ));
104+ }
105+
97106 @ Override
98107 public List <Module > getValidModules ()
99108 {
@@ -126,50 +135,6 @@ public static List<Module> getValidModules(Project project)
126135 return result ;
127136 }
128137
129- public PyCommonOptionsFormData getCommonOptionsFormData ()
130- {
131- return new PyCommonOptionsFormData ()
132- {
133- @ Override
134- public Project getProject ()
135- {
136- return AbstractPythonRunConfiguration .this .getProject ();
137- }
138-
139- @ Override
140- public List <Module > getValidModules ()
141- {
142- return AbstractPythonRunConfiguration .this .getValidModules ();
143- }
144-
145- @ Override
146- public boolean showConfigureInterpretersLink ()
147- {
148- return false ;
149- }
150- };
151- }
152-
153- @ Override
154- public final SettingsEditor <T > getConfigurationEditor ()
155- {
156- SettingsEditor <T > runConfigurationEditor = PythonExtendedConfigurationEditor .create (createConfigurationEditor ());
157-
158- SettingsEditorGroup <T > group = new SettingsEditorGroup <>();
159-
160- // run configuration settings tab:
161- group .addEditor (ExecutionLocalize .runConfigurationConfigurationTabTitle (), runConfigurationEditor );
162-
163- // tabs provided by extensions:
164- //noinspection unchecked
165- PythonRunConfigurationExtensionsManager .getInstance ().appendEditors (this , (SettingsEditorGroup ) group );
166- group .addEditor (ExecutionLocalize .logsTabTitle (), new LogConfigurationPanel <>());
167-
168- return group ;
169- }
170-
171- protected abstract SettingsEditor <T > createConfigurationEditor ();
172-
173138 @ Override
174139 public void checkConfiguration () throws RuntimeConfigurationException
175140 {
@@ -198,7 +163,7 @@ private void checkExtensions() throws RuntimeConfigurationException
198163
199164 private void checkSdk () throws RuntimeConfigurationError
200165 {
201- if (!myUseModuleSdk )
166+ if (!isUseModuleSdk () )
202167 {
203168 if (StringUtil .isEmptyOrSpaces (getSdkHome ()))
204169 {
@@ -222,8 +187,8 @@ else if(!PythonSdkType.getInstance().isValidSdkHome(getSdkHome()))
222187 @ Override
223188 public String getSdkHome ()
224189 {
225- String sdkHome = mySdkHome ;
226- if (StringUtil .isEmptyOrSpaces (mySdkHome ))
190+ String sdkHome = getOptions (). getSdkHome () ;
191+ if (StringUtil .isEmptyOrSpaces (sdkHome ))
227192 {
228193 Sdk projectJdk = PythonSdkType .findPythonSdk (getModule ());
229194 if (projectJdk != null )
@@ -238,7 +203,7 @@ public String getSdkHome()
238203 public String getInterpreterPath ()
239204 {
240205 String sdkHome ;
241- if (myUseModuleSdk )
206+ if (isUseModuleSdk () )
242207 {
243208 Sdk sdk = PythonSdkType .findPythonSdk (getModule ());
244209 if (sdk == null )
@@ -256,7 +221,7 @@ public String getInterpreterPath()
256221
257222 public Sdk getSdk ()
258223 {
259- if (myUseModuleSdk )
224+ if (isUseModuleSdk () )
260225 {
261226 return PythonSdkType .findPythonSdk (getModule ());
262227 }
@@ -267,94 +232,57 @@ public Sdk getSdk()
267232 }
268233
269234 @ Override
270- public void readExternal (Element element ) throws InvalidDataException
271- {
272- super .readExternal (element );
273- myInterpreterOptions = JDOMExternalizerUtil .readField (element , "INTERPRETER_OPTIONS" );
274- readEnvs (element );
275- mySdkHome = JDOMExternalizerUtil .readField (element , "SDK_HOME" );
276- myWorkingDirectory = JDOMExternalizerUtil .readField (element , "WORKING_DIRECTORY" );
277- myUseModuleSdk = Boolean .parseBoolean (JDOMExternalizerUtil .readField (element , "IS_MODULE_SDK" ));
278- String addContentRoots = JDOMExternalizerUtil .readField (element , "ADD_CONTENT_ROOTS" );
279- myAddContentRoots = addContentRoots == null || Boolean .parseBoolean (addContentRoots );
280- String addSourceRoots = JDOMExternalizerUtil .readField (element , "ADD_SOURCE_ROOTS" );
281- myAddSourceRoots = addSourceRoots == null || Boolean .parseBoolean (addSourceRoots );
282- if (!mySkipModuleSerialization )
283- {
284- getConfigurationModule ().readExternal (element );
285- }
286-
287- setMappingSettings (PathMappingSettings .readExternal (element ));
288- // extension settings:
289- PythonRunConfigurationExtensionsManager .getInstance ().readExternal (this , element );
235+ public String getInterpreterOptions ()
236+ {
237+ return StringUtil .notNullize (getOptions ().getInterpreterOptions ());
290238 }
291239
292- protected void readEnvs (Element element )
240+ @ Override
241+ public void setInterpreterOptions (String interpreterOptions )
293242 {
294- String parentEnvs = JDOMExternalizerUtil .readField (element , "PARENT_ENVS" );
295- if (parentEnvs != null )
296- {
297- setPassParentEnvs (Boolean .parseBoolean (parentEnvs ));
298- }
299- EnvironmentVariablesComponent .readExternal (element , getEnvs ());
243+ getOptions ().setInterpreterOptions (interpreterOptions );
300244 }
301245
302246 @ Override
303- public void writeExternal (Element element ) throws WriteExternalException
304- {
305- super .writeExternal (element );
306- JDOMExternalizerUtil .writeField (element , "INTERPRETER_OPTIONS" , myInterpreterOptions );
307- writeEnvs (element );
308- JDOMExternalizerUtil .writeField (element , "SDK_HOME" , mySdkHome );
309- JDOMExternalizerUtil .writeField (element , "WORKING_DIRECTORY" , myWorkingDirectory );
310- JDOMExternalizerUtil .writeField (element , "IS_MODULE_SDK" , Boolean .toString (myUseModuleSdk ));
311- JDOMExternalizerUtil .writeField (element , "ADD_CONTENT_ROOTS" , Boolean .toString (myAddContentRoots ));
312- JDOMExternalizerUtil .writeField (element , "ADD_SOURCE_ROOTS" , Boolean .toString (myAddSourceRoots ));
313- if (!mySkipModuleSerialization )
314- {
315- getConfigurationModule ().writeExternal (element );
316- }
317-
318- // extension settings:
319- PythonRunConfigurationExtensionsManager .getInstance ().writeExternal (this , element );
320-
321- PathMappingSettings .writeExternal (element , getMappingSettings ());
247+ public String getWorkingDirectory ()
248+ {
249+ return StringUtil .notNullize (WorkingDirectoryAware .super .getWorkingDirectory ());
322250 }
323251
324- protected void writeEnvs (Element element )
252+ @ Override
253+ public void setWorkingDirectory (@ Nullable String workingDirectory )
325254 {
326- JDOMExternalizerUtil .writeField (element , "PARENT_ENVS" , Boolean .toString (isPassParentEnvs ()));
327- EnvironmentVariablesComponent .writeExternal (element , getEnvs ());
255+ WorkingDirectoryAware .super .setWorkingDirectory (workingDirectory );
328256 }
329257
330258 @ Override
331- public String getInterpreterOptions ()
259+ public Map < String , String > getEnvs ()
332260 {
333- return myInterpreterOptions ;
261+ return EnvironmentAware . super . getEnvs () ;
334262 }
335263
336264 @ Override
337- public void setInterpreterOptions ( String interpreterOptions )
265+ public void setEnvs ( Map < String , String > envs )
338266 {
339- myInterpreterOptions = interpreterOptions ;
267+ EnvironmentAware . super . setEnvs ( envs ) ;
340268 }
341269
342270 @ Override
343- public String getWorkingDirectory ()
271+ public boolean isPassParentEnvs ()
344272 {
345- return myWorkingDirectory ;
273+ return EnvironmentAware . super . isPassParentEnvs () ;
346274 }
347275
348276 @ Override
349- public void setWorkingDirectory ( String workingDirectory )
277+ public void setPassParentEnvs ( boolean passParentEnvs )
350278 {
351- myWorkingDirectory = workingDirectory ;
279+ EnvironmentAware . super . setPassParentEnvs ( passParentEnvs ) ;
352280 }
353281
354282 @ Override
355283 public void setSdkHome (String sdkHome )
356284 {
357- mySdkHome = sdkHome ;
285+ getOptions (). setSdkHome ( sdkHome ) ;
358286 }
359287
360288 @ Nullable
@@ -367,37 +295,37 @@ public Module getModule()
367295 @ Override
368296 public boolean isUseModuleSdk ()
369297 {
370- return myUseModuleSdk ;
298+ return getOptions (). isUseModuleSdk () ;
371299 }
372300
373301 @ Override
374302 public void setUseModuleSdk (boolean useModuleSdk )
375303 {
376- myUseModuleSdk = useModuleSdk ;
304+ getOptions (). setUseModuleSdk ( useModuleSdk ) ;
377305 }
378306
379307 @ Override
380308 public boolean shouldAddContentRoots ()
381309 {
382- return myAddContentRoots ;
310+ return getOptions (). isAddContentRoots () ;
383311 }
384312
385313 @ Override
386314 public boolean shouldAddSourceRoots ()
387315 {
388- return myAddSourceRoots ;
316+ return getOptions (). isAddSourceRoots () ;
389317 }
390318
391319 @ Override
392320 public void setAddSourceRoots (boolean flag )
393321 {
394- myAddSourceRoots = flag ;
322+ getOptions (). setAddSourceRoots ( flag ) ;
395323 }
396324
397325 @ Override
398326 public void setAddContentRoots (boolean flag )
399327 {
400- myAddContentRoots = flag ;
328+ getOptions (). setAddContentRoots ( flag ) ;
401329 }
402330
403331 public static void copyParams (AbstractPythonRunConfigurationParams source , AbstractPythonRunConfigurationParams target )
@@ -543,7 +471,8 @@ public String getTestSpec(Location<?> location, AbstractTestProxy failedTest)
543471 */
544472 public String getWorkingDirectorySafe ()
545473 {
546- String result = StringUtil .isEmpty (myWorkingDirectory ) ? getProject ().getBasePath () : myWorkingDirectory ;
474+ String workingDirectory = getWorkingDirectory ();
475+ String result = StringUtil .isEmpty (workingDirectory ) ? getProject ().getBasePath () : workingDirectory ;
547476 if (result != null )
548477 {
549478 return result ;
0 commit comments