github import using remix lib

pull/713/head
aniket-engg 4 years ago committed by Aniket
parent c17687b5f8
commit ee391dbb17
  1. 47
      apps/remix-ide/src/app/compiler/compiler-imports.js
  2. 36
      libs/remix-url-resolver/src/resolve.ts

@ -19,42 +19,14 @@ const profile = {
} }
module.exports = class CompilerImports extends Plugin { module.exports = class CompilerImports extends Plugin {
constructor (fileManager) { constructor (fileManager) {
super(profile) super(profile)
this.fileManager = fileManager this.fileManager = fileManager
this.previouslyHandled = {} // cache import so we don't make the request at each compilation.
}
handleGithubCall (root, path, cb) {
let param = '?'
// const token = await this.call('settings', 'getGithubAccessToken') // const token = await this.call('settings', 'getGithubAccessToken')
const token = globalRegistry.get('config').api.get('settings/gist-access-token') // TODO replace with the plugin call above https://github.com/ethereum/remix-ide/issues/2288 const token = globalRegistry.get('config').api.get('settings/gist-access-token') // TODO replace with the plugin call above https://github.com/ethereum/remix-ide/issues/2288
param += token ? 'access_token=' + token : '' this.urlResolver = new RemixURLResolver(token)
const regex = path.match(/blob\/([^/]+)\/(.*)/) this.previouslyHandled = {} // cache import so we don't make the request at each compilation.
if (regex) {
// if we have /blob/master/+path we extract the branch name "master" and add it as a parameter to the github api
// the ref can be branch name, tag, commit id
const reference = regex[1]
param += '&ref=' + reference
path = path.replace(`blob/${reference}/`, '')
}
return request.get(
{
url: 'https://api.github.com/repos/' + root + '/contents/' + path + param,
json: true
},
(err, r, data) => {
if (err) {
return cb(err || 'Unknown transport error')
}
if ('content' in data) {
cb(null, base64.decode(data.content), root + '/' + path)
} else if ('message' in data) {
cb(data.message)
} else {
cb('Content not received')
}
})
} }
handleSwarmImport (url, cleanUrl, cb) { handleSwarmImport (url, cleanUrl, cb) {
@ -138,7 +110,7 @@ module.exports = class CompilerImports extends Plugin {
if (imported) { if (imported) {
return cb(null, imported.content, imported.cleanUrl, imported.type, url) return cb(null, imported.content, imported.cleanUrl, imported.type, url)
} }
var handlers = new RemixURLResolver().getHandlers() var handlers = this.urlResolver.getHandlers()
var found = false var found = false
handlers.forEach(function (handler) { handlers.forEach(function (handler) {
@ -146,20 +118,21 @@ module.exports = class CompilerImports extends Plugin {
return return
} }
var match = handler.match.exec(url) var match = handler.match(url)
if (match) { if (match) {
found = true found = true
loadingCb('Loading ' + url + ' ...') loadingCb('Loading ' + url + ' ...')
handler.handle(match).then(function(content) { handler.handle(match).then(function(result) {
const { content, cleanUrl } = result
self.previouslyHandled[url] = { self.previouslyHandled[url] = {
content: content, content,
cleanUrl: cleanUrl, cleanUrl,
type: handler.type type: handler.type
} }
cb(null, content, cleanUrl, handler.type, url) cb(null, content, cleanUrl, handler.type, url)
}).catch(function (error) { }).catch(function (error) {
cb('Unable to import "' + cleanUrl + '": ' + error) cb('Unable to import url : ' + error)
return return
}) })
// function (err, content, cleanUrl) { // function (err, content, cleanUrl) {

@ -18,8 +18,11 @@ interface Handler {
export class RemixURLResolver { export class RemixURLResolver {
private previouslyHandled: PreviouslyHandledImports private previouslyHandled: PreviouslyHandledImports
constructor() { gistAccessToken: string
constructor(gistToken?: string) {
this.previouslyHandled = {} this.previouslyHandled = {}
this.gistAccessToken = gistToken ? gistToken : ''
} }
/** /**
* Handle an import statement based on github * Handle an import statement based on github
@ -27,40 +30,51 @@ export class RemixURLResolver {
* @params filePath path of the file in github * @params filePath path of the file in github
*/ */
async handleGithubCall(root: string, filePath: string) { async handleGithubCall(root: string, filePath: string) {
let param = '?'
param += this.gistAccessToken ? 'access_token=' + this.gistAccessToken : ''
const regex = filePath.match(/blob\/([^/]+)\/(.*)/)
if (regex) {
// if we have /blob/master/+path we extract the branch name "master" and add it as a parameter to the github api
// the ref can be branch name, tag, commit id
const reference = regex[1]
param += '&ref=' + reference
filePath = filePath.replace(`blob/${reference}/`, '')
}
//eslint-disable-next-line no-useless-catch //eslint-disable-next-line no-useless-catch
try { try {
const req: string = 'https://api.github.com/repos/' + root + '/contents/' + filePath const req: string = 'https://api.github.com/repos/' + root + '/contents/' + filePath + param
const response: AxiosResponse = await axios.get(req) const response: AxiosResponse = await axios.get(req)
return Buffer.from(response.data.content, 'base64').toString() return { content: Buffer.from(response.data.content, 'base64').toString(), cleanUrl: root + '/' + filePath }
} catch(e) { } catch(e) {
throw e throw e
} }
} }
/** /**
* Handle an import statement based on http * Handle an import statement based on http
* @params url The url of the import statement * @param url The url of the import statement
* @params cleanURL * @param cleanUrl
*/ */
async handleHttp(url: string, _: string) { async handleHttp(url: string, cleanUrl: string) {
console.log('Inside libs handleHttpCall') console.log('Inside libs handleHttpCall')
//eslint-disable-next-line no-useless-catch //eslint-disable-next-line no-useless-catch
try { try {
const response: AxiosResponse = await axios.get(url) const response: AxiosResponse = await axios.get(url)
return response.data return { content: response.data, cleanUrl}
} catch(e) { } catch(e) {
throw e throw e
} }
} }
/** /**
* Handle an import statement based on https * Handle an import statement based on https
* @params url The url of the import statement * @param url The url of the import statement
* @params cleanURL * @param cleanUrl
*/ */
async handleHttps(url: string, _: string) { async handleHttps(url: string, cleanUrl: string) {
console.log('Inside libs handleHttpsCall')
//eslint-disable-next-line no-useless-catch //eslint-disable-next-line no-useless-catch
try { try {
const response: AxiosResponse = await axios.get(url) const response: AxiosResponse = await axios.get(url)
return response.data return { content: response.data, cleanUrl}
} catch(e) { } catch(e) {
throw e throw e
} }

Loading…
Cancel
Save