10
10
import java .nio .file .SimpleFileVisitor ;
11
11
import java .nio .file .attribute .BasicFileAttributes ;
12
12
import java .util .ArrayList ;
13
+ import java .util .Arrays ;
13
14
import java .util .Collection ;
15
+ import java .util .Collections ;
14
16
import java .util .List ;
15
17
import java .util .NavigableMap ;
16
18
import java .util .ResourceBundle ;
19
21
import com .redhat .ceylon .cmr .api .ArtifactResult ;
20
22
import com .redhat .ceylon .cmr .api .Logger ;
21
23
import com .redhat .ceylon .cmr .api .ModuleQuery ;
24
+ import com .redhat .ceylon .cmr .api .ModuleQuery .Type ;
22
25
import com .redhat .ceylon .cmr .api .ModuleVersionDetails ;
23
26
import com .redhat .ceylon .cmr .api .ModuleVersionQuery ;
24
27
import com .redhat .ceylon .cmr .api .ModuleVersionResult ;
25
28
import com .redhat .ceylon .cmr .api .Repository ;
26
29
import com .redhat .ceylon .cmr .api .RepositoryManager ;
27
30
import com .redhat .ceylon .cmr .impl .JULLogger ;
31
+ import com .redhat .ceylon .cmr .spi .ContentHandle ;
32
+ import com .redhat .ceylon .cmr .spi .ContentStore ;
33
+ import com .redhat .ceylon .cmr .spi .OpenNode ;
28
34
import com .redhat .ceylon .cmr .util .JarUtils ;
29
35
import com .redhat .ceylon .common .Constants ;
36
+ import com .redhat .ceylon .common .FileUtil ;
30
37
import com .redhat .ceylon .common .Messages ;
31
38
import com .redhat .ceylon .common .ModuleDescriptorReader ;
32
39
import com .redhat .ceylon .common .ModuleUtil ;
@@ -208,22 +215,45 @@ protected String checkModuleVersionsOrShowSuggestions(RepositoryManager repoMgr,
208
215
throw new ToolUsageError (err );
209
216
}
210
217
}
211
-
218
+
219
+ // finding a single compiled version in the output repo is a lot cheaper than query everything so let's
220
+ // try that first
221
+ if (version == null && !ModuleUtil .isDefaultModule (name )){
222
+ String compiledVersion = findCompiledVersion (repoMgr , name , version , type , binaryMajor , binaryMinor );
223
+ if (compiledVersion != null )
224
+ return compiledVersion ;
225
+ }
226
+
212
227
boolean suggested = false ;
213
- Collection <ModuleVersionDetails > versions = getModuleVersions (repoMgr , name , version , type , binaryMajor , binaryMinor );
228
+ Collection <ModuleVersionDetails > versions = null ;
229
+
230
+ // if we did not find any version in the output repo, see if we have a single one in the source repo, that's
231
+ // a lot cheaper than looking the version up
232
+ ModuleVersionDetails srcVersion = null ;
233
+ if (allowCompilation && version == null ){
234
+ srcVersion = getVersionFromSource (name );
235
+ if (srcVersion != null ){
236
+ // we found some source, let's compile it and not even look up anything else
237
+ versions = Collections .emptyList ();
238
+ }
239
+ }
240
+
241
+ // find versions unless we have one in sources waiting to be compiled
242
+ if (versions == null )
243
+ versions = getModuleVersions (repoMgr , name , version , type , binaryMajor , binaryMinor );
244
+
214
245
if (version != null ) {
215
246
// Here we either have a single version or none
216
247
if (versions .isEmpty () || forceCompilation || shouldRecompile (checkCompilation , repoMgr , name , version , type )) {
217
248
if (allowCompilation ) {
218
- Collection <ModuleVersionDetails > srcVersions = getVersionFromSource (name );
219
- if (!srcVersions .isEmpty () && version .equals (srcVersions .iterator ().next ().getVersion ())) {
249
+ if (srcVersion != null && version .equals (srcVersion .getVersion ())) {
220
250
// There seems to be source code that has the proper version
221
251
// Let's see if we can compile it...
222
252
if (!runCompiler (repoMgr , name , type )) {
223
253
throw new ToolUsageError (Messages .msg (bundle , "compilation.failed" ));
224
254
}
225
255
// All okay it seems, let's use this version
226
- versions = srcVersions ;
256
+ versions = Arrays . asList ( srcVersion ) ;
227
257
}
228
258
}
229
259
if (versions .isEmpty ()) {
@@ -242,17 +272,16 @@ protected String checkModuleVersionsOrShowSuggestions(RepositoryManager repoMgr,
242
272
|| checkCompilation )) {
243
273
// If there are no versions at all or only in remote repositories we
244
274
// first check if there's local code we could compile before giving up
245
- Collection <ModuleVersionDetails > srcVersions = getVersionFromSource (name );
246
- if (!srcVersions .isEmpty ()) {
275
+ if (srcVersion != null ) {
247
276
// There seems to be source code
248
277
// Let's see if we can compile it...
249
- String srcver = srcVersions . iterator (). next () .getVersion ();
278
+ String srcver = srcVersion .getVersion ();
250
279
if (!checkCompilation || shouldRecompile (checkCompilation , repoMgr , name , srcver , type )) {
251
280
if (!runCompiler (repoMgr , name , type )) {
252
281
throw new ToolUsageError (Messages .msg (bundle , "compilation.failed" ));
253
282
}
254
283
// All okay it seems, let's use this version
255
- versions = srcVersions ;
284
+ versions = Arrays . asList ( srcVersion ) ;
256
285
}
257
286
}
258
287
}
@@ -291,6 +320,42 @@ protected String checkModuleVersionsOrShowSuggestions(RepositoryManager repoMgr,
291
320
}
292
321
}
293
322
323
+ private String findCompiledVersion (RepositoryManager repoMgr , String name , String version , Type type , Integer binaryMajor , Integer binaryMinor ) throws IOException {
324
+ File outDir = DefaultToolOptions .getCompilerOutDir ();
325
+ if (outDir != null ){
326
+ Repository outDirRepository = null ;
327
+ List <Repository > repositories = repoMgr .getRepositories ();
328
+ for (Repository repository : repositories ){
329
+ OpenNode root = repository .getRoot ();
330
+ // it has binaries if it is not a folder
331
+ if (root .isRemote () || root .hasBinaries ())
332
+ continue ;
333
+ ContentStore service = root .getService (ContentStore .class );
334
+ if (service == null )
335
+ continue ;
336
+ ContentHandle content = service .getContent (root );
337
+ // again skip binaries
338
+ if (content == null || content .hasBinaries ())
339
+ continue ;
340
+ File repoFile = content .getContentAsFile ();
341
+ if (repoFile != null && FileUtil .sameFile (repoFile , outDir )){
342
+ outDirRepository = repository ;
343
+ break ;
344
+ }
345
+ }
346
+ if (outDirRepository != null && outDirRepository .isSearchable ()){
347
+ ModuleVersionQuery query = getModuleVersionQuery (name , version , type , binaryMajor , binaryMinor );
348
+ ModuleVersionResult result = new ModuleVersionResult (query .getName ());
349
+ outDirRepository .completeVersions (query , result );
350
+ NavigableMap <String , ModuleVersionDetails > outRepoVersions = result .getVersions ();
351
+ if (outRepoVersions .size () == 1 ){
352
+ return outRepoVersions .get (outRepoVersions .firstKey ()).getVersion ();
353
+ }
354
+ }
355
+ }
356
+ return null ;
357
+ }
358
+
294
359
protected String getModuleNotFoundErrorMessage (RepositoryManager repoMgr , String name , String version ) {
295
360
StringBuilder err = new StringBuilder ();
296
361
err .append (Messages .msg (bundle , "module.not.found" , name ));
@@ -361,30 +426,32 @@ private boolean onlyRemote(Collection<ModuleVersionDetails> versions) {
361
426
return true ;
362
427
}
363
428
364
- private Collection <ModuleVersionDetails > getVersionFromSource (String name ) {
365
- Collection <ModuleVersionDetails > result = new ArrayList <ModuleVersionDetails >();
429
+ private ModuleVersionDetails getVersionFromSource (String name ) {
366
430
try {
367
431
List <File > srcDirs = DefaultToolOptions .getCompilerSourceDirs ();
368
432
for (File srcDir : srcDirs ) {
369
- ModuleDescriptorReader mdr = new ModuleDescriptorReader (name , srcDir );
370
- String version = mdr .getModuleVersion ();
371
- if (version != null ) {
372
- ModuleVersionDetails mvd = new ModuleVersionDetails (version );
373
- mvd .setLicense (mdr .getModuleLicense ());
374
- List <String > by = mdr .getModuleAuthors ();
375
- if (by != null ) {
376
- mvd .getAuthors ().addAll (by );
433
+ try {
434
+ ModuleDescriptorReader mdr = new ModuleDescriptorReader (name , srcDir );
435
+ String version = mdr .getModuleVersion ();
436
+ if (version != null ) {
437
+ ModuleVersionDetails mvd = new ModuleVersionDetails (version );
438
+ mvd .setLicense (mdr .getModuleLicense ());
439
+ List <String > by = mdr .getModuleAuthors ();
440
+ if (by != null ) {
441
+ mvd .getAuthors ().addAll (by );
442
+ }
443
+ mvd .setRemote (false );
444
+ mvd .setOrigin ("Local source folder" );
445
+ return mvd ;
377
446
}
378
- mvd .setRemote (false );
379
- mvd .setOrigin ("Local source folder" );
380
- result .add (mvd );
381
- break ;
447
+ }catch (ModuleDescriptorReader .NoSuchModuleException x ){
448
+ // skip this source folder and look in the next one
382
449
}
383
450
}
384
451
} catch (Exception ex ) {
385
452
// Just continue as if nothing happened
386
453
}
387
- return result ;
454
+ return null ;
388
455
}
389
456
390
457
private boolean runCompiler (RepositoryManager repoMgr , String name , ModuleQuery .Type type ) {
0 commit comments