Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Use specified solc version range to determine which solc to download #1480

Merged
merged 4 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 45 additions & 32 deletions packages/truffle-compile/compilerSupplier.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ CompilerSupplier.prototype.getVersions = function() {
* @param {Object} allVersions (see `getVersions`)
* @return {String} url ex: "soljson-v0.4.21+commit.dfe3193c.js"
*/
CompilerSupplier.prototype.getVersionUrlSegment = function(
version,
allVersions
) {
CompilerSupplier.prototype.getVersionUrlSegment = (version, allVersions) => {
if (allVersions.releases[version]) return allVersions.releases[version];

const isPrerelease =
Expand All @@ -230,42 +227,55 @@ CompilerSupplier.prototype.getVersionUrlSegment = function(
}
}

const versionToUse = findNewestValidVersion(version, allVersions);
if (versionToUse) return allVersions.releases[versionToUse];

return null;
};

const findNewestValidVersion = (version, allVersions) => {
if (!semver.validRange(version)) return null;
const satisfyingVersions = Object.keys(allVersions.releases)
.map(solcVersion => {
if (semver.satisfies(solcVersion, version)) return solcVersion;
})
.filter(solcVersion => solcVersion);
if (satisfyingVersions.length > 0) {
return satisfyingVersions.reduce((newestVersion, version) => {
if (semver.gtr(version, newestVersion)) return version;
}, "0.0.0");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice reduce

} else {
return null;
}
};

/**
* Downloads solc specified by `version` after attempting retrieve it from cache on local machine,
* @param {String} version ex: "0.4.1", "0.4.16-nightly.2017.8.9+commit.81887bc7"
* @return {Module} solc
*/
CompilerSupplier.prototype.getByUrl = function(version) {
const self = this;
CompilerSupplier.prototype.getByUrl = async function(version) {
const allVersions = await this.getVersions(this.config.versionsUrl);
const file = this.getVersionUrlSegment(version, allVersions);
if (!file) throw this.errors("noVersion", version);

return self.getVersions(self.config.versionsUrl).then(allVersions => {
const file = self.getVersionUrlSegment(version, allVersions);

if (!file) throw self.errors("noVersion", version);

if (self.isCached(file)) return self.getFromCache(file);

const url = self.config.compilerUrlRoot + file;
const spinner = ora({
text: "Downloading compiler",
color: "red"
}).start();

return request
.get(url)
.then(response => {
spinner.stop();
self.addToCache(response, file);
return self.compilerFromString(response);
})
.catch(err => {
spinner.stop();
throw self.errors("noRequest", url, err);
});
});
if (this.isCached(file)) return this.getFromCache(file);

const url = this.config.compilerUrlRoot + file;
const spinner = ora({
text: "Downloading compiler",
color: "red"
}).start();

try {
const response = await request.get(url);
spinner.stop();
this.addToCache(response, file);
return this.compilerFromString(response);
} catch (error) {
spinner.stop();
throw this.errors("noRequest", url, error);
}
};

/**
Expand Down Expand Up @@ -494,7 +504,10 @@ CompilerSupplier.prototype.errors = function(kind, input, err) {

const kinds = {
noPath: "Could not find compiler at: " + input,
noVersion: "Could not find compiler version:\n" + input + ".\n" + info,
noVersion:
`Could not find a compiler version matching ${input}. ` +
`Please ensure you are specifying a valid version, constraint or ` +
`build in the truffle config. ${info}`,
noRequest:
"Failed to complete request to: " +
input +
Expand Down
4 changes: 3 additions & 1 deletion packages/truffle-compile/test/test_supplier.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ describe("CompilerSupplier", function() {
};

compile(version4PragmaSource, options, err => {
assert(err.message.includes("Could not find compiler version"));
assert(
err.message.includes("Could not find a compiler version matching")
);
done();
});
});
Expand Down