Skip to content

Commit c934282

Browse files
committedMay 6, 2014
RepoUsingTool ceylon/ceylon-runtime#53: look for module first in compiled output, then in source
If found, we do not check for other versions as this is the most likely candidate, and the fastest too
1 parent e7d43e0 commit c934282

File tree

1 file changed

+91
-24
lines changed

1 file changed

+91
-24
lines changed
 

‎ceylon/src/main/java/com/redhat/ceylon/cmr/ceylon/RepoUsingTool.java

+91-24
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import java.nio.file.SimpleFileVisitor;
1111
import java.nio.file.attribute.BasicFileAttributes;
1212
import java.util.ArrayList;
13+
import java.util.Arrays;
1314
import java.util.Collection;
15+
import java.util.Collections;
1416
import java.util.List;
1517
import java.util.NavigableMap;
1618
import java.util.ResourceBundle;
@@ -19,14 +21,19 @@
1921
import com.redhat.ceylon.cmr.api.ArtifactResult;
2022
import com.redhat.ceylon.cmr.api.Logger;
2123
import com.redhat.ceylon.cmr.api.ModuleQuery;
24+
import com.redhat.ceylon.cmr.api.ModuleQuery.Type;
2225
import com.redhat.ceylon.cmr.api.ModuleVersionDetails;
2326
import com.redhat.ceylon.cmr.api.ModuleVersionQuery;
2427
import com.redhat.ceylon.cmr.api.ModuleVersionResult;
2528
import com.redhat.ceylon.cmr.api.Repository;
2629
import com.redhat.ceylon.cmr.api.RepositoryManager;
2730
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;
2834
import com.redhat.ceylon.cmr.util.JarUtils;
2935
import com.redhat.ceylon.common.Constants;
36+
import com.redhat.ceylon.common.FileUtil;
3037
import com.redhat.ceylon.common.Messages;
3138
import com.redhat.ceylon.common.ModuleDescriptorReader;
3239
import com.redhat.ceylon.common.ModuleUtil;
@@ -208,22 +215,45 @@ protected String checkModuleVersionsOrShowSuggestions(RepositoryManager repoMgr,
208215
throw new ToolUsageError(err);
209216
}
210217
}
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+
212227
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+
214245
if (version != null) {
215246
// Here we either have a single version or none
216247
if (versions.isEmpty() || forceCompilation || shouldRecompile(checkCompilation, repoMgr, name, version, type)) {
217248
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())) {
220250
// There seems to be source code that has the proper version
221251
// Let's see if we can compile it...
222252
if (!runCompiler(repoMgr, name, type)) {
223253
throw new ToolUsageError(Messages.msg(bundle, "compilation.failed"));
224254
}
225255
// All okay it seems, let's use this version
226-
versions = srcVersions;
256+
versions = Arrays.asList(srcVersion);
227257
}
228258
}
229259
if (versions.isEmpty()) {
@@ -242,17 +272,16 @@ protected String checkModuleVersionsOrShowSuggestions(RepositoryManager repoMgr,
242272
|| checkCompilation)) {
243273
// If there are no versions at all or only in remote repositories we
244274
// 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) {
247276
// There seems to be source code
248277
// Let's see if we can compile it...
249-
String srcver = srcVersions.iterator().next().getVersion();
278+
String srcver = srcVersion.getVersion();
250279
if (!checkCompilation || shouldRecompile(checkCompilation, repoMgr, name, srcver, type)) {
251280
if (!runCompiler(repoMgr, name, type)) {
252281
throw new ToolUsageError(Messages.msg(bundle, "compilation.failed"));
253282
}
254283
// All okay it seems, let's use this version
255-
versions = srcVersions;
284+
versions = Arrays.asList(srcVersion);
256285
}
257286
}
258287
}
@@ -291,6 +320,42 @@ protected String checkModuleVersionsOrShowSuggestions(RepositoryManager repoMgr,
291320
}
292321
}
293322

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+
294359
protected String getModuleNotFoundErrorMessage(RepositoryManager repoMgr, String name, String version) {
295360
StringBuilder err = new StringBuilder();
296361
err.append(Messages.msg(bundle, "module.not.found", name));
@@ -361,30 +426,32 @@ private boolean onlyRemote(Collection<ModuleVersionDetails> versions) {
361426
return true;
362427
}
363428

364-
private Collection<ModuleVersionDetails> getVersionFromSource(String name) {
365-
Collection<ModuleVersionDetails> result = new ArrayList<ModuleVersionDetails>();
429+
private ModuleVersionDetails getVersionFromSource(String name) {
366430
try {
367431
List<File> srcDirs = DefaultToolOptions.getCompilerSourceDirs();
368432
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;
377446
}
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
382449
}
383450
}
384451
} catch (Exception ex) {
385452
// Just continue as if nothing happened
386453
}
387-
return result;
454+
return null;
388455
}
389456

390457
private boolean runCompiler(RepositoryManager repoMgr, String name, ModuleQuery.Type type) {

0 commit comments

Comments
 (0)
Please sign in to comment.