3
3
import com .intellij .execution .RunManager ;
4
4
import com .intellij .execution .RunnerAndConfigurationSettings ;
5
5
import com .intellij .execution .configurations .ConfigurationFactory ;
6
+ import com .intellij .execution .configurations .ConfigurationTypeUtil ;
6
7
import com .intellij .openapi .actionSystem .AnAction ;
7
8
import com .intellij .openapi .actionSystem .AnActionEvent ;
8
9
import com .intellij .openapi .application .Application ;
9
10
import com .intellij .openapi .application .ApplicationManager ;
10
11
import com .intellij .openapi .project .Project ;
12
+ import com .intellij .openapi .project .ProjectUtil ;
11
13
import com .intellij .openapi .ui .MessageType ;
12
14
import com .intellij .openapi .ui .Messages ;
13
15
import com .intellij .openapi .util .ThrowableComputable ;
14
16
import com .intellij .openapi .util .io .FileUtil ;
15
17
import com .intellij .openapi .vfs .VfsUtil ;
16
18
import com .intellij .openapi .vfs .VirtualFile ;
17
19
import com .intellij .util .exception .RootRuntimeException ;
20
+ import com .jetbrains .cidr .cpp .cmake .model .CMakeTarget ;
18
21
import com .jetbrains .cidr .cpp .cmake .workspace .CMakeWorkspace ;
22
+ import com .jetbrains .cidr .cpp .execution .CMakeBuildConfigurationHelper ;
23
+ import com .jetbrains .cidr .cpp .execution .CMakeRunConfigurationType ;
24
+ import com .jetbrains .cidr .execution .BuildTargetAndConfigurationData ;
25
+ import com .jetbrains .cidr .execution .BuildTargetData ;
26
+ import com .jetbrains .cidr .execution .ExecutableData ;
19
27
import org .apache .commons .lang .text .StrSubstitutor ;
28
+ import org .intellij .lang .annotations .MagicConstant ;
20
29
import org .jdom .Attribute ;
21
30
import org .jdom .Element ;
22
31
import org .jdom .JDOMException ;
@@ -84,22 +93,23 @@ public static void updateProject(Project project) {
84
93
application .runWriteAction (() -> {
85
94
String fileName = null ;
86
95
try {
96
+ final VirtualFile projectDir = ProjectUtil .guessProjectDir (project );
97
+ if (projectDir == null ) return ;
87
98
fileName = ".gitignore" ;
88
99
writeProjectFile (project ,
89
100
() -> FileUtil .loadTextAndClose (ConvertProject .class .getResourceAsStream ("tmpl_gitignore.txt" )),
90
101
fileName , projectData , STRATEGY .CREATEONLY );
91
102
92
103
fileName = "CMakeLists_template.txt" ;
93
- VirtualFile cmakeTemplate = project . getBaseDir () .findOrCreateChildData (project , fileName );
104
+ VirtualFile cmakeTemplate = projectDir .findOrCreateChildData (project , fileName );
94
105
String templateText ;
95
106
if (cmakeTemplate .getLength () <= 0 ) {
96
107
templateText = loadCmakeTemplateFromResources ();
97
108
VfsUtil .saveText (cmakeTemplate , templateText );
98
109
} else {
99
110
templateText = VfsUtil .loadText (cmakeTemplate );
100
111
}
101
- fileName = "CMakeLists.txt" ;
102
- writeProjectFile (project , () -> templateText , fileName , projectData , STRATEGY .OVERWRITE );
112
+ writeProjectFile (project , () -> templateText , "CMakeLists.txt" , projectData , STRATEGY .OVERWRITE );
103
113
} catch (IOException e ) {
104
114
String msg = String .format ("%s:\n %s " , fileName , e .getLocalizedMessage ());
105
115
application .invokeLater (() ->
@@ -110,7 +120,9 @@ public static void updateProject(Project project) {
110
120
111
121
CMakeWorkspace cMakeWorkspace = CMakeWorkspace .getInstance (project );
112
122
if (cMakeWorkspace .getCMakeDependencyFiles ().isEmpty ()) {
113
- cMakeWorkspace .selectProjectDir (VfsUtil .virtualToIoFile (project .getBaseDir ()));
123
+ final VirtualFile projectDir = ProjectUtil .guessProjectDir (project );
124
+ if (projectDir != null )
125
+ cMakeWorkspace .selectProjectDir (VfsUtil .virtualToIoFile (projectDir ));
114
126
}
115
127
cMakeWorkspace .scheduleClearGeneratedFilesAndReload ();
116
128
ApplicationManager .getApplication ().executeOnPooledThread (() ->
@@ -131,6 +143,7 @@ public static void updateProject(Project project) {
131
143
);
132
144
}
133
145
146
+ @ SuppressWarnings ("SpellCheckingInspection" )
134
147
@ Nullable
135
148
public static ProjectData loadProjectData (Project project ) {
136
149
Element cProject = detectAndLoadFile (project , CPROJECT_FILE_NAME );
@@ -161,9 +174,11 @@ private static String loadCmakeTemplateFromResources() throws IOException {
161
174
private static void writeProjectFile (Project project , ThrowableComputable <String , IOException > template ,
162
175
String fileName , ProjectData projectData , STRATEGY strategy )
163
176
throws IOException {
164
- VirtualFile projectFile = project .getBaseDir ().findChild (fileName );
177
+ final VirtualFile projectDir = ProjectUtil .guessProjectDir (project );
178
+ if (projectDir == null ) return ;
179
+ VirtualFile projectFile = projectDir .findChild (fileName );
165
180
if (projectFile == null ) {
166
- projectFile = project . getBaseDir () .createChildData (project , fileName );
181
+ projectFile = projectDir .createChildData (project , fileName );
167
182
} else if (strategy == STRATEGY .CREATEONLY ) {
168
183
return ;
169
184
}
@@ -174,14 +189,25 @@ private static void writeProjectFile(Project project, ThrowableComputable<String
174
189
private static void modifyCMakeConfigs (Project project , ProjectData projectData ) {
175
190
RunManager runManager = RunManager .getInstance (project );
176
191
@ SuppressWarnings ("ConstantConditions" )
177
- ConfigurationFactory factory = runManager . getConfigurationType ( OpenOcdConfigurationType . TYPE_ID )
178
- .getConfigurationFactories ()[0 ];
192
+ ConfigurationFactory factory =
193
+ ConfigurationTypeUtil . findConfigurationType ( OpenOcdConfigurationType . TYPE_ID ) .getConfigurationFactories ()[0 ];
179
194
String name = "OCD " + projectData .getProjectName ();
180
195
if (runManager .findConfigurationByTypeAndName (OpenOcdConfigurationType .TYPE_ID , name ) == null ) {
181
- RunnerAndConfigurationSettings newRunConfig = runManager .createRunConfiguration (name , factory );
182
- newRunConfig .setSingleton (true );
196
+ RunnerAndConfigurationSettings newRunConfig = runManager .createConfiguration (name , factory );
197
+ newRunConfig .setShared (true );
183
198
OpenOcdConfiguration configuration = (OpenOcdConfiguration ) newRunConfig .getConfiguration ();
184
- configuration .setupDefaultTargetAndExecutable ();
199
+ configuration .setAllowRunningInParallel (false );
200
+ final CMakeBuildConfigurationHelper helper = CMakeRunConfigurationType .getHelper (project );
201
+ CMakeTarget target = helper .getDefaultTarget ();
202
+ if (target != null && !target .isExecutable ()) {
203
+ target = helper .getTargets ().stream ().filter (CMakeTarget ::isExecutable ).findFirst ().orElse (null );
204
+ }
205
+ if (target != null ) {
206
+ final BuildTargetData buildTargetData = new BuildTargetData (project .getName (), target .getName ());
207
+ final BuildTargetAndConfigurationData data = new BuildTargetAndConfigurationData (buildTargetData , null );
208
+ configuration .setTargetAndConfigurationData (data );
209
+ configuration .setExecutableData (new ExecutableData (buildTargetData ));
210
+ }
185
211
ApplicationManager .getApplication ().invokeLater (() -> {
186
212
configuration .setBoardConfigFile (SelectBoardDialog .selectBoardByPriority (projectData , project ));
187
213
ApplicationManager .getApplication ().runWriteAction (() ->
@@ -223,14 +249,15 @@ private static String loadDefines(Context context) {
223
249
.collect (Collectors .joining (" " ));
224
250
}
225
251
226
- private static String fetchValueBySuperClass (Context context , String key ) {
252
+ private static String fetchValueBySuperClass (Context context , @ MagicConstant String key ) {
227
253
XPathExpression <Attribute > xPath = XPathFactory .instance ()
228
254
.compile (".//*[@superClass='" + key + "']/@value" , ATTRIBUTES_ONLY );
229
255
return xPath .evaluateFirst (context .cProjectElement ).getValue ();
230
256
}
231
257
232
258
private static Element detectAndLoadFile (Project project , String fileName ) {
233
- VirtualFile child = project .getBaseDir ().findChild (fileName );
259
+ final VirtualFile projectDir = ProjectUtil .guessProjectDir (project );
260
+ VirtualFile child = projectDir == null ? null : projectDir .findChild (fileName );
234
261
if (child == null || !child .exists () || child .isDirectory ()) {
235
262
Messages .showErrorDialog (
236
263
String .format ("File %s is not found in the project directory %s" , fileName , project .getBasePath ()),
@@ -249,7 +276,7 @@ private static Element detectAndLoadFile(Project project, String fileName) {
249
276
}
250
277
251
278
@ Override
252
- public void actionPerformed (AnActionEvent event ) {
279
+ public void actionPerformed (@ NotNull AnActionEvent event ) {
253
280
254
281
Project project = getEventProject (event );
255
282
updateProject (project );
0 commit comments