From c8de8b970003f6a7ffdbb2e2e5b26d240109c4e1 Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 18 Apr 2024 10:42:01 +0200 Subject: [PATCH] pull github folder --- .../src/lib/compiler-content-imports.ts | 11 ++++- .../workspace/src/lib/actions/workspace.ts | 45 +++++++++++-------- .../src/github-folder-resolver.ts | 36 +++++++++++++++ libs/remix-url-resolver/src/index.ts | 1 + 4 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 libs/remix-url-resolver/src/github-folder-resolver.ts diff --git a/libs/remix-core-plugin/src/lib/compiler-content-imports.ts b/libs/remix-core-plugin/src/lib/compiler-content-imports.ts index 54f99a2984..ded1c69e6d 100644 --- a/libs/remix-core-plugin/src/lib/compiler-content-imports.ts +++ b/libs/remix-core-plugin/src/lib/compiler-content-imports.ts @@ -1,12 +1,12 @@ 'use strict' import { Plugin } from '@remixproject/engine' -import { RemixURLResolver } from '@remix-project/remix-url-resolver' +import { RemixURLResolver, githubFolderResolver } from '@remix-project/remix-url-resolver' const profile = { name: 'contentImport', displayName: 'content import', version: '0.0.1', - methods: ['resolve', 'resolveAndSave', 'isExternalUrl'] + methods: ['resolve', 'resolveAndSave', 'isExternalUrl', 'resolveGithubFolder'] } export type ResolvedImport = { @@ -212,4 +212,11 @@ export class CompilerImports extends Plugin { throw new Error(e) } } + + async resolveGithubFolder (url) { + const ghFolder = {} + const token = await this.call('settings', 'get', 'settings/gist-access-token') + await githubFolderResolver(url, ghFolder, token) + return ghFolder + } } diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index b40afef632..fee26d27e1 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -238,6 +238,7 @@ export type UrlParametersType = { shareCode: string url: string language: string + ghfolder: string } export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDefault', opts?) => { @@ -284,28 +285,34 @@ export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDe await workspaceProvider.set(path, content) } if (params.url) { - const data = await plugin.call('contentImport', 'resolve', params.url) - - path = data.cleanUrl - content = data.content - - try { - content = JSON.parse(content) as any - if (content.language && content.language === 'Solidity' && content.sources) { - const standardInput: JSONStandardInput = content as JSONStandardInput - for (const [fname, source] of Object.entries(standardInput.sources)) { - await workspaceProvider.set(fname, source.content) + if (params.ghfolder === 'true') { + const files = await plugin.call('contentImport', 'resolveGithubFolder', params.url) + console.log(files) + for (const [path, content] of Object.entries(files)) { + await workspaceProvider.set(path, content) + } + } else { + const data = await plugin.call('contentImport', 'resolve', params.url) + path = data.cleanUrl + content = data.content + try { + content = JSON.parse(content) as any + if (content.language && content.language === 'Solidity' && content.sources) { + const standardInput: JSONStandardInput = content as JSONStandardInput + for (const [fname, source] of Object.entries(standardInput.sources)) { + await workspaceProvider.set(fname, source.content) + } + return Object.keys(standardInput.sources)[0] + } else { + // preserve JSON whitespace if this isn't a Solidity compiler JSON-input-output file + content = data.content + await workspaceProvider.set(path, content) } - return Object.keys(standardInput.sources)[0] - } else { - // preserve JSON whitespace if this isn't a Solidity compiler JSON-input-output file - content = data.content + } catch (e) { + console.log(e) await workspaceProvider.set(path, content) } - } catch (e) { - console.log(e) - await workspaceProvider.set(path, content) - } + } } return path } catch (e) { diff --git a/libs/remix-url-resolver/src/github-folder-resolver.ts b/libs/remix-url-resolver/src/github-folder-resolver.ts new file mode 100644 index 0000000000..6d02f4316f --- /dev/null +++ b/libs/remix-url-resolver/src/github-folder-resolver.ts @@ -0,0 +1,36 @@ +// eslint-disable-next-line no-unused-vars +import axios, { AxiosResponse } from 'axios' + +export const githubFolderResolver = async (url, obj = {}, token) => { + const child = await pullFolder(url, token) + for (const item of child) { + console.log(item) + if (item.type === 'file') { + const response: AxiosResponse = await axios.get(item.download_url, { transformResponse: res => res }) + obj[item.path] = response.data + } else { + // dir + await githubFolderResolver(item.html_url, obj, token) + } + } + return obj +} + +const pullFolder = async (url, token) => { + url = new URL(url); + const pathname = url.pathname; + const pathParts = pathname.split('/'); + const username = pathParts[1]; + const repo = pathParts[2]; + const folderPath = pathParts.slice(5).join('/'); + const apiUrl = `https://api.github.com/repos/${username}/${repo}/contents/${folderPath}`; + const response = await axios.get(apiUrl, + { + headers: { + Authorization: `Bearer ${token}` + } + }); + const data = await response.data; + return data +} + diff --git a/libs/remix-url-resolver/src/index.ts b/libs/remix-url-resolver/src/index.ts index 30e99022f8..a1ba855607 100644 --- a/libs/remix-url-resolver/src/index.ts +++ b/libs/remix-url-resolver/src/index.ts @@ -1 +1,2 @@ export { RemixURLResolver } from './resolve' +export { githubFolderResolver } from './github-folder-resolver'